The Linux Terminal

Video + 11 min read · includes a lab

You
web01Linux
ali@web01:~$
db01Linux
Cloud VMLinux
ssh ali@web01
The Linux Terminal
0:00 / 4:57 · The Linux Terminal
Chapters and transcript

The Linux Terminal Most servers on the internet run Linux. Web servers, databases and cloud machines. When a security person connects to one, there is no mouse and no windows. There is only a prompt, waiting for typed commands. In this video you will learn to move around, read files and search for clues, using only the terminal.

Why Linux Why do security people use Linux? First, servers. Web servers, databases and cloud machines very often run Linux, so to defend or test them, you need to know it. Second, tools. Many security tools, like n map, are made for Linux first. And third, control. Almost everything can be done with text commands, which are fast, exact and easy to automate.

Terminal and shell The terminal is the window where you type. Inside it runs the shell, the program that reads your commands and runs them. The most common shell is called bash. Before you type, bash shows a prompt. Let us read one. Ali is the user you are logged in as. Server is the name of the computer. The tilde means your home folder. And the dollar sign means a normal user. A hash sign would mean root, the all-powerful admin.

The file system Linux keeps everything in one big tree of folders. The top of the tree is called root, and it is written as a single slash. Slash home holds the home folders of normal users. Slash e t c holds system settings. Slash var slash log holds the logs, the records of what happened. And slash temp is for temporary files, where any user can write.

Paths A path tells Linux where a file is. An absolute path starts with a slash and works from anywhere. A relative path starts from the folder you are in now. Three shortcuts help a lot. One dot is the current folder. Two dots are the parent folder, one level up. And the tilde is your home folder.

Moving around Let me show you on a practice server. I type p w d, print working directory. I am in slash home slash ali. Now l s lists what is here: two folders and a text file. With l s dash a, for all, hidden files appear too. Their names start with a dot, and they sometimes hold secrets. Then c d docs moves into a folder, and c d dot dot takes me back up.

Quick check Quick check. You are in slash home slash ali slash docs. You type c d dot dot. Which folder are you in now? Take a moment, then choose.

Searching for clues Here is why searching matters. An attacker keeps guessing the root password on this server. Every attempt is written to a log file, next to thousands of normal lines. Reading all of it by eye would take hours. A defender needs to pull out only the lines that matter, and the terminal does that in one command.

Reading files First, cat prints a whole file to the screen. Here are my notes. Now the useful one. grep, then a pattern, then a file, shows only the lines that contain the pattern. I search auth dot log for the word Failed, and only the failed logins appear, from the same address. Remember, grep is case-sensitive. Capital F Failed and small f failed are different.

Finding files To find files by name, use find. Dot means start here, and look in every folder below. The star is a wildcard, meaning any characters. So star dot t x t finds every text file, even one inside the docs folder. Searching for star backup star would find any file with backup in its name. Finally, whoami tells me which user I am logged in as.

Getting help Nobody remembers every option, and that is fine. Man, then a command name, opens its manual page. Press q to quit. Dash dash help prints a short summary of the options. Press the Tab key while typing a name, and the shell completes it for you. And the Up arrow brings back your earlier commands.

Putting it together Let us put it together. P w d shows where you are. L s lists files, and dash a shows the hidden ones. C d moves between folders, and c d dot dot goes up one. Cat reads a file, and grep finds the lines you need. Find searches for files by name. Now read the lesson below, then try the lab and hunt for clues on a server.

Most servers on the internet run Linux. Most security tools are built for Linux too. If you want to work in security, you need to be comfortable typing commands into a Linux terminal. This lesson teaches the basics.

Why security people use Linux

  • Servers: web servers, databases and cloud machines very often run Linux. To defend or test them, you need to know how they work.
  • Tools: many security tools, such as nmap, are made for Linux first. Distributions like Kali Linux come with hundreds of them installed.
  • Control: almost everything in Linux can be done with text commands, which are fast, exact and easy to automate.

Terminal, shell and prompt

The terminal is the window where you type. The shell is the program inside it that reads your commands and runs them. The most common shell is called bash.

Before you type, the shell shows a prompt. It usually looks like this:

ali@server:~$
Part Meaning
ali The user you are logged in as
server The name of the computer
~ The folder you are in (~ means your home folder)
$ You are a normal user. A # means you are root, the all-powerful admin user

The file system tree

Linux keeps everything in one big tree of folders (also called directories). The top of the tree is called root and is written as a single slash: /.

Path What it holds
/ The root, the top of everything
/home Home folders for normal users, such as /home/ali
/etc System settings and configuration files
/var/log Log files, the records of what happened on the system
/tmp Temporary files. Any user can write here

Paths: absolute and relative

A path tells Linux where a file or folder is.

  • An absolute path starts with / and works from anywhere: /home/ali/notes.txt
  • A relative path starts from the folder you are in now: notes.txt or docs/report.txt

Three shortcuts are very common:

Symbol Meaning
. The current folder
.. The parent folder, one level up
~ Your home folder

Where am I? pwd

pwd (print working directory) shows the full path of the folder you are in.

ali@server:~$ pwd
/home/ali

What is here? ls

ls lists the files and folders in the current folder.

ali@server:~$ ls
docs  notes.txt  scripts

Files whose names start with a dot, such as .bash_history, are hidden. Use ls -a (all) to see them. Hidden files often contain settings, and sometimes secrets.

ali@server:~$ ls -a
.  ..  .bash_history  .ssh  docs  notes.txt  scripts

Use ls -l (long) to see details such as size, owner and permissions. You will learn to read this output in the next lesson.

ali@server:~$ ls -l
drwxr-xr-x 2 ali ali 4096 Mar  3 10:12 docs
-rw-r--r-- 1 ali ali  220 Mar  3 10:15 notes.txt
drwxr-xr-x 2 ali ali 4096 Mar  3 10:20 scripts

Moving around: cd

cd (change directory) moves you to another folder.

ali@server:~$ cd docs
ali@server:~/docs$ cd ..
ali@server:~$ cd /var/log
ali@server:/var/log$ cd ~
ali@server:~$

Typing cd with nothing after it also takes you home.

Reading files: cat

cat prints the contents of a file to the screen.

ali@server:~$ cat notes.txt
Backup server is 10.0.0.5
Remember to rotate the logs on Friday

Searching inside files: grep

grep <pattern> <file> shows only the lines in a file that contain the pattern. This is one of the most useful commands in security, because log files can have thousands of lines.

ali@server:~$ grep Failed auth.log
Mar  3 09:41:02 server sshd: Failed password for root from 203.0.113.7
Mar  3 09:41:05 server sshd: Failed password for root from 203.0.113.7

By default, grep is case-sensitive: Failed and failed are different.

Finding files: find

find . -name <pattern> searches for files by name, starting from the current folder (.) and going into every folder below it. The * wildcard means "any characters".

ali@server:~$ find . -name "*.txt"
./notes.txt
./docs/report.txt

find . -name "*backup*" finds every file with "backup" anywhere in its name.

Other useful commands

Command What it does
whoami Shows which user you are logged in as
clear Clears the screen
help Lists the commands built into the shell

Learning more

Nobody remembers every option. Linux has built-in help:

  • man ls opens the manual page for ls. Press q to quit.
  • ls --help prints a short summary of the options for most commands.

Tip: press the Tab key while typing a file or folder name, and the shell will complete it for you. Press the Up arrow to repeat earlier commands.

In the lab, you will use these commands in a simulated shell to hunt for hidden clues on a server.

HANDS-ON LAB

Terminal Treasure Hunt

A flag is hidden somewhere in this Linux machine. Find it using only the terminal.

Start the lab →

Check your understanding

Answer every question correctly to complete this lesson. If you miss one, try the quiz again.

1. What does the command pwd show?

2. How do you see hidden files such as .bash_history in a folder?

3. What does cd .. do?

Locked: answer every quiz question above correctly to complete this lesson.

Spotted a mistake or something unclear? Report a mistake