Lab: living on a remote server - editors, sudoedit, startup files, job control and tmux
In a throwaway container with its own SSH server: fix a missing alias by understanding login and non-login startup files, edit a root-owned file with sudoedit, use job control, then drop an SSH connection on purpose and see which of four jobs survive and why.
Contentsand lesson path
On this page
- Goal and setup
- Step 1: A user, a key and an SSH server
- Step 2: Which startup file does each shell read?
- Step 3: History search and completion
- Step 4: Edit a file with vim or nano
- Step 5: Edit a root-owned file with sudoedit
- Step 6: Job control: suspend, background, foreground
- Step 7: Predict, then drop the connection
- Step 8: Come back and collect the result
- Check your understanding
- Apply it
- Review prompt
Lessons of Advanced Shell & Text Processing
- Patterns that travel: regular expressions in grep, sed and awk Concept · 25 min
- Lab: from a raw access log to a report with grep, sed and awk Lab · 30 min
- Bash beyond the basics: expansions, arrays, here-documents and process substitution Worked example · 25 min
- When a script misbehaves: tracing, syntax checks, safe input and portability to dash Worked example · 20 min
- Lab: living on a remote server - editors, sudoedit, startup files, job control and tmux Lab · 30 min
- Check: shell fluency tasks and a log-report script to repair Knowledge check · 10 questions
What you will learn
- Manage long-running work with job control (jobs, fg, bg, disown), nohup and tmux, and explain how SIGHUP and process groups decide what survives a logout.
- Edit configuration files on a remote server with vim or nano and sudoedit, and configure the interactive shell with startup files, aliases, history search and completion.
Prerequisites: Scripting Basics
01. Goal and setup
Three things often go wrong on a server you reach over SSH: an alias is missing after login, a root-owned file must be edited without running an editor as root, and a long job dies when the Wi-Fi drops. This lab makes each happen on purpose.
The principle: what survives a disconnect is decided by sessions, process groups and the SIGHUP ("hang up") signal, not by luck. A session is the set of processes started from one login, with one controlling terminal; each job is a process group inside it. Signals were introduced in processes, signals and resource usage, SSH in remote work over SSH.
You need Docker. Everything runs as root inside a throwaway Debian 13 container (the python:3.11.14-slim image is built on Debian 13) with its own SSH server and a user ops; never practise sudoers changes on a shared server.
docker run --rm -it --init --hostname lab --name shell-lab python:3.11.14-slim bashapt-get update -qq
apt-get install -y -qq --no-install-recommends tmux sudo vim-tiny nano procps \
openssh-server openssh-client >/dev/null--init adds a small init process that reaps exited processes, so killed jobs do not linger as zombies in ps. Later blocks run in the container as root.
02. Step 1: A user, a key and an SSH server
Create ops, give root a key that ops accepts, start sshd and test a key login. accept-new trusts the server key on first contact, acceptable only because the server is your own container. -n stops ssh from reading standard input, which would swallow the lines pasted after it.
useradd -m -s /bin/bash ops
mkdir -p /run/sshd ~/.ssh /home/ops/.ssh
ssh-keygen -q -t ed25519 -N '' -f ~/.ssh/id_ed25519
cp ~/.ssh/id_ed25519.pub /home/ops/.ssh/authorized_keys
chown -R ops:ops /home/ops/.ssh; chmod 700 /home/ops/.ssh
ssh-keygen -A >/dev/null; /usr/sbin/sshd
printf '%s\n' '#!/bin/sh' 'for i in 1 2 3; do echo "tick $i"; sleep 1; done' \
'sleep 1004' > /home/ops/ticker.sh
chown ops:ops /home/ops/ticker.sh; chmod 755 /home/ops/ticker.sh
ssh -n -o StrictHostKeyChecking=accept-new ops@localhost 'id -un; echo "$0"'Warning: Permanently added 'localhost' (ED25519) to the list of known hosts.
ops
bashticker.sh is the long job for step 7: it prints three lines, then waits. $0 is bash without a dash: a command sent over SSH runs in a shell that is neither a login shell nor interactive.
03. Step 2: Which startup file does each shell read?
An interactive login shell, the kind an SSH login gives you, reads /etc/profile and then the first of ~/.bash_profile, ~/.bash_login and ~/.profile that exists. An interactive non-login shell, such as one you start by typing bash, reads ~/.bashrc. A script reads neither. tmux starts login shells by default, so a tmux window reads ~/.bash_profile too. Aliases therefore belong in ~/.bashrc, and ~/.bash_profile should load it.
The block gives ops Debian's default files plus an alias and a marker line, then a ~/.bash_profile that does not load ~/.bashrc, as happens when a profile is copied from another system. Predict: in which of the four shells is ll defined?
cp /etc/skel/.bashrc /etc/skel/.profile /home/ops/
echo 'echo "[.bashrc]"; alias ll="ls -alF"' >> /home/ops/.bashrc
printf '%s\n' 'echo "[.bash_profile]"' 'export EDITOR=nano' > /home/ops/.bash_profile
chown ops:ops /home/ops/.bashrc /home/ops/.bash_profile /home/ops/.profile
probe='alias ll >/dev/null 2>&1 && echo "ll: defined" || echo "ll: missing"'
echo '== login shell (like an SSH login)'
su ops -c "cd; bash -li -c '$probe'" 2>/dev/null
echo '== new tmux window (tmux defaults)'
su ops -c "cd; tmux new-session -d -s p -x 150; sleep 0.5
tmux send-keys -t p '$probe' Enter; sleep 0.5
tmux capture-pane -p -t p | grep -E '^(\[|ll:)'; tmux kill-server"
echo '== interactive non-login shell (like typing bash)'
su ops -c "cd; bash -i -c '$probe'" 2>/dev/null
echo '== script (non-interactive)'
su ops -c "cd; bash -c '$probe'"== login shell (like an SSH login)
[.bash_profile]
ll: missing
== new tmux window (tmux defaults)
[.bash_profile]
ll: missing
== interactive non-login shell (like typing bash)
[.bashrc]
ll: defined
== script (non-interactive)
ll: missingThe login shell and the tmux window found ~/.bash_profile, stopped looking, and never read ~/.bashrc. The fix is one line in ~/.bash_profile; Debian's ~/.bashrc returns early in non-interactive shells, so loading it is harmless for scripts.
echo '[ -f ~/.bashrc ] && . ~/.bashrc' >> /home/ops/.bash_profile
su ops -c "cd; bash -li -c '$probe'" 2>/dev/null[.bash_profile]
[.bashrc]
ll: defined04. Step 3: History search and completion
History search: Ctrl+R, then part of an earlier command; Ctrl+R again goes further back, Enter runs the match, Esc leaves it for editing. Completion: Tab completes names, and the complete builtin defines what to offer for a command, here an imaginary svc. Debian's ~/.bashrc sets HISTCONTROL=ignoreboth: a repeat of the previous command, or one typed after a leading space, is not saved.
su ops -c 'cd; bash -i' 2>/dev/null <<'IN'
bind -P | grep -E '^(reverse-search-history|complete) '
echo "HISTCONTROL=$HISTCONTROL HISTSIZE=$HISTSIZE"
complete -W 'start stop status reload' svc
compgen -W 'start stop status reload' -- st
IN[.bashrc]
complete can be found on "\C-i", "\e\e\000".
reverse-search-history can be found on "\C-r".
HISTCONTROL=ignoreboth HISTSIZE=1000
start
stop
statuscompgen prints what Tab would offer after svc st; the bash-completion package ships such definitions for real commands.
05. Step 4: Edit a file with vim or nano
In vim (here vi, the small vim.tiny build) you start in normal mode, where keys are commands: /text searches, n finds the next match, cw changes a word, i inserts text, Esc returns to normal mode, dd deletes a line, u undoes, :wq writes and quits, and :q! quits without saving. :%s/old/new/g replaces on every line. In nano the commands are listed at the bottom: Ctrl+O writes, Ctrl+X exits, Ctrl+W searches.
The block first runs colon commands in vim's ex mode (-es), without a screen. Then by hand: in vi /tmp/shop.conf type /8080, Enter, cw, 8081, Esc, :wq, Enter.
mkdir -p /etc/shop && printf 'listen 8080\nworkers 2\n' > /etc/shop/shop.conf
cp /etc/shop/shop.conf /tmp/shop.conf
vi -es -c '%s/^workers 2$/workers 4/' -c 'wq' /tmp/shop.conf
cat /tmp/shop.conflisten 8080
workers 4vi /tmp/shop.conf # in vi: /8080 Enter, cw, 8081, Esc, :wq Enter
grep listen /tmp/shop.conflisten 808106. Step 5: Edit a root-owned file with sudoedit
ops must change the root-owned /etc/shop/shop.conf. sudo vi would run the whole editor as root, and an editor can start a shell or plugins with those rights. sudoedit (sudo -e) copies the file to a temporary file owned by you, runs your editor as you, and copies the result back as root only if it changed. The sudoers rule names one file; the Linux users and permissions skill covers such rules, and visudo -c checks them.
A stand-in editor prints who runs it, then changes 8080 to 9090; a second run uses true, which changes nothing. -S reads a lab-only password from standard input; a real server prompts for it.
echo 'ops ALL=(root) sudoedit /etc/shop/shop.conf' > /etc/sudoers.d/ops-shop
chmod 440 /etc/sudoers.d/ops-shop; visudo -cq && echo 'sudoers OK'
cat > /usr/local/bin/show-editor <<'SH'
#!/bin/sh
echo "editor runs as $(id -un), editing $1"
sed -i 's/^listen 8080$/listen 9090/' "$1"
SH
chmod 755 /usr/local/bin/show-editor
echo 'ops:lab-only-pass' | chpasswd
su ops -c 'echo lab-only-pass | SUDO_EDITOR=show-editor sudoedit -S /etc/shop/shop.conf'
su ops -c 'echo lab-only-pass | SUDO_EDITOR=true sudoedit -S /etc/shop/shop.conf'
cat /etc/shop/shop.conf; ls -l /etc/shop/shop.conf | cut -d' ' -f1,3,4
su ops -c 'echo "workers 9" >> /etc/shop/shop.conf' 2>&1sudoers OK
[sudo] password for ops: editor runs as ops, editing /var/tmp/shopSYrbPrXO.conf
[sudo] password for ops: sudoedit: /etc/shop/shop.conf unchanged
listen 9090
workers 2
-rw-r--r-- root root
bash: line 1: /etc/shop/shop.conf: Permission deniedThe editor ran as ops on a copy in /var/tmp; the real file changed and is still root's, so ops still cannot write it directly. With true nothing changed, so sudoedit left the file alone. It picks the editor from SUDO_EDITOR, then VISUAL, then EDITOR (step 2 exported EDITOR=nano), and it checks no syntax: run the service's own check, such as nginx -t, before reloading.
07. Step 6: Job control: suspend, background, foreground
Ctrl+Z stops the foreground job, bg resumes it in the background, fg brings it back, jobs -l lists jobs with PIDs and disown removes one from the shell's job table. The shell does not deliver Ctrl+C or Ctrl+Z: the kernel's terminal driver sends SIGINT or SIGTSTP to every process in the terminal's foreground process group. With job control on, a pipeline is one job and one group, so all its processes get the signal; the interactive shell, in a group of its own, gets nothing. The block types the commands into a tmux pane and prints the screen.
tmux new-session -d -s jc -x 88 -y 40 'bash --norc -i'; sleep 0.5
tmux send-keys -t jc "PS1='$ '" Enter 'clear' Enter; sleep 0.4
tmux send-keys -t jc 'sleep 500' Enter; sleep 0.5; tmux send-keys -t jc C-z; sleep 0.5
for c in 'jobs -l' 'bg %1' 'sleep 600 &' 'disown %2' 'sleep 700 | cat &' 'jobs -l' \
'ps -o pid,pgid,tpgid,stat,args -p $$ --ppid $$'; do
tmux send-keys -t jc "$c" Enter; sleep 0.5
done
tmux send-keys -t jc 'fg %1' Enter; sleep 0.5; tmux send-keys -t jc C-c; sleep 0.4
tmux send-keys -t jc 'echo "status $?"' Enter; sleep 0.4
tmux capture-pane -p -t jc | grep -v '^$'; tmux kill-server$ sleep 500
^Z
[1]+ Stopped sleep 500
$ jobs -l
[1]+ 821 Stopped sleep 500
$ bg %1
[1]+ sleep 500 &
$ sleep 600 &
[2] 830
$ disown %2
$ sleep 700 | cat &
[2] 836
$ jobs -l
[1]- 821 Running sleep 500 &
[2]+ 835 Running sleep 700
836 | cat &
$ ps -o pid,pgid,tpgid,stat,args -p $$ --ppid $$
PID PGID TPGID STAT COMMAND
815 815 841 Ss bash --norc -i
821 821 841 S sleep 500
830 830 841 S sleep 600
835 835 841 S sleep 700
836 835 841 S cat
841 841 841 R+ ps -o pid,pgid,tpgid,stat,args -p 815 --ppid 815
$ fg %1
sleep 500
^C
$ echo "status $?"
status 130
$Each job is its own process group, with the job's first PID as PGID: sleep 700 and cat share 835. TPGID, the terminal's foreground group, is 841, the ps itself, not the shell's 815: a Ctrl+C at that moment would reach ps alone. The shell no longer lists or signals the disowned sleep 600. Status 130 is 128 + 2 (SIGINT). Kill the leftover: kill 830 (your PID).
08. Step 7: Predict, then drop the connection
From a tmux pane that plays your laptop's terminal, root logs in to ops over SSH and starts four long jobs:
sleep 1001 &;nohup sleep 1002 >/dev/null 2>&1 &;sleep 1003 & disown;tmux new -d -s work ./ticker.sh.
Then the block kills the SSH client with SIGKILL. To the server this looks like a lost link: ops's terminal hangs up. Predict: which jobs will still be in the ps listing, and why?
tmux new-session -d -s client -x 88 -y 30 'ssh -tt ops@localhost'; sleep 1.5
for c in 'sleep 1001 &' 'nohup sleep 1002 >/dev/null 2>&1 &' 'sleep 1003 & disown' \
'tmux new -d -s work ./ticker.sh'; do
tmux send-keys -t client "$c" Enter; sleep 0.7
done
ps -o pid,pgid,sid,tty,stat,args -u ops
echo '--- the link drops: SIGKILL the ssh client ---'
kill -KILL "$(pgrep -x ssh)"; sleep 1.5
ps -o pid,pgid,sid,tty,stat,args -u ops PID PGID SID TT STAT COMMAND
864 857 857 ? S sshd-session: ops@pts/2
865 865 865 pts/2 Ss+ -bash
870 870 865 pts/2 S sleep 1001
874 874 865 pts/2 S sleep 1002
877 877 865 pts/2 S sleep 1003
882 882 882 ? Ss tmux new -d -s work ./ticker.sh
883 883 883 pts/3 Ss+ /bin/sh ./ticker.sh
884 883 883 pts/3 S+ sleep 1
--- the link drops: SIGKILL the ssh client ---
PID PGID SID TT STAT COMMAND
874 874 865 ? S sleep 1002
877 877 865 ? S sleep 1003
882 882 882 ? Ss tmux new -d -s work ./ticker.sh
883 883 883 pts/3 Ss+ /bin/sh ./ticker.sh
889 883 883 pts/3 S+ sleep 1Before the drop, the login shell -bash leads the session (its PID equals the SID) on terminal pts/2, and the three sleeps sit in that session, each in its own process group. When pts/2 hung up, the kernel sent SIGHUP to -bash, and Bash sent SIGHUP to every job in its table and exited.
sleep 1001was in the table and did not ignore SIGHUP, so it died.sleep 1002was in the table, butnohuphad set SIGHUP to "ignore" before starting it, and that setting survivesexec.sleep 1003had been disowned, so Bash never signalled it.- The tmux processes have their own sessions (SIDs 882 and 883) and terminal,
pts/3, which belongs to the tmux server, not to SSH.
Simplification: orphaned process groups and Bash's huponexit option (SIGHUP on a normal exit too, off by default) also play a part, without changing this result.
09. Step 8: Come back and collect the result
Log in again and look for the session. Interactively you would run tmux attach -t work and detach with Ctrl+B then D; the block lists the session and prints its screen.
ssh -n ops@localhost 'tmux ls -F "#{session_name}: #{session_windows} window(s)"
tmux capture-pane -p -t work | grep tick'work: 1 window(s)
tick 1
tick 2
tick 3nohup and disown keep a job alive, but you cannot return to it, and nohup sends output meant for the terminal to nohup.out. tmux keeps a terminal to return to. Work that must survive a reboot or restart on failure belongs to a service manager, such as the systemd timers in the idempotent backup job lab. Leave with exit; --rm deletes the container.
10. Check your understanding
11. Apply it
Finish the skill's practice task in the container: copy your logreport.sh to ops's home, start it in a tmux session over SSH, kill the SSH client as in step 7, log in again and collect the report. Then answer in writing: a colleague's six-hour export must survive their laptop sleeping and restart if the server reboots. Which of nohup, disown, tmux and a systemd unit or timer would you choose, and what would you give up?
12. Review prompt
Without looking back, explain the chain from "the network link drops" to "sleep 1001 is gone", naming the session, the process group and the signal, and why the other three jobs survived. Then explain which processes a Ctrl+C reaches, why an alias in ~/.bashrc can be missing in an SSH login, and why sudoedit is safer than an editor run with sudo.
References
Your progress
Reading state
Sign in to save that you finished reading this lesson.
Prove this skill
10 questions · about 8 minutes · scored automatically. Marking a lesson as read never demonstrates a skill.