Haproxycentos 7 Haproxycentos 7
Haproxycentos 7 Haproxycentos 7
From the main boot menu choose the entry Graphical install, and press the Enter
key to proceed.
Language Selection
Next, choose the language you prefer to be used during the entire installation
process. The dialogs and messages are translated accordingly. This selection
does not determine irrevocably the language your Linux system will have, you
can always choose a different language later.
The image below shows the dialog box. English is already pre-selected, and so
you just have to click the Continue button on the lower-right corner of the dialog
box to proceed.
Location Selection
Third, make a selection regarding your location (see image below). Based on
your language setting made before, the countries are listed in which the chosen
language is mainly spoken. This also influences the locale settings like the time
zone your computer is in. In order to have a different setting choose the entry
titled other from the end of the list and go on from there. When you are done,
click the Continue button to proceed with step four.
Keyboard Selection
Fourth, choose your keyboard layout from the list (see image below). For the
United States the pre-selection is American English. If you use a different
keyboard layout select the right one from the list. If done click the Continue
button to proceed with step five.
Network Setup
Step five includes loading the installer components from the ISO image, and the
detection of the network hardware in order to load the correct network driver.
Then, the installer tries to connect to the internet to retrieve an IP address via
DHCP from your local network service.
When done, you can set up the hostname of your computer (see image below).
Choose a unique name for your machine that consists of a single name and does
not exist yet in your local network segment. It is common to use names of fruits,
places, musical instruments, composers and characters from movies. In this case
we choose the name debian95 that simply represents the Linux distribution and
its version number.
When you are done, click the Continue button to proceed with step six to add a
domain name like yourcompany.com (see image below). In this case it is not
needed. That’s why we leave the entry field empty. Click the Continue button on
the lower-right corner to proceed with the installation.
Users and Roles
Our Linux system needs at least two users in order to be operated properly. One
is an administrative user that has a fixed name root and the other is a regular user
that we just give the name of User in this case.
In the next two steps you set the password for the user root (see image below)
and both the full name and account name for the regular user. For simplicity we
use Debian User as the full name and User as the account name. For both users,
choose a password that is dissimilar and that you can remember. You will need
these passwords later in order to log onto your computer.
Time Zone
Setting the correct time zone is of significant importance for communication
with other services, especially in a network. Choose the value from the list as
seen in the image below. The entries in the list are based on the location you
have selected before. When done, click the Continue button to define the storage
media and the accompanying partitions.
Storage Media and Partitioning A Linux system can be distributed across a
number of different storage media like hard disks and flash drives. Over and
above, a storage media can be separated into multiple disk partitions. In order to
do so, the setup program of Debian has the following methods available (see
image below):
Guided - use entire disk: follow the steps as provided and use the entire
disk space for the Linux installation. This creates partitions with fixed
sizes.
Guided - use entire disk and set up LVM: follow the steps as provided and
use the entire disk space for the Linux installation. This option makes use
of Logical Volume Management (LVM) in order to create partitions with
sizes that can be changed later on.
Guided - use entire disk and set up encrypted LVM: follow the steps as
provided and use the entire disk space for the Linux installation. This
option makes use of Logical Volume Management (LVM) in order to
create encrypted partitions with sizes that can be changed later on.
Manual: create partitions individually. This is the expert mode and
requires deeper knowledge about partitions and file system parameters.
From the list choose the entry Guided - use entire disk. The values for partition
sizes are chosen according to experience, implemented as an algorithm. A
manual calculation is not required. Click the Continue button on the lower-right
corner to proceed with the installation.
Next, select the disk to partition. In our case we have only one disk available
(see image below). Later on in this guide the disk will be referred to as /dev/sda
for the 1st SCSI disk.
A disk partition refers to a piece of the storage media that is organized separately
and is intended to contain a branch of the Linux file system tree. There is no
universal way to do this separation correctly. This guide shows a simple but safe
solution that works for a basic system. The menu in the dialog box offers the
following options:
All files in one partition: use just a single partition to keep programs and
user data
Separate /home partition: store programs and user data in separate
partitions
Separate /home, /var, and /tmp partitions: keep user data, variable data
and temporary data in separate partitions
As shown in the image below, choose the third entry Separate /home, /var, and
/tmp partitions. Click the Continue button on the lower-right corner to proceed
with the installation.
The next step is to confirm the partition scheme. This is calculated automatically
based on experience and contains these partitions:
sda1: the first partition of the first SCSI disk is a primary partition with a
size of 3 GB, formatted with the ext4 file system, and referred to as the
root part of the file system tree (indicated with /)
sda5: the fifth partition of the first SCSI disk is a logical partition with a
size of 1.3 GB, formatted with the ext4 file system, and reserved to store
variable data of the file system tree (indicated with /var)
sda6: the sixth partition of the first SCSI disk is a logical partition with a
size of 3.3 GB, formatted as a swap file system
sda7: the seventh partition of the first SCSI disk is a logical partition with
a size of 311 MB, formatted with the ext4 file system, and reserved to
store temporary data of the file system tree (indicated with /tmp)
sda8: the eighth partition of the first SCSI disk is a logical partition with a
size of 8.8 GB, formatted with the ext4 file system, and reserved to store
the user data of the file system tree aka home directories (indicated with
/home)
Due to historical reasons a hard disk can contain four primary partitions only.
The fourth one is called an Extended Partition if divided into so-called logical
partitions or logical drives. In our case the logical partitions /dev/sda5,
/dev/sda6, /dev/sda7 and /dev/sda8 are stored on the primary partition /dev/sda4.
The partitions /dev/sda2 and /dev/sda3 are not in use.
From the above list, choose the entry Finish partitioning and write changes to
disk. Click the Continue button on the lower-right corner to proceed and to
confirm the partition scheme (see below image). Choose yes from the list and
click the Continue button to partition the disk. Note that all the data on the
selected storage device will be lost and the disk will be empty.
Having divided the storage media into single partitions, the partitions will be
formatted with the file system as defined before. In our case the partitions
/dev/sda1, /dev/sda5, /dev/sda7 and /dev/sda8 will get an ext4 file system, and
the partition /dev/sda6 will get a swap filesystem. As soon as this step is
completed, the base system of Debian will be installed next.
Package Management
The ISO image contains the installer and a number of packages to set up the
Linux base system. For example this includes the Linux kernel being installed
below.
Next you need to decide whether to use additional installation media or not. In
our case we have just a single installation disk and can consequentially select No
from the menu (see image below). Then click the Continue button to proceed.
As pointed out earlier, the software for Debian is organized in packages. These
packages are provided in multiple software repositories. The repositories are
made available via package mirrors that are maintained by universities, private
persons, companies and other organizations. These mirrors are located in
different countries.
In the next step you will have to decide from which country you would like to
retrieve your Debian packages. It is recommended to choose a mirror that is
geographically located near you to minimize the time that is needed to transport
the data from the mirror to your computer via network. As an initial step, choose
your desired country.
As a second step choose a preferred mirror from the list (see image below). The
list contains universities, internet providers, government services and other
organizations.
In case your computer network includes a proxy server to communicate with the
outside world, enter the according information here. In this case we don’t have
that and leave the entry field empty. Click the Confirm button to proceed.
As soon as the single parameters are set, the Debian installer connects to the
previously selected package mirror and retrieves the package lists from there. A
package list contains the packages that are available, including the name, size
and description. Depending on the quality and bandwidth of your network
connection this step can take a while.
Next, you are asked to take part in Popcon, the Debian package popularity
contest (see image below). This information is optional, and is used only by the
team of developers that is responsible for the Debian packages. Based on
Popcon, they figure out which Debian packages are the ones that are installed
most often. The information has a direct influence on the preparation of
installation images and which packages to keep or to dismiss for the different
architectures.
In our case we do not participate in Popcon, and leave the selection to No. Click
the Confirm button to proceed with the selection of software tasks (tasksel).
Software Selection
Debian offers carefully arranged selections of packages, so-called tasks. The
idea behind them is to group packages for specific uses in order to simplify the
installation. From the list in the dialog window (see image below) you can
choose between different desktop environments, as well as a web server, a print
server, a SSH server and the standard system utilities. To have a minimal
installation just enable the last two entries from the list. We will install the XFCE
desktop environment later.
Having confirmed the software selection, the Debian installer retrieves the
needed packages from the package mirror, unpacks and then installs them. The
bar below shows the progress. In this case 140 packages have to be downloaded.
Setting up GRUB
In order to start our newly installed Debian system, we have to set this
information too. This process is called booting the system. The software
component that handles this step is named Grand Unified Boot Loader (GRUB
or GNU GRUB to be precise). The entry Yes is already pre-selected, and so we
can click Continue to proceed.
The Debian installer needs to know where to install GRUB. The menu in the
image below lists the storage media to be considered. In our case we choose the
second entry from the list (/dev/sda). Then, click Continue to proceed.
Now that we are nearly finished with the basic installation, the only thing left to
do is just a single dialog box to read.
Finishing the Installation The following dialog box informs you that the
installation is complete. Then click Continue to reboot the newly installed
system.
After a few seconds, the text-based GRUB boot menu will appear on the screen
(see image below). The boot menu includes two options: Debian GNU/Linux
and Advanced options for Debian GNU/Linux. The first menu item is
highlighted and pre-selected. The second menu entry allows you to set specific
boot options. For a comprehensive list of these options have a look at the GNU
GRUB manual at www.gnu.org/software/grub/manual/grub
Keep the first menu item selected and press Enter to proceed and boot the new
system.
Your Debian system will start and initialize the necessary system services. This
step will take a few seconds to be completed. Finally, a black-and-white screen
will be visible on the first text terminal named tty1 (see image below) and ask
you to log into the system.
The login prompt consists of two components: the host name of your Linux
system (debian95) followed by a space and the word login.
Debian GNU/Linux 9 debian95 tty1
debian95 login: _
Having logged into the system you will add further software to be able to use a
graphical user interface, based on the XFCE desktop. At this step of the
installation process, adding further software can only be done by logging in as
the administrative root user.
In order to do so, type in root at the login prompt, press Enter, wait for the text
Password: to appear and type in the password set for the root user you defined
before. The Linux system will welcome you (see image below) with a login
message. The first line of the login message will give you a display on the time
of your last login followed by the version of the Linux kernel that is currently
running (line two) and the usage advice (lines five to eight).
A few seconds later a graphical login screen will be visible (see image below).
Log in to the system with the regular user named user as created earlier. Type in
user, press Enter and type in the password for the user. Then, press Enter again
to confirm and log in.
Next, the XFCE desktop will be visible (see image below). The desktop comes
with a number of default elements: an upper navigation bar, a lower navigation
bar and desktop icons. These elements are explained in more detail below.
Upper navigation bar: this shows buttons to access the different
applications, the four virtual desktop screens, the clock and a button for
various user actions such as to lock the screen, change the user, change to
stand-by mode and exit the current session
Lower navigation bar: this bar contains several buttons to hide all the
opened windows and show the empty desktop, to open a terminal, the file
manager, a web browser, to find an application and to open the file
manager directly with your home directory.
Desktop icons: from top to bottom there is a trash bin, file manager icons
that link to the computer, and the home directory
A right-click on the desktop opens a context menu that allows you to access the
various applications (see image below).
4.4 Adding Additional Software
Up until now the software available to be used on your Linux system has been
rather limited. The next thing to do is to add the following four Debian packages
to make your life a bit easier:
firefox-esr: the web browser Mozilla Firefox (Extended Support Release)
gnome-terminal: a terminal emulation maintained by the GNOME project
xscreensaver: a basic screensaver for the X11 system
vlc: the video player Video Lan Client (VLC)
The installation of the three packages will be done using the command-line in a
terminal emulator (we will look at terminals in detail later in the guide).
Currently, on your system the X11 terminal emulator xterm is installed. In order
to open xterm, click on the terminal button in the lower navigation bar or select
the entry Application > System > Xterm from the context menu.
As step one, open xterm. Next, type in the command su next to the command-
line prompt as follows and press Enter:
user@debian95: ~$ su
Password:
You may remember from the previous steps that only an administrative user can
install, update or remove software on a Debian system. The su command
abbreviates switch user and changes your current role. Used without an
additional name, the role changes to the administrative root user. At the
password prompt type in the password for the administrative user and press
Enter.
As an administrative user, install the packages: firefox-esr, gnome-terminal,
xscreenserver and vlc as follows:
root@debian95: ~# apt-get install firefox-esr gnome-terminal xscreensaver vlc
The output is seen below:
After the installation of the four packages, you can switch back to your role as a
regular user. Press Ctrl+D to quit the admin part, and press Ctrl+D again to close
xterm.
The installation of Firefox has the following effects:
The new software Firefox is available from the application menu
The new command firefox is available from the command-line
The earth icon from the lower navigation bar links to the Firefox web
browser
The installation of the GNOME terminal package has the following
effects:
The new software gnome-terminal is available from both the command-
line and the application menu
The terminal icon from the lower navigation bar links to the GNOME
terminal
The entry Open Terminal Here from the context menu refers to the
GNOME terminal
In order to see the changes, select the entry Open Terminal Here from the
context menu. The image below shows the terminal window. It comes with a
white background and a bigger font that is easier to read.
4.5 Exiting Linux
In order to use Linux properly, you also have to learn how to exit Linux and to
reboot the system. Below you will learn how to quit the XFCE desktop
environment, to shut down the Linux system and to reboot the system. We will
look at two methods; the first is based on the graphical interface and the second
can also be used on non-graphical systems like servers and wireless routers.
Quitting the XFCE Desktop Environment The easiest way to quit the XFCE
desktop and to logout from your current session is to click on the button in the
right corner of the upper navigation bar. The button is labeled with your user
name, which in our case is Debian User. From the small menu select the last
item labelled Log Out (see image below). Alternatively, you can choose either
the item labeled Log Out from the Application button in upper-left corner or
from the context menu.
A second window opens that contains the five buttons labeled Log Out, Restart,
Shutdown, Suspend and Hibernate (see image below). Click on Log Out to quit
your session. Subsequently you will return to the login screen.
Shutting Down the Linux System The way to shut down the entire Linux
system is similar to exiting XFCE. Click on the button in the right corner of the
upper navigation bar. The button is labeled with your user name which in our
case is Debian User. From the small menu (see image above) select the item
labelled Shut Down. From the next dialog window click on the button labelled
with Shut Down to stop the Linux system.
From a terminal session you can run the commands halt or shutdown -h now as
an administrative user. A regular user is not allowed to issue these commands.
# halt
Rebooting the Linux System In order to restart the Linux system, click on the
button in the right corner of the upper navigation bar. The button is labeled with
your user name, which in our case is Debian User. From the small menu select
the item labelled Restart (see image above). From the next dialog window click
on the button labelled Restart to reboot the Linux system.
From a terminal session you can run the command reboot or shutdown -r now as
an administrative user. A regular user is not allowed to issue these commands.
# reboot
Subsequently the system will reboot and you will return to the boot screen.
5. Navigating Linux
File systems on UNIX/Linux systems contain different types of entries. This
includes regular files, directories (or folders), symbolic links, block and
character devices, named pipes and sockets. This chapter deals with directories
and the corresponding Linux tools.
5.1 The Filesystem Hierarchy Standard (FHS)
Modern Linux systems have a rather consistent directory structure, which is
based on the Filesystem Hierarchy Standard (FHS). This definition specifies
which main directories exist, and which content is stored in them. The FHS is
maintained by the Linux Foundation. The latest version of the FHS is 3.0 and
was released on 3 June 2015.
The Linux distributions mentioned in Chapter 3 follow the FHS structure.
Nevertheless, there are distribution-specific extensions. Below are the common
directories that are in use:
Directory Description
/ primary hierarchy root and root directory of the
entire file system hierarchy
/bin essential command binaries for all the users
/boot the boot loader
/etc abbreviates etcetera and contains host-specific
system-wide configuration files
/home the home directories of the users
/lib libraries essential for the binaries in /bin and /sbin
/media mount points for removable media
/proc abbreviates processes and is the virtual file system
of the Linux kernel
/root the home directory for the root user
/sbin essential system binaries
/tmp temporary data and files
/usr Unix System Resources, contains user utilities and
applications
/var variable data, such as log files and database files
Some Linux distributions have additional directories like /opt or use symbolic
links. Linux also has some abbreviations in use. They can be quite helpful in
your daily work:
Abbreviation Description
$HOME local variable that contains the user’s home
directory
~ points to the user’s home directory like /home/tom
. points to the current directory
.. points to the parent directory
You can use these abbreviations on the command line in a terminal and in shell
scripts to shorten commands. Next we will look at some of these commands.
5.2 Commands for Directories
As with files, Linux offers a range of commands to list, create, remove, change
and rename directories. You can use these commands in your terminal.
Listing Directories
In order to list the directories use the command ls with the switch --directory
(short -d) as follows:
$ ls -d */
finances/
projects/
work/
...
$
This example lists the names of the directories in the current directory. The line
“...” in the output indicates that the list is longer and the output is shortened here.
In order to list all the directories including the hidden ones starting with “.” you
may type the find command as follows:
$ find . -maxdepth 1 -type d
./projects
./.mozilla
./.ipython
./.dbus
./video
...
$
In order to learn more about the ls command and also list other entries, have a
look at Chapter 6 which covers terminal commands.
The tree command searches the directory recursively and outputs an entire
directory structure quite nicely. The switch -d limits the output to directories and
looks as follows:
$ tree -d
.
├── archive
│ ├── ballet-zebola │ │ ├── documents │ │ └── invoices │ ├── charles-darwin-
university ├── projects
├── .dbus
├── .ipython
├── .mozilla
...
$
Creating Directories In order to create a directory, use the mkdir command
(make directory). The next example creates a new entry named “training” in the
current directory:
$ mkdir training
$
The switch --verbose (short -v) prints an additional message that the command
succeeded.
$ mkdir -v training
mkdir: directory "training" created
$
The switch --parents (short -p) allows you to create entire subdirectory
structures. Parent directories that do not exist yet are created as well. The
following example creates the directory “training” as well as its subdirectories
“linux” (level 1) and “lpic1” (level 2).
$ mkdir -p training/linux/lpic1
$
In order to create two subdirectories on the same level, make use of the
capabilities the underlying command line interpreter has. The Bash uses values
in brackets {...} to achieve this. The next example shows you how to create the
two directories “training/linux /lpic1” and “training/linux/lpic2” in one go:
$ mkdir -p training/linux/{lpic1,lpic2}
$
Removing Directories Removing a directory is done using the rmdir command
(remove directory). The next example removes the entry named “training” in the
current directory:
$ rmdir training
$
As with mkdir, the switch --verbose (short -v) prints an additional message that
the command succeeded.
$ rmdir -v training
mkdir: directory is removed, "training"
$
This works well, as long as the directory is empty. In case your directory
contains any entries like files or subdirectories, the rmdir command will
complain when using this message, and keep the directory unchanged:
$ rmdir training
rmdir: failed to remove `training': Directory not empty
$
To solve this situation the rm command (remove) combined with the switch --
recursive (short -r) becomes quite handy. Then, rm removes all the entries
recursively. The following example combines the two switches -r and -v in order
to delete one file and three directories.
$ tree training/
training/
├── lpic1
│ └── introduction.txt └── lpic2
2 directories, 1 file
$ rm -rv training/
„training/lpic1/introduction.txt“ was removed
Directory was removed: „training/lpic1“
Directory was removed: „training/lpic2“
Directory was removed: „training/“
$
Changing Directories The cd (change directory) command allows you to move
through the system. To move one level upwards towards the root directory use
this command:
$ cd ..
$
To move downwards into a subdirectory enter the cd command followed by the
directory name. The command cd /home/user/test will take you straight into the
/home/user/test directory.
As already explained earlier, there are some shortcuts available. The commands
cd ~ (tilde) and cd $HOME will always take you to your home directory whereas
cd / will take you to the / directory.
Tip: Because Linux is case-sensitive, cd is not the same as CD. You need to be
careful when using upper and lower case.
Renaming Directories This can be achieved using the mv command (move). In
order to change the name of a directory from “lpic1” to “lpic-1” type in the
following:
$ mv lpic1 lpic-1
$
5.3 Terminal-based File Managers
Navigating Linux on the command line is not the most ideal situation. Terminal-
based and graphical file managers are a great alternative. Terminal-based file
managers run inside a terminal and do not open a separate window on your
desktop. There are tons of programs available, that’s why we only present you
with a selection of our favorites.
GNU Midnight Commander (mc) www.midnight-commander.org
This is a free software, full-screen, text mode, visual file manager that lets you
search, copy, move and also delete both single and multiple files, and even a
whole directory tree. The mc integrates a viewer and editor.
The Vi File Manager (vifm) https://vifm.info/
Similar to mc, the Vi File Manager (vifm) comes with two navigation windows
based on a curses interface. It provides a Vi(m)-like environment for managing
files and directories. If you are familiar with the Vi key bindings, this is the file
manager for you.
5.4 Graphical File Managers
Similar to terminal-based file managers, graphical file manager assist in
navigating Linux. These file managers do open in separate windows, but provide
a much nicer user experience. Again, there are tons of programs available, so we
only present you with a selection of our favorites.
Konqueror
https://packages.debian.org/stretch/konqueror
Konqueror is a powerful file manager for the KDE desktop environment.
Konqueror offers simple file management functionalities such as copying,
moving, searching and deleting files and directories plus some advanced features
and functionalities, such as access to archives, browse and rip audio CDs as well
as support for access to FTP and SFTP servers and SAMBA (Windows) shares.
Nautilus
https://wiki.gnome.org/Apps/Nautilus
Nautilus is the file manager on the GNOME desktop. It offers easy navigation
and management of files on a Linux system. This includes simple file
management functionalities such as copying, moving, searching and deleting
files and directories, as well as easy access to local and remote files.
Dolphin
www.kde.org/applications/system/dolphin/
Dolphin is a lightweight file manager developed as part of the KDE applications
package. Designed for simplicity, flexibility and full customization, it allows
users to browse, locate, open, copy and move files around a Linux system with a
lot of ease.
Thunar
https://docs.xfce.org/xfce/thunar/start
Thunar is a modern, lightweight file manager for the XFCE desktop. It is
designed to be fast, responsive and easy to use. We use it here because it has a
clean and intuitive interface with few and important user options available.
6. Introduction to Linux Terminals
Working with the Linux operating system requires you to have knowledge about
the terminal and the command line. It is essential to know what these things are,
to be at least slightly familiar with their usage and the standard commands
available. A few of these commands have already been introduced in Chapter 5.
6.1 What is a Terminal?
A terminal is described as "a program that emulates a video terminal within
some other display architecture. Though typically synonymous with a shell or
text terminal, the term terminal covers all remote terminals, including graphical
interfaces. A terminal emulator inside a graphical user interface is often called a
terminal window”.
To be precise, a terminal is simply the outside. Inside a terminal runs a command
line interpreter that is called a shell.
Debian Linux supports a long list of terminal software. This includes the Aterm,
as well as the GNOME terminal, the Kterm, the Mate Terminal, the Rxvt, the
Xterm and the Xvt. These different implementations of terminal software vary in
terms of stability, support for character sets, design, colors and fonts, as well as
the possibility to apply background images or work with transparency In this
book we will use the GNOME terminal because of its stability, simplicity and
adjustability. In order to increase and decrease the size of the content that is
displayed inside the terminal window; use the two-key combinations CTRL+
and CTRL-.
6.2 What is a Shell?
Simply speaking, a shell is a sophisticated command-line interpreter. In a loop
the shell reads characters, modifies them under certain conditions, and executes
the result.
Under certain conditions, between reading from the command-line and the
execution of the actual result, the shell has to interpret special characters that are
part of your input. The table below displays the special characters that are
available:
Character Meaning
$name substitution of a variable
name= assignment of a variable
'command' substitution of a command
$( command ) substitution of a command
< > >> 2> 2>> redirection of input and output
| pipelining of commands
" ' \ quoting
* [ ] ? creation of file names (globbing)
; separation of commands
& run the command in the background
|| && run the commands under certain conditions
{ } ( ) cramp commands
For interest sake, the shell divides the command-line into single words in order
to determine the commands, by means of the following process:
Step Description Special Characters
1 read until the command separator \n & | || && ; $(...) \ "..." '..."
and tagging (substitution of
commands)
2 tagging (input-/output redirection, < > >> 2> 2>> name=
assignment of values)
3 division into single words space, tabulator
4 substitution of variables $name
5 substitution of commands tagging, see step 1 and 2
6 input-/output redirection tagging, see step 1 and 2
7 assignment of variables tagging, see step 1 and 2
8 division into words based on IFS
9 creation of file names * [ - ]
10 command execution
In steps 1 and 2 tagging happens for steps 5 to 7 only, and no further action takes
place. Steps 5 to 7 are executed if tagging is completed. Next, the shell removes
all the special characters. In step 10 the command-line contains only the
arguments that are needed to execute the command. The shell interprets the first
word as the name of the command to be executed, and the remaining words are
its arguments:
command arg1 arg2 arg3 ...
6.3 Available Shells
Your Linux system allows the usage of various shells. Each shell is available as
a separate software package through the Debian package manager, Aptitude, we
installed earlier. The list of shells is quite long, so we list only a selection of the
available shells that are the most popular:
Almquist Shell (ash)
Bourne Again Shell (bash)
Debian Almquist Shell (dash)
Z Shell (zsh)
C Shell (csh)
Korn Shell (ksh)
Tenex C Shell (tcsh)
Unless otherwise stated the examples in this document are based on the Bourne
Again Shell (bash). At the time of writing this document this is the default shell
on Debian.
Which other shells are allowed in your Linux system, is set up in the
configuration file /etc/shells. Using the cat command you can see the list of
allowed shells:
user@debian95:~$ cat /etc/shells
# /etc/shells: valid login shells
/bin/sh
/bin/dash
/bin/bash
/bin/rbash
/usr/bin/screen
user@debian95:~$
The name of the shell that is currently in use in your terminal is kept in the shell
variable $0. The following simple command outputs the shell’s name:
user@debian95:~$ echo $0
bash
user@debian95:~$
In order to change the shell currently in use, have a look at the chsh command
and the configuration file /etc/passwd covered in the following section.
7. Essential Linux Commands
In this chapter we explain basic Linux commands that will help you to deal with
files and directories, text processing commands, users and groups, process
management, networks, and the help system.
7.1 Files and Directories
touch
The touch command is used to update the access date or modification date of a
file and works in two ways: if the file already exists, the timestamp for access
and modification of the file is set to the current timestamp. In case the file does
not exist yet, an empty file will be created that has the current timestamp (see
image below).
Use the touch command in order to set the timestamp of a file and to figure out
if you have the appropriate permissions to write to a directory or an entire
filesystem.
ls
This command lists the entries of a directory. The image below shows two
common ways: without any options, and with the options -la (short for -l -a
which means long all). The first output displays the entry names only for regular
files, whereas the second output lists both regular and hidden entries. Over and
above, it shows all the information like type of entry, permissions, name of
owner, size of entry, access date and name of entry.
In order to list directories only, use the option -d (abbreviates --directory).
mkdir
This command is used in order to create a directory (make directory). The
following example creates a new directory named “training” in the current
directory:
$ mkdir training
$
rmdir
This command is used in order to remove an empty directory (remove
directory). The following example deletes an empty directory named “training”
in the current directory.
$ rmdir training
$
rm
This command abbreviates the word “remove” and deletes files and directories.
In order to delete all the files ending with .txt that reside in the current directory
issue the following command:
$ rm *.txt
$
In order to be on the safe side when deleting files and directories, use the option
-i (or --interactive) in combination with -v (for --verbose). Before deleting a file
rm will then request your explicit confirmation, and prints a status message:
$ rm -iv invoice156.txt
rm: remove regular file "invoice156.txt"? y
"invoice156.txt" was deleted
$
cp
The cp command copies files. In order to operate properly, it requires two
names: the name of the original file and the name of the copy. The next example
creates a copy of the calendar file that is named “calendar-2018”.
$ cp calendar calendar-2018
$
The original file is not touched and stays intact. The copy has the same content
as the original, but with the current timestamp. Use the option -i (or --
interactive) to prevent overwriting existing files.
mv
The mv command abbreviates the word “move” and moves and renames files
and directories. It requires two names: the name of the original file and its new
name. The following example renames the “calendar” file to “calendar-2018”.
$ mv calendar calendar-2018
$
The original file is removed and the new entry receives the current timestamp.
Use the option -i (or --interactive) to prevent overwriting existing files.
cd
The cd (change directory) command allows you to move through the system. To
move into the subdirectory “work”, use this command:
$ cd work
$
file
The file command determines the type of entry in the file system. There are three
sets of tests that are performed in the following order: filesystem tests, magic
tests, and language tests. The first test that succeeds causes the file type to be
printed. As shown in the image below “Music” is classified as a directory,
“testfile” as an empty file and “/etc/passwd” as a text file. This command also
detects PDF files as well as various image formats.
du and df
These two very similar commands tell you more about the disk space that is in
use, du abbreviates “disk usage” and df means “disk free”.
du calculates the amount of disk space that is used by a directory. The regular
output states the value for every single entry and can be a bit confusing. In order
to get a summary for a directory, extend the command line call by the three
parameters -s, -c and -h. -s abbreviates “summary”, -c means “total” and -h
outputs the value in human-readable format. The image below shows the disk
usage of your home directory.
In contrast, the df command shows how much space is left on the devices. The
image below shows the available disk space of your system. From left to right
the columns cover the filesystem, the disk size, the amount of space that is used,
the amount of space that is still available and the device that is mounted to that
directory.
7.2 Output and Text Processing
echo
echo is a built-in shell command and is intended to output text:
$ echo help
help
$
As already shown earlier, the shell evaluates the command and prints the value
of variables too. The next example prints the value of the shell variable $HOME
which represents your home directory.
$ echo $HOME
/home/user
$
cat and tac
These two commands print a file, line by line. cat starts with the first line up to
the last line, and tac starts with the last line up to the first line. The next example
uses a simple plain text file named “places” that contains the names of one city
per line. For cat Amsterdam comes first, and for tac it is Cape Town.
$ cat places
Amsterdam
Berlin
Bern
Cape Town
$ tac places
Cape Town
Bern
Berlin
Amsterdam
$
grep
This command abbreviates the description “global regular expression print”. The
command acts as a filter that only prints the lines of text that match a given
pattern. grep needs data to work on, in combination with a pattern to look for.
Based on the example used above for cat and tac, the following command line
call only outputs the lines from the file that contain the character string “Ber”.
Keep in mind that grep filters are case-sensitive, i.e. it looks for the character
string that starts with an uppercase “B” followed by the two lowercase letters “e”
and “r”. It does not matter whether the pattern is at the beginning, in the middle
or at the end of text.
$ grep Ber places
Berlin
Bern
$
When it comes to patterns, grep supports character strings and regular
expressions (RegEx). In order to find all the strings that end with the letter “n”
use the option -E (or --extended-regexp) followed by the pattern “n$” as shown
below:
$ grep -E "n$" places
Berlin
Bern
Cape Town
$
head and tail
head outputs the first lines from a file. Invoked without further parameters it
outputs up to ten lines. In contrast tail does the same thing, but starts at the end
of a file. In order to output the first two lines, add the option -n 2 as follows:
$ head -n 2 places
Amsterdam
Berlin
$
In order to output the last two lines, do the same thing using tail as follows. In
contrast to tac (see earlier in this chapter) tail does not change the order of
output.
$ tail -n 2 places
Bern
Cape Town
$
nl
nl is similar to cat (see earlier in this chapter) but adds a line number at the
beginning of each line of output.
$ nl places
1 Amsterdam
2 Berlin
3 Bern
4 Cape Town
$
wc
This command abbreviates the phrase “word count” and counts lines, words and
single characters of the input data. Unless otherwise specified, all three values
are printed:
$ wc places
4 5 32 places
$
Amongst others wc offers the following options to limit the output:
-l: output the number of lines only, followed by the filename
-w: output the number of words only, followed by the filename
-c: output the number of characters only, followed by the filename
This example shows how to count only the lines in a file:
$ wc -l places
4 places
$
7.3 Users and Groups
These commands deal with a variety of actions in order to manage the users and
groups of your Linux system. Unless explicitly stated, these commands can be
run as a regular user.
whoami
This command returns your current user ID as follows:
$ whoami
user
$
users, who and w
The users, who and the w commands show the users that are currently logged
into your Linux system. w extends the output of who by the uptime information
and another column that contains the last command that was executed. In
contrast, users simply outputs the name of the users as a space-separated list in a
single line (see image below).
The single columns for who start with the login name of the user. The output is
followed by the name of the terminal, where “console” represents a login
terminal, and “pts/1” abbreviates the first pseudo terminal session. The last two
columns contain the login time and the host the user comes from, in brackets
(see image below).
The single columns for w contain the login name of the user (titled LOGIN), the
name of the terminal (titled TTY), the name of the host the user comes from
(titled FROM), the login time (titled LOGIN@), the activity (idle time and CPU
usage titled IDLE, JCPU, and PCPU) as well as the last command the user
executed (titled WHAT) (see image below).
id and groups
The id command outputs the user and group information of the current user (see
image below). From left to right the columns show the user ID (uid=1000(user)),
the group id (gid=1000(group)) and the name of the groups the user is a member
of.
In order to list the names of all the groups the user belongs to, you can also
invoke the groups command (see image below). The output is a space-separated
list of the group names.
passwd
As described in Chapter 4, the Linux system has at least two users: an
administrative root user and a regular user that we simply called user. Every
account is also secured with a password.
In order to change your password, use the passwd command from the Debian
“passwd” package (refer to Chapter 4 on how to install additional software). As
shown below, type in the current password first, press Enter, type in the new
password, press Enter to confirm, retype the new password and press Enter to
confirm, again.
As a regular user, you are allowed to change your own password only. The
administrative root user is able to set a new password for itself and other users as
well. In such a scenario, we call passwd as follows:
# passwd felix
Changing password for felix.
(current) UNIX password:
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
#
The password is stored as a hashed value in the configuration file “/etc/shadow”.
The content of this file is only visible to the administrative user. The example
below shows how to extract the information for the user “user” with the help of
the grep command (see earlier in this chapter).
chfn
This command is also from the Debian “passwd” package and changes the user
information that is stored on your system in the file /etc/passwd. During the
installation of your Debian system the basic setup was already done. In order to
modify this information you can run chfn without further parameters in
interactive mode, or with one or more of the following options to adapt only a
specific value:
-f or --full-name: change the full name of the user
-h or --home-phone: change the home phone of the user
-r or --room: change the room number of the user
-w or --work-phone: change the work phone of the user
The following example changes the entry for the home phone number to 135:
$ chfn -h 135
Password:
$
chsh
This command (also from the Debian “passwd” package) changes the entry for
the shell that you use to log into your Linux system. Again, this information is
stored in the file /etc/passwd. Which shells are allowed to be used are limited by
the entries in the configuration file /etc/shells.
chsh works similar to the chfn command. Invoked without further options an
interactive method is used (see image below).
chsh accepts the option -s (short for --shell) in order to set the shell in non-
interactive mode. The following example shows the according command line
call:
$ chsh -s /bin/bash
$
In order to modify the shell for a different user other than yourself, invoke the
chsh command with the user name as a parameter. Note that only the
administrative user can do this for a different user. The next example shows how
to do that for the user “felix”.
# chsh felix
Changing the login shell for felix
Enter the new value, or press ENTER for the new value
Login shell [/bin/bash]:
#
su and sudo
In order to change your role from one user to another, you utilize the su
command. su abbreviates “switch user”. Invoked without further options you
change to the root user as follows:
$ su
Password:
#
Working as the administrative root user comes with great responsibility and
presumes that you know exactly what you are doing. To work as a different user
than root, invoke the su command with the desired user name as follows:
$ su felix
Password:
$
The su command changes the current role permanently. In order to run only a
single command as an administrative user, use the sudo command. This requires
the Debian “sudo” package to be installed (refer to Chapter 4 on installing
addition software) and the additional user to be added to the configuration file
/etc/sudoers using the visudo command. This step will be explained in further
detail later on.
adduser
The command adduser creates new user accounts. The image below shows the
information that is required. This includes a new entry in the file /etc/passwd as
well as the creation of a new group, plus home directory. Furthermore, prepared
data from the directory /etc/skel is copied into the previously created home
directory. Afterwards, the account information is modified using the chsh
command (see earlier in this chapter).
Having set up the new user, the entry in the file /etc/passwd looks as follows:
Deleting user accounts and modifying user accounts is done with the help of the
two commands deluser and usermod. The usage of these commands will be
explained in further detail later on.
7.4 Process Management
ps
This command lists the running processes of your Linux system. The image
below shows the processes in your current terminal session, where the call ps ax
(see the image thereafter) lists all the processes in your Linux system. The ps
command has a long list of options that will be explained later on in more detail.
pgrep
pgrep is the grep command (see earlier in this chapter) for processes. This
command looks through the currently running processes and outputs the process
ID’s that match the given pattern.
The image below shows two calls. The first call asks pgrep to search for the
processes that have the string pattern “xterm” in its name, and to output the
resulting process ID only. The second call uses the two options -l (short for --list-
name) and -a (short for --list-full) in order to show the process ID and the
resulting process name.
The partner of pgrep is pkill. Both commands are part of the Debian “procps”
package (refer to Chapter 4 on how to install additional software packages).
kill
This command sends a specific signal to a process. In order to terminate this
process use the SIGTERM signal. The next example shows how to end the
process that has the process ID 12345.
The command kill requires a process ID to work properly. Use ps or pgrep to
obtain the according process ID first (see earlier in this chapter).
$ kill 12345
$
killall
This command is part of the essential Debian package “psmisc” (refer to Chapter
4 on installing additional packages). killall sends a signal to all the processes that
match the specified command. As an example, the command
killall firefox
sends the signal SIGTERM to all the processes that have the name “firefox” in
order to terminate the process. Amongst other options, killall allows these
parameters:
-e (short for --exact): require an exact match
-I (short for --ignore-case): treat the process name case-insensitive
-i (short for --interactive): interactively ask for confirmation before
terminating the process
-v (short for --verbose): send a confirmation message if the process
terminated
The following example shows how to combine the two commands pgrep and
killall. First, obtain the process ID using pgrep as well as the exact name of the
process. Second, invoke killall with the two options -i and -v to terminate all the
processes that have that name. Prior to killing the processes killall asks you for
confirmation, and afterwards outputs a confirmation message.
pkill
The two commands pkill and pgrep (see earlier in this chapter) belong to the
same software package. Whilst pgrep scans the process list and outputs the
process ID, the pkill command will send the termination signal to the processes
that match the given pattern. In order to terminate all the “xterm” processes,
invoke the following command:
$ pkill xterm
$
top
The top command displays the processes according to their activity. The most
active process is on top, followed by the less active ones. The list is updated
every second. The single columns contain the process ID (titled PID), the user
name of the owner of the process (titled USER), the process priority (titled PR),
the nice level (titled NI), the virtual memory usage (titled VIRT), the reserved
memory (titled RES), the shared memory (titled SHR), both the percentile CPU
and memory usage (titled %CPU and %MEM), the running time of the process
(titled TIME+) as well as the command that was used to invoke the process
(titled COMMAND).
In order to quit top press the q key.
htop
“htop” is an additional software package (refer to Chapter 4 on installing
additional packages) and contains a more interactive version of top (which we
mentioned above). The arrangement of the columns is similar to top. Use the
navigation and function keys in order to select the processes, sort them, or delete
them. The function keys are:
F1: show help
F2: configure htop
F3: search within the process list
F4: filter the terminal output
F5: display the processes as a process tree
F6: change the sort order of the processes
F7: decrease the nice level of the selected process
F8: increase the nice level of the selected process
F9: terminate the selected process
F10: quit the program
7.5 Network and System Information
uname
uname abbreviates the term “UNIX name”. The command displays system
information such as the exact name and version of the Linux kernel and the host
name of your computer.
The image below shows the call of the uname command with the parameter -a
(short for --all). The output contains the name of the operating system (Linux),
the hostname (debian95), the kernel version and its build date (4.9.8-7-amd64 #1
SMP Debian 4.9.110-1 (2018-07-05)) as well as the architecture of the system
(x86_64).
uptime
This command shows how long the system is running. It displays the current
time (07:36:47) followed by the uptime in hours (21:56), the number of logged
in users (1 user) and the average load (load average: 0.47, 0.43, 0.29) for the last
1, 5 and 15 minutes.
In order to see the uptime in a nicer way, use the option -p (short for --pretty).
The image below displays a more human-readable version of the information.
The system is up 22 hours and 3 minutes.
ip
The ip command (along with the two keywords address show) displays the
current network configuration. The image below shows the loopback interface
(lo) and the ethernet interface (enp0s3). The ethernet interface is configured with
the IP address 10.0.2.15. The network interfaces are abbreviated as follows:
lo: the loopback interface. It is used to access local services such as a
proxy or webserver http://127.0.0.1/
eth0: the first Ethernet interface connected to a network switch or router
wlan0: the first wireless interface
ppp0: the first point-to-point interface, used to connect via VPN or dial up
service
ping
ping sends ICMP network packets to the given IP address or hostname, and
displays the turnaround time. The image below demonstrates this for the host
http://www.google.com.
8. Getting Help
UNIX/Linux is a rather complex operating system, especially for beginners.
When you get started, the list of programs can be quite confusing. Each program
has a longer list of parameters or options than the last. And they are not easy to
memorize, they have long and short names, and are not standardized. Asking for
help when you get stuck is not a mistake or sign of low knowledge.
In order to get help on your Linux system, there are several ways to go about it.
This includes the manual pages (called man pages), information pages (called
info pages), as well as the integrated help of each command. In this chapter we
will have a closer look at these help systems and get familiar with the commands
man, info, whereis and whatis.
8.1 Man Pages
Man pages are the traditional way of distributing documentation about
programs. The term “man page” is short for “manual page”, as they correspond
to the pages of the printed manual. Man pages are part of the basic installation of
your Linux system. The corresponding Debian package is named “manpages”.
A man page corresponds to a specific section in the full UNIX manual: 1 for
commands, 2 for system calls, etc. To get information regarding a specific
command, open the corresponding man page in a terminal window. As an
example, for the cp command it is done as follows:
$ man cp
The resulting man page looks like this:
The key bindings are similar to the ones from the text editor vi(m). Use the
navigation keys to scroll up and down, and q to exit the man page and to return
to the terminal.
8.2 Info Pages
Info is the default format for documentation inside the GNU project. In the early
1990s, the GNU project decided that the man documentation system was
outdated, and wrote the info command to replace it. info has basic hyperlinking
features and a simpler markup language to use (compared to the troff system
used for man pages). Nowadays both systems exist in parallel.
The basic installation of Debian does not contain the info pages. In order to use
it, post-install the package named “info” (refer to Chapter 4 on how to install
additional packages). To get information regarding a specific command, open the
corresponding info page in a terminal window. As an example, for the cp
command it is done as follows:
$ info cp
The resulting info page looks like this:
8.3 Integrated Help
Aside from man and info pages, many commands have an option -h (short for --
help). Calling the command with this option opens a specific help section. The
image below shows the built-in help of the uname command.
Alternatively, the whatis command is helpful to get brief information about
Linux commands or functions. It displays a single line man page description for
the command that matches the string passed as a command line argument to the
whatis command.
The whatis command searches for the string in its index database, which is
maintained by the mandb program, and picks a short description from the
NAME section of the man page for that command. The image below shows this
for the two commands mv and man.
Another option is the whereis command, which is helpful to locate the manual
page of the command in the Linux system. It is very simple utility. The image
below shows this for the two commands mv and man.
Every command also provides additional information and configuration
examples. This is kept in the directory /usr/share/doc, and every program has its
own subdirectory. The content is standardized and differs from program to
program. The content is also accessible for every user. A snippet of the directory
is shown below.
8.4 External Help
If the help already provided with the software tools is not enough, and further
explanation is necessary, external resources come into play. There are a few
online communities that can be consulted such as:
LinuxHint - https://support.linuxhint.com
LinuxHelp - https://www.linuxhelp.com
Another option is Linux User Groups (LUG). A LUG is a group of people from
the same place sharing their common interest in the Linux operating system.
They are organized as a loosely associated number of people, or an association
that meets regularly. A full list of LUGs worldwide is available from LugsList at
http://lugslist.com
Many LUGs organize meet-ups and events in order to share ideas, exchange
experiences, and to learn from each other. This includes local and international
events.
Further Reading
If you found this book helpful, please consider leaving a review on Amazon. It
shows my wife and I that the long nights we put into creating it were worth it ;)
For further reading on Linux, keep a lookout for more books within this series
coming soon.
About the Author
Nathan Clark is an expert programmer with nearly 20 years of experience in the
software industry.
With a master’s degree from MIT, he has worked for some of the leading
software companies in the United States and built up extensive knowledge of
software design and development.
Nathan and his wife, Sarah, started their own development firm in 2009 to be
able to take on more challenging and creative projects. Today they assist high-
caliber clients from all over the world.
Nathan enjoys sharing his programming knowledge through his book series,
developing innovative software solutions for their clients and watching classic
sci-fi movies in his free time.