Lesson 1 of 10 - Foundations: the server, the network and Git

Get around a Linux server from the shell

By Branko Petric - Updated - About 7 min - Free

The short answer

The shell is a program that runs the commands you type. On a server it is usually the only way in. Learn where you are (pwd), how to move (cd), how to look (ls, cat, less), and how to read logs (tail -f, grep). That covers most first steps in any incident.

Already know this? Skip to the 3 questions

What the shell is

When you log in to a server over SSH, you get a shell. It is a program that reads the commands you type, runs them, and prints the result. On most Linux servers the shell is bash.

There is no desktop and no file browser. Everything you do, from checking a log to restarting a service, starts with a command. The good news: a small set of commands covers most of the work.

The line where you type is the prompt. It often looks like deploy@web-1:~$. That tells you the user (deploy), the server (web-1) and the current directory (~, which is short for your home directory).

Paths: where you are and how to move

Linux has one tree of directories that starts at /, called the root. Every file has a path from there, such as /var/log/syslog.

  • pwd prints the directory you are in.
  • cd /var/log moves you there. This is an absolute path, because it starts with /.
  • cd log moves into log inside the current directory. This is a relative path.
  • cd .. goes up one level. cd ~ (or just cd) takes you home.
pwd
# /home/deploy
cd /var/log
ls -lh
# -rw-r----- 1 syslog adm 2.1M Nov 28 09:14 syslog
# drwxr-xr-x 2 root   root 4.0K Nov 27 06:25 nginx

ls -lh lists files with details: permissions, owner, size in readable units (-h) and the last change time. Add -a to see hidden files, the ones whose names start with a dot, like .bashrc.

Reading files without breaking them

You will read far more files than you edit. Use the right tool for the size.

  • cat file prints the whole file. Fine for a short config, painful for a 2 GB log.
  • less file opens the file one screen at a time. Press Space to go down, /word to search, q to quit. It does not load the whole file into memory, so it is safe on big logs.
  • head -n 20 file shows the first 20 lines. tail -n 50 file shows the last 50.

Most logs live under /var/log. Web servers usually have their own folder, such as /var/log/nginx/.

Following and searching logs

When something is failing right now, you want to see new log lines as they arrive.

tail -f /var/log/nginx/error.log

-f means follow. The command keeps running and prints each new line. Reload the page in your browser and watch what appears. Press Ctrl+C to stop.

To find specific lines, use grep:

grep "upstream timed out" /var/log/nginx/error.log
grep -i error /var/log/syslog | tail -n 20
grep -c " 500 " /var/log/nginx/access.log

The first finds an exact phrase. The second ignores case (-i) and uses a pipe (|) to send the matches into tail, so you only see the last 20. The third counts matching lines (-c), here the number of requests that returned 500.

A small real example: users say the site is slow. You SSH in, run tail -f on the nginx error log, and see upstream timed out every few seconds. Now you know nginx is fine and the app behind it is slow. Five minutes, three commands, and you are looking in the right place.

Common beginner mistakes

  • Running cat on a huge log. It floods your terminal and can take minutes. Use less or tail -n instead.
  • Forgetting where you are. Running a command in the wrong directory, especially one that changes or deletes files, is a classic accident. Run pwd first when in doubt.
  • Searching with the wrong case. grep error misses ERROR. Add -i when you do not know how the app writes its messages.

Check your understanding

3 questions. Retry as often as you like - nothing counts against you.

Question 1 of 3

You want to watch new lines appear in /var/log/nginx/error.log while you reload the site. Which commands do that?

Select all that apply - one or more answers can be right.

You want to watch new lines appear in /var/log/nginx/error.log while you reload the site. Which commands do that?

Want the bigger picture? This lesson maps to this step of the DevOps roadmap.