Module 1 · 1.4
English
Finding your way around the command line
Module 1: Your first server · Reading time: 11 minutes
Learning objective: After this lesson you can find your way around a Linux server: move through directories, read and edit files, see how much disk space and memory is free, and look up help for a command yourself.
Key concepts: shell, prompt, directory and path, absolute and relative path, /etc, /var/log, /home, ls -l, cd, cat, less, nano, man, --help, tab completion, uname, df, free, uptime, pipe, grep
The shell and the prompt
After logging in, you are talking to the shell: the program that reads and runs your commands. On Debian and Ubuntu that is usually bash. To the left of the cursor is the prompt:
anna@web1:~$
root@web1:~#It tells you three things: who you are (anna or root), which server you are on (web1) and which directory you are in (~ is your own home directory). The last character matters: $ means an ordinary user, # means root. If you see #, every command runs with full privileges. Type carefully there.
Directories and paths
Everything on Linux hangs from a single tree that starts at / (the root directory, not to be confused with the user root). You will come across a few directories again and again in this course:
| Directory | What it contains |
|---|---|
/etc | Configuration: SSH (/etc/ssh), web server (/etc/nginx), users (/etc/passwd) |
/var/log | Log files (alongside the journal, lesson 6.2) |
/var/lib | Program data, for example databases and Docker |
/home | Home directories of ordinary users, such as /home/anna |
/root | Home directory of root |
/usr | Installed programs and libraries |
/tmp | Temporary files, cleaned up automatically |
/srv or /var/www | Usual place for websites and services |
An absolute path starts at /, for example /etc/ssh/sshd_config. A relative path starts where you are now: if you are in /etc, then ssh/sshd_config is the same file. .. is the directory above, . the current directory.
anna@web1:~$ pwd
/home/anna
anna@web1:~$ cd /etc/ssh
anna@web1:/etc/ssh$ ls
moduli ssh_config ssh_config.d sshd_config sshd_config.d ssh_host_ed25519_key ssh_host_ed25519_key.pub ...
anna@web1:/etc/ssh$ cd ..
anna@web1:/etc$ cd
anna@web1:~$cd with nothing after it takes you back to your home directory.
Looking at files
ls -l shows more than just names:
anna@web1:/etc/ssh$ ls -l sshd_config ssh_host_ed25519_key ssh_host_ed25519_key.pub
-rw-r--r-- 1 root root 3248 Aug 12 10:01 sshd_config
-rw------- 1 root root 399 Nov 2 14:20 ssh_host_ed25519_key
-rw-r--r-- 1 root root 91 Nov 2 14:20 ssh_host_ed25519_key.pubFrom left to right: the permissions, the number of links, owner, group, size in bytes, date and name. Notice the difference: only root may read the secret host key (-rw-------), while everyone may read the public one (-rw-r--r--). In lesson 3.1 you will learn to read these permissions exactly. ls -la also shows hidden files: names that start with a dot, such as .ssh.
To read the contents:
cat bestand: the whole file at once, handy for short files;less bestand: scroll with the arrow keys and space, search with/woord, quit withq;head -n 20 bestandandtail -n 20 bestand: the first or last lines;tail -fkeeps watching as new lines are added.
Editing a file with nano
You change configuration in a text editor. nano is the simplest:
root@web1:~# nano /etc/hostsThe shortcuts are listed at the bottom; ^ means Ctrl. Save with Ctrl+O and Enter, exit with Ctrl+X, search with Ctrl+W. If you already work with vim, feel free to use it; on a minimal installation vim is not always fully available.
Three habits that will save you a lot of trouble later:
- Make a copy first:
cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak. - Test the configuration before you restart a service. Many programs have their own command for this:
sshd -t,nginx -t(lessons 2.2 and 7.2). - Change one thing at a time, so you know what caused an error.
How is your server doing?
A few commands you will use in every situation:
anna@web1:~$ uname -a
Linux web1 6.12.0-... #1 SMP ... x86_64 GNU/Linux
anna@web1:~$ uptime
10:12:44 up 3 days, 2:10, 1 user, load average: 0.08, 0.05, 0.01
anna@web1:~$ df -h /
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 40G 3.1G 35G 9% /
anna@web1:~$ free -h
total used free shared buff/cache available
Mem: 3.8Gi 412Mi 2.9Gi 1.0Mi 720Mi 3.4Gi
Swap: 0B 0B 0Buname -a: which kernel is running.uptime: how long the server has been up and the load average over 1, 5 and 15 minutes. Roughly: a load that stays above the number of processor cores for a long time means things are waiting.df -h: free disk space (-hmeans "human readable", with G and M). A full disk is one of the most common reasons a server "just" stops.free -h: memory. Look at theavailablecolumn, notfree: Linux uses free memory as cache (buff/cache) and gives it back when a program needs it.
And your network, from lesson 1.2: ip -brief address.
Finding help
You don't need to memorise every option. You do need to know where to find them:
man sshopens the manual page of a command. Scroll as inless, search with/.man sshd_configdescribes every setting you can use in that file.ls --helpgives a short list of options for most commands.- The Tab key completes names of commands and files. Pressing Tab twice shows the possibilities. That saves typos in long paths.
- The up arrow brings back earlier commands;
historyshows them all.
Manual pages are sometimes missing on a minimal installation. On Debian and Ubuntu you install them with apt install man-db (module 4).
Pipes and grep
You can chain commands together with a pipe (|): the output of one becomes the input of the next. grep filters lines that contain a word:
anna@web1:~$ grep PasswordAuthentication /etc/ssh/sshd_config
#PasswordAuthentication yes
anna@web1:~$ ls -l /etc | lessLines that start with # are comments: they do nothing and often show the default value. In lesson 2.2 you will see why that can be confusing with SSH.
Pitfalls
- Pasting without reading. Commands from the internet do exactly what they say, even if that is different from what the article's title promises. Read every command before you run it, especially when you are root.
rmhas no recycle bin. Gone is gone. Be extra careful withrm -r(a directory and everything in it) and with wildcards such as*.- Case matters.
/etc/SSHdoes not exist;/etc/sshdoes. - Spaces in paths need quotation marks:
cat "/srv/mijn site/index.html". Better still: don't use spaces in names on a server.
Summary
- The prompt tells you who you are, on which server and where;
#means root. - Configuration lives in
/etc, logs in/var/log, program data in/var/lib, home directories in/home. - Read with
cat,less,headandtail; edit withnano, after making a copy, and test the configuration before you restart. df -h,free -h,uptimeandip -brief addressshow how your server is doing.- Help is in
manand--help; Tab completes; with|andgrepyou filter output.
Quiz
Exercise
Exercise O1.4: Getting to know your server. Run the commands from this lesson on your own server and fill in: distribution and version, kernel, free disk space, available memory, load, IPv4 and IPv6, and which files in /etc/ssh only root can read. Then answer two questions about what you saw.
Exercise O1.4: Getting to know your server
You do this exercise yourself; no one checks it. Tick off what you've done and mark the exercise as done.
available also counts the cache memory that Linux releases. The .pub file is the public part that every client sees; the file without .pub is secret (600, root), because whoever has it can impersonate your server.
Your ticks and notes only stay in this browser; we don't send them anywhere. If you're signed in, we only save in My courses that you've done the exercise.
Further reading
- Increasing the disk space of your VPS
- SSH access and the Terminal in DirectAdmin: which packages and how to log in
- Debian Reference, chapter 1 (command-line tutorial): https://www.debian.org/doc/manuals/debian-reference/ch01.en.html
- Filesystem Hierarchy Standard 3.0: https://refspecs.linuxfoundation.org/FHS_3.0/fhs/index.html
