3.1. Sessions and Administrative Privileges
A session is more than a running shell. It binds a user identity to a
terminal or pseudo-terminal, a set of processes, a login time, and an entry
in the system's record of who is logged in. systemd-logind registers each
login; the kernel's terminal driver delivers keyboard signals to the process
group in the foreground; and the shell reads its startup files and prints a
prompt. Administrative work adds an identity transition on top of that
session: sudo or su starts a process as another user, usually root, while
the original login remains in place.
3.1.1. Logging In Through Consoles, Terminals, and Remote Sessions
Debian offers several entry points that all end at the same account database
and the same session machinery. A local virtual console is served by a getty
program; a serial line can run another; and the OpenSSH daemon accepts network
logins. All three authenticate through PAM and then start the account's login
shell. Figure 3.1 shows the common path and the two privilege transitions
covered in the next section.
Figure 3.1. Console, serial, and SSH logins converge on PAM authentication
and a logind session running a shell. sudo and su change identity inside
that session; dashed boxes mark the entry points that need local hardware.
The virtual consoles are the text screens reachable with Ctrl+Alt+F1
through Ctrl+Alt+F6 on a local keyboard. getty@tty1.service runs agetty
on /dev/tty1, prints a login: prompt, reads a user name, and hands
authentication to login and PAM. A graphical desktop normally occupies one
virtual terminal and leaves the others for text logins. The service units are
visible without privilege:
$ systemctl is-active getty@tty1.service
active
$ systemctl is-active serial-getty@ttyS0.service
active
Both units are active on the example system. A serial console follows the
same model on /dev/ttyS0; it needs a cable, a terminal emulator, and
matching line settings, so it is most often used on servers without a
monitor. Console login itself is simply: at the login: prompt enter the
account name, enter the password when asked (it is not displayed), and end
the session with exit or Ctrl+D.
A remote session begins when an SSH client connects to sshd on TCP port 22.
Before authenticating, the client verifies the server's identity: the
first connection records the server's host key, and every later connection
compares the key against that record. To make that comparison meaningful,
obtain the fingerprint on the server itself, in a channel you already trust -
for example, console access or an already-verified session - and keep it
beside you:
$ ssh-keygen -lf /etc/ssh/ssh_host_ed25519_key.pub
256 SHA256:JgkpuCwzlfFHX2BC73pcFwcaoe6wu/NkIszx3f1gkf4 root@trixie (ED25519)
ssh-keygen -l prints the fingerprint of a public key; -f names the file.
The host's public keys are world-readable, so this needs no privilege. Now
connect from the client and compare what ssh shows with the value you
obtained:
$ ssh john@127.0.0.1
The authenticity of host '127.0.0.1 (127.0.0.1)' can't be established.
ED25519 key fingerprint is SHA256:JgkpuCwzlfFHX2BC73pcFwcaoe6wu/NkIszx3f1gkf4.
This key is not known by any other names.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '127.0.0.1' (ED25519) to the list of known hosts.
Linux trixie 6.12.86+deb13-amd64 #1 SMP PREEMPT_DYNAMIC Debian 6.12.86-1 (2026-05-08) x86_64
The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.
Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
Last login: Wed Nov 18 09:04:02 2026 from 127.0.0.1
john@trixie:~$ id -un
john
john@trixie:~$ pwd
/home/john
john@trixie:~$ exit
logout
Connection to 127.0.0.1 closed.
The fingerprints match, so answer yes. That single decision is the whole
host-key ceremony: the client writes the server's keys into
~/.ssh/known_hosts, and from then on verification is automatic. Typing
no instead aborts the connection. Answer yes only after the comparison -
the prompt itself proves nothing. ssh then authenticates with a password
or a key (Chapter 20 sets up key authentication), allocates a
pseudo-terminal, and starts the login shell; the banner and the Last
login: line follow authentication, and exit ends the session and prints
logout.
ssh-keygen option |
Effect |
|---|---|
-l |
Print the fingerprint of a key file. |
-f file |
Name the key file to inspect. |
-F host |
List the known-hosts entries matching host. |
-R host |
Remove the known-hosts entries for host. |
The record that makes later connections silent can be inspected on the client:
$ ssh-keygen -F 127.0.0.1
# Host 127.0.0.1 found: line 1
|1|kWDxXTtYPmV4PzLrSNXJZ1LQ3gc=|jhNVSngKC8t5kY1QR9vhhY0n5s4= ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIN/dQeqlOnCajoy0aN40YDoELxym9ldqwRrm2fkMLXDl
[...]
Selected output: the client stored one line per host key the server offered
(ed25519 first; the ssh-rsa and ecdsa lines are elided). The |1| prefix is
the hashed form of the host name, which keeps the file from leaking the names
of every machine you have visited. A second connection now verifies the key
without a prompt:
$ ssh john@127.0.0.1
Linux trixie 6.12.86+deb13-amd64 #1 SMP PREEMPT_DYNAMIC Debian 6.12.86-1 (2026-05-08) x86_64
The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.
Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
Last login: Wed Nov 18 09:07:37 2026 from 127.0.0.1
john@trixie:~$ id -un
john
john@trixie:~$ pwd
/home/john
john@trixie:~$ ps -o pid,ppid,pgid,sid,tty,stat,cmd -p $$
PID PPID PGID SID TT STAT CMD
366955 366954 366955 366955 pts/1 Ss -bash
john@trixie:~$ exit
logout
Connection to 127.0.0.1 closed.
The ps line inside that session shows the machinery a login creates. $$
is the shell's own process ID. The shell (PID 366955) is a child of the
sshd-session process that handled the connection (PPID 366954), it leads
its own process group and session (PGID and SID equal its PID), it runs on
the pseudo-terminal pts/1, its state Ss means sleeping and session leader,
and its command shows as -bash: the leading dash is how a login shell
announces itself. Chapter 9 develops process
and signal handling; here it is enough to recognize the shape.
A session can be examined with ordinary tools. Inside an interactive login,
tty names the terminal, and who am i prints one line for it:
$ tty
/dev/pts/0
$ who am i
john sshd pts/0 2026-11-18 08:13 (127.0.0.1)
The second column names the login's terminal. On this system who lists
logins through systemd-logind, so the name combines the session's service
with its device - sshd pts/0 for this SSH login on pts/0 - and shows
sshd alone when a session has no terminal at all. The address in parentheses
is the client. who itself lists every current login, one line per session;
with three of john's logins open at once (selected output; one of john's
identical lines and the administrative logins are omitted):
$ who
john sshd pts/0 2026-11-18 09:07 (127.0.0.1)
john sshd pts/0 2026-11-18 09:07 (127.0.0.1)
[...]
loginctl shows the same information as logind sees it, with a session ID
per login (selected output; rows for other users omitted):
$ loginctl list-sessions
SESSION UID USER SEAT LEADER CLASS TTY IDLE SINCE
14265 1001 john - 363205 manager - no -
14278 1001 john - 366987 user - no -
14279 1001 john - 367005 user - no -
14280 1001 john - 367006 user - no -
[...]
Each user row is one login session - three are open here. The manager
rows are the per-user service instances that logind starts for each logged-in
user; Chapter 8 covers them. The session ID in the
first column is what logind commands such as loginctl show-session and
loginctl terminate-session accept. The w command adds idle time and the
current command of each session to the same login list.
The session also defines the environment the shell inherits. sshd exports
SSH_CONNECTION, whose four fields are the client address, client port,
server address, and server port:
$ printf "%s\n" "$SSH_CONNECTION"
127.0.0.1 42296 127.0.0.1 22
One warning completes the picture. If a server's host key has changed when
the record says it should not have, ssh refuses to connect and prints a
REMOTE HOST IDENTIFICATION HAS CHANGED warning that names the offending
key, the known-hosts file, and the line number. A changed key means the
machine was reinstalled, its keys were regenerated, or something is
impersonating the server. Verify the reason out of band - on the server, or
with whoever administers it - and only then remove the old entry with
ssh-keygen -R host and accept the new key at the next connection as if it
were the first. Chapter 20 covers host keys, client keys, and
server configuration in depth.
3.1.2. Using sudo and su
Debian systems are normally administered from an ordinary account that can elevate individual commands, because working permanently as root removes the distinction between reading a system and rewriting it. Two tools provide that elevation, and they differ in what they authenticate:
sudoauthenticates the invoking user and then runs a command as root (or as another user). Whether a command is allowed is decided by the sudoers policy: membership in thesudogroup grants every command, after john's own password; prebuilt images and automation sometimes addNOPASSWDentries sosudonever asks, which is a deliberate local policy choice, not a Debian default.suauthenticates the target user. Anyone who knows root's password can become root withsu -, regardless of sudoers.
Chapter 1 demonstrated the policy listing with
sudo -l; Chapter 5 covers sudoers rules in
full. When the policy requires a password, sudo prints
[sudo] password for john: before doing anything else, reads the password
without echoing it, and remembers a successful authentication for fifteen
minutes so that repeated commands do not re-prompt. The transcripts in this
book show each command with its output after authentication, as in
Chapter 1.
The most direct proof of elevation is a command that reports the executing
user. id -u prints the user ID; run through sudo, that user is root:
$ sudo id -u
0
sudo option |
Effect |
|---|---|
-l |
List the commands the invoking user may run. |
-i |
Start a login shell as the target user, in their home directory. |
-u user |
Run the command as user instead of root. |
-n |
Never prompt; fail instead of asking for a password. |
For sustained work, one command per change becomes tedious, so start a root
shell with sudo -i and leave it with exit:
$ sudo -i
root@trixie:~# id -un
root
root@trixie:~# pwd
/root
root@trixie:~# exit
logout
-i gives the shell a login environment: the prompt is root's, the working
directory is /root, and ~ expands there, not to john's home. Keep paths
explicit across that boundary - a cd ~/notes typed from habit in the root
shell refers to /root/notes, which is a different directory from
/home/john/notes.
sudo can also act as other users. The rest of this section uses a second
account, mira, created for the demonstrations; account management itself is
Chapter 5's subject. Create the account and look
at the result. useradd normally prints nothing; -m creates the home
directory and copies the skeleton files, and -s sets the login shell:
$ sudo useradd -m -s /bin/bash mira
$ getent passwd mira
mira:x:1002:1002::/home/mira:/bin/bash
$ sudo passwd -S mira
mira L 2026-11-18 0 99999 7 -1
passwd -S reports the account's password state. The L means locked:
useradd creates accounts without a password, so mira cannot log in
interactively yet. The remaining fields are the last-change date and the
password-aging policy. Now run one command as mira:
$ sudo -u mira id -un
mira
sudo su - combines the two tools: it starts a login shell for the named
user through root, without any target password, because root is exempt from
su's authentication. Used with a command it reports the identity and the
home directory of that user:
$ sudo su - mira -c 'id -un; pwd'
mira
/home/mira
$ sudo su - -c 'id -un; pwd'
root
/root
The interactive form sudo su - drops into a root shell like sudo -i does;
sudo -i is the more direct route and does not depend on su's behavior.
The mira demonstrations end here. Remove the account with
sudo userdel -r mira; the command also deletes the home directory and
reports a missing mail spool on standard error, which is harmless.
Plain su is the traditional alternative. It prompts for the target user's
password - root's, for su -:
$ su -
Password: su: Authentication failure
This attempt supplied no valid password, so su failed and exited with
status 1. Root's password is set on the example system, which is why
passwd -S reports a usable password for it:
$ sudo passwd -S root
root P 2026-05-19 0 99999 7 -1
Typing the correct password at the prompt starts a root login shell; su -
user does the same for another user with that user's password. An account
with a locked password such as mira can never be reached by su from outside
- there is nothing to type - while root reaches any account without a
prompt. If root's password is not set, su - cannot succeed at all, and
the sudo-group arrangement is the only path; a Debian installation with a
blank root password configures exactly that, and
Chapter 2 showed where the choice is made.
sudo |
su - |
|
|---|---|---|
| Authenticates | The invoking user's password | The target user's password |
| Controlled by | The sudoers policy | The target account's password state |
| Runs as | Root, or any user selected with -u |
The named user, default root |
| Typical use | One command, or a shell with -i |
A full login shell when root's password is known |
| Needs configuration | sudoers rules or the sudo group |
A password on the target account |
Never edit /etc/sudoers or files under /etc/sudoers.d directly with a
text editor: a syntax error can lock every administrator out of elevation.
Use visudo, which checks the file before installing it, or drop a file
into /etc/sudoers.d and validate it with visudo -cf. Ownership and
permissions matter as well - the files must be root-owned and not
group-writable. Chapter 5 shows complete
sudoers examples.
3.1.3. Recognizing the Current User, Host, and Working Directory
Every example in this book names the account it runs as and the directory it
runs in; three commands make the shell report both, and the prompt encodes
them continuously. whoami prints just the account name:
$ whoami
john
id is the fuller form - user ID, group ID, and the group list, which is
how the sudo membership from the previous section is visible:
$ id
uid=1001(john) gid=1001(john) groups=1001(john),27(sudo)
The host has its own identity. hostname prints the name; hostname -f
prints the fully qualified domain name, which falls back to the short name
when no domain is configured:
$ hostname
trixie
$ hostname -f
trixie
pwd prints the shell's working directory:
$ pwd
/home/john
| Command | Shows |
|---|---|
whoami |
The account name of the invoking user. |
id |
User ID, primary group, and all group memberships. |
logname |
The account that logged in, even inside a nested shell. |
hostname |
The system's name; -f for the fully qualified name. |
pwd |
The working directory; pwd -P resolves symlinks (Section 3.3.2). |
logname is worth one demonstration because it differs from whoami in a
subtle way: after a later identity change it still names the account that
logged in.
$ logname
john
The prompt encodes the same facts. Debian's default bash prompt is
john@trixie:~$ - user, host, and a ~ standing for the home directory.
Its definition lives in PS1:
$ declare -p PS1
declare -- PS1="\${debian_chroot:+(\$debian_chroot)}\u@\h:\w\$ "
\u is the user, \h the host, \w the working directory, and \$ a $
for ordinary users or # for root - so the root shell in the previous
section showed root@trixie:~#. The debian_chroot expression prints a
chroot's name when the shell is inside one, and nothing otherwise.
Prompts differ between systems and shells; what matters is reading them, not
customizing them.
The login also fixed the environment the shell inherited:
$ env | grep -E '^(HOME|USER|LOGNAME|SHELL)='
SHELL=/bin/bash
LOGNAME=john
HOME=/home/john
USER=john
HOME deserves attention because ~ expands to it: john's ~ is
/home/john, but the root shell's ~ is /root. Section 3.2.3 returns to
environment variables; the next chapters rely on the distinction whenever a
root shell is open.
The examples in this book keep their working files in a dedicated directory under john's home. Create it once and work there:
$ mkdir -p ~/debian-handbook/shell-basics
mkdir -p creates the whole path and succeeds silently if it already
exists. Enter it and confirm:
$ cd ~/debian-handbook/shell-basics
$ pwd
/home/john/debian-handbook/shell-basics
Chapters that follow use their own subdirectories of
~/debian-handbook, so example files never mix with personal files, and
cleanup is one rm -r of a directory you created for the purpose.