Linux Sysadmin · Lesson 10 · 20 min

Shell skills

You know the commands. Now learn the tricks that make you fast: repeating yourself without retyping, making your own shortcuts, finding help without Google, keeping work running after you disconnect, and setting a server's clock and name.

You will learn

  • history, !! and the famous sudo !!
  • Variables, env, export, alias and ~/.bashrc
  • Getting help: --help, man and man -k
  • watch, plus tmux and screen to survive disconnects
  • date, timedatectl and hostnamectl

History: never type it twice

Same on both
history              # every command you've run, numbered
history | grep ssh   # "what was that ssh command yesterday?"
!!                   # run the last command again
sudo !!              # run the last command again, WITH sudo
!42                  # run command number 42 from the history list
!cat                 # run the most recent command that started with "cat"

sudo !! is the one everybody learns first. You type a long command, get “Permission denied,” and instead of retyping you just say “that again, but as admin.” Bash prints the expanded command before running it, so you can see what !! became.

On a real terminal: Ctrl+R

Press Ctrl+R and start typing any part of an old command. Bash searches your history as you type. Press Ctrl+R again for older matches, and Enter to run it. (The playground doesn't have this one, so use history | grep.)

Spot the difference

Ubuntu's default ~/.bashrc sets HISTCONTROL=ignoreboth: repeated commands are saved only once, and commands you start with a space aren't saved at all (handy for commands with a password in them). Rocky doesn't set it, so every command is saved. You can add the line to your own ~/.bashrc on Rocky.

Variables and the environment

A variable is a name that holds a value. $ in front of a name means “put the value here.” Some are set for you, and together they're called the environment:

Same on both
echo $HOME $USER $SHELL     # your home folder, your name, your shell
echo $PATH                  # where bash looks for commands
env                         # the whole environment
PET=cat                     # make a variable (NO spaces around =)
echo "My pet is a $PET"     # double quotes: $ still works
echo 'My pet is a $PET'     # single quotes: printed exactly as typed
export PET                  # share it with programs you start
export EDITOR=nano          # make and export in one go

Want to put a command's output into a variable, like n=$(ls | wc -l)? That's lesson 12. Without export, only your shell can see the variable. With export, every program you start from this shell gets a copy. That's how EDITOR=nano tells crontab -e which editor to open (lesson 5).

Your own shortcuts: alias and ~/.bashrc

Rocky / RHEL
alias update='sudo dnf upgrade'
alias                   # list all aliases
type update             # what is "update"?
Ubuntu / Debian
alias update='sudo apt update && sudo apt upgrade'
alias
type update

Aliases and variables disappear when you log out. To keep them, add the same lines to the end of ~/.bashrc, a hidden settings file that bash runs every time it starts. Run source ~/.bashrc to load it right away. Rocky and Ubuntu start with different ready-made aliases: try alias on each.

Getting help without leaving the terminal

Same on both
ls --help            # quick summary of the options
man ls               # the full manual. / to search, q to quit
man -k compress      # "which command does…?" Searches every manual's description
apropos compress     # same thing as man -k

--help is fast, and almost every command has it. man is the full manual, and it's the source of truth when an AI or a website invents an option that doesn't exist. man -k helps when you know what you want to do but not the command's name.

watch: rerun a command every few seconds

Same on both
watch -n 2 df -h                 # refresh disk space every 2 seconds
watch -n 1 'ss -tn | wc -l'      # count connections live (quote pipes!)

Great for watching a copy fill a disk or a service come back up. Press Ctrl+C to stop.

Surviving disconnects: tmux and screen

You start a two-hour upgrade over SSH, your Wi-Fi drops, and the upgrade is killed halfway. Ouch. A terminal multiplexer keeps a session running on the server. You can disconnect (“detach”), go home, reconnect, and pick up exactly where you left off.

Rocky / RHEL
sudo dnf install tmux
tmux new -s work       # start a named session
# Ctrl+B then D  = detach
tmux ls                # list sessions
tmux attach -t work    # come back

RHEL dropped screen in version 8. It's only in EPEL now (sudo dnf install epel-release first). tmux is the modern choice.

Ubuntu / Debian
tmux new -s work       # already installed
# Ctrl+B then D  = detach
tmux ls
tmux attach -t work
screen -S work         # screen is here too
# Ctrl+A then D  = detach
screen -r work

Ubuntu Server ships both tmux and screen.

tmux can do much more, like splitting the window into panes (Ctrl+B then %), but detach and attach are the part that saves your work.

Time and name: timedatectl and hostnamectl

Same on both
date                                         # the current date and time
date +%F                                     # just 2026-09-27: great for file names
timedatectl                                  # time zone and clock sync status
timedatectl list-timezones | grep America
sudo timedatectl set-timezone America/Chicago
hostnamectl                                  # the name, OS and kernel
sudo hostnamectl set-hostname web01          # rename the server
Spot the difference

The clock is kept accurate over the internet by different programs: chronyd on Rocky and systemd-timesyncd on Ubuntu. timedatectl works the same on both. After renaming an Ubuntu server, also fix the 127.0.1.1 line in /etc/hosts, or sudo complains that it can't find the new name.

Many admins keep servers on UTC, so logs from servers in different countries line up. A school server in one city is fine on local time. Just pick one and be consistent.

Try it: speed run ⚡

Quick check

1. You typed systemctl restart nginx and got “Access denied.” Fastest fix?

2. You made an alias, logged out, and logged back in. It's gone. Why?

3. You're about to start a long upgrade over SSH on flaky Wi-Fi. What should you do first?

4. You want to install screen on Rocky 9, but dnf says “No match for argument: screen.” Why?

Finished the missions and the quiz? Mark it done to track your progress.