The terminal is your way into Linux. It is where you install a package, read a log or start a service, often on a machine with no graphical interface. This guide assumes a Linux machine, desktop or server, and no experience with the terminal: you open it, read what it displays and type your first commands, in under ten minutes. The history, completion and shortcuts of Bash have their own guide: Getting started with Bash.
What you will learn
- Open a terminal on a graphical desktop or over SSH
- Read the prompt and identify the user, the machine and the current directory
- Tell apart terminal, shell and session
- Run discovery commands:
whoami,pwd,hostname,date - Recognise the first error messages
- Copy and paste safely and leave the terminal cleanly
Terminal, shell, session: three words to tell apart
Three words come up constantly as soon as you talk about the terminal. It is better to tell them apart before typing any command.
| Word | What it is |
|---|---|
| Terminal | The graphical application (or the SSH window) that displays text |
| Shell | The program that interprets your commands (Bash, Zsh, Fish…) |
| Session | A user's connection to the machine, from opening (login) to closing (logout), for example over SSH or on the console |
When you open a terminal, it starts a shell. The shell waits for your
commands and runs them. When you close the terminal or type exit,
the shell ends. The session only ends if that shell was the one of an SSH
connection or of a console: in a graphical terminal window, your desktop session
stays open.
The diagram follows a command from your keyboard to the screen. Look at each one's role: the terminal passes on what you type and displays text, whereas the shell is the only one that reads the line and starts the requested program. The program writes its output directly to the terminal, without going through the shell. The shell waits for the program to finish, receives its exit code (a number that says whether the command succeeded), then displays the next prompt.
Open a terminal
How you open it depends on your machine: a shortcut or a menu on a graphical
desktop, an ssh command for a remote machine. Choose the tab that matches your
case.
On Ubuntu, the keyboard shortcut is Ctrl + Alt + T. It does not always exist on other GNOME distributions: search for "Terminal" in the applications instead.
On a KDE Plasma desktop, open Konsole from the applications menu. The Ctrl + Alt + T shortcut works on some setups.
From another computer, connect to the remote machine with SSH (Secure Shell, an encrypted connection to another computer):
ssh user@ip-addressReplace user with your account on the remote machine and ip-address with
its address (or its name), which your administrator or your host gave you. You
land directly in a shell (Bash by default on most distributions).
If you work on Windows, the guide Install WSL2 gives you a Linux terminal in which the commands of this page work.
Once the terminal is open, you see a line of text waiting for your input: that is the prompt.
Understand the prompt
The default Bash prompt on Debian and Ubuntu looks like this:
bob@server:~$Each part has a precise role:
| Element | Meaning |
|---|---|
bob | Name of the logged-in user |
@ | Separator |
server | Name of the machine (hostname) |
: | Separator |
~ | Current directory (~ = home directory, /home/bob) |
$ | Regular user (# if you are root) |
The current directory is the folder in which your commands run. It changes when you move around the folders.
That last sign deserves a glance before every command. If the prompt
ends with #, you are logged in as root, the administrator. Stay a
regular user unless you explicitly need otherwise: a mistake as root asks for
no confirmation and can break the system. To run a single command with root
rights, the guide
Use sudo: run a command as root
explains how to do it without switching accounts.
On RHEL, the default prompt looks like [bob@server ~]$: the same information,
inside square brackets and without the colon, and only the name of the current
directory is shown, not its full path. The prompt can be customised with the
PS1 variable, as shown in
Customise Bash: .bashrc, prompt, bash-it and fzf.
A customised prompt may no longer show the user or the directory: the whoami
command then tells you which account you are acting as, and pwd which
directory you are in.
Check which shell is active
The $SHELL variable (an environment variable: a name to which the shell
attaches a value) holds the default shell of your account, the one it starts at
login (its login shell). It is not necessarily the one you are using right
now: if you type zsh from Bash, $SHELL still says Bash. Two commands show the
shell actually running. The first prints the name under which it was started:
echo "$0"In a login shell, the kind an SSH session or a login on the console opens,
the result starts with a dash: -bash. In a graphical terminal window you will
usually read bash, without the dash.
The second command gives the bare name, whatever the way it was started. It queries the process table (the list of running programs):
ps -p $$ -o comm=Here $$ is the process number (PID) of the current shell: ps therefore
prints the name of the program that carries this number. If the result is
bash, zsh or fish, that is the shell you are working in right now.
Type your first commands
A command is a word typed on the keyboard and confirmed with Enter. The shell starts the matching program, and its result is displayed in the terminal. The options and arguments that can follow it are explained in Anatomy of a Linux command.
-
Who am I?
Terminal window whoamiDisplays the name of the logged-in user (e.g.
bob). -
Where am I?
Terminal window pwdDisplays the full path of the current directory (e.g.
/home/bob). This is the path that the prompt shortens to~when you are in your home directory. -
On which machine?
Terminal window hostnameDisplays the name of the machine.
-
What date and time is it?
Terminal window dateDisplays the system date and time.
-
Which distribution?
Terminal window cat /etc/os-releasecatdisplays the content of a file. Here,/etc/os-releasedescribes the installed distribution: its name and its version. -
Which kernel?
Terminal window uname -rDisplays the version of the kernel, the core program of Linux, the one that drives the hardware.
A trap shows up from the very first command: Linux distinguishes upper case
from lower case, in command names as well as in file names. whoami works,
Whoami does not, and the message you get says nothing about case: it only
says the command cannot be found.
Understand the first errors
When you make a mistake, Bash displays a message. These are the most common errors at the very beginning:
| What you type | Message | What happened |
|---|---|---|
Whoami | command not found | No command with that name: wrong case or typo, or the program is missing |
ls /missing-directory | No such file or directory | The path does not exist |
cat /root/secret | Permission denied | You do not have the rights |
cd /etc/passwd | Not a directory | It is a file, not a directory |
Read the whole message before typing the command again. It almost always says what is wrong: an incorrect name, a path that does not exist, a missing right. Retyping at random wastes far more time than reading one line. Depending on the language configured on the machine, these messages may appear in another language, such as French, rather than in English: their meaning stays the same.
Copy and paste in the terminal
The terminal does not use the same shortcuts as the rest of the desktop.
| Action | Shortcut |
|---|---|
| Copy | Ctrl + Shift + C |
| Paste | Ctrl + Shift + V |
One precaution matters when pasting. From Bash 5.1 on, bracketed paste is enabled by default: the shell inserts the pasted text without running it, and nothing is sent before you press Enter. An older shell, or a terminal that does not support this mode, on the other hand runs each line ending with a line break as soon as it is pasted. So always reread what you paste, especially when it comes from a web page: an extra command can hide at the end of the block.
Leave the terminal
Two ways to close the shell cleanly. Over an SSH connection or on a console, this also closes the session:
exitOr press Ctrl + D on an empty line: this shortcut tells the shell that
there is nothing more to read (an end of input, or "end of file"), and the
shell closes because it has no command left to wait for. It also works for a
command that reads your input, such as cat run without an argument.
Ctrl + D does not always close the shell. If the IGNOREEOF variable is set,
Bash requires several consecutive Ctrl + D, 10 if the variable has no numeric
value. That is why exit remains the reliable method, whatever the
configuration of the machine.
Do not confuse Ctrl + D with Ctrl + C, which interrupts the running program without closing the shell. They are two different actions, and interrupting a program can leave things half done. The box below, optional on a first pass, explains why Ctrl + D does not act like Ctrl + C.
Put it into practice
Reading the prompt in a guide and reading it on your own machine are not the same exercise. The lab prepares a separate working folder on your machine, with a premiers-pas.txt file to fill in. The commands act on your own machine: you find your user (whoami), the machine name (hostname), the date (date) and your home directory (echo $HOME, which the lab gives you), then write these four values into the file. The validation compares them with the real state of your session.
Key points
- The terminal is the interface, the shell is the program that interprets your commands.
- The prompt tells you who you are, on which machine and in which directory.
- The prompt ends with
$for a regular user and with#for root. whoami,pwd,hostname,date: four commands to find out where you stand.- Linux is case-sensitive:
whoamiexists,Whoamidoes not. - Read the error messages: they almost always explain the problem.
- Ctrl+D signals the end of input and usually closes the shell on an empty line; Ctrl+C interrupts the running program.
exitcloses the shell cleanly, and the session if it was an SSH connection or a console.
Knowledge check
Check that the essentials of this guide stuck. The questions cover only what has just been explained here.
Knowledge check
Check what you have learned with this interactive quiz
How it works
- The timer starts when you click Start
- Multiple choice, true/false and short answer questions
- You can move between questions
- Detailed results are shown at the end
Starts the quiz and the timer
Review
(0/0)Result saved on this device. It will be sent to your account at the next sync. Sync now
Score by domain
A domain groups several guides and gets several questions, so its score is more reliable than a per-topic score, which often rests on a single question.
Summary
Strengths
No topic passed at 80% on at least two questions yet.
Weak areas
No topic below the pass mark.
Recommended next
Hints before you try again?
Continue the course: Anatomy of a Linux command
Continue the course: Navigate and manage files on Linux
A new full quiz with random questions
Work again only on the questions you missed