This is what happened when I executed
sudo su followed by
$ sudo su # exit exit $
The exit command does not close my terminal emulator.
Is it a child terminal?
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It only takes a minute to sign up.Sign up to join this community
Once you execute
sudo su or
su user a new shell is being created.
exit (or Ctrl+D) will exit the newly create shell and will return you to your previous shell.
Starting point - bash shell is running on PID 25050:
$ ps PID TTY TIME CMD 25050 pts/17 00:00:00 bash 25200 pts/17 00:00:00 ps
sudo su creates a new bash process which is running on PID 25203:
$ sudo su # ps PID TTY TIME CMD 25201 pts/17 00:00:00 sudo 25202 pts/17 00:00:00 su 25203 pts/17 00:00:00 bash 25213 pts/17 00:00:00 ps # exit
sudo su and returning to the starting point - bash shell is running on PID 25050:
$ ps PID TTY TIME CMD 25050 pts/17 00:00:00 bash 25214 pts/17 00:00:00 ps $
Child processes you run from a shell, including child shells, use the same terminal automatically. This is not specific to
sudo in any way--this is generally how it works when you run any program from your shell.
Shells and terminals are different things. A shell is what you use to run commands in a terminal. A shell can operate interactively--it gives you a prompt, you give it a command, it runs the command or shows an error about why it cannot, and the process repeats until you quit the shell. Or it can operate noninteractively, running a script.
Even though your terminal is (probably!) emulated, not physical, nonetheless Unix-like operating systems like Ubuntu assign device nodes to each of your terminals, and you can check what terminal you are using with the
tty command. It will usually be
/dev/pts/2, etc., for a terminal window or SSH connection, or
/dev/tty2, etc., for virtual consoles. Really what
tty does is to tell you which terminal, if any, input is being taken from; see below for details.
ek@Io:~$ tty /dev/pts/1 ek@Io:~$ bash ek@Io:~$ tty /dev/pts/1 ek@Io:~$ exit exit ek@Io:~$ sudo su [sudo] password for ek: root@Io:/home/ek# tty /dev/pts/1 root@Io:/home/ek# exit exit ek@Io:~$
You can see that, even though
sudo su creates a new shell as Yaron very well explains, the terminal you are using doesn't change.
Of course, there's another way to observe that the terminal is the same: you're still writing input the same way and in the same place, and reading output the same way and in the same place.
Most commands you run in a shell--such as
bash, and many more--cause a child process to be created. This child process has your shell as its parent but it is a separate program. By default, it is connected to the same terminal as your shell.
Your shell is still running, but it waits in the background for the program to terminate (or for you to suspend it). When the program terminates, the shell is still running and it resumes operation, prompting you for your next command.
These are the major exceptions:
cddon't create a new process at all, unless they are used in a construct that employs a subshell (explained here), such as
)grouping and pipelines.
your-command arguments... &)
disownbuiltin, you can detach jobs from your terminal.
Though I wouldn't really consider this an exception to child processes being connected to the same terminal as their parent, note that a process you run from a shell in a terminal will not always be taking input from that terminal or sending output to that terminal:
tty command only checks which terminal its standard input is, you can "fool" it:
ek@Io:~$ tty /dev/pts/1 ek@Io:~$ tty </dev/pts/0 /dev/pts/0
Or, less deviously:
ek@Io:~$ tty </dev/null # not a terminal not a tty ek@Io:~$ tty <&- # closes the input stream not a tty