Chapter 6 - Linux Mix
Chapter 6 - Linux Mix
Chapter 6 - Linux Mix
Introduction
The sixth chapter is not focused on a special Linux subject as it is rather a combination of
references to many basic utilities. The main idea behind this approach is that it is hard to learn
Linux from scratch in 8 courses, if all chapters focus deeply on a single subject. This one is a
bit of everything and I was not sure if I should use the Linux Mix title or Linux Salad.
Even if the topics presented here won't be explored in detail, you should know about all of
them if you want to start learning Linux seriously.
First of all, we discuss about two special types of files: links and pipes. The section will show
why do those types of files exist and how can you use them for your own purposes.
Than it is time to talk about the commands used for creating, coping, moving and removing of
files and folders. I know, you already used them many times, but there are some details that
should be mentioned here.
Two sections will be dedicated to archiving and text filtering. You can't live without this. This
is why this paper has labs attached for both topics. You will not only learn how to create and
extract Linux archives, but you will also learn how to create archives that can be exported to
Windows systems.
Searching for files and folders is one of the most important operations that a system
administrator should handle easily. Therefore, the dedicated section will investigate mlocate
related utilities (locate) and the versatile find.
Finally, Secure Shell tools and server will be discussed in the last section of the chapter. I
must mention that secure shell or SSH is a very common protocol that can be used to access
servers or network devices from a variety of terminals: laptops, desktops, tablets, mobile
phones, etc. You will find SSH clients developed for Windows, Linux and MacOS, as well as
for popular mobile OSes like Andoid and iOS.
We got a lot of work to do, so let's move on and present the first point.
Let's start with symbolic links. A simlink simply represents a separate file containing a path to
another file and marked as link.
See the last entry in the output of the ll command? That is a symbolic link. Remark the first
character in that line. It is 'l' and it stands for link. Another important thing to remark is the
size of the file: 4 bytes (5th column). Given that a byte can store one character, what
characters can be stored inside this file? Well, it is the target of the link: /tmp.
What can we do with such link? Most of the things that we can do with a normal file. Some
executables have options to follow the symbolic links and work directly with the target of the
link. For example, if you will try to edit a link, the editor will open the target file. However,
when you will remove the symlink using rm, only the link itself will be deleted and the target
will be left untouched.
ln -s <target_resource> <link_file>
If you omit the -s switch, the ln command will create a hard link. A hard link represents a new
file system entry that point to the same disk content as the target of the link. That means that
the file system entry is associated with the same inode. Let's create hard and symbolic links to
a file (/root/file). Check the output of the stat command:
Note that the hard link is associated with the same inode number as the target, while the
symlink has a different inode associated. The hard link is not a different file, it is just another
file system entry for a content on the partition.
Note that you cannot create hard links for folders. You can imagine all files from a partition
like hard links to content on the drive. Therefore, if you delete the original file with rm, you
will still be able to access the content using the hard link. The same applies to the hard link:
even if you delete it, the content is not removed from the drive.
ln <target_file> <link_name>
touch <file>
It is also possible to create a file by redirecting content to the file using '>' operator. File
reading can be done using cat, more and less viewers.
More text viewer:
mkdir <folder>
You can use the -p switch if you need to create a more complex structure of directories from a
single line.
To copy files or folders from command line you have to use cp command:
cp <source> <destination>
The command works straightforward for normal files. However, when you intent to copy
folders, you have to add the -R (recursive) flag.
Removing resources is performed using rm command.
rm <resource>
However, for folders you will have to use the -R switch again. Moreover, if you want to avoid
delete confirmation, you should add the -f flag also.
Grep
Grep stands for GNU Regular Expression Parser and is a tool used to find strings that match a
specific regular expression in a text or in the output of a command. You will often see grep
used like:
The previous command uses grep to parse the output of the <command> passed through an
anonymous pipe to the next command (grep). Explaining regular expression is completely out
of this book's purpose and we will continue showing some simple examples. Also please
check the lab associated with this topic.
When processing files you can use grep with two arguments, the second one being the
inspected file.
The -n flag is used to add the line number before each line that matches the regex.
Archiving refers to the process of grouping a set of resources (folders, files) into one single
file: the archive.
On the other hand, compression refers to reducing the size of a resource by using special
compression algorithms. Example of compression algorithms are Gzip, Zip, Bzip2, etc.
First of all, let's present tar, the mostly used command on Linux for creating and restoring
archives. Let's list some relevant switches and options:
Let's create a simple archive. First I create a 50MB file full of zeros.
See the archive.tar file? That is the archive file. Note the size: it is at least as big as the files
that are included in the archive. Now let's create an archive with gzip compression algorithm
to reduce the size.
The new archive consists of only 51 Kilobytes. Why? Because the compression algorithm is
smart enough to identify the long set of zeros from the 50MB file and compress it.
Searching
Locating files and folders is a basic task that should be simple and efficiently done by any
system administrator as well as system user. In Linux (CentOS and many other distros) you
can use two important tools for this purpose: locate and find.
Let's discuss about locate first. Locate is part of the mlocate package which must be installed
on the system. The approach of this tools is rather special: it first builds an indexed database
based on the state of the file system. The command responsible for this is updatedb (also part
of mlocate). Only after this database is built, anyone can use locate to find files and folders.
You can search only within the basename of the file (only the file name) using the -b flag.
You can also add the -r flag to switch on the regular expression support.
Searching with locate is really fast. However, search operations represent queries on a
database built from a file system snapshot. And that snapshot might not be actual at the
current moment. Nobody guarantees that once you delete a file the database will be instantly
updated. To keep the locate database up to date it is a good idea to schedule the update
command as a cron job.
A different searching tool is find. Unlike locate, find performs an actual search on the real file
system. It always provides up to date results, but the drawback is that it performs slower than
locate.
Find is a very complex command and has its own option guide lines (uses long options
prefixed with single dash). The general usage is:
Find provides recursive search from the <search start path>. You can search by (using the
options)
Many examples are provided in the special searching lab. For example: to fine files owned by
root, from /etc, with 600 permissions you can use:
Secure Shell
Secure Shell, also known as SSH is a protocol widely used for providing remote access to
devices. What kind of devices? Well, SSH servers can be found on switches, routers and
firewalls, but you will mostly use it to access servers. Especially Linux servers. While
Windows servers make extensive use of Remote Desktop and WMI, Linux powered devices
rely heavily on SSH.
SSH provides a secure encrypted channel for accessing a shell on the server from a remote
client. The server is always running on the machine that you want to access and the client can
run on a laptop, desktop, tablet or other similar terminal device.
Note that the server is listening for new SSH connection on port 22, TCP.
Now going back to encryption. SSH uses asymmetric keys for encryption and authentication.
Asymmetric cryptography is based on key pairs. Each entity uses two keys: a public key and a
private key. The public key is publicly available and is shared with other entities that want to
send data through the secure channel. It is used only for encrypting data and cannot be used
for decryption. On the other side, the private key stored on the server is used for decrypting
data, the data encrypted with server's public key.
Therefore, before any relevant data is sent through the secure channel there is a public key
exchange. The client (C ) sends his public key to the server (S). The server sends his public
key to the client. After that, the client will encrypt data sent to server using S's public key and
the server will respond with data encrypted with C's public key. For decrypting the data
received, each subject will use his own private key.
Ok, let's move on the practical part. To get the server working, you need to install openssh-
server package on your system. The client tools are included in openssh and openssh-clients
packages.
When connecting to a server you will use an account (username/password) recognized on the
server (NEVER from your client machine). The general command for ssh CLI command is:
ssh <username>@<remote_host>
After issuing a similar valid command you will be prompted to authenticate on the remote
machine using 's password. After successful authentication you will get shell access to server.
It is also possible to move files over the ssh connection using scp client. The syntax is similar
to cp:
scp [email protected]:/tmp/a.txt ~/
Just like ssh, scp can perform password authentication by checking credentials on the remote
server. However, both tools can perform authentication based on public-private keys. The
procedure requires the upload of the public key on the remote device, process that can be
performed using ssh-copy-id command. What is this tool actually doing? Well, suppose that
you want to perform public key authentication for user alex on server srv1. The ssh-copy-id
will require you to login remotely on server srv1 using alex account. Than it will simply
append the public key in the /home/alex/.ssh/authorized_keys file from srv1. Simple like that.
After that, when user alex will try to login through ssh, the server will try a challenge
authentication by encrypting something with user's public key. If the client can decrypt that, it
means that it has the private hey associated with the public key from the server.
The SSH server from the Linux server is the sshd daemon, part of the openssh-server package.
The configuration file is stored in /etc/ssh/sshd_config. Edit this file to perform configurations
for the server. Note that after performing a configuration change you should restart the server
in order to re-read the file. Do that by issuing service sshd restart command.
The ssh lab presents the authentication process based on public-private keys. There are also
instructions for disabling the possibility to perform ssh authentication for user root, which is a
recommended security practice.
Conclusions
This chapter represents a brief introduction to some important topics related to Linux. If you
want to be a good system administrator, you should take each subject presented here and
perform deeper research. And please walk through all labs associated with the topics from this
chapter. This will provide you the basic practical experience for SSH access, file/folder
searching and text parsing.