2

Is it possible to create a temporary empty environment for a new user in a Shell like Bash or zsh?

I mean like the incognito mode in Chrome which opens an isolated environment in which everything you did gets removed locally (no history, no cookies, no cache, etc). Is such a thing possible in a Linux Shell?

5
  • 3
    Create a new ramdisk as /home/$USER on every login and delete it when logout/disconnected. Jul 3, 2018 at 16:57
  • what do you mean by 'environment'? there are environment variables, but also filesystems (/tmp?), network connections, etc.
    – Jeff Schaller
    Jul 3, 2018 at 17:03
  • With 'environment' I mean something like an isolated place to do some testing works without interacting with the system but using system resources (binaries, libraries and stuff). Think of it like a temporary user that gets created which has an empty $HOME dir. If you leave the shell, the user gets removed without a trace. The idea with the ramdisk is good but not the solution I am looking for. I want a temporary user which gets created when I need it and gets deleted when I leave the Shell.
    – soner
    Jul 3, 2018 at 17:11
  • 1
    What kind of properties are you expecting from that temporary environment? Do you trust the program to play nicely, or do you need to protect against malicious code? What system features does the program need to access — should it be able to see what programs you have installed? Should it be able access the network? Should it be able to see information about your machine (e.g. hardware configuration) that could identify it uniquely? I'm voting this question as too broad because these are completely different problems and you've already had three solutions to three different problems. Jul 3, 2018 at 20:29
  • 1
    And another closed question I'm interested in... Are some people turned on by closing questions ?
    – bob dylan
    Dec 26, 2018 at 14:48

4 Answers 4

3

To just add the obvious (at this point it hasn’t been mentioned), this is one of the intentions of jails/containers/zones, and although it may seem like overkill to the uninitiated (I’m assuming nothing), it’s trivial if one is already using them. (And if one is not using them, attempting to reinvent this wheel will be terribly error-prone.)

In short, create a temp docker instance, do your worst, then remove it afterwards. https://docs.docker.com/get-started/

See also this backgrounder: https://blog.jessfraz.com/post/containers-zones-jails-vms/

2

You can use this if you just want to run the program without your configuration files, but you trust that the program is not malicious and not to try to look outside the box.

I wrote a little something a while back that sets up a temporary directory and starts a shell with a minimal environment. I use this many times daily to test my solutions to questions for this site.

https://github.com/kusalananda/shell-toolbox

FreeBSD users can also use the devel/shell-toolbox port.

The command is called shell and creates a temporary disposable working directory, optionally pre-populated with the contents of another directory, and starts a shell in it. When the shell session exits, the temporary directory is disposed of (depending on command line flags used).

This can also be used to start another shell other than the user's login shell.

It doesn't create a temporary user (as this would require administrative permissions), but it does set the HOME variable to the temporary directory, which means that any references to $HOME or ~ would resolve to the temporary directory and not to the "real home" of the user. It is not a "safe" environment in the sense that the rest of the system is protected, but it provides an interactive shell in a throw-away directory where created files and directories will be cleaned up when finished with minimal impact from your ordinary shell environment.

Example:

[box] $ shell -s "$HOME/skel" zsh
shell: info: Copying /home/kk/skel into /tmp_mfs/shell-zsh.5IAh9F2B
shell: info: Starting /usr/local/bin/zsh in /tmp_mfs/shell-zsh.5IAh9F2B
$ ls -a
.          .Xdefaults .cvsrc     .mailrc    .vimrc
..         .cshrc     .login     .profile   .zshrc
$ echo "$HOME"
/tmp_mfs/shell-zsh.zqHtjz49
$ env
HOME=/tmp_mfs/shell-zsh.zqHtjz49
PATH=/usr/bin:/bin:/usr/sbin:/sbin:/usr/X11R6/bin:/usr/local/bin
PS1=$
SHELL=/usr/local/bin/zsh
TERM=screen
LOGNAME=kk
SHLVL=1
PWD=/tmp_mfs/shell-zsh.zqHtjz49
OLDPWD=/tmp_mfs/shell-zsh.zqHtjz49
_=/usr/bin/env
$ exit
shell: info: Removing /tmp_mfs/shell-zsh.5IAh9F2B

It doesn't do chroot or anything fancy like that. It's basically a glorified wrapper around env -i.

See the shell documentation.

0

Use a snapshot in a virtual machine.

  1. Create a virtual machine with the needed prerequisites.
  2. Create a snapshot of the VM.
  3. Do any test in your VM.
  4. Restore the snapshot, deleting any modification you did to the system.
  5. Go to 3.
-1

If all you want a totally clean shell void of all environment variables or aliases, you can spawn a new shell then type:

$ unalias -a
$ unset `set|grep '^[A-Z_a-z].*=' |sed 's|=.*$||g'`

This will pretty much remove everything that can be removed. Of course you'll want to set PS1 to something otherwise you won't be able to tell if you're in a quote or at the command prompt.

Note: This only applies to bash and zsh.

4
  • 1
    This is a seriously complicated and not even correct way of achieving an empty environment. (It can fail if there are variables whose value contains a line break.) Just use env -i /bin/sh. Jul 3, 2018 at 20:31
  • line breaks won't matter since it's stripping everything after =. And there is no harm in unsetting a variable that doesn't exist.
    – tk421
    Jul 3, 2018 at 20:52
  • Hmm, I think it does work in bash because set escapes newlines. But try it in dash or Busybox sh with an environment variable whose value is a newline followed by A * =. Jul 3, 2018 at 21:27
  • Ahh, I only verified it on bash on zsh since that was what the question mentioned.
    – tk421
    Jul 3, 2018 at 21:34

Not the answer you're looking for? Browse other questions tagged .