Almost every server in the world runs Linux, and so do Docker containers and CI/CD runners. When an app breaks in production at 2 a.m., there's no mouse and no desktop: there's a terminal. Being comfortable with the Linux command line is the first and most important DevOps skill.
This lesson covers the commands you'll actually use, grouped by task: moving around, working with files, permissions, processes, logs, networking, SSH, services and installing software. Each comes with a real example.
How to practise
You don't need a Linux computer:
- Windows: install WSL (Windows Subsystem for Linux) by running
wsl --installin PowerShell as administrator, then open "Ubuntu". - Mac: the Terminal app is very similar; most commands here work as they are.
- Online: GitHub Codespaces or a free cloud server.
Commands are typed after a prompt such as user@server:~$. Below, lines starting with $ are what you type; the rest is output. Don't type the $.
Moving around
$ pwd # print working directory: where am I?
/home/ahana
$ ls # list files
app logs notes.txt
$ ls -la # long format, including hidden files
$ cd app # change directory
$ cd .. # up one level
$ cd ~ # your home directory
$ cd - # back to the previous directory
The Linux file system is one tree starting at / (the "root"). Directories you'll meet often:
| Path | Holds |
|---|---|
/home/<user> | Users' own files (also written ~) |
/etc | System configuration files |
/var/log | Log files |
/tmp | Temporary files, cleared on reboot |
/usr/bin, /bin | Programs |
/opt | Extra software you install |
Press Tab to complete names, and the up arrow to repeat earlier commands. They'll save you hours.
Working with files
$ mkdir -p projects/api # make a directory, and its parents
$ touch notes.txt # create an empty file
$ cp notes.txt backup.txt # copy
$ cp -r projects projects-old # copy a directory (-r: recursive)
$ mv backup.txt old.txt # move or rename
$ rm old.txt # delete a file
$ rm -r projects-old # delete a directory
rmdoesn't use a recycle bin: deleted means gone. Be very careful withrm -rf, and never run it with a path you haven't double-checked.
Reading files
$ cat config.yml # print a whole file
$ less /var/log/syslog # scroll through a long file; q to quit
$ head -n 20 app.log # first 20 lines
$ tail -n 50 app.log # last 50 lines
$ tail -f app.log # follow: show new lines as they're written
tail -f is the command you'll use most when debugging a live server: start it, reproduce the bug, and watch the errors appear.
Editing files
nano config.yml opens a simple editor: the shortcuts are shown at the bottom (Ctrl+O saves, Ctrl+X exits). You'll also meet vim, which is powerful but confusing at first. To escape it, press Esc, then type :q! and Enter to quit without saving.
Searching
$ grep "ERROR" app.log # lines containing ERROR
$ grep -i "timeout" app.log # ignore case
$ grep -r "DATABASE_URL" . # search every file under here
$ grep -c "ERROR" app.log # count matching lines
$ find . -name "*.log" # find files by name
$ find /var/log -size +100M # files over 100 MB
Pipes: combining commands
The | (pipe) sends one command's output into the next. This is where the command line becomes powerful:
$ grep "ERROR" app.log | wc -l # how many errors?
$ cat access.log | cut -d' ' -f1 | sort | uniq -c | sort -rn | head -5
The second line finds the 5 IP addresses that made the most requests: take the first field of each line, sort, count duplicates, sort by count, show the top 5. Each small command does one job; together they answer a real question.
Redirect output to a file with > (overwrite) or >> (append): grep ERROR app.log > errors.txt.
Permissions
Run ls -l and you'll see something like:
-rwxr-xr-- 1 ahana devops 2048 Dec 2 10:15 deploy.sh
The first part, rwxr-xr--, is three groups of three: what the owner (ahana), the group (devops) and everyone else can do. r = read, w = write, x = execute.
$ chmod +x deploy.sh # make a script executable
$ ./deploy.sh # run it
$ chmod 600 secret.key # only the owner can read and write
$ chown ahana:devops app.log # change owner and group
The numbers in chmod 600 add up per group: read = 4, write = 2, execute = 1. So 7 is rwx, 6 is rw-, 5 is r-x, 4 is r--. 755 is common for scripts and directories, 644 for normal files, and 600 for private keys. SSH refuses to use a private key that others can read.
sudo runs a command as the administrator (root): sudo apt update. Use it only when needed.
Processes
$ ps aux | grep python # find running Python processes
$ top # live CPU and memory use; q to quit
$ htop # a friendlier top, if installed
$ kill 4821 # ask process 4821 to stop
$ kill -9 4821 # force it to stop, as a last resort
Disk and memory
$ df -h # free disk space on each drive, human-readable
$ du -sh logs/ # size of a directory
$ free -h # memory use
"The disk is full" is one of the most common causes of production outages, usually because of log files that grew forever. df -h and du -sh find it in seconds.
Networking
$ curl https://api.example.com/health # make an HTTP request
$ curl -I https://example.com # show only the response headers
$ ping -c 4 example.com # is the host reachable?
$ ss -tlnp # which ports are programs listening on?
$ nslookup example.com # DNS lookup
curl is your best friend for testing APIs and health checks from a server. ss -tlnp answers "is my app actually listening on port 8000?"
SSH: logging in to servers
$ ssh-keygen -t ed25519 -C "ahana@laptop" # create a key pair, once
$ ssh ahana@203.0.113.10 # log in to a server
$ scp app.zip ahana@203.0.113.10:/opt/app/ # copy a file to it
ssh-keygen makes two files: a private key that never leaves your computer, and a public key (.pub) that you put on servers. Key-based login is safer than passwords, and is what CI/CD pipelines use to deploy.
Services and logs
Servers run apps as services managed by systemd, so they start on boot and restart if they crash:
$ sudo systemctl status nginx # is it running?
$ sudo systemctl restart nginx # restart it
$ sudo systemctl enable nginx # start it on boot
$ journalctl -u nginx -f # follow the service's logs
Installing software
$ sudo apt update # refresh the package list (Ubuntu, Debian)
$ sudo apt install -y nginx git # install packages
Red Hat-based systems (RHEL, Amazon Linux, Fedora) use dnf (or yum) instead of apt. Alpine Linux, common in Docker images, uses apk.
Environment variables
$ export APP_ENV=production # set for this session
$ echo $APP_ENV # read it
production
$ env | grep APP # list matching variables
Apps read settings like database addresses from environment variables, which keeps secrets out of the code, as explained in secrets and environment variables in GitHub Actions.
Practice
- Your app's log is at
/var/log/myapp/app.log. Show the last 100 lines, then watch new lines as they arrive.
Show the answer
tail -n 100 /var/log/myapp/app.log
tail -f /var/log/myapp/app.log
- The server says "No space left on device". Which commands help you find out why?
Show the answer
df -h to see which drive is full, then du -sh /var/log/* (or du -sh /*) to find the largest directories. Old logs are the usual culprit.
- You wrote
backup.sh, but./backup.shsays "Permission denied". Fix it.
Show the answer
chmod +x backup.sh
./backup.sh
The file needs execute permission.
- Count how many lines in
app.logcontain the word "timeout", in any case.
Show the answer
grep -ci "timeout" app.log
Interview questions
What does chmod 755 mean?
The owner can read, write and execute (7 = 4 + 2 + 1); the group and others can read and execute (5 = 4 + 1). It's typical for scripts and directories.
How do you find which process is using a port?
ss -tlnp | grep :8080 (or sudo lsof -i :8080) shows the listening process and its PID.
What is the difference between kill and kill -9?
kill sends SIGTERM, asking the process to shut down cleanly. kill -9 sends SIGKILL, which ends it immediately without cleanup; use it only if SIGTERM fails.
How do you follow a log file in real time?
tail -f file.log, or journalctl -u service -f for systemd services.
What to learn next
- Work with your team's code in the Git branching workflow for teams.
- Revisit the big picture in what is DevOps?