Cybersecurity Essentials - Operating Systems - Getting Started With Linux v1
Cybersecurity Essentials - Operating Systems - Getting Started With Linux v1
Linux
[Additional Information]
Cybersecurity Essentials: Operating Systems
CE4101: Getting Started with Linux
June 2022
Version: 1.0
© 2022 Sophos Limited. All rights reserved. No part of this document may be used or reproduced in any form or by any
means without the prior written consent of Sophos.
Sophos and the Sophos logo are registered trademarks of Sophos Limited. Other names, logos and marks mentioned in this
document may be the trademarks or registered trademarks of Sophos Limited or their respective owners.
While reasonable care has been taken in the preparation of this document, Sophos makes no warranties, conditions or
representations (whether express or implied) as to its completeness or accuracy. This document is subject to change at any
time without notice.
Sophos Limited is a company registered in England number 2096520, whose registered office is at The Pentagon, Abingdon
Science Park, Abingdon, Oxfordshire, OX14 3YP.
DURATION 90 minutes
In this course you will learn the basics of using the Linux operating system; including, managing
permissions and services, editing files, port tunnelling, and scheduling tasks.
Linux is an operating system, which is the software that manages all the hardware resources of your
device, whether that is a desktop, laptop, tablet, or phone. To put it simply, the operating system
manages the communication between your software and your hardware. Without the operating
system (OS), the software wouldn’t function. From smartphones to cars, supercomputers and home
appliances, home desktops to enterprise servers, the Linux operating system is everywhere.
[Additional Information]
Image: https://commons.wikimedia.org/wiki/File:Linux_Lite_5.2_Desktop_en.png
https://distrowatch.com/
There are many different versions of Linux to suit any type of user. From new users to experienced
users, you’ll find a flavor of Linux to match your needs. These versions are called distributions, or
distros for short. Nearly every distribution of Linux can be downloaded for free, written to a disk or
USB drive, and installed on as many machines as you like.
Each distribution has a different take on the desktop. Some opt for very modern user interfaces (such
as GNOME and Elementary OS’s Pantheon), whereas others stick with a more traditional desktop
environment (openSUSE uses KDE).
You can check out the top 100 distributions on the Distrowatch.
When logging into a Linux system, you can either login locally, if you have physical access to the
device, or you connect remotely. Some Linux installations have a user interface, as shown here, while
others only have a command line interface. With a GUI, simply fill out the username and password to
access the system.
SSH can be used to pass specific commands to the system and/or start interactive
shells
C:\>ssh [email protected]
The authenticity of host 'sophoslab-648685.westeurope.cloudapp.azure.com
(20.224.226.54)' can't be established.
ECDSA key fingerprint is SHA256:wOGVqD6JlXEi0aY5bJD/pEhlMYVCNfLFF1OAjI1xMhU.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'sophoslab-
648685.westeurope.cloudapp.azure.com,20.224.226.54' (ECDSA) to the list of known hosts.
[email protected]'s password:
Welcome to Ubuntu 20.04.4 LTS (GNU/Linux 5.13.0-1029-azure x86_64)
labuser@SophosLab-648685:~$
For remote systems, SSH, or secure shell, is most used to access the command line interface. To do
this you use an SSH client, most modern operating systems have one built-in, including Windows, as
shown here.
SSH can also be used to pass specific commands to the system instead of starting interactive sessions.
Output
Generating public/private rsa key pair.
Enter file in which to save The
the key
key fingerprint is:
SHA256:CAjsV9M/tt5skazroTc1ZRGCBz+kGtYUIPhRvvZJYBs
(/home/username/.ssh/id_rsa):
username@hostname
The key's randomart image is:
Created directory '/home/username/.ssh'.
Enter passphrase (empty for +---[RSA 3072]----+
no passphrase):
Enter same passphrase again:|o ..oo.++o .. |
| o o +o.o.+... |
|. . + oE.o.o . |
our identification has been saved in
| . . oo.B+ .o |
/home/username/.ssh/id_rsa.
| . .=S.+ + |
Your public key has been saved in
|
/home/username/.ssh/id_rsa.pub. . o..* |
| .+= o |
| .=.+ |
| .oo+ |
+----[SHA256]-----+
Although passwords are sent to the server in a secure manner, they are generally not complex or long
enough to be resistant to repeated, persistent attackers. Modern processing power combined with
automated scripts make brute-forcing a password-protected account very possible. Although there are
other methods of adding additional security, SSH keys prove to be a reliable and secure alternative.
The systems in the lab environment are temporary and do not contain proprietary data, which is why
we have not implemented key protection in accessing the system. You are welcome to secure your
session with an RSA key, but it is not required.
[Additional Information]
https://www.ssh.com/academy/ssh/keygen
Your user context will affect your ability to modify certain files, execute certain
programs, and execute certain commands
Try running shutdown as the labuser in your lab; you will see a permission error due
to your user account’s insufficient privileges
labuser@SophosLab-646956:~/$ shutdown
Failed to set wall message, ignoring: Interactive authentication required.
Failed to call ScheduleShutdown in logind, no action will be taken: Interactive
authentication required.
All processes run on a Linux system as a User. Each User has assigned or inherited permissions to
interact with files and programs on the system. Your user context will affect your ability to modify
certain files, execute certain programs, and execute certain commands. Each file has read, write, and
execute attributes that affect a given user’s ability to interact with that program or file. Programs will
also execute with the identity of a user and will inherit that user and user’s group’s associated
permissions on the file system.
For example, if you attempted to use the shutdown command as a user it will fail. To run this
command, you will need elevated privileges.
labuser@SophosLab-649918:~$ whoami
Labuser
labuser@SophosLab-649918:~$ sudo su
root@SophosLab-649918:/home/labuser# whoami
root
You can find out details about the user you are logged in as with the id command. This will display
your user and group memberships.
The root user is the most privileged on the system and is needed to run most administrative
commands. This is also referred to as the superuser.
You can use the sudo command to run commands as the root user with the elevated privileges that
are associated with that account.
If you want to change user you can use the su, or switch user, command.
You can also combine these to login as the root user by running sudo su.
Best practice for Linux systems is to avoid logging in as the root user unless necessary. As the root user
can do anything, if you enter the wrong command, you can make the system usable, and often you will
not be prompted for confirmation. For this reason, when logging in via SSH, Linux will often not allow
you to login as root.
You can also use su to login as another user by providing the username of the user you want to login
as. If you are root you will not be prompted for a password, but if you try this from another user
account you will need to have the user’s password.
labuser@SophosLab-649918:~$ uname
Linux
labuser@SophosLab-649918:~$ uname -a
Linux SophosLab-649918 5.13.0-1029-azure #34~20.04.1-Ubuntu SMP Thu Jun 9
12:37:07 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux
labuser@SophosLab-649918:~$ cd /
labuser@SophosLab-649918:/$ ls
bin dev home lib32 libx32 media opt root sbin srv tmp var
boot etc lib lib64 lost+found mnt proc run snap sys usr
You can find out about what is running on the Linux server using the command ps, which stands for
process. By default, it will only show processes running as your user, but you can use additional
arguments to display all processes, and more details about the processes.
The uname command can be used to find out about the Linux server. It will display information on the
operating system, kernel, and architecture.
To change directory, you can use the cd command, followed by the directory you want to navigate to.
To display the files and folders in that location, use the ls command, which stands for list.
Here are some useful system commands. Take a moment to read through the descriptions for each.
You will be using these throughout the lab.
The lab task information can be found in the Lab Workbook which is available in the Lesson Resources
of this course.
PARTITIONS
PHYISCAL STORAGE
Like all operating systems, Linux and UNIX require somewhere to store applications and files, this is
done through several layers or organization and abstraction from the physical storage.
The physical storage can be divided into multiple partitions for storing different data.
Each partition will have a file system. Linux and UNIX operating systems support multiple different file
systems, and partitions do not all have to have the same file system.
The file system defines how the data is stored, including characteristics such as the length of the file
name, or the set of characters that can be used for file name, and the logical structure of files on a
memory segment.
There is virtual file system that Linux uses to abstract itself from the differences between file systems
so that the kernel can use a standard set of commands when interacting with storage.
/bin/ /cache/
/include/ /log/
/lib/ /spool/
/sbin/ /tmp/
Linux operating systems store their data in a hierarchal directory structure, where directories can be
on the same or different partitions or physical storage.
At the top of the tree is the root, denoted by a /. If you are familiar with Windows, you can think of
this like the C-drive.
Here are the first level of directories, these are the most important directories on the device, and
within those are the subdirectories.
Linux uses a directory tree to manage directories and files. The directory tree information should also
be stored on a storage device and this part is called the root file system (the "/" directory).
Hover over the blue first level directories to see a description for each.
[Additional Information]
/bin: Contains essential system commands that can be executed by any user
/boot: Files related to system startup, such as kernel files and bootloader (grub) files, etc…
/dev: Contains the device files for every hardware device attached to the system. These are not device
drivers, rather they are files that represent each device on the computer and facilitate access to those
devices
/etc: Contains the local system configuration files for the host computer, such as user information,
service startup scripts, configuration files for common services, etc. It's similar to Windows Registry
/home: The default location to log in and save user data. Each user has a subdirectory in /home
/lib: Contains essential shared library files that are required to boot the system
/media: Used to mount external removable media devices such as floppy disks, CDs, and USB thumb
labuser@SophosLab-649918:~$ cd /etc
labuser@SophosLab-649918:~$ cd ..
labuser@SophosLab-649918:/$ pwd
/
labuser@SophosLab-649918:~$ cd -
labuser@SophosLab-649918:/$ pwd
/etc
Let’s see how you can navigate through the directory structure using standard commands.
To see where you are you can use the command pwd, which standard for print working directory.
To change directory, use the command cd, followed by the directory you want to change to. This can
either be the absolute path starting from root, /, or it can be the relative path from where you are, in
which case you omit the slash prefix.
To list the files in a directory, use ls. This has several options for displaying more comprehensive
information.
-l shows more details for each file, including the permissions, size, and last modified date.
-a shows all files, including hidden files, which are those starting with a period.
In this example we have piped the output to the head command to only display the first five lines of
the output.
$ ls -l filename.txt
User Group
-rw-r--r-- 12 LinuxUser users 12.0K Apr 8 20:51 filename.txt
$ ls -l filename.txt
You may need to take ownership of a file or modify its permissions in order to be able to work with the
file or launch an application. This can be accomplished in multiple ways depending on the outcome
desired.
The chown command allows you to change the user and/or group ownership of a given file, directory,
or symbolic link. In Linux, all files are associated with an owner and a group and assigned with
permission access rights for the file owner, the group members, and others.
Here we can see that that the file filename.txt is owned by the user LinuxUser and the group users. By
running the chown command, we can replace the owner of filename.txt with SophosUser.
[Additional Information]
To learn more about chown you can visit https://linuxize.com/post/linux-chown-command/
$ ls -l filename.txt
-rw-r--r--
File type:
- File
l Link
d Directory Permissions for Permissions for Permissions for
user owner group owner everyone
In Linux, access to the files is managed through the file permissions, attributes, and ownership. This
ensures that only authorized users and processes can access files and directories. The chmod
command can be used to change the access permissions of files and directories.
Here we can see the output of the ls command for the file filename.txt.
The first flag is for the file type. This will be a hyphen (-) for a regular file, an l for a link, or a d for a
directory.
The first is for the user owner of the file, the second is for the group owner of the file, and third are for
everyone.
For each of these three sets of permissions there are three flags that can either be set where there is a
letter or unset where there is a hyphen (-). These are an r for read, w for write and x for execute.
[Additional Information]
To learn more about chmod, you can visit https://linuxize.com/post/chmod-command-in-linux/
Here are some useful system commands for navigating the file system and searching files. Take a
moment to read through the descriptions for each. You will be using these throughout the lab.
The lab task information can be found in the Lab Workbook which is available in the Lesson Resources
of this course.
apt update
yum update
apt upgrade
yum update <PACKAGE>
apt dist-upgrade
yum upgrade
apt install <PACKAGE>
yum remove <PACKAGE>
apt remove <PACKAGE>
Linux and Unix systems commonly use package manager utilities to add and maintain software
packages on the system. If you want to add a new program to a Linux system, the system’s package
manger is the way that you are going to do that. If you are familiar with OSX, homebrew is the
equivalent service on that platform. Yum is most commonly associated with the RedHat and CentOS
family of Linux systems, and apt is more common with the Debian family Linux systems.
The package manager will acquire the package from the appropriate repositories and install all
package components in the appropriate directories for the program’s runtime. Dependencies,
configuration files, binaries, etc, will all be placed at different portions of the filesystem.
To use the package manager, you will need to have elevated privileges.
Our lab uses Ubuntu, which is a Debian-based Linux, and so we will be using apt. apt is like the app
store of the Debian Linux operating systems.
apt is a command line interface for retrieval of packages and information about them from
authenticated sources and for installation, upgrade and removal of packages together with their
dependencies.
There are two versions of apt, apt and apt-get. For this course we will use apt, which is a simplified
version of apt-get.
In the example shown here, we use apt to install the nginx package. apt installs all the necessary
dependencies and unpacks all the packages to the appropriate directory in the server filesystem.
nginx is an open-source web server program and is one of the most widely deployed programs in this
application family. Nginx is now owned by F5 but is maintained as an open-source web server with
premium products available for sale. nginx.com is the website commercial offering, and nginx.org is
the website for open-source version.
Many high traffic web pages on the public internet use nginx, such as jd.com.
Options:
-?,-h : help
-v : show version and exit
-V : show version and configure options then exit
-t : test configuration and exit
-T : test configuration, dump it and exit
-q : suppress non-error messages during configuration testing
-s signal : send signal to a master process: stop, quit, reopen,
reload
-p prefix : set prefix path (default: /usr/share/nginx/)
-c filename : set configuration file (default: /etc/nginx/nginx.conf)
-g directives : set global directives out of configuration file
Most Linux programs support a help program argument, usually with -h. Here you can see the
additional program arguments and their suggested syntax for nginx.
Basic functions, like starting and stopping the service, can be accomplished with systemctl, which we
will cover shortly.
Using exact syntax with program arguments is essential; even an incorrect capitalization or extra space
can break the syntax and render your commands unusable by your target program.
The lab task information can be found in the Lab Workbook which is available in the Lesson Resources
of this course.
All service management begins with systemctl. This utility allows an administrator to query service
status, view logs, stop, and start the services running on the system. Once you have installed the
necessary software packages using apt or yum, you can then enable, start, and manage the running
service using systemctl.
For mode detailed information on using systemctl, please see the documentation linked from the
course notes.
[Additional Information]
https://man7.org/linux/man-pages/man1/systemctl.1.html
When you installed it, nginx was enabled to start when the system booted, but if it wasn’t we can
enable it using systemctl enable nginx.
When you using systemctl to start a service you won’t see any output unless there is an error.
Jun 14 10:54:32 SophosLab-649918 systemd[1]: Starting A high performance web server and a reverse
proxy server...
Jun 14 10:54:32 SophosLab-649918 systemd[1]: Started A high performance web server and a reverse
proxy server.
When you check the service, you see all the runtime information about the processes, followed by the
most recent log data.
The lab task information can be found in the Lab Workbook which is available in the Lesson Resources
of this course.
https://www.openvim.com/
Vi and Vim (Vi improved) have been used for editing files on the Linux-based systems for decades. You
can pick up the basic usage quickly, and this will be sufficient for general day-to-day usage. Gaining
mastery of vi or vim can be challenging, as they have their own very specific command syntax, but this
allows you to use these powerful programs to their full ability.
[Additional Information]
https://www.openvim.com/
Vim has two modes, command mode and insert mode. In command mode, you can search for text
strings, move your cursor position in the file, delete full lines, and much more. In insert mode, you are
editing the file via text editor, a much more traditional document editing experience. To enter insert
mode, press the i key once. To exit insert mode back to command mode, press Esc.
Type Use
/string n to go to the next match
to search for a string in the file N to go to the previous match
Here you can see an example where we have searched for ‘dhcp’ in the file by typing /dhcp.
You can then use lowercase n and uppercase N to go to the next and previous match respectively.
Here are some useful Vim commands when editing files. These are all used in command mode, not
insert mode, except for escape to exit insert mode.
<!DOCTYPE html>
<html>
<body>
</body>
</html>
view-source:https://www.weather.gov/
HTML (Hyper Text Markup Language) is a syntax used to structure webpages. You can go to your
favorite webpage, right-click and select to view the page source. You are viewing the HTML source of
the webpage.
You can use the link shown here to view the source for the weather.gov site.
Hypertext is text that is used to reference other pieces of text, while a markup language is a series of
markings that tells web servers the style and structure of a document.
The HTML code, as we see in the example on screen, is parsed by your web browser and rendered as
the web pages that we interact with every day.
As part of the lab, you will be creating a webpage using an HTML template we will provide.
To learn more about HTML, please visit the sites linked in the course notes.
[Additional Information]
https://html.com/
https://developer.mozilla.org/en-US/docs/Learn/HTML
https://www.w3schools.com/html/
The lab task information can be found in the Lab Workbook which is available in the Lesson Resources
of this course.
Cronjobs are used to execute scheduled tasks on a configured interval on Linux systems. System
administrators can create scripts and programs and configure cron to execute them as needed. Cron
has a unique syntax type in defining the period interval in which to execute these tasks. The URL
references on screen are useful in understanding this syntax and automatically generating your own
cron syntax based on your desired parameters.
[Additional Information]
Cron syntax guide: https://www.netiq.com/documentation/cloud-manager-2-5/ncm-
reference/data/bexyssf.html
Cron syntax creator:
http://www.cronmaker.com/;jsessionid=node018fef9s6cpavh1izquywo6iox3486245.node0?0
We can use the crontab command to add, modify, or delete the cronjobs running on our Linux
system. For the lab, we will use Vim, but the system will present you with a few options for text
editors.
To edit cronjobs for the currently logged in user run crontab –e. If you want to just display the
cronjobs you can run crontab –l.
There are other options included in the course notes, including how to view the cronjobs of other
users.
When you edit cronjobs you can also choose which editor you want to use.
[Additional Information]
usage: crontab [-u user] file
crontab [ -u user ] [ -i ] { -e | -l | -r }
(default operation is replace, per 1003.2)
-e (edit user's crontab)
-l (list user's crontab)
-r (delete user's crontab)
-i (prompt before deleting user's crontab)
The Linux Crontab format is expressed as Minute, Hour, Day, Month, Day of Week, and finally the
command to run on a single line. The hour field is expressed in 24-hour format and the day of week is
from 0 to 6. If you want the command to run ever day or every hour, for example, you would put an
Asterix in that place.
If you would like to run the command multiple times in a day, for example during business hours, you
would define a range in the hour field by using a dash. So, to run a command each hour starting at
8:00 am and ending at 5:00pm you would put 00 in the minute field and then 08-17 in the hour field.
This instructs Cron to run at every hour between 8:00 and 17:00. The same can be done for the days,
month, and day of the week sections.
[Additional Information]
Any additional information, for example, URL’s, knowledge base article numbers, commands, log
directories etc are added here.
Curl is a command line utility used to transfer data, supporting dozens of protocols and countless
parameters. When you need to get data of almost any type from a remote system onto your own, curl
is an excellent place to start. In the lab, we use curl to acquire state weather data from the National
Weather Service’s public API.
In the example shown here we are downloading the webpage sophostest.com to a file.
[Additional Information]
Full documentation available here - https://curl.se/docs/manpage.html
Here we have some examples of common curl usage. These different program arguments can be
chained together as well, where you might want to authenticate to an API service with curl -H and
your authentication token, and then output the API response to a file on your system.
The lab task information can be found in the Lab Workbook which is available in the Lesson Resources
of this course.
CloudLabs
SSH connection from
Student PC to
Ubuntu Server
SSH can be used for more than logging into a server interactively, you can also use it to securely tunnel
a port between a client and a server, this is called SSH tunnelling.
In your lab environment you will use SSH as a secure tunnel for the web port on the Ubuntu server,
port 80, and broadcast it on port 8080 on your local computer. You will then be able to connect to port
8080 on your computer and the request will be tunneled through to port 80 on the Ubuntu server and
you will be able to access the webpage.
Syntax
ssh –N <USER>@<SSH SERVER> -L [LOCAL_IP:]<LOCAL_PORT>:<DEST_IP>:<DEST_PORT>
Example
ssh -N [email protected] -L 8080:localhost:80
Here you can see the syntax for port tunnelling with SSH. As you can see, you specify the local port,
the destination IP address on the remote server, and the destination port on the remote server. You
can also optionally specify the local IP address to broadcast on. If no IP address is provided, localhost
is used.
If you have an SSH connection open, such as with the tunnelling command, but it is idle, then you will
be disconnected. You can prevent this be also using the ServerAliveInterval option.
We are going to look at doing a local port forward which will forward a port on our local machine (the
SSH client) to a port on the remote server (the SSH server). When this is configured, the local machine
will listen for traffic destined for a specific port and when detected, it will forward the traffic to the
remote SSH server.
[Additional Information]
The -N switch tells SSH not to run a remote command.
The -L switch tells the SSH client to create a local port forward.
If you do not supply the local IP address it will use 127.0.0.1, also known as localhost.
You need to supply the local port you will be connecting to.
You need to supply the IP or hostname and port of the destination computer and service to be
tunnelled.
The lab task information can be found in the Lab Workbook which is available in the Lesson Resources
of this course.
Image downloaded by
imagepull.sh cronjob
JSON downloaded by
cronjob.sh cronjob
Congratulations! You have made it all the way from executing your first file system commands,
through using a command line text editor, installing up a webserver, using scheduled jobs and scripts,
and are now viewing your webserver through a secure SSH tunnel! The concepts and tools that you
used throughout this lab should serve as foundational steps in understanding how Linux systems are
used, the world that the administrators and users of these systems live in, and allow you to continue
your usage and development on the Linux platform.
If you would like to try implementing more detailed HTML on your webserver you can access guides
and templates at w3schools.com and nicepage.com. You will find links in the course notes.
[Additional Information]
https://www.w3schools.com/html/
https://nicepage.com/html-templates
All files and folders on Linux have owners, groups, and read, write, and execute permissions. Processes
run as users which determine the permissions they have. The root user has the highest level of
permissions.
The package manager is like the app store for Linux. It can install, upgrade, and remove software from the
Linux system. You need root access to use the package manager.
You can create scheduled tasks using crontab. You edit the cronjobs using an editor like vim. Cronjobs can
automate tasks that include downloading content using tools like curl.
Here are the three main things you learned in this chapter.
All files and folders on Linux have owners, groups, and read, write, and execute permissions. Processes
run as users which determine the permissions they have. The root user has the highest level of
permissions.
The package manager is like the app store for Linux. It can install, upgrade, and remove software from
the Linux system. You need root access to use the package manager.
You can create scheduled tasks using crontab. You edit the cronjobs using an editor like vim. Cronjobs
can automate tasks that include downloading content using tools like curl.