SEC201.2 Linux Fundamentals
SEC201.2 Linux Fundamentals
Linux Fundamentals
ir
All Rights Reserved
.
01
This page intentionally left blank.
de
hi
. ir
SEC201 | Intro to Computer Fundamentals 2
01
Day 2 Intro
de
hi
What Is Linux? 10
Linux Uses 12
Linux Distributions 13
Directory Structure 51
Hidden Files 57
Symlinks 58
Linux Permissions 60
ir
Linux Permissions – The Short Way 62
.
SEC201 | Intro to Computer Fundamentals 3
01
This page intentionally left blank.
de
hi
Linux Processes 66
Linux Terminal 67
ir
LAB: Mini-Lab Setup 77
.
SEC201 | Intro to Computer Fundamentals 4
01
This page intentionally left blank.
de
hi
ir
Linux Terminal – Commands - grep 113
.
SEC201 | Intro to Computer Fundamentals 5
01
This page intentionally left blank.
de
hi
ir
Linux Terminal – Commands - chmod 142
.
SEC201 | Intro to Computer Fundamentals 6
01
This page intentionally left blank.
de
hi
ir
Linux – rpm 167
.
SEC201 | Intro to Computer Fundamentals 7
01
This page intentionally left blank.
de
hi
Linux Troubleshooting – I’m typing a command but it says it can’t be found 204
ir
Linux Troubleshooting – I’m sure I ran an update but nothing has changed 205
.
SEC201 | Intro to Computer Fundamentals 8
01
This page intentionally left blank.
de
hi
EXERCISE: Linux – Installing Arch Linux (Optional Out of Class Exercise) 212
. ir
SEC201 | Intro to Computer Fundamentals 9
01
This page intentionally left blank.
de
hi
• The name for a group of operating systems that use the Linux
kernel as the core of the OS
• Many distributions of Linux that have been customized for
different purposes
• Some are server distributions with no Graphical User Interface
designed for efficiency
• Others are desktop distributions with a built in Graphical User
Interface. Many of them have been customized to make Linux
easier.
. ir
SEC201 | Intro to Computer Fundamentals 10
01
What Is Linux? (1)
Linux is a name for a group of operating systems that use the Linux kernel as the core of the operating system. Linux was based on Unix;
de
however, where Unix was proprietary and had to be licensed, Linux is open-source software. Linux is fragmented into ‘distributions’, many of
which have specialized functions; however, they all use the Linux kernel. The Linux kernel was originally written by Linus Torvalds; however, it
is now being maintained by the open-source software community.
hi
Linux distributions are often separated into desktop releases and server releases. A desktop release often comes with software pre-installed and,
most notably, a Graphical User Interface (GUI) of some sort is included. Traditionally even desktop releases have been quite difficult for non-
technical users to work with; however, more recently there has been a big push to lower the barrier to entry for Linux with distributions such as
Ubuntu and Elementary OS, making it easier than ever for non-technical users to utilize Linux. On the other hand, server releases often come with
minimal software pre-installed and even lack a Graphical User Interface. This means that server distributions are more efficient; however, the lack
of a GUI means that you have to master the command line.
. ir
SEC201 | Intro to Computer Fundamentals 11
01
What Is Linux? (2)
Linux is typically more difficult to use than other operating systems, such as Windows and Mac. On the other hand, Linux runs extremely well on
de
even old hardware. Furthermore, Linux is highly customizable and there are very few restrictions on what a user can do. Of course, that also
means it’s very easy to break a Linux install if you don’t know what you are doing.
hi
Linux Uses
. ir
SEC201 | Intro to Computer Fundamentals 12
01
Linux Uses
Linux is widely used as a server operating system for two main reasons. First of all, Linux runs extremely well on just about any hardware, which
de
means it’s much more efficient at using the resources that are available. Secondly, server distributions are even more efficient because they don’t
include any unnecessary software, even down to not having a graphical user interface (which of course is unnecessary for a server).
On the other hand, Linux is less widely used as a desktop operating system. However, the security community uses Linux desktop distributions
hi
extensively. This is precisely because of how customizable Linux is. Most security tools are developed first for Linux; some of them are then
ported to other operating systems, however not all. You will always need to have a Linux install available to you.
Linux Distributions
. ir
SEC201 | Intro to Computer Fundamentals 13
01
Linux Distributions
There are many Linux distributions floating around. Many of them even have a specific purpose, such as Kali Linux: a security-focused Linux
de
distribution that comes with many security tools pre-installed. Originally, many Linux distributions were built from scratch around the Linux
kernel, however more and more distributions started to appear based on existing Linux distributions instead. This has evolved to the point where
there are two main ‘base’ distributions, which most other distributions are built from. This is actually a good thing, as it simplifies the learning of
each distribution. It’s much easier to learn one of the base distributions, as most of your knowledge can then apply to other distributions, which are
hi
based on those. Take, for example, Ubuntu, a relatively famous Linux distribution. It was originally built from Debian. It shares many
commonalities with Debian, to the extent where if you know how to use Debian, you know nearly everything you need to know about Ubuntu.
The two main base distributions are Debian and Fedora. They are quite different from each other and most people have a preference one way or
the other. If you learn one of these base distributions you should be able to sit down at any Linux distribution based on them and be able to work it
to a reasonable degree.
. ir
SEC201 | Intro to Computer Fundamentals 14
01
Lab: Installing Linux (1)
de
hi
. ir
SEC201 | Intro to Computer Fundamentals 15
01
Lab: Installing Linux (2)
de
hi
. ir
SEC201 | Intro to Computer Fundamentals 16
01
Lab: Installing Linux (3)
de
hi
. ir
SEC201 | Intro to Computer Fundamentals 17
01
Lab: Installing Linux (4)
de
hi
. ir
SEC201 | Intro to Computer Fundamentals 18
01
Lab: Installing Linux (5)
de
hi
. ir
SEC201 | Intro to Computer Fundamentals 19
01
Lab: Installing Linux (6)
de
hi
.ir
SEC201 | Intro to Computer Fundamentals 20
01
Lab: Installing Linux (7)
de
hi
ir
.
SEC201 | Intro to Computer Fundamentals 21
01
Lab: Installing Linux (8)
de
hi
. ir
SEC201 | Intro to Computer Fundamentals 22
01
Lab: Installing Linux (9)
de
hi
ir
.
SEC201 | Intro to Computer Fundamentals 23
01
Lab: Installing Linux (10)
de
hi
. ir
SEC201 | Intro to Computer Fundamentals 24
01
Lab: Installing Linux (11)
de
hi
ir
.
SEC201 | Intro to Computer Fundamentals 25
01
Lab: Installing Linux (12)
de
hi
ir
.
SEC201 | Intro to Computer Fundamentals 26
01
Lab: Installing Linux (13)
de
hi
ir
.
SEC201 | Intro to Computer Fundamentals 27
01
Lab: Installing Linux (14)
de
hi
.ir
SEC201 | Intro to Computer Fundamentals 28
01
Lab: Installing Linux (15)
de
hi
ir
.
SEC201 | Intro to Computer Fundamentals 29
01
Lab: Installing Linux (16)
de
hi
. ir
SEC201 | Intro to Computer Fundamentals 30
01
Lab: Installing Linux (17)
de
hi
ir
.
SEC201 | Intro to Computer Fundamentals 31
01
Lab: Installing Linux (18)
de
hi
ir
.
SEC201 | Intro to Computer Fundamentals 32
01
Lab: Installing Linux (19)
de
hi
. ir
SEC201 | Intro to Computer Fundamentals 33
01
Lab: Installing Linux (20)
de
hi
ir
.
SEC201 | Intro to Computer Fundamentals 34
01
Lab: Installing Linux (21)
de
hi
ir
.
SEC201 | Intro to Computer Fundamentals 35
01
Lab: Installing Linux (22)
de
hi
ir
.
SEC201 | Intro to Computer Fundamentals 36
01
Lab: Installing Linux (23)
de
hi
ir
.
SEC201 | Intro to Computer Fundamentals 37
01
Lab: Installing Linux (24)
de
hi
ir
.
SEC201 | Intro to Computer Fundamentals 38
01
Lab: Installing Linux (25)
de
hi
ir
.
SEC201 | Intro to Computer Fundamentals 39
01
Lab: Installing Linux (26)
de
hi
. ir
SEC201 | Intro to Computer Fundamentals 40
01
Lab: Installing Linux (27)
de
hi
ir
.
SEC201 | Intro to Computer Fundamentals 41
01
Lab: Installing Linux (28)
de
hi
ir
.
SEC201 | Intro to Computer Fundamentals 42
01
Lab: Installing Linux (29)
de
hi
ir
• Then press ‘enter’ to continue
.
SEC201 | Intro to Computer Fundamentals 43
01
Lab: Installing Linux (30)
de
hi
ir
.
SEC201 | Intro to Computer Fundamentals 44
01
Lab: Installing Linux (32)
de
hi
. ir
SEC201 | Intro to Computer Fundamentals 45
01
Lab: Installing Linux (33)
de
hi
ir
.
SEC201 | Intro to Computer Fundamentals 46
01
Lab: Installing Linux (34)
de
hi
ir
.
SEC201 | Intro to Computer Fundamentals 47
01
Lab: Installing Linux (35)
de
hi
ir
.
SEC201 | Intro to Computer Fundamentals 48
01
Lab: Installing Linux (36)
de
hi
. ir
SEC201 | Intro to Computer Fundamentals 49
01
Lab: Installing Linux (37)
de
hi
• There are some things you should keep in mind about Linux:
• Linux is case sensitive. Two files called ‘File’ and ‘FILE’ are completely
different.
• In Linux, everything is a file. A file is a file, a folder is a file (that references
other files), a USB drive is a file, even a mouse is considered to be a file.
This will make more sense later, just bear it in mind!
• In Linux, the superuser account called ‘root’ is allowed to do absolutely
anything. Most distributions now force you to run as a normal user and
you have to elevate your privileges to the ‘root’ level temporarily when you
need to.
. ir
SEC201 | Intro to Computer Fundamentals 50
01
Linux – Key Points
You should be aware of several key points in Linux that might become stumbling blocks for you.
de
Linux is case sensitive. This is a common mistake because Windows is not case sensitive. On Linux, two files called ‘File’ and ‘FILE’ are
separate files entirely. If you type ‘File’ when meaning to access ‘FILE’ then you will be accessing the wrong file.
hi
In Linux, everything is represented as a file. This may seem weird, but take a look into the ‘/proc’ directory and you will see each process running
on the computer is represented as a file in that directory. You might also look into the ‘/dev’ directory where you will see every hardware device
connected to the computer (including hard disks) is represented as a file. Even folders are just files with references to the files contained within
them.
In Linux, usually, you run as a normal ‘un-privileged’ user. This user will be restricted from doing certain things such as installing software,
adding user accounts and so on. To do things that require privileges you must switch over to the ‘root’ account. Root is the ‘superuser’ account
and is allowed to do absolutely anything on the computer. Keep in mind that ‘root’ is dangerous if you’re not sure what you’re doing. You will be
allowed to do anything including deleting critical system files or even erasing the entire hard drive and you usually won’t be warned or prompted
to make sure you want to proceed. Most modern Linux distributions will not tell you what the ‘root’ password is to prevent you from logging in to
it directly. It’s much better practice to do everything as a normal user and temporarily elevate your privileges to root as necessary.
• In Linux, the directory structure starts at ‘/’. This is the ‘root’ of the
filesystem or the top level if you prefer.
• Within ‘/’ there are several folders. We will go through them one by
one.
• ‘/bin’ contains executable binary files, which are programs that
come with the operating system
• ‘/boot’ holds files that are important during the boot process,
including the Linux kernel itself
. ir
SEC201 | Intro to Computer Fundamentals 51
01
Directory Structure (1)
In Linux, the directory structure starts at ‘/’ which represents the ‘root’ of the filesystem or the top level of the file system. Remember, ‘root’ is the
de
starting point, the base that everything else is built on.
From the root, we have several directories. There is no way to enforce the usage for each of the directories but we will cover what they are
typically used for.
hi
/ - The root of the file system.
/bin – Typically contains binary executable files that come with the operating system.
/boot – Typically holds files that are important to the boot process, including the Linux kernel itself.
/dev – Contains the devices that are connected to the system, such as the hard disks, keyboard, mouse, etc.
/etc – Typically contains application configuration files.
/home – Typically contains the home directories of users on the system (except for the home directory of the root account).
/lib – Contains library files and kernel modules which are required by the operating system.
/media – Contains media devices that are connected to the OS, such as CD-ROMs and DVDs.
/mnt – Contains the files of any file system mounted to the operating system (such as hard disks, USB devices, etc).
/opt – Opt stands for ‘optional’. The user can decide what to put in here. It is commonly used to store binary executable files that do not come
with the operating system.
/proc – Contains files that represent each process running on the OS currently.
/root – The home directory for the ‘root’ account. This is the only one that is not in /home by default.
/sbin – Similar to /bin, it is typically used for binary executable files that come with the operating system.
/tmp – The temporary directory. Anything can write to it but files here are meant to be temporary. The files in /tmp are deleted periodically and after a
reboot.
/usr – Typically contains binary executable programs, libraries and source code for the normal user level accounts.
/var – Typically contains log files and lock files (to tell if a file is being used or not).
. ir
01
de
hi
As with many things on Linux, nothing is forcing you to use these conventions. You could put your programs into /var if you want, but it isn’t
recommended. Knowing the directory structure will help you to understand the most likely places to find things on the file system; however, not
everyone follows these conventions so you may need to do some searching around.
. ir
01
de
hi
• ‘/dev’ contains the devices that are connected to the Linux system.
(Remember, devices are files so they have to be somewhere!)
• ‘/etc’ contains application configuration files
• ‘/home’ contains the home directories of users on the system
• ‘/lib’ contains library files and kernel modules which are required
by the operating system
• ‘/media’ contains the media devices that are connected to the Linux
system (such as CD-ROMS)
. ir
SEC201 | Intro to Computer Fundamentals 53
01
Directory Structure (2)
de
hi
. ir
SEC201 | Intro to Computer Fundamentals 54
01
Directory Structure (3)
de
hi
. ir
SEC201 | Intro to Computer Fundamentals 55
01
Directory Structure (4)
de
hi
. ir
SEC201 | Intro to Computer Fundamentals 56
01
Directory Structure (5)
de
hi
Hidden Files
• Hidden files and folders have a ‘.’ at the start of their name
• You can create a hidden file by calling it something that starts with
a ‘.’
• Hidden files can be viewed with the right command, described later
. ir
SEC201 | Intro to Computer Fundamentals 57
01
Hidden Files
On Linux, hidden files are denoted with a period (‘.’) at the start of the file name. This makes the file invisible to normal viewing, although it can
de
still be viewed using the right commands or by changing your file explorer software settings to be able to view hidden files on a GUI.
hi
Symlinks
ir
file here instead
.
SEC201 | Intro to Computer Fundamentals 58
01
Symlinks
A symlink is a ‘symbolic link’. It is a way of creating a file that references another file, similar to a shortcut in Windows. Referencing a symlink is
de
the same as referencing the file it is linked to. For example, if you write some data to a symlink of a file, you will be writing to the original file
itself. You can, however, delete a file which is a symlink without deleting the original file.
hi
• Most distributions come with a lot of users already set up. Only one
of these is meant for use by a user
• The other users can’t be used for logging in and are instead used by
software that runs on the operating system
• Each user belongs to a group
• Groups are just for organising ‘permissions’: essentially, which user
is allowed to do what
. ir
SEC201 | Intro to Computer Fundamentals 59
01
Linux Users & Groups
Linux distributions use a permissions scheme to specify who has access to which files. Users can be put into groups as a way of controlling who
de
can access which files. Most distributions come with several groups already set up, such as the ‘wheel’ group. Root is able to create new groups
and also add users to groups.
hi
Linux Permissions
• Each file in Linux has three sets of permissions. The first set is
‘Owner’ which is the set that applies to the owner of the file.
• This is typically the person who created the file but it can be
changed (more on that later)
• The next set is ‘Group’, which is the set that applies to a user who
isn’t the Owner but is in the Group that owns the file. This can also
be changed.
• The last set is ‘Everyone’, which is the set of permissions that
applies to anyone that isn’t the Owner and isn’t in the matching
Group
. ir
SEC201 | Intro to Computer Fundamentals 60
01
Linux Permissions
Each file on Linux has three sets of permissions. The first set is ‘Owner’. These are the permissions that apply to the owner of the file (typically
de
the creator of the file, unless the owner was specifically changed). The next set of permissions is ‘Group’. This is the set of permissions that apply
to whoever is in the group that owns the file. This is typically the group that the user who created the file was in but, again, it can be changed. The
last set of permissions is for ‘Everyone’. This set of permissions applies to everyone who isn’t the owner or the group.
hi
. ir
SEC201 | Intro to Computer Fundamentals 61
01
Linux Permissions – The Long Way
rwxr-xr--
de
The first three letters are for the Owner. The next three are for the group and the last three are for everyone. ‘r’ stands for read, ‘w’ stands for
hi
write and ‘x’ stands for execute. Finally, a ‘-’ means no permission was set in that space. Taking the first example, the Owner of the file can read,
write and execute the file. Anyone in the group that owns the file can read and execute the file, but not write to it. Finally, everyone else can read
the file, but not write to it or execute it.
Writing permissions like this is the long form. You will see it frequently, specifically when you list the files in a directory.
• You may also hear people talk about setting permissions to ‘755’,
for example. This is a shorthand way of expressing permissions.
• Many people struggle with how to calculate this, but it’s actually
quite simple. You’ll need to know your binary though.
• Each set of permissions is represented by one digit, so the Owner is
7, the Group is 5 and Everyone is 5
• 7 in binary is 111. Notice that there are three digits in binary and
there are three permissions, rwx. Each binary digit represents one
of the permissions. A 1 means that permission is enabled, a 0
means it is disabled.
. ir
SEC201 | Intro to Computer Fundamentals 62
01
Linux Permissions – The Short Way (1)
Linux permissions can also be expressed as a numerical value such as 755. This is considered shorthand, but you will need to know it specifically
de
when it comes to setting permissions on files. Many people struggle with calculating permissions like this, but it’s actually simple when you have
a background in counting in binary, as you should already do.
Simply put, each set of permissions is represented by a single digit. So in the case of ‘755’ the ‘7’ represents the Owner’s permissions, the first ‘5’
hi
represents the Group’s permissions and finally the last ‘5’ represents everyone else’s permissions. Let’s look more closely at the first ‘7’, the
Owner’s permission.
7 in Binary is written: 111. Notice that the binary representation has three digits. Each of these digits represents whether a permission is on or off.
rwx
111
. ir
SEC201 | Intro to Computer Fundamentals 63
01
Linux Permissions – The Short Way (2)
So we can see a 1 under ‘r’ means that read permissions are enabled, a 1 under ‘w’ means that write permissions are enabled and a 1 under ‘x’
rwx
101
de
means that execute permissions are enabled. So let’s look at 5 now. 5 in binary is 101.
hi
So in the case of 5, ‘read’ is enabled and ‘execute’ is enabled but ‘write’ is not enabled.
Ultimately, the permissions 755 means that the Owner can read, write and execute, but the Group and Everyone can only read and execute.
. ir
SEC201 | Intro to Computer Fundamentals 64
01
Linux Permissions – The Short Way (3)
de
hi
. ir
SEC201 | Intro to Computer Fundamentals 65
01
Linux Permissions – The Shorter Way
There’s an even quicker way to calculate permissions in shorthand and that is just to remember that ‘read’ is worth 4, ‘write’ is worth 2 and
de
‘execute’ is worth 1. By adding up the values you want to set, you get an accurate result.
For example, ‘read’ and ‘execute’ would be 4 + 1 = 5. In binary ‘read’ and ‘execute’ is 101, which is 5 in denary, so we can see that this method
works. If you can’t remember the values, you can always calculate them from first principles by looking at the binary again.
hi
Linux Processes
. ir
SEC201 | Intro to Computer Fundamentals 66
01
Linux Processes
We already know that a process is just a running program. On Linux, each process has an ID associated with it and each process is represented by
a file that lives in ‘/proc’.
Often you will need to find the process ID of a process before you can terminate the process from the command line.
Linux Terminal
• The Linux Terminal is the most important interface the user can
have with the operating system
• Many times you will not even have a GUI and will have to work in
the terminal alone
• The terminal consists of a ‘shell’ program which is responsible for
interpreting your commands. Each shell has different features.
• A common default shell is the ‘bash’ shell. It supports features such
as tab-completion.
. ir
SEC201 | Intro to Computer Fundamentals 67
01
Linux Terminal
The Linux Terminal is by far the most useful and important interface the user can have within the Linux operating system. You can access it from
de
the ‘terminal’ application on desktop distributions but on server distributions, the terminal is all you get. It’s a purely text-based interface. Each
terminal has a ‘shell’ program which is responsible for interpreting your commands. There are many different types of shell and many people have
their own preferences but usually the default one is the bash shell. The bash shell has many useful features such as tab completion which allows
you to type something then press tab part of the way through to have the terminal automatically fill the rest in for you.
hi
ir
in the order that they appear
.
SEC201 | Intro to Computer Fundamentals 68
01
Linux Terminal – Environment Variables
Environment variables are a way of storing data and giving that data a name so that it can be accessed later. Perhaps the most important
de
environment variable is the ‘PATH’ environment variable. Environment variables can be accessed by appending the $ symbol to the front, for
example, $PATH means ‘get the value in the environment variable PATH’. It’s important to note that environment variables are typically
temporary. Normally, the PATH environment variable is set to be loaded the first time the terminal starts. That means if you want to alter the
PATH and have it stay that way permanently then you need to alter the place that loads the PATH variable up the first time the terminal starts.
hi
This is typically in the configuration file of your shell program (such as .bashrc for the bash shell).
The PATH environment variable is a particularly important one: it consists of a semi-colon separated list of folder paths where the shell looks for
programs when you type a command. This means instead of typing /sbin/ls, for example, to access the ls command, you can simply type ‘ls’ on its
own and, as long as /sbin is in your PATH, the shell will look there for the command you typed. The PATH directory list is in order of priority,
therefore if you have two programs called ‘foo’, one in /sbin and one in /bin and they do different things, when you type ‘foo’, the executable file
in the first directory in the PATH will be the one that gets used.
If you type a command and it isn’t found but you know that it is installed somewhere, it’s likely that the directory it is installed in is not in the
PATH and therefore you need to add it.
• In the Linux Terminal, most shells support tab completion. You can
complete a command or a folder name by pressing tab when you
are part of the way through typing it.
• This is a big time saver, however you should be aware that tab
completion will not work if there is more than one possible match
• For example, if you have typed fold and there are two folders,
folder1 and folder2, it wont know which one you want
• You can press tab twice quickly to get a list of possible matches
. ir
SEC201 | Intro to Computer Fundamentals 69
01
Linux Terminal – Tab Completion
Most shell programs you can use with the Terminal offer tab completion. This allows you to complete a command or folder name by pressing tab
de
when you are part way through typing it. This can save you a lot of typing, particularly if you are typing out a long folder path. You should know
that tab completion will not work if there is more than one possible match.
Say you have two folders in your current directory, ingeniously named ‘folder1’ and ‘folder2’. If you were typing ‘fold’ and then pressed tab,
hi
nothing would happen. That is because the terminal doesn’t know to match folder1 or folder2, both are possible matches. You can press tab twice
in quick succession to output a list of possible matches to your command, which is also quite useful.
• Another big time saver is using the up and down arrows on the
keyboard to cycle through previous commands
• Be warned though that sometimes it’s faster to re-type a command
than cycling back through twenty or so commands to find one you
typed earlier
• The up arrow goes back through the command history and the
down arrow goes forward in the command history
• You can also perform a search on all your previous commands with
CTRL + r. This will allow you to type a partial command and get a
list of all previously typed commands that match. You can navigate
ir
the list with the up and down arrow keys.
.
SEC201 | Intro to Computer Fundamentals 70
01
Linux Terminal – Previous Commands
Another useful aspect to the terminal is the ability to cycle through previously typed commands and re-run them. You can use the up and down
de
arrow keys to cycle through previous commands. This is useful, but don’t fall into the trap of spending longer cycling through previous commands
when it might have been faster to type the command out from scratch. Luckily there is a method that is the best of both worlds: you can perform a
reverse command search using ctrl + r. This will allow you to type a partial command and then use the up and down arrows to cycle through
previous commands that match your partially typed command.
hi
• In the Linux Terminal there are some symbols which have special
meanings
• The ‘/’ symbol represents the root of the filesystem. It’s also the
character that separates folders in a file path. For example,
/home/username/Desktop. Whereas on Windows it’s a backslash
‘\’ that does it.
• The ‘~’ (tilde) symbol represents the home folder of the current
user
• The characters ‘..’ represent the folder one level higher in the
directory structure
ir
• The character ‘.’ represents the current directory
.
SEC201 | Intro to Computer Fundamentals 71
01
Linux Terminal – Important Symbols
de
/ – The forward slash represents the root of the filesystem. It also separates folders directories in a file path. For example:
/home/username/Desktop. The first slash is obviously the root of the file system. So from the root, we go to the home directory then the username
directory and then the Desktop directory.
hi
~ – The tilde symbol represents the home folder of the current user, so, for example, ~/Desktop will take you to /home/<<name of user>>/Desktop
.. – The ‘..’ characters represent the directory one level higher in the directory structure. For example, if you are in /home/username/Desktop and
you go to the ‘..’ folder, you will end up in /home/username.
. – The ‘.’ character represents the current directory
• The ‘./’ symbol means ‘execute the file in the current folder’
• The ‘*’ symbol (asterisk) is known as the wildcard symbol. This
means that it matches anything. For example, if you wanted to
match all the files in a folder that end in .jpg you could do: *.jpg.
. ir
SEC201 | Intro to Computer Fundamentals 72
01
Linux Terminal – Important Symbols
./ - The ‘./’ character means ‘execute the file in the current directory’. So, for example, ‘./find’ will execute the file called find that is in the current
directory.
de
directory. This is important because if you just type ‘find’ on its own, the shell will look in the PATH for a file called ‘find’ and not in the current
* - The asterisk symbol is known as the wildcard symbol. It matches anything. For example, if you wanted to list all files in a folder that end in
.jpg you could type: ls *.jpg. The wildcard would match any filename that ends in .jpg.
hi
ir
documentation or error and suggest you read the help
.
SEC201 | Intro to Computer Fundamentals 73
01
Linux Terminal – Parameters
Many terminal commands in Linux allow for additional parameters to be passed in that change or modify the way the command functions.
de
Parameters are usually, but not always preceded by a single dash (-) or double dash (--). Usually, a single dash (-) is used when the parameter is a
letter such as –h for the help menu. A double dash (--) is more commonly used when the parameter is a word, such as in --help.
Multiple single letter parameters can be written on the same dash, such as in ‘ls –alth’.
hi
If a program requires a parameter to function and you do not provide it, either an error message will be printed to the screen or the program will
perform some other default behavior such as printing the help menu for that command.
• If the binary executable you want to run is in the PATH, you just
have to type the name of the program to run it from wherever you
are
• If the program is not in the PATH and is saved somewhere else and
you want to run it, you need to use ‘./program’
• You can also do it from a remote directory using a longer path such
as: ‘./home/username/Desktop/program’
. ir
SEC201 | Intro to Computer Fundamentals 74
01
Linux Terminal – Running Programs
Running a program that is located somewhere in the PATH is as simple as typing the name of the file. On the other hand, if you want to run a
‘./home/username/Desktop/file’.
de
program that is in the current directory you need to use ‘./’ such as ‘./program’. You can also use a file path instead such as
If there is a conflict between the name of a program in the PATH and one elsewhere, be sure you use the right method when running the file or
hi
you may see unexpected behavior such as running the wrong program.
• Once a program is running, you can exit it and get back to the
prompt by pressing ‘ctrl+c’. This sends a terminate signal to end
the process
• It is not recommended you use this, unless there is no other way to
exit the program
• If the program allows you to exit by pressing ‘q’, for example, you
should do it the way the program wants
• Terminating a program with ‘ctrl+c’ is a last resort
. ir
SEC201 | Intro to Computer Fundamentals 75
01
Linux Terminal – Exiting a Program
To exit a running program from the terminal, you can use ctrl-c. This sends an interrupt signal to end the running process. This is a bit of a nuclear
de
option, so you should use this method only as a last resort. In some programs there may be a way to shut down the program gracefully, so be sure
to use that if it is available. For example, some programs let you exit if you press ‘q’.
hi
ir
by typing ‘fg’ on its own
.
SEC201 | Intro to Computer Fundamentals 76
01
Linux Terminal – Backgrounding a Task
A running task can be sent to the background so you can work on other things without having to create a new terminal window. This is
de
particularly useful if you are connecting to a remote server over SSH (Secure Shell) and you don’t want to have to create a new SSH session.
To do that, first, we have to pause the running task with ctrl + z. With the task paused, we can type ‘bg’ to resume the task in the background.
With the task running in the background, we can bring it back to the foreground by typing ‘jobs’ to list all the running background tasks. To bring
hi
a particular background task back to the foreground, you can type ‘fg’ followed by the id of the job.
If you pressed ctrl + z by mistake and didn’t intend to pause the task, you can resume it by typing ‘fg’ on its own.
. ir
SEC201 | Intro to Computer Fundamentals 77
01
Linux Terminal – Mini-Lab Setup (1)
de
hi
. ir
SEC201 | Intro to Computer Fundamentals 78
01
Linux Terminal – Mini-Lab Setup (2)
de
hi
. ir
SEC201 | Intro to Computer Fundamentals 79
01
Linux Terminal – Mini-Lab Setup (3)
de
hi
. ir
SEC201 | Intro to Computer Fundamentals 80
01
Linux Terminal – Mini-Lab Setup (4)
de
hi
• When the process is finished, you should see the new virtual
machine in the VMware Player Library
. ir
SEC201 | Intro to Computer Fundamentals 81
01
Linux Terminal – Mini-Lab Setup (5)
de
hi
. ir
SEC201 | Intro to Computer Fundamentals 82
01
Linux Terminal – Mini-Lab Setup (6)
de
hi
. ir
SEC201 | Intro to Computer Fundamentals 83
01
Linux Terminal – Mini-Lab Setup (7)
de
hi
• When the account loads, you will see an ‘activities’ icon in the top
left
• Select it, then select the terminal icon from the dock on the left
. ir
SEC201 | Intro to Computer Fundamentals 84
01
Linux Terminal – Mini-Lab Setup (8)
de
hi
• With the terminal open, you are ready to walk through the mini-
labs in the next section
. ir
SEC201 | Intro to Computer Fundamentals 85
01
Linux Terminal – Mini-Lab Setup (9)
de
hi
. ir
SEC201 | Intro to Computer Fundamentals 86
01
Linux Terminal – Commands - echo (1)
The echo command just prints text to the screen. It’s used quite often in shell scripts to display output, but it’s also useful for printing the values of
de
environment variables. You could, for example, do echo $PATH to print the value of the PATH environment variable. That can be helpful when
you’re diagnosing why a program you installed can’t be found. Usually this will be because it was installed into a folder that isn’t in your PATH.
hi
• Open up the terminal in your Linux virtual machine and play with
the echo command a bit
• Try the following commands:
• $ echo hello world!
• $ echo $PATH
• $ echo $PS1
• $ echo $SHELL
. ir
SEC201 | Intro to Computer Fundamentals 87
01
Linux Terminal – Commands - echo (2)
de
hi
. ir
SEC201 | Intro to Computer Fundamentals 88
01
Linux Terminal – Commands - echo (3)
de
hi
. ir
SEC201 | Intro to Computer Fundamentals 89
01
Linux Terminal – Commands - ls (1)
The ‘ls’ command stands for list. It is used to list the contents of a directory. If you use ‘ls’ on its own, it will list the files and folders in the
de
current directory. You can also pass in a folder or file name as a parameter. For example, ‘ls /home’ will display the files and folders in the
‘/home’ directory. There are other parameters that the command takes, such as ‘–l’, which outputs a more detailed view which includes file
permissions and ownership information.
hi
• The ‘ls’ command can also be used to view hidden files with the ‘-a’
parameter
• It’s ‘a’ for ‘all’
• This will show you all files and folders, even those prefixed with a ‘.’
. ir
SEC201 | Intro to Computer Fundamentals 90
01
Linux Terminal – Commands - ls (2)
de
hi
• Open up the terminal in your Linux virtual machine and play with
the ls command a bit
• Try looking at the help:
• $ ls --help
• Then try the following commands:
• $ ls -l
• $ ls –h
• $ ls –a
• $ ls –halt
• $ ls /bin
ir
• $ ls ..
.
SEC201 | Intro to Computer Fundamentals 91
01
Linux Terminal – Commands - ls (3)
de
hi
ir
information!
.
SEC201 | Intro to Computer Fundamentals 92
01
Linux Terminal – Commands - pwd (1)
The ‘pwd’ command stands for ‘print working directory’. It tells you which directory you are currently in. Most terminals already show you this
de
information in the prompt but sometimes you may come across instances where the prompt has been customized to remove such information.
There are other circumstances in which you may not have a prompt at all, so you need to know how to find this information under those
circumstances.
hi
If you see this command being run on your servers, and you haven’t removed such information from your prompt, then seeing this command can
indicate that someone has compromised your system.
• Open up the terminal in your Linux virtual machine and play with
the pwd command a bit
• Try the following command:
$ pwd
. ir
SEC201 | Intro to Computer Fundamentals 93
01
Linux Terminal – Commands - pwd (2)
de
hi
ir
• Using the ‘..’ shortcut you can also do: cd ‘../username/Desktop’
.
SEC201 | Intro to Computer Fundamentals 94
01
Linux Terminal – Commands - cd (1)
The ‘cd’ command stands for ‘change directory’. This command allows you to navigate the file system. All you need to do is to type cd followed
de
by the path to your folder. You can do ‘cd Desktop’ or you can use an absolute path. Absolute paths start with a ‘/’ which indicates it starts from
the root of the file system. It’s a common beginner mistake to put a ‘/’ at the start of relative paths. Of course, this does not work, as a leading ‘/’
in, for example, ‘/Desktop’ means start at the root of the file system then go to the folder called Desktop which does not exist there.
hi
• Open up the terminal in your Linux virtual machine and play with
the cd command a bit
• First go to ‘~/Documents/day2/terminal/cd’ by typing:
$ cd ~/Documents/day2/terminal/cd
• List the contents, you should know how by now
• You will see two folders, cd into folder1 by typing:
$ cd folder1
. ir
SEC201 | Intro to Computer Fundamentals 95
01
Linux Terminal – Commands - cd (2)
de
hi
. ir
SEC201 | Intro to Computer Fundamentals 96
01
Linux Terminal – Commands - cd (3)
de
hi
. ir
SEC201 | Intro to Computer Fundamentals 97
01
Linux Terminal – Commands - hostname (1)
The hostname command prints out the hostname of the computer. The hostname is just a way of identifying the computer. Usually, this
de
information is shown in the terminal prompt, unless you customized the prompt. This command can be useful if you have multiple SSH sessions
open with many different computers as it can help you identify which computer each session is connected to.
hi
• Open up the terminal in your Linux virtual machine and play with
the hostname command a bit
• Look at the help by typing:
$ hostname –h
• Try the following command:
$ hostname
. ir
SEC201 | Intro to Computer Fundamentals 98
01
Linux Terminal – Commands - hostname (2)
de
hi
• The ‘cp’ command stands for ‘copy’. It can copy a file or folder from
one place to another
• By default the ‘cp’ command can only copy files. To copy a folder
you need to add a parameter to make it recursive
• To do that you need ‘cp –R’
• ‘Recursive’ just means that if you specify a folder it will also apply
to each item within that folder
• There are many commands which take a ‘recursive’ parameter
. ir
SEC201 | Intro to Computer Fundamentals 99
01
Linux Terminal – Commands - cp (1)
The ‘cp’ command stands for copy. It does what it sounds like; it copies files from one location to another. In its default setting, the cp command
de
can only copy files and not folders. To make it copy folders you need to add a parameter to make it ‘recursive’. To do that you need to use cp –R.
hi
• Open up the terminal in your Linux virtual machine and play with
the cp command a bit
• Let’s look at the help for cp first:
$ cp --help
• Now cd to ~/Documents/day2/terminal/cp. Here you will see a
folder called folder1. Within is a text file called file1.txt. Copy it to
file2.txt:
$ cp folder1/file1.txt folder1/file2.txt
• Now make a copy of folder1 called folder2:
$ cp –R folder1 folder2
. ir
SEC201 | Intro to Computer Fundamentals 100
01
Linux Terminal – Commands - cp (2)
de
hi
• The ‘mv’ command stands for ‘move’. It can move a file or folder
from one place to another
• It can also be used as a ‘rename’ function, since you can move a file
to the same folder but with a new name
• You first specify the file you want to move, then the place you want
to move it to
• For example: ‘mv file1 /opt/file1renamed’. In this case file1 in the
current working directory will be moved into /opt and renamed to
file1renamed.
. ir
SEC201 | Intro to Computer Fundamentals 101
01
Linux Terminal – Commands - mv (1)
The ‘mv’ command stands for ‘move’. It can move a file or folder from one place to another. Note, this is not the same as a copy. With a move,
de
the file will no longer be in the original location after it is moved, whereas with copy the original will also remain in place. Move is also frequently
used to rename a file. Think about it, if you move a file from one location to the same location but you specify a new name, it’s the same as
renaming the file.
hi
• Open up the terminal in your Linux virtual machine and play with
the mv command a bit
• Let’s look at the help for mv first:
$ mv --help
• Now cd into ‘~/Documents/day2/terminal/mv’. Here you will see
folder1. Within is a text file called file1.txt. Rename it to file2.txt:
$ mv folder1/file1.txt folder1/file2.txt
• Now let’s move folder1 to folder2:
$ mv folder1 folder2
• You will see that folder1 is now called folder2
. ir
SEC201 | Intro to Computer Fundamentals 102
01
Linux Terminal – Commands - mv (2)
de
hi
. ir
SEC201 | Intro to Computer Fundamentals 103
01
Linux Terminal – Commands - less (1)
The ‘less’ command is a program that can be used to read files. The good thing about less is that it works really well for reading very large files.
de
That’s because it doesn’t load the whole file into memory all at once, it reads the file by ‘pages’. Less is also an example of a program that has a
text-based interface where you can use the arrow keys to navigate the text, but you have to press ‘q’ to exit the program. Less is based off of an
older program called ‘more’ which did much the same thing.
hi
• Open up the terminal in your Linux virtual machine and play with
the less command a bit
• First cd into ~/Documents/day2/terminal/less. You will see a file
called ‘lessismore.txt’. Open it up in less with:
$ less lessismore.txt
• Navigate through the document with the arrow keys and when you
are done quit by hitting ‘q’
. ir
SEC201 | Intro to Computer Fundamentals 104
01
Linux Terminal – Commands - less (2)
de
hi
. ir
SEC201 | Intro to Computer Fundamentals 105
01
Linux Terminal – Commands - cat (1)
The ‘cat’ command stands for concatenate. The command is used for getting the contents of several files and concatenating them (adding them
de
together) and printing the results to the terminal. However, the more common usage for the ‘cat’ command is to use it on a single file, which will
just print the contents of the file to the terminal.
hi
• Open up the terminal in your Linux virtual machine and play with
the cat command a bit
• First cd into ~/Documents/day2/terminal/cat. You will see three
text files: file1.txt file2.txt and file3.txt.
• Try these commands:
• $ cat file1.txt
• $ cat file1.txt file2.txt
• $ cat file1.txt file2.txt file3.txt
• $ cat file*.txt
. ir
SEC201 | Intro to Computer Fundamentals 106
01
Linux Terminal – Commands - cat (2)
de
hi
. ir
SEC201 | Intro to Computer Fundamentals 107
01
Linux Terminal – Commands - rm (1)
The ‘rm’ command stands for remove. It’s used for deleting files and folders. Similar to the ‘cp’ command, it does not work on folders by default.
de
If you need to delete a folder you need to pass in the recursive parameter (-R). Be very careful when using this command, as there is no undo
button. The files will be instantly deleted, even if you asked it to delete recursively from the root of the file system, it will go ahead and delete
everything and break your installation.
hi
• Open up the terminal in your Linux virtual machine and play with
the rm command a bit
• First cd to ~/Documents/day2/terminal/rm. You will see a folder
called folder1. Within is a text file file.txt. Delete the text file with
rm:
$ rm folder1/file.txt
• Next try deleting folder1:
$ rm folder1
• Notice it didn’t work
. ir
SEC201 | Intro to Computer Fundamentals 108
01
Linux Terminal – Commands - rm (2)
de
hi
. ir
SEC201 | Intro to Computer Fundamentals 109
01
Linux Terminal – Commands - rm (3)
de
hi
• The ‘find’ command can search the filesystem for particular files
• It can be quite tricky to use
• First you specify the location to search in and then you specify how
you’re matching the file
• For example: ‘find / -name sans’ would search in the root of the
filesystem and all files and folders within it for files with the
filename ‘sans’
• There are other ways you can match, for example, size for file size
• On older systems, you may need to tell find to print the results to
the screen with the –print parameter.
. ir
SEC201 | Intro to Computer Fundamentals 110
01
find
The ‘find’ command is used to search the filesystem for matching files. The syntax can be quite tricky and it often sends people running in fear,
de
but it’s not too bad once you get the hang of it. First you have to specify the location you want to search in; then, you need to specify the match
condition. So, for example, if you wanted to search the whole filesystem for files called ‘sans’ you would do: find / -name sans. You can use other
match conditions, such as –size to match based on file size. See the manual page for find for the full list.
hi
• Open up the terminal in your Linux virtual machine and play with
the find command a bit
• First cd to ~/Documents/day2/terminal/find. Within you will find
a text file called ‘file_name.txt’.
• First search within ~/Documents/day2/terminal/find for files with
the name ‘name’ as part of the name with:
$ find ~/Documents/day2/terminal/find –name “*name*”
• The search should find file_name.txt
. ir
SEC201 | Intro to Computer Fundamentals 111
01
Linux Terminal – Commands - find (1)
de
hi
. ir
SEC201 | Intro to Computer Fundamentals 112
01
Linux Terminal – Commands - find (2)
de
hi
• The ‘grep’ command can search for keywords within a text file and
returns the surrounding text
• You first specify the keyword you are searching for, then the
filename of the file to search in
• For example: grep “password” logfile.txt
• You can also specify multiple files to search in
. ir
SEC201 | Intro to Computer Fundamentals 113
01
Linux Terminal – Commands - grep (1)
The grep command can search for keywords within a text file and returns the surrounding text. First you have to specify the keyword you are
de
searching for; then, you need to specify the file or files you want to search in. You can pass in multiple files as parameters.
hi
• Open up the terminal in your Linux virtual machine and play with
the grep command a bit
• First cd into ~/Documents/day2/terminal/grep. You fill find
several text files in here. Search for the word ‘SANS’ within them:
$ grep ‘SANS’ textfile1.txt
• You could do it one by one, or you could do them all at once:
$ grep ‘SANS’ textfile1.txt textfile2.txt
• Even better:
$ grep ‘SANS’ *.txt
. ir
SEC201 | Intro to Computer Fundamentals 114
01
Linux Terminal – Commands - grep (2)
de
hi
. ir
SEC201 | Intro to Computer Fundamentals 115
01
Linux Terminal – Commands - which (1)
The ‘which’ command is incredibly useful for locating the executable files that various commands call. You type ‘ls’, for example, but where is
de
the ‘ls’ program located in the file system? Which can tell you. You just need to type ‘which ls’.
hi
• Open up the terminal in your Linux virtual machine and play with
the which command a bit
• Find the location of the ‘passwd’ command
$ which passwd
• How about the location of the ‘which’ command?
$ which which
. ir
SEC201 | Intro to Computer Fundamentals 116
01
Linux Terminal – Commands - which (2)
de
hi
. ir
SEC201 | Intro to Computer Fundamentals 117
01
Linux Terminal – Commands - man (1)
The ‘man’ command stands for manual. It can show you a manual page or man page for most programs installed on the system. This is usually
de
more in depth than the help pages which can be accessed by passing in –h or --help. When you use man, the manual pages are actually opened in
‘less’ so you can navigate using the arrow keys and you have to remember to quit by pressing ‘q’.
hi
• Open up the terminal in your Linux virtual machine and play with
the man command a bit
• Let’s look at some of the man pages for the commands we’ve
learned so far:
• $ man less
• $ man grep
• $ man ls
• $ man cd
• $ man find
• $ man man
. ir
SEC201 | Intro to Computer Fundamentals 118
01
Linux Terminal – Commands - man (2)
de
hi
. ir
SEC201 | Intro to Computer Fundamentals 119
01
Linux Terminal – Commands - apropos (1)
The ‘apropos’ command is used to search man pages for particular information. This is useful if you know what you want to accomplish but you
de
don’t know which tool you could use to accomplish it. In English, apropos means ‘very appropriate to a particular situation.’ This may help you to
remember the command.
hi
• Open up the terminal in your Linux virtual machine and play with
the apropos command a bit
• First let’s look for a single keyword ‘print’:
$ apropos print
• Then let’s look for two keywords by using the –a parameter (and):
$ apropos print –a lines
• And more:
$ apropos print –a lines –a pattern
. ir
SEC201 | Intro to Computer Fundamentals 120
01
Linux Terminal – Commands - apropos (2)
de
hi
• The strings command looks in files for any ASCII characters that
look like they might form a string and prints them to the terminal
• The characters don’t necessarily make sense, as it tries to interpret
everything as ASCII whether it is or not
• It’s useful for reverse engineering and sometimes you can even pull
passwords out of files
• Keep in mind that, by default, any strings three characters or less
will be ignored. You can change this with the –n parameter.
. ir
SEC201 | Intro to Computer Fundamentals 121
01
Linux Terminal – Commands - strings (1)
The strings command looks in files for any ASCII characters that look like they might form a string (a word or sentence) and prints them to the
de
terminal. The characters it finds don’t necessarily make sense, as it tries to interpret everything as ASCII whether it is or not. Typically the strings
command looks for any 4 consecutive characters or more but you can change this default behavior with the –n parameter where you can specify
the number of characters to match. For example, strings –n 3 to match three consecutive characters or more.
hi
• Open up the terminal in your Linux virtual machine and play with the strings
command a bit
• First cd to ~/Documents/day2/terminal/strings. Here you will find two binary
executables that have been compiled from C.
• If you run binary1 with:
$ ./binary1
• It will ask for a password. Find the password using strings. First ctrl + c to exit
binary1. Then run strings on binary1:
$ strings binary1
• You should be able to search the output for the password
• Re-run binary1 and enter the password to check you got it right!
. ir
SEC201 | Intro to Computer Fundamentals 122
01
Linux Terminal – Commands - strings (2)
de
hi
• Now try running binary2. It’s the same deal, so run strings against
binary2
• The problem is, you can’t see the password. That’s because this
program was written so the password is broken up in chunks of
three characters and only assembled in memory.
• To get around that you need to pass the –n parameter to strings.
$ strings –n 3 binary2
• You should be able to assemble the password now, so run binary2
to check you got it right!
. ir
SEC201 | Intro to Computer Fundamentals 123
01
Linux Terminal – Commands - strings (3)
de
hi
• The ‘nano’ command is a command line text editor that can be used
to create, modify and read files
• If the filename you specify exists, it will be opened. If it doesn’t
exist, a new file with that name will be created.
. ir
SEC201 | Intro to Computer Fundamentals 124
01
Linux Terminal – Commands - nano (1)
The ‘nano’ command is a command line text editor which can be used to create, modify and read files. It’s very easy to use compared to say Vi or
de
Vim (which we’ll be covering shortly), however it has fewer features than more advanced editors. Simply type ‘nano’ and then pass in a file name
as a parameter. If the file exists, it will be opened in the text editor, if the file doesn’t exist then it will be created. Nano has a text-based interface
with clear shortcuts for things like scrolling through pages, find, and saving.
hi
• Open up the terminal in your Linux virtual machine and play with
the nano command a bit
• First cd to ~/Desktop
• Create a file called nano_demo and put some random text in there:
$ nano nano_demo
• Then type something and press Ctrl + x to exit. Make sure to save
the file when it asks.
. ir
SEC201 | Intro to Computer Fundamentals 125
01
Linux Terminal – Commands - nano (2)
de
hi
• You can check the contents of the file to make sure it saved
properly with:
$ cat nano_demo
• Then you can go ahead and edit to add more text again with:
$ nano nano_demo
. ir
SEC201 | Intro to Computer Fundamentals 126
01
Linux Terminal – Commands - nano (3)
de
hi
. ir
SEC201 | Intro to Computer Fundamentals 127
01
Linux Terminal – Commands - vim (1)
The vim command is another text editor that is similar to nano, however it has many more features. It’s equally more difficult to use than nano, but
de
it is worthwhile learning both Vim and its predecessor Vi. That’s because you will occasionally find yourself in a situation when all you have is Vi
or Vim and you aren’t allowed to install nano. Vim and Vi are both frequently installed by default for many distributions, while nano is not as
globally available.
hi
• Open up the terminal in your Linux virtual machine and play with
the vim command a bit
• First cd to ~/Desktop
• Create a file called vim_demo:
$ vi vim_demo
• Enter insert mode to add or edit text: press ‘i’. Now you can add
text.
. ir
SEC201 | Intro to Computer Fundamentals 128
01
Linux Terminal – Commands - vim (2)
de
hi
. ir
SEC201 | Intro to Computer Fundamentals 129
01
Linux Terminal – Commands - vim (3)
de
hi
. ir
SEC201 | Intro to Computer Fundamentals 130
01
Linux Terminal – Commands - vim (4)
de
hi
. ir
SEC201 | Intro to Computer Fundamentals 131
01
Linux Terminal – Commands - top (1)
The ‘top’ command acts as a resource monitor, similar to the task manager in windows. It’s useful for looking at system resources, how much load
de
the computer is under and which processes are using up the most resources. Where load is shown, the first value is the current load, the other two
are previous load values. This is done so you can see a trend in resource usage, i.e. is it falling or rising.
hi
• Open the terminal in your Linux virtual machine and play with the
top command a bit
• Just type:
$ top
• and look around
• When you’re done, exit with Ctrl + C
. ir
SEC201 | Intro to Computer Fundamentals 132
01
Linux Terminal – Commands - top (2)
de
hi
. ir
SEC201 | Intro to Computer Fundamentals 133
01
Linux Terminal – Commands - ps (1)
The ‘ps’ command lists running processes. By default it only shows the processes that are running under your username, but you can add
de
parameters to show all running processes and also the command that was used to execute the processes. This is a good example why it’s never a
good idea to pass passwords to programs on the command line, because any other user on the system will be able to see the parameters you
entered when starting that process. There are two types of syntax for the ‘ps’ command, generally they both work although you should not use
them interchangeably.
hi
• Open the terminal in your Linux virtual machine and play with the
ps command a bit
• First type
$ ps
• on its own and note the processes
• Then type
$ ps aux
• This is BSD syntax, it will show you all processes along with
detailed information on them
. ir
SEC201 | Intro to Computer Fundamentals 134
01
Linux Terminal – Commands - ps (2)
de
hi
. ir
SEC201 | Intro to Computer Fundamentals 135
01
Linux Terminal – Commands - ps (3)
de
hi
. ir
SEC201 | Intro to Computer Fundamentals 136
01
Linux Terminal – Commands - history (1)
The ‘history’ command shows you a list of previously typed commands on the system. In reality, the commands are stored in a history file and the
de
history command only prints out the contents of the file. This is a good method for retracing your steps, it can also show you what an attacker has
been doing on the system if he forgot to clean up after himself. It’s possible to clear your history file with ‘history –c’ or by simply deleting the
history file wherever it is stored.
hi
Each user has their own history file, so you won’t be able to see what other people have been doing unless you have permissions to access their
history file.
• Open the terminal in your Linux virtual machine and play with the history
command a bit
• First type:
$ history
• to view the history file
• Once you’ve seen it, type
$ history –c
• to clear it
• Now view the history file with
$ history
• again and look at the difference
. ir
SEC201 | Intro to Computer Fundamentals 137
01
Linux Terminal – Commands - history (2)
de
hi
. ir
SEC201 | Intro to Computer Fundamentals 138
01
Linux Terminal – Commands - diff (1)
The ‘diff’ command shows you the differences between files. This is good for looking at differences in sections of code, such as when a patch has
de
been released. The syntax is simple, you just need to pass in the two files to compare as parameters.
hi
• Open the terminal in your Linux virtual machine and play with the
diff command a bit
• First go to ~/Documents/day2/terminal/diff. You will see two text
files here. ‘text1.txt’ and ‘text2.txt’
• Find the differences between them with the ‘diff’ command
$ diff text1.txt text2.txt
. ir
SEC201 | Intro to Computer Fundamentals 139
01
Linux Terminal – Commands - diff (2)
de
hi
. ir
SEC201 | Intro to Computer Fundamentals 140
01
Linux Terminal – Commands - wget (1)
The ‘wget’ command can be used to download files from the internet. The syntax is simple, ‘wget’ followed by the link to the file to download.
de
The file will be saved in the current working directory.
hi
• Open your terminal in your Linux virtual machine and play with
the wget command a bit
• Find an image in google and copy the image link
• In your terminal, download the file with wget:
$ wget https://imgs.xkcd.com/comics/security.png
• Open the image to make sure it downloaded correctly
. ir
SEC201 | Intro to Computer Fundamentals 141
01
Linux Terminal – Commands - wget (2)
de
hi
ir
the permissions of system files.
.
SEC201 | Intro to Computer Fundamentals 142
01
Linux Terminal – Commands - chmod (1)
The ‘chmod’ command can be used to change the permissions of a file or folder. It can take a parameter such as +x which tells it to make the file
de
executable for everyone. It can also take a number such as 755, which is much more thorough. You should remember how to calculate Linux
permissions and express them as a number in this manner.
By default chmod can work on a file or a group of files or even a folder, but if you want to apply the permissions not just to that folder but also to
hi
subfolders and files contained within that folder, you need to pass in the recursive parameter –R. Make sure you don’t overwrite the permissions
on system files unless you’re absolutely sure that they are wrong or else you risk corrupting your system. Imagine if you accidentally change the
permissions on chmod so that you can’t change the permissions back.
• Open the terminal in your Linux virtual machine and play with the
chmod command a bit
• First cd to ~/Documents/day2/terminal/chmod. Within you have two
folders. In folder1 set the permissions of the file file1.txt to read only
for all. Remember how to calculate permissions?
$ chmod 444 folder1/file1.txt
• Use nano to open the file and make some changes. Can you save it
afterwards? If you can, the permissions are still wrong.
• Let’s do some recursive changes. Let’s remove all permissions from
folder2:
$ chmod –R 000 folder2
ir
• Can you cd to it still?
.
SEC201 | Intro to Computer Fundamentals 143
01
Linux Terminal – Commands - chmod (2)
de
hi
. ir
SEC201 | Intro to Computer Fundamentals 144
01
Linux Terminal – Commands - ln (1)
The ‘ln’ command can be used to create a symbolic link (symlink). Think of it as ‘link’. The syntax is ‘ln –s /path/to/original /path/to/symlink’.
de
This can be a good way to install applications wherever you want but also keeping them in the path. For example, if you install a program into /opt
and /opt isn’t in the path you could symlink the binary from its location in /opt to /bin or /usr/bin which is in the path. Keep in mind that if you
make changes to a link, the changes will apply to the original, but if you delete a link the original file will stay behind.
hi
• Open the terminal in your Linux virtual machine and play with the ln
command a bit
• First cd to ~/Documents/day2/terminal/ln. Within there is a text file,
text1.txt.
• Use ln to generate a symlink in the same folder with the filename
text2.txt:
$ ln –s text1.txt text2.txt
• Edit text1.txt and make some changes. Save them and examine
text2.txt to verify if the changes also applied there.
• Then edit text2.txt and examine text1.txt
ir
• Finally, use ‘rm’ to delete text2.txt. Verify that text1.txt is still there.
.
SEC201 | Intro to Computer Fundamentals 145
01
Linux Terminal – Commands - ln (2)
de
hi
. ir
SEC201 | Intro to Computer Fundamentals 146
01
Linux Terminal – Commands - chown (1)
Chown stands for ‘change ownership’. It can be used to change the owner or group of a file or folder. Usually, only the current owner of the file
de
can change the owner or group, essentially this means giving away ownership rights. Root can also change the ownership of any file, no matter
who owns it. The syntax is easy, simply ‘chown username:group file’. Like with chmod, if you want to apply the permissions changes to a folder
and any items within it, you need to add the recursive parameter (-R).
hi
• Open the terminal in your Linux virtual machine and play with the
chown command a bit
• First cd to ~/Documents/day2/terminal/chown. Within is a folder
‘folder1’. The folder is readable only by the owner, which is
currently you.
• Change the ownership of the folder to root with:
$ sudo chown –R root:root folder1
• Now try to cd to the folder. Does it still work?
• Look at the permissions of the folder with
$ ls –halt
. ir
SEC201 | Intro to Computer Fundamentals 147
01
Linux Terminal – Commands - chown (2)
de
hi
ir
locking yourself out of sudo.
.
SEC201 | Intro to Computer Fundamentals 148
01
Linux Terminal – Commands - sudo (1)
The ‘sudo’ command lets you run a command as another user. This is most typically used to run commands with ‘root’ privileges without actually
de
logging in as root. This is actually the recommended behavior and many distributions don’t even give you the root password anymore.
The way it works is simple, you use ‘sudo’ followed by the command you want to run and that will try to run the command as the root user. You
will be prompted for your account password and then the command will run as root. Why does this work? Well, your account has to be in the
hi
‘sudoers’ file, which is a list of users who are allowed to sudo and what users they are allowed to become.
Only root is able to edit the sudoers file, but it’s actually quite dangerous to do it. Imagine if you don’t know the root password, you then go edit
the sudoers file and corrupt it making sudo stop working for you. You can then no longer edit the sudoers file back to how it was because you are
no longer allowed to use sudo. That’s why using the command ‘visudo’ is a better way to edit the sudoers file. Visudo will check if the sudoers file
is valid before overwriting it.
• Open the terminal in your Linux virtual machine and play with the
sudo command a bit
• First type:
$ whoami
• This is a command that tells you what user you are currently logged in
as
• Now type:
$ sudo whoami
• You will be asked for a password, this is your user’s account password
and not root’s. See the difference between what is reported?
• You can use sudo privileges to do anything that root can do including
ir
changing permissions on files you don’t own
.
SEC201 | Intro to Computer Fundamentals 149
01
Linux Terminal – Commands - sudo (2)
de
hi
• The ‘su’ command stands for switch user and it can allow you to
switch to another user
• By default it tries to switch to the root user
• It is good practice to always add a – to the end of the command so
that the environment variables from the user you are switching to
get loaded
• E.g. su –
• Or su user2 –
• You will need to enter the account password for the account you
ir
are switching to
.
SEC201 | Intro to Computer Fundamentals 150
01
Linux Terminal – Commands - su (1)
The ‘su’ command stands for switch user. Using this command you can switch your terminal session to be logged in as another user. If you don’t
de
pass in any parameters, the default user to switch to is the root user. Otherwise, you have to specify the user you want to switch to. It’s also good
practice to add a – to the end of the ‘su’ command. This will load various environment variables for that user. If you forget to add the -, some
things may not work as intended.
hi
Once you have switched to another user, you can go back to your old user by typing ‘exit’.
• Open the terminal in your Linux virtual machine and play with the
su command a bit
• First switch to the root user with
• $ su –
• Enter the root password, which is: ‘usingSUDOisWayBetter’
. ir
SEC201 | Intro to Computer Fundamentals 151
01
Linux Terminal – Commands - su (2)
de
hi
• Now type
$ whoami
• Finally, exit out of the switched context with the command:
$ exit
• Now type
$ whoami
. ir
SEC201 | Intro to Computer Fundamentals 152
01
Linux Terminal – Commands - su (3)
de
hi
. ir
SEC201 | Intro to Computer Fundamentals 153
01
Linux Terminal – Pipes (1)
A pipe allows you to pass (or pipe) output from one command to another. A pipe character looks like this: |. Take, for example, a strings
First strings will be run on the filename, then the result from the strings command will be piped to the grep command. So grep will run looking for
hi
the keyword in the results of the strings command. You could go even further and pipe the output of grep into less so you’d end up with: strings
filename | grep keyword | less.
Don’t ignore pipes, they are incredibly powerful. It’s tough to work in Linux without using them.
• You can chain them as much as you want. Take this one, for
example: strings file | grep password | less which will do the same
thing as the previous command, but the output will be opened in
less.
• Pipes are incredibly useful when you need to narrow down a large
output
. ir
SEC201 | Intro to Computer Fundamentals 154
01
Linux Terminal – Pipes (2)
de
hi
. ir
SEC201 | Intro to Computer Fundamentals 155
01
Linux Terminal – Pipes (3)
de
hi
• Try it again, but this time let’s only show the process you need:
$ ps aux | grep top
• Now that we have the process id for top, let’s kill it:
$ kill [process id]
• The top process running in your other terminal window should
have exited now
. ir
SEC201 | Intro to Computer Fundamentals 156
01
Linux Terminal – Pipes (4)
de
hi
. ir
SEC201 | Intro to Computer Fundamentals 157
01
Linux Terminal – Redirects (1)
Redirects function similar to pipes, except they allow output from a command to be written to a file. There are two types of redirects, overwrite
To perform an overwrite redirect you need to use >. As in ‘ps aux > filename.txt’. To perform an append redirect you need to use >>. As in ‘ps
hi
aux >> filename.txt’.
You can also use redirects to send output that you don’t want to see to /dev/null. This is an area which contains nothing and therefore anything that
it sent there stops existing. This is particularly useful if you’re setting up scheduled tasks and you don’t want to have the results of whatever
command you scheduled e-mailed to you (for example).
. ir
SEC201 | Intro to Computer Fundamentals 158
01
Linux Terminal – Redirects (2)
de
hi
. ir
SEC201 | Intro to Computer Fundamentals 159
01
Linux Terminal – Redirects (3)
de
hi
. ir
SEC201 | Intro to Computer Fundamentals 160
01
Linux Terminal – Redirects (4)
de
hi
. ir
SEC201 | Intro to Computer Fundamentals 161
01
Linux – Installing Programs (1)
Depending on the methods available, installing programs in Linux is either extremely simple or extremely difficult. In modern distributions, you
de
can find most software has been packaged for use with package managers. These are tools which come with the distributions which can
automatically install software and any related dependencies for you. Package managers are also very helpful in managing updating software, you
can generally update any software that was installed with a package manager with one command.
hi
. ir
SEC201 | Intro to Computer Fundamentals 162
01
Linux – Installing Programs (2)
Not all packages are found in package manager repositories, however. Sometimes you might find software that has been packaged in either .deb or
de
.rpm formats. These are package formats for Debian or Fedora based operating systems (respectively) and they can be installed using the tools
available for those distributions (dpkg or rpm). With this method, package dependencies will not be automatically installed but it will clearly tell
you what you are missing.
hi
On the more difficult end of the spectrum is software that hasn’t been packaged at all. In these cases, you will have to compile the code from
source. Usually, a makefile will be provided, which greatly simplifies things. However, in this case, it is up to the user to manage dependencies. It
may not even be clear from the error message what software you are missing, so often troubleshooting these things takes some research.
. ir
SEC201 | Intro to Computer Fundamentals 163
01
Linux – Package Managers
Package managers are the easiest way to install software, everything is handled for you, even the installation of programs which the program you
de
want to install requires. Most distributions (though not all) will have one of two package managers. The first package manager to consider is ‘apt-
get’ and the second is ‘yum’. They both function in a similar manner. Apt-get is usually found in Debian based distributions, while Yum is usually
found on Fedora based distributions. Package managers require root privileges to install software, so make sure you use them with ‘sudo’.
hi
Linux – apt-get
• With apt-get the ‘update’ parameter updates the list of software packages and
their versions
• sudo apt-get update
• The ‘upgrade’ parameter will upgrade your installed software packages
automatically
• sudo apt-get upgrade
• The ‘dist-upgrade’ parameter will upgrade your operating system to a newer
version
• sudo apt-get dist-upgrade
• The ‘install’ parameter will install new software
• sudo apt-get install nano
The ‘remove’ parameter will uninstall software
ir
•
• sudo apt-get remove nano
.
SEC201 | Intro to Computer Fundamentals 164
01
Linux – apt-get
de
hi
Linux – yum
. ir
SEC201 | Intro to Computer Fundamentals 165
01
Linux – yum
de
hi
Linux – dpkg
. ir
SEC201 | Intro to Computer Fundamentals 166
01
Linux – dpkg
Dpkg is a program which can install software that has been packaged in the ‘.deb’ format. It is not a package manager, it does not automatically
de
resolve dependencies; however, it is a much easier solution than compiling programs from the source code. Dpkg is usually found on Debian
based distributions.
hi
Linux – rpm
. ir
SEC201 | Intro to Computer Fundamentals 167
01
Linux – rpm
RPM is a program which can be used to install software that has been packaged in the ‘.rpm’ format. It is the equivalent of ‘dpkg’, except it is for
Fedora based distributions.
de
hi
ir
first!
.
SEC201 | Intro to Computer Fundamentals 168
01
Linux – Compiling from Source
Not all software has been conveniently packaged for us, sometimes you will need to compile it from the source code for yourself. This is often less
You should first read the INSTALL file that comes with the source code, most software will have this and it will usually contain instructions for
hi
compiling the program. The usual process, however, is to run the configure file with:
$ ./configure
This will generate a Make file which is customized to work with your system. Once that has been done, you run the make file with:
$ make
Finally, you will need to install the program into a directory in your PATH. You can do this with make install; however, you will have to have root
privileges to do this:
# make install
. ir
SEC201 | Intro to Computer Fundamentals 169
01
Linux – apt-get Lab (1)
de
hi
ir
.
SEC201 | Intro to Computer Fundamentals 170
01
Linux – apt-get Lab (2)
de
hi
. ir
SEC201 | Intro to Computer Fundamentals 171
01
Linux – dpkg Lab (1)
de
hi
.ir
SEC201 | Intro to Computer Fundamentals 172
01
Linux – dpkg Lab (2)
de
hi
.ir
SEC201 | Intro to Computer Fundamentals 173
01
Linux – dpkg Lab (3)
de
hi
. ir
SEC201 | Intro to Computer Fundamentals 174
01
Linux – Compile Nmap Lab (1)
de
hi
. ir
SEC201 | Intro to Computer Fundamentals 175
01
Linux – Compile Nmap Lab (2)
de
hi
ir
.
SEC201 | Intro to Computer Fundamentals 176
01
Linux – Compile Nmap Lab (3)
de
hi
. ir
SEC201 | Intro to Computer Fundamentals 177
01
Linux – Compile Nmap Lab (4)
de
hi
. ir
SEC201 | Intro to Computer Fundamentals 178
01
Linux – Compile Nmap Lab (5)
de
hi
. ir
SEC201 | Intro to Computer Fundamentals 179
01
Linux – Compile Nmap Lab (6)
de
hi
. ir
SEC201 | Intro to Computer Fundamentals 180
01
Linux – Compile Nmap Lab (7)
de
hi
. ir
SEC201 | Intro to Computer Fundamentals 181
01
Linux – Networking (1)
Knowing how to configure your network settings is absolutely crucial. You can use the ‘ifconfig’ command to get information on your network
de
settings and also to make changes to your network settings. The changes you make with ifconfig are only temporary, though; they won’t survive a
reboot, so keep that in mind. Each network adapter or network card on your computer has its own network settings, and each one gets its own
identifier. For wired Ethernet connections, the adapter tends to be called ‘eth’ followed by a number starting from 0, so, for example, eth0, eth1,
etc. For wireless cards, the adapters tend to start with ‘wlan’ followed by a number starting from 0, for example, wlan0, wlan1, etc.
hi
You can use ifconfig on its own to list your network adapters and their settings. To change the settings of a particular adapter you can use ifconfig,
followed by the adapter identifier and then pass in parameters for the network settings. For example, ifconfig eth0 192.168.0.5 netmask
255.255.255.0. This assigns the IP address 192.168.0.5 to the eth0 network adapter and sets the netmask to 255.255.255.0.
To set the gateway or router address, you must use the ‘route’ command, for example, route add default gw 192.168.0.1.
. ir
SEC201 | Intro to Computer Fundamentals 182
01
Linux – Networking (2)
You can disable a network interface using the ifdown command, for example, ifdown eth0. You can bring a disabled network interface back up
de
using ifup, for example, ifup eth0. This is useful in VMware when you change the network settings on the virtual machine hardware and need to
refresh them from inside the VM.
hi
. ir
SEC201 | Intro to Computer Fundamentals 183
01
Linux – Networking (3)
To make more permanent changes to your network settings, you will have to edit the /etc/network/interfaces file. This file controls the network
de
settings for each network adapter. Usually, the default setting is to automatically get network settings via DHCP. This is a reasonable default and
will work out of the box for most people. If you do need to use static network settings, you can edit the interfaces file and use the following
settings, replacing the network settings with your own:
hi
auto eth0
iface eth0 inet static
address 192.168.0.5
netmask 255.255.255.0
gateway 192.168.0.1
• As for setting your DNS servers, you will also need to edit a
configuration file and as always with Linux it can vary between
distributions
• However, the most common file is at: /etc/resolv.conf
• Here you just enter the DNS servers you prefer to use in the order
of priority
• For example:
nameserver 8.8.8.8
nameserver 8.8.4.4
. ir
SEC201 | Intro to Computer Fundamentals 184
01
Linux – Networking (4)
To edit your DNS servers, you will need to modify /etc/resolv.conf. All you need to do is put in nameserver followed by the IP address. It is good
de
practice to use multiple DNS servers so that if one goes down, it will automatically fail over to use the next one in the list.
hi
. ir
SEC201 | Intro to Computer Fundamentals 185
01
Linux – Networking (5)
Once configuration files have been edited, you need to restart the computer or restart the networking service. You can restart the networking
service using:
. ir
SEC201 | Intro to Computer Fundamentals 186
01
Linux – Scheduled Tasks (1)
In Linux, a scheduled task is called a ‘cron job’. Simply put, you can specify a time for a command to be executed and whenever that time is
$ crontab –l
de
reached the command will run. Each user has their own cron file which lists the jobs, and each command is run as the user in question. You can
hi
To edit the cron file you can use:
$ crontab –e
. ir
SEC201 | Intro to Computer Fundamentals 187
01
Linux – Scheduled Tasks (2)
It’s also important that you use the full (absolute) path of the command when you enter it.
. ir
SEC201 | Intro to Computer Fundamentals 188
01
Linux – Scheduled Tasks (3)
de
hi
. ir
SEC201 | Intro to Computer Fundamentals 189
01
Linux – Xwindows (1)
The XWindows Server is the software that is responsible for drawing the Graphical User Interface (GUI) on the screen. Most desktop operating
de
systems automatically start it on boot. If for some reason it does not start automatically and you wind up at a terminal prompt, you can try to start
it with the ‘startx’ command. Likewise, you can stop it with the ‘stopx’ command.
hi
. ir
SEC201 | Intro to Computer Fundamentals 190
01
Linux – Xwindows (2)
The X-Windows system is not responsible for the design of the interface, it is only responsible for drawing interface elements on screen. The
de
design of the interface is handled by a separate program which is layered on top of X-Windows. The two popular interfaces are KDE and
GNOME, it’s really personal preference which one you prefer. There are other less well-known ones; some of them have specific purposes such
as having as minimal a footprint as possible.
hi
. ir
SEC201 | Intro to Computer Fundamentals 191
01
Linux – Setting up a Web Server Lab (1)
de
hi
. ir
SEC201 | Intro to Computer Fundamentals 192
01
Linux – Setting up a Web Server Lab (2)
de
hi
. ir
SEC201 | Intro to Computer Fundamentals 193
01
Linux – Setting up a Web Server Lab (3)
de
hi
. ir
SEC201 | Intro to Computer Fundamentals 194
01
Linux – Setting up a Web Server Lab (4)
de
hi
. ir
SEC201 | Intro to Computer Fundamentals 195
01
Linux – Setting up a Web Server Lab (5)
de
hi
. ir
SEC201 | Intro to Computer Fundamentals 196
01
Linux – Setting up a Web Server Lab (6)
de
hi
. ir
SEC201 | Intro to Computer Fundamentals 197
01
Linux – Setting up a Web Server Lab (7)
de
hi
. ir
SEC201 | Intro to Computer Fundamentals 198
01
Linux – Setting up a Web Server Lab (8)
de
hi
. ir
SEC201 | Intro to Computer Fundamentals 199
01
Linux – Setting up a Web Server Lab (9)
de
hi
. ir
SEC201 | Intro to Computer Fundamentals 200
01
Linux – Setting up a Web Server Lab (10)
<html>
<head>
</head>
<body>
<title>It Works!</title>
de
hi
<h1>It Works!</h1>
<p><?php echo “hello”; ?></p>
</body>
</html>
. ir
SEC201 | Intro to Computer Fundamentals 201
01
Linux – Linux – Setting up a Web Server Lab (11)
de
hi
. ir
SEC201 | Intro to Computer Fundamentals 202
01
Linux Troubleshooting – Can’t Run a Binary Executable
If you are having trouble running a file which you believe should be executable, most likely the file does not have execute permissions set on it.
de
You can use the ‘ls’ command to view the file permissions:
$ ls –halt
hi
If you see no ‘x’ permissions for your user account (owner, group or other, depending on which you are), then you need to add them. You can do
this with the chmod command:
$ chmod +x file
. ir
SEC201 | Intro to Computer Fundamentals 203
01
Linux Troubleshooting – Compiling an Executable Manually Fails
The reasons a manual compilation can fail are many, too many to cover here. The most common reason would be a missing dependency or
de
dependencies. The error message is usually a helpful indication of what you are missing. Use your package manager to install the missing package
and then try again. If it fails with a different error, install that dependency too, and so on until the program works.
If the error message is not clear, however, try searching for it in google. More often than not, someone else will have had the same problem.
hi
ir
your PATH
.
SEC201 | Intro to Computer Fundamentals 204
01
Linux Troubleshooting – I’m Typing a Command but It Says It Can’t be Found
If you’ve been trying to run a program and Linux complains that the command is not found, then it may not have been installed into your PATH
de
properly. The PATH is an environment variable which specifies which folders Linux should look in to find executable files when you type a
command. The list is in priority order, so the first entry in the PATH is searched first and if it is found, the other directories are never searched.
This means if you have multiple copies of an executable, you need to make a decision about which one takes priority.
hi
To find if an executable is in your PATH, you can use the ‘which’ command. The syntax is as follows:
$ which [command]
Which will give you the folder which the command is installed in. If it can’t, then that executable file is not in your PATH. You need to figure out
where it was installed and either move it into a folder in your PATH or add the folder it was installed into your PATH. Remember the PATH is an
environment variable so it typically does not survive a reboot. You’ll need to update it in the .bashrc file in your home folder. (Covered in a few
slides).
You can see your PATH with the echo command as in:
$ echo $PATH
Linux Troubleshooting – I’m Sure I Ran an Update but Nothing Has Changed…
. ir
SEC201 | Intro to Computer Fundamentals 205
01
Linux Troubleshooting – I’m Sure I Ran an Update but Nothing Has Changed…
If you are trying to update software on your system with the built-in package manager, make sure you are using the right command. For example,
de
with the ’apt’ package manager on Debian based distributions, the command ‘apt-get update’ does not actually update software packages installed
on your system. Instead, that command will update the repository list, which is used to determine if any of your programs needs to be updated.
In order to actually perform the update, after you’ve updated your repository list, you must follow up with an ‘apt-get upgrade’.
hi
• “I’m trying to start this network service (web server, ftp server,
etc.), but it just won’t start…”
• There may be an application already listening to that port installed
• Remember that each port is unique to an application
• Check with the command:
$ netstat –tpan
• This should show you the application that is listening to the port
you wish to use
. ir
SEC201 | Intro to Computer Fundamentals 206
01
Linux Troubleshooting – Network Service Won’t Start (1)
If you are trying to start a network service and it is failing, it may be because there is already an application listening on that port. You can use the
de
‘netstat’ command to view a list of listening ports. See if the port your application is trying to use is in that list. The syntax is:
$ netstat –tpan
hi
Once you’ve found the application that is listening on the port you want to use, you should either uninstall or, if you need a temporary solution
only you can kill the process.
• You can then uninstall it, or if you just need to kill it you can get the
PID of the application with:
$ ps aux
• and then
$ kill [PID]
• Note that it may start again at boot if it was setup as a service
. ir
SEC201 | Intro to Computer Fundamentals 207
01
Linux Troubleshooting – Network Service Won’t Start (2)
In order to kill a process that is using a port, first, you must determine the process name. Once you have the name determined you need the PID or
de
Process Identifier for that process. You can find that using the ‘ps’ command. The syntax is as follows:
$ ps aux
hi
You should be able to find the process amongst that list. It may help to pipe the results to grep such as:
Once you have the PID, you can kill the process with the kill command:
$ kill [PID]
• “I don’t know what type of file this is, there is no file extension…”
• Linux does not make much use of file extensions, instead it focuses
on the ‘file header’
• The file header is part of the file and says what kind of file it is
• Linux provides a ‘file’ command which can read the header and tell
you what kind of file it is
• Just type:
$ file filename
. ir
SEC201 | Intro to Computer Fundamentals 208
01
Linux Troubleshooting – I Don’t Know What Type of File This Is…
Most files except for plaintext files and encrypted files will contain a file header. The file header is a series of bytes which indicates the type of
as follows:
$ file filename
de
file it is. Linux has a ‘file’ command, which reads these bytes and looks them up against a listing to tell you what type of file it is. The syntax goes
hi
• The ‘.bashrc’ file is a hidden file that lives in the user’s home
directory
• It is a list of commands that get run when the user opens up an
interactive shell/terminal. This also applies to when a user logs in
through the GUI.
• You can do things like export environment variables here so that
they always exist
. ir
SEC201 | Intro to Computer Fundamentals 209
01
Linux Tips – .bashrc (1)
The .bashrc file is a hidden file that lives in the user’s home directory. It contains a list of commands that get run when a user opens up an
de
interactive shell / terminal. This applies when logging in through the GUI, as well as over SSH. It is a good place to export environment variables
so that they persist across a reboot.
hi
• The .bashrc file also allows you to setup aliases. This means you
can specify a command name and then what will run when you
type it.
• A common one would be to map ‘sl’ to ‘ls’ because it is commonly
mistyped
• In addition to aliasing commands, you can change the bash prompt
here
• The bash prompt is actually an environment variable called $PS1
• By editing the variable you can change the appearance of the
prompt
. ir
SEC201 | Intro to Computer Fundamentals 210
01
Linux Tips – .bashrc (2)
You can also use the .bashrc file to create aliases for commands. A common one is to alias ‘ls’ to ‘ls –Gh’ to turn on color coding of different
de
filetypes. There are usually some reasonable defaults in this file already so you can use them as a reference. Finally, you can use the .bashrc file to
customize the terminal prompt. The $PS1 environment variable is the one to change here.
hi
. ir
SEC201 | Intro to Computer Fundamentals 211
01
Linux Tips – .bashrc (3)
The $PS1 setting: \\u@h: \\w]\\$ will display a prompt such as: SEC201@slingshot: ~/Documents/day2$. The format is:
u = username
h = hostname (up to the first ‘.’)
w = current working directory path
de
hi
By changing this format you can either add more information to the prompt or remove some information. Since the file is in .bashrc the
environment variable will only be changed after closing the terminal and opening a new one.
. ir
SEC201 | Intro to Computer Fundamentals 212
01
Linux – Installing Arch Linux (Optional Out of Class Exercise)
de
hi