9

This is what happened when I executed sudo su followed by exit

$ sudo su
# exit
exit
$ 

The exit command does not close my terminal emulator.

Is it a child terminal?

15

Once you execute sudo su or su user a new shell is being created.

Executing exit (or Ctrl+D) will exit the newly create shell and will return you to your previous shell.

  1. 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
    
  2. Running 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
    
  3. Exiting 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
    $
    
  • So can that be treated as a child shell? – Reeshabh Ranjan Sep 17 '17 at 7:44
  • 1
    @ReeshabhRanjan - yes – Yaron Sep 17 '17 at 7:44
17

Same terminal, different shell.

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/0, /dev/pts/1, /dev/pts/2, etc., for a terminal window or SSH connection, or /dev/tty1, /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.

A few technical details

Most commands you run in a shell--such as ls, cp, mv, rm, touch, wc, du, df, ssh, su, sudo, sh, 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:

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:

Since the 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.