3.6. Getting Help and Investigating Command Failures
Two habits keep a system administrator effective without memorizing anything: asking the system how a command works before using it, and reading failures precisely enough to know which component failed. Debian installs its own documentation with every package; this section covers how to reach it and how to read what went wrong.
3.6.1. Reading Manual Sections and Command Usage
Manual pages are divided into numbered sections, and the same name can have
a page in several of them. passwd is both a command (how you change a
password) and a file format (/etc/passwd) - and asking for "the passwd
page" is therefore ambiguous until you know the sections:
| Section | Subject |
|---|---|
| 1 | User commands. |
| 5 | File formats and configuration files. |
| 6 | Games. |
| 7 | Overviews and conventions (for example hier, the filesystem layout). |
| 8 | System administration commands, usually run as root. |
man -w prints which page a request resolves to, without opening it:
$ man -w passwd
/usr/share/man/man1/passwd.1.gz
With no section given, the search takes the lowest-numbered match - section 1 here. Name the section to reach the others:
$ man -w 5 passwd
/usr/share/man/man5/passwd.5.gz
$ man -w 8 sudo
/usr/share/man/man8/sudo.8.gz
And a page that does not exist in the section you asked for is its own answer - the status is 16, man's code for "nothing found":
$ man -w 1 sudo
No manual entry for sudo in section 1
$ man -w 8 passwd
No manual entry for passwd in section 8
Read the fifth-section page with the section number as the first argument.
In a transcript, col -b strips the formatting that a terminal would
render, and sed -n "1,8p" bounds the excerpt:
$ man 5 passwd | col -b | sed -n "1,8p"
PASSWD(5) File Formats and Configuration PASSWD(5)
NAME
passwd - the password file
DESCRIPTION
/etc/passwd contains one line for each user account, with seven fields
delimited by colons (“:”). These fields are:
The header names the page and its section - a habit worth copying when you
cite a manual page, because passwd(1) and passwd(5) are different
documents. When you only know a topic, search the one-line descriptions
instead of guessing names. whatis matches exact page names:
$ whatis passwd
passwd (1) - change user password
passwd (1ssl) - OpenSSL application commands
passwd (5) - the password file
apropos (the same tool as man -k) searches descriptions with a regular
expression:
$ apropos "^passwd$"
passwd (1) - change user password
passwd (1ssl) - OpenSSL application commands
passwd (5) - the password file
The listing shows every section the name exists in - including 1ssl, a
name collision with OpenSSL's command list. Inside man, q quits,
/text searches forward, and n finds the next match - the same keys as
less, because man uses it.
Two smaller helps cover what manual pages do not. Shell builtins have no page of their own; bash documents them itself:
$ help cd | head -n 4
cd: cd [-L|[-P [-e]] [-@]] [dir]
Change the shell working directory.
Change the current directory to DIR. The default DIR is the value of the
And most external commands summarize their options with --help, which is
faster than a manual page when you know the command and only forgot a flag:
$ ls --help | head -n 4
Usage: ls [OPTION]... [FILE]...
List information about the FILEs (the current directory by default).
Sort entries alphabetically if none of -cftuvSUX nor --sort is specified.
| Command | Answers |
|---|---|
man name / man N name |
The full manual page; section N when given. |
man -w name |
Which page file the request resolves to. |
whatis name |
One-line descriptions of every page with that name. |
apropos pattern |
Pages whose name or description matches. |
help name |
Documentation for a bash builtin. |
cmd --help |
The command's own option summary. |
Manual pages live compressed under /usr/share/man, and each package
installs the pages for its own commands. Larger documentation - release
notes, READMEs, licenses - is under /usr/share/doc/package/, also
installed by the package.
Chapter 1 surveys these sources together with the
info manuals and Debian's web documentation.
3.6.2. Distinguishing Shell Errors from Program Errors
When a command fails, the first question is who is speaking. The shell prints its own errors with its own name; a program that runs and fails prints with its name; the two require different repairs. One session collects the common cases. First a small non-executable script in the workspace to complete the set:
$ cd ~/debian-handbook/shell-basics
$ printf '#!/bin/sh\necho hello\n' > hello.sh
Both are silent. Now a session that collects the common failures and the status each one leaves behind - the transcript's banner is omitted:
$ ssh john@127.0.0.1
john@trixie:~$ cd ~/debian-handbook/shell-basics
john@trixie:~/debian-handbook/shell-basics$ nosuchcommand
-bash: nosuchcommand: command not found
john@trixie:~/debian-handbook/shell-basics$ echo status=$?
status=127
john@trixie:~/debian-handbook/shell-basics$ ls /no/such
ls: cannot access '/no/such': No such file or directory
john@trixie:~/debian-handbook/shell-basics$ echo status=$?
status=2
john@trixie:~/debian-handbook/shell-basics$ bash no-script.sh
bash: no-script.sh: No such file or directory
john@trixie:~/debian-handbook/shell-basics$ echo status=$?
status=127
john@trixie:~/debian-handbook/shell-basics$ ./hello.sh
-bash: ./hello.sh: Permission denied
john@trixie:~/debian-handbook/shell-basics$ echo status=$?
status=126
john@trixie:~/debian-handbook/shell-basics$ chmod +x hello.sh
john@trixie:~/debian-handbook/shell-basics$ ./hello.sh
hello
john@trixie:~/debian-handbook/shell-basics$ exit
logout
Connection to 127.0.0.1 closed.
Read each failure by its speaker. -bash: nosuchcommand: command not
found comes from the shell - the leading -bash is the login shell from
Section 3.1.1 - and means the lookup itself failed: no such program, no
function, nothing in PATH. Status 127 is the shell's code for exactly
that. ls: cannot access '/no/such' comes from the program: the shell
found ls and ran it, and ls could not open the argument; its status is
2. bash: no-script.sh: No such file or directory looks like a shell
error but is bash acting as a program - you invoked it with a script
name it could not open, so it is another 127. And -bash: ./hello.sh:
Permission denied is the shell relaying the kernel's refusal: the file
exists and was requested for execution, but lacks execute permission -
status 126.
The repair for each follows from the diagnosis: install or correct the
name for 127; check the path and its spelling for 2; chmod +x for 126 -
which is exactly what the session does, after which the script prints its
greeting. The script runs under /bin/sh, dash, because its first line
says so (Section 3.2).
| Status | Meaning | From the transcript |
|---|---|---|
| 0 | Success | ./hello.sh after chmod +x |
| 1 | Generic failure | grep -q finding no match (Section 3.4.3) |
| 2 | Program could not process its input | ls /no/such |
| 126 | Found but not executable | ./hello.sh before chmod |
| 127 | Not found | nosuchcommand, bash no-script.sh |
| 128 + N | Killed by signal N | 143 for SIGTERM (Section 3.2.4) |
When a name that should work does not, ask the shell what it resolves to and whether it is installed at all:
$ type -a ls; command -v nosuchcommand || echo not-found
ls is /usr/bin/ls
ls is /bin/ls
not-found
type -a lists every file that ls could mean - both paths are the same
program through the usrmerge link from Section 3.3.1 - and the
command -v name || echo pattern is how a script checks for a dependency
before using it.
3.6.3. Collecting Error Messages Without Exposing Secrets
Diagnosing a failure means showing someone the evidence: the command, its output, and often the system log. The discipline is to collect exactly that and nothing more, because terminals, traces, and logs record whatever passes through them - including passwords, tokens, and keys that happened to be in scope.
The hazard is easiest to see in a shell script. Create a small script in
an errors directory that switches on tracing, sets a variable, and prints
it - the variable stands in for a real secret:
$ mkdir -p errors && cd errors
$ cat > secret-demo.sh <<'EOF'
#!/bin/bash
set -x
token=example-not-real
printf "token=%s\n" "$token"
EOF
The here-document is silent, like every creation with cat >. What the
script does matters: set -x turns on tracing, printing each command to
standard error after expansion. Run it with both streams captured to
files, reporting the status:
$ bash secret-demo.sh > trace.out 2> trace.err; echo "status=$?"
status=0
The program's own output, on standard output:
$ cat trace.out
token=example-not-real
And the trace, on standard error:
$ cat trace.err
+ token=example-not-real
+ printf 'token=%s\n' example-not-real
The trace shows the value twice - once at the assignment, once expanded
inside the printf. That is the leak: a set -x trace of anything that
handles a real token, password, or key records the secret in a plain file,
and pasting such a trace into a bug report or a chat publishes it. Two
practices keep evidence clean:
- Filter before sharing.
grep -vdrops the lines that mention the secret:
$ grep -v "token=" trace.err; echo "status=$?"
status=1
Nothing printed, and the status 1 is grep's "no lines selected" - every
trace line contained the token, so there was nothing left to show. The
filter proves the file is unsafe in its entirety; when that happens,
produce fresh evidence instead of sharing the contaminated file.
- Keep secrets out of the traced path in the first place: read them from
a file or a prompt rather than a command-line argument (arguments also
appear in ps output), and bracket the sensitive section with set +x
to suspend tracing around it.
The same discipline applies to error reports. Capture both streams of the failing command to files, and the report writes itself:
$ ls /no/such 2> ls.err > ls.out; echo "status=$?"
status=2
$ cat ls.err
ls: cannot access '/no/such': No such file or directory
$ wc -c < ls.out
0
The status, the diagnostic, and the fact that standard output was empty (0 bytes) together say precisely what happened: the program was invoked, failed on its argument, and produced no results. Nothing else is needed - and nothing else is included.
For failures that involve the system rather than one command, the journal
holds the recent record, newest last. -b limits the output to the current
boot, --no-pager prints instead of paging, and -n 5 keeps it short:
$ journalctl -b --no-pager -n 5
Nov 18 08:21:52 trixie sshd-session[339330]: Disconnected from user john 127.0.0.1 port 55098
Nov 18 08:21:54 trixie sshd-session[339366]: Received disconnect from 127.0.0.1 port 42998:11: disconnected by user
Nov 18 08:21:54 trixie sshd-session[339366]: Disconnected from user john 127.0.0.1 port 42998
Nov 18 08:21:57 trixie sshd-session[339402]: Received disconnect from 127.0.0.1 port 43010:11: disconnected by user
Nov 18 08:21:57 trixie sshd-session[339402]: Disconnected from user john 127.0.0.1 port 43010
Hint: You are currently not seeing messages from other users and the system.
Users in groups 'adm', 'systemd-journal' can see all messages.
Pass -q to turn off this notice.
The five entries are john's own session traffic - connect and disconnect
records, the same events who and SSH_CONNECTION showed from the inside.
The hint that follows goes to standard error: an ordinary user sees only
their own journal, and membership in adm or systemd-journal widens the
view. Chapter 8 covers the journal in depth, and
Chapter 5 the group grants.
The kernel keeps its own short log, readable as root, of errors and warnings since boot:
$ sudo dmesg --level=err,warn | tail -n 5
[71028.838975] systemd-ssh-generator[161293]: Failed to query local AF_VSOCK CID: Cannot assign requested address
[71037.464324] systemd-ssh-generator[161394]: Failed to query local AF_VSOCK CID: Cannot assign requested address
[71199.899157] systemd-ssh-generator[166746]: Failed to query local AF_VSOCK CID: Cannot assign requested address
[71202.647932] systemd-ssh-generator[166861]: Failed to query local AF_VSOCK CID: Cannot assign requested address
[71203.159394] systemd-ssh-generator[166917]: Failed to query local AF_VSOCK CID: Cannot assign requested address
dmesg prints the kernel ring buffer; --level=err,warn keeps errors and
warnings; tail -n 5 bounds it. The bracketed number is seconds since
boot. These particular warnings come from systemd's SSH socket generator
probing for a virtual-socket address that this KVM guest does not provide -
noise, but the kind of noise an administrator learns to recognize.
Hardware errors, filesystem errors, and driver failures land in this
buffer; Chapter 7 and
Chapter 12 return to it where those subjects need it.
Both sources need the same last step before anything is shared: read the excerpt, and check it for host names, addresses, user names, and secrets that the investigation does not require. A diagnostic that names a service and a timestamp is usually enough; the rest is someone else's data.