CSCI 4460/5460
NETWORK OPERATION &
DEFENSE
01: LINUX INTRO &
BASH
Abdullah Yasin Nur 1
SYSTEM ADMINISTRATION
A system administrator, or sysadmin, or admin is a
person who is responsible for
the upkeep,
configuration,
reliable operation of computer systems
especially multi-user computers, such as servers.
The system administrator seeks to ensure that
the uptime,
performance,
resources,
security of the computers they manage
meet the needs of the users
[1
2
]
WHAT EXACTLY DOES A SYSADMIN
DO?
System administration in a larger company, these may all
be separate positions within a computer support or
Information Services department.
IT Support
Database administrator (DBA)
Network administrator
Security administrator
Web administrator
Computer operator
SRE Site Reliability Engineer
In a smaller group they may be shared by a few
sysadmins, or even a single person.
[1
3
]
SYSADMIN’S DUTIES
Analyzing system logs and identifying potential issues with computer systems.
Applying operating system updates, patches, and configuration changes.
Installing and configuring new hardware and software.
Adding, removing, or updating user account information, resetting passwords,
etc.
Answering technical queries and assisting users.
Responsibility for security.
Responsibility for documenting the configuration of the system.
Troubleshooting any reported problems.
System performance tuning.
Ensuring that the network infrastructure is up and running.
Configuring, adding, and deleting file systems.
Ensuring parity between dev, test and production environments.
Training users
Plan and manage the machine room environment
[1
4
]
[2
5
]
[3] 6
[3] 7
IMPORTANT NOTES ON LINUX
Linux is a kernel, not OS.
Linux is not a UNIX clone, it was written from scratch.
A Linux distribution is the Linux kernel and a collection of
software that together creates an OS.
Why Linux is so popular?
Open Source
Lightweight
Secure
Multiuser – Multitask
Simplified update for all installed software
Multiple distributions ( RedHat, Debian, etc)
[4
8
]
LINUX HISTORY
1991: The Linux kernel is publicly announced on 25 August
by the 21-year-old Finnish student Linus Benedict Torvalds.
[5 9
]
LINUX HISTORY
1992: The Linux kernel is relicensed under the GNU GPL
which means Linux became Open Source. The first Linux
distributions are created.
1993: Over 100 developers work on the Linux kernel. The
oldest currently existing Linux distribution, Slackware, is
released for the first time. Later in the same year, the
Debian project is established.
1994: The XFree86 project contributes a graphical user
interface (GUI). Commercial Linux distribution makers Red
Hat and SUSE publish version 1.0 of their Linux distributions.
1995: Linux is ported to the DEC Alpha and to the Sun
SPARC.
1996: Version 2.0 of the Linux kernel is released. The kernel
can now serve several processors at the same time using
symmetric multiprocessing.
[5,6 10
]
LINUX HISTORY
1998: Many major companies such as IBM, Compaq and
Oracle announce their support for Linux. In addition, a
group of programmers begins developing the graphical user
interface KDE.
1999: A group of developers begin work on the graphical
environment GNOME, destined to become a free
replacement for KDE.
GNOME has been crucial to the spread of desktop Linux. It has
given us a number of programs that we use today on desktop
Linux.
2000: Dell announces that it is now the No. 2 provider of
Linux-based systems worldwide and the first major
manufacturer to offer Linux across its full product line.
[5,6 11
]
LINUX HISTORY
2001: Version 2.4 of the Linux kernel is released.
2002: Red Hat Enterprise Linux which is the first
commercial Linux for Business IT was released. RHEL is one
of the few Linux distributions that changed Linux forever.
2003: Version 2.6 of the Linux kernel is released.
2004: Ubuntu 4.10 released.
2005: 2005: Linus Torvalds created Git which is software for
tracking changes in any set of files, usually used for
coordinating work among programmers collaboratively
developing source code during software development. Its
goals include speed, data integrity, and support for
distributed, non-linear workflows (thousands of parallel
branches running on different systems).
[5,6 12
]
LINUX HISTORY
2006: Oracle releases its own distribution of Red Hat
Enterprise Linux.
2007: Dell starts distributing laptops with Ubuntu pre-
installed on them.
2008: Android version 1.0 which is a mobile operating
system based on Linux Kernel released. Linux took the first
step in the world of mobile OS.
2009: Google started Chrome OS project
2011: Version 3.0 of the Linux kernel is released.
2012: The aggregate Linux server market revenue exceeds
that of the rest of the Unix market.
[5,6 13
]
LINUX HISTORY
2013: Google's Linux-based Android claims 75% of the
smartphone market share, in terms of the number of
phones shipped.
2014: Ubuntu claims 22,000,000 users.
2015: Version 4.0 of the Linux kernel is released.
2017: All of Top500 list of fastest supercomputers run Linux.
2019: Version 5.0 of the Linux kernel is released.
[5,6 14
]
THE LINUX DISTRIBUTION
ZOO …
15
THE MAIN LINUX
DISTRIBUTIONS
Debia RedHa
SuSE
Gento
Small Secure
n t o
OpenSuS Damn
Ubuntu RHEL SELinux
E Small
Enterpris
Kali CentOS Puppy
e
Mint Fedora
Yellow
Dog
16
THE FILE SYSTEM & NAMING
CONVENTIONS
Hierarchical (of course):
/one/two/three/file.ext
path file name
(w/ extension)
Home directories:
/home/<userid>
System directories:
/usr/local/share/emacs
scope category application
17
SCOPE & CATEGORIES
/ (root) /usr /usr/games /usr/kerberos /usr/local /usr/X11
Program Hardwar
Docs Config Develop Web Display Runtime
s e
includ
bin doc etc cgi-bin fonts dev var
e
sbin info init.d src html X11 mnt lock
public_
lib man rc.d misc log
html
libexec share www mail
proc
spool
18
OS DIRECTORIES
/boot
Boot files (kernel image), /boot/vmlinuz
/lost+found
Recovered files during FS check
/proc
Info about the running processes
/proc/self
/proc/version
/proc/uptime
/proc/<process-id>
19
FILE TYPES
ls
List down the files and sub-directories within your current
directory
One of the most frequently used commands in Linux
ls [flags] [directory]
ls –l
Displays the mode, number of links, owner, group, size (in
bytes), and time of last modification for each file.
https://www.ibm.com/docs/en/power6?topic=commands-ls-command
20
FILE TYPES
First character of ls -l
- regular file
b block device
c character device
d directory
l symbolic link
p named pipe
21
PERMISSIONS ATTRIBUTES
Owner / Group / All
r read
w write
x execute
Attribute manipulation
chmod
chown
chgrp
22
PERMISSIONS ATTRIBUTES
chmod u+x file (assign execute permission to user in file)
To use chmod to set permissions, we need to tell it:
Who: Who we are setting permissions for.
u: User, meaning the owner of the file.
g: Group, meaning members of the group the file belongs to.
o: Others, meaning people not governed by the u and g
permissions.
a: All
What: What change are we making? Are we adding or
removing the permission?
–: Minus sign. Removes the permission.
+: Plus sign. Grants the permission.
=: Equals sign. Set a permission and remove others.
Which: Which of the permissions are we setting?
r: The read permission.
w: The write permission.
x: The execute permission.
23
PERMISSIONS ATTRIBUTES
Chown : change owner of the file or directory
chown owner_name file_name
e.g. chown root newFile
Chgrp: change the group ownership of a file or directory
24
DESKTOP ENVIRONMENT
GNOME (GNU Network Object Model Environment)
Default desktop environment of many major Linux
distributions
KDE (Kool Desktop Environment)
KDE features more applications, customization options,
and extra functionalities
It can be overwhelming for users
GNOME provides a clean user experience in a modern
look
GNOME is more user experience, KDE is more
customization
25
BASIC THINGS TO KNOW ABOUT THE
SHELL
File location
find, slocate, which, whereis
Basic text file manipulation
grep, cut, paste, tr, sort, uniq, tee
Advanced text file manipulation
sed, awk, m4, perl, python
Archives
tar, gzip/gunzip, compress/uncompress,
zcat, bzip2/bunzip2, zip/unzip,
uuencode/uudecode
26
BASIC THINGS TO KNOW ABOUT THE
SHELL
File comparison
diff/diff3/sdiff/comm, cmp, md5sum
Disks & filesystems
df/du, mount/umount, fsck, sync
Users & processes
id, whoami, logname, users, w, who,
finger, ps, pidof, kill, nice, top
Superuser/remote connection
su, sudo, ssh, sftp
27
28 SHELL-BASED TEXT
PROCESSING
TASK #1: FIND YOUR TEXT
EDITOR …
The classical
vi/vim
The quick & easy
pico/nano
The lifestyle
emacs
Graphical
gedit, geany
29
FLASH RECALL: I/O STREAMS,
REDIRECTION, PIPES
stdin stdout stdin stdout
Process Process
stderr
I/O streams Stream redirection
err.log
pipe
P0 P1
Stream pipelining
30
STREAM REDIRECTION
(BASH)
> Redirect stdout &> Redirect stdout
to a file; and stderr a file;
create/overwrite file. create/ overwrite file.
>> Redirect stdout to a
< Read stdin from a
file; Append to/create
file.
file.
2> Redirect stderr to a << Accept text on
file; create/overwrite following lines as
file. stdin.
2>> Redirect stderr to <> Use file both for
a file; Append to/create stdin and stdout.
file.
31
PIPING BETWEEN
PROCESSES
ls –l wc -l
List files w/ attributes Count number of text lines
> ls –l | wc -l
Count number of files (+ 1)
ls -l | wc -l
32
UNIX-STYLE COMMAND
COMPOSITION
UNIX-STYLE COMMAND
COMPOSITION
find –name '*.pdf'
find –name '*.pdf' > pdf.lst
Find all PDF files in current subtree. Place result in pdf.lst
34
UNIX-STYLE COMMAND
COMPOSITION
find –name '*.pdf' > pdf.lst
Find all PDF files in current subtree. Place result in pdf.lst
35
COMMAND COMPOSITION
XARGS
• Xargs will take a stream of arguments and pass them as a
list.
• Some commands such as grep and awk, can take in
arguments from stdin
• However, others cannot and that is why we need this
find –name '*.pdf' | xargs ls -lh
36
COMMAND COMPOSITION
TEE
find –name '*.pdf' | xargs ls –lh | tee pdf.lst
Save the results to pdf.lst and also send them stdout.
(I.e., save the intermediate results, continue streaming.)
37
COMMAND COMPOSITION
GREP
find –name '*.pdf' | xargs ls –lh | grep "K "
Find all lines in the listing containing string K<space>.
(I.e., select files with size measures in Kbytes.)
38
COMMAND COMPOSITION
GREP
find –name '*.pdf' | xargs ls –lh | grep " [0-9][0-9][0-9]K "
Find all lines in the listing containing the pattern
<space><digit><digit>K<space.
(I.e., select files with size between 100K and 999K.)
39
USING GREP
grep [options] regexp [files]
-c Count matching lines.
-f Specify a pattern input file.
-i Ignore case.
-r Search recursively (when used with files).
grep –r eth0 /etc/*
Find all files in /etc that contain the "eth0" string.
(I.e., find all references to the eth0 (Ethernet) device
in the configuration files.)
40
USING GREP
REGULAR EXPRESSIONS (1)
[] Bracket expression. Match any of the characters.
b[aeiou]g <-> bag, beg, big, bog, bug
- Range expression.
a[1-3]z <-> a1z, a2z, a3z
. Any single character.
a.z <-> a1z, a2z, aaz, aCz, …
^$ Start/end of line.
^abc$ line consisting of "abc" only
42
REGULAR EXPRESSIONS (2)
?*+ Repetition operators: 0-1, 0+, 1+ occurrences
a.*z anything starting with "a" and ending on "z"
(regardless of length)
() Grouping.
Used to disambiguate interpretation.
\ Escaping.
google\.com matches "google.com".
43
COMMAND COMPOSITION
(4) AWK
find –name '*.pdf' | xargs ls –lh | grep " [0-9][0-9][0-9]K "
| awk '{print $5}'
Print only the size column (column #5).
44
COMMAND COMPOSITION
(4) CUT
find –name '*.pdf' | xargs ls –lh| grep " [0-9][0-9][0-9]K "
| cut –c 21-25
Print only characters 21-25 from every line. Same output.
45
COMMAND COMPOSITION
(5) SED
find –name '*.pdf' | xargs ls –lh | grep " [0-9][0-9][0-9]K "
| awk '{print $5}'| sed 's/K//'
Remove trailing "K".
find –name '*.pdf' | xargs ls –lh | grep " [0-9][0-9][0-9]K "
| awk '{print $5}'| sed 's/K/AA/'
Replace “K“ with “AA”
46
COMMAND COMPOSITION (6) SUM,
SORT, UNIQ
find –name '*.pdf' | xargs ls –lh | grep " [0-9][0-9][0-9]K "
| awk '{print $5}'| sed 's/K//'| sum
Add up all the size numbers.
find . –name *.pdf | xargs ls –lh| grep " [0-9][0-9][0-9]K "
| awk '{print $5}'| sed 's/K//' |sort|uniq|wc -l
How many different file size are there b/w 100K & 999K.
find . –name *.pdf | xargs ls –lh| grep " [0-9][0-9][0-9]K "
| sort –k 3
Sort results by column #3 (user).
47
USING SHELL
SCRIPTS
48
"Advanced Bash-Scripting Guide"
by Mendel Cooper [7]
CREATING SCRIPTS
Start with "shabang"
#!/bin/bash
Make the file executable
chmod +x script.sh
Run it
sh script.sh
Document your code w/ comments
# This is a comment
49
CREATING SCRIPTS
50
CREATING SCRIPTS
Printf:
printf “ABC”
printf “Print First: %s\nPrint Second: %s\n" "30" “40“
OUTPUT:
Print First: 30
Print Second: 40
%s – Replace the argument
51
TOPICS & SCRIPTS (1)
Command-line parameters (arguments)
02-01-cmd-args.sh
Variable assignments
02-02-var-assign.sh bash-guide book (bg) 4.1-4.2
Variable typing
02-03-var-typing.sh bg-4.3
Quoting
02-04-quoting.sh bg-5.1
Escaping
02-05-escaping.sh bg-5.2
52
TEST CONSTRUCTS
Remember:
By Unix convention, exit status of 0
means success.
[[ … ]] (( … )) let … produce exit status
If the expression expands to a non-zero value, return is 0.
let "1<2" returns 0 ("1<2" expands to "1")
(( 0 && 1 )) returns 1 ( "0 && 1" expands to "0")
02-06-if-test.sh bg-7.1
53
TESTS, IF-THEN CONSTRUCT
if [ condition-true ]
then
command 1
command 2
...
else # Or else ...
# Adds default code block
# executing if original condition tests false.
command 3
command 4
...
fi
Testing (boolean expression)
02-06-if-test.sh bg 7.1
54
IF-ELIF-THEN CONSTRUCT
if [ condition-true ]
then
command 1
command 2
...
elif [ condition2 ] # Same as else if
then
command 3
command 4
...
else
default-command # if all conditions fail do this
fi
55
TEST, [ ], AND [[ ]]
02-07-testing.sh
bash$ type test
test is a shell builtin
bash$ type '['
[ is a shell builtin
test: both a built-in and a command
bash$ type '[['
[[ is a shell keyword
bash$ type ']]'
]] is a shell keyword [: both a built-in and a command
bash$ type ']'
bash: type: ]: not found
bash$ ls -l /usr/bin/test /usr/bin/[
… 47584 2009-10-06 06:06 /usr/bin/[
… 31184 2009-10-06 06:06 /usr/bin/test
56
[ ] VS. [[ ]] VS. (( ))
NB: The [[ ]] construct, extended test, is
more versatile than [ ]. We shall use [[ ]]
exclusively.
02-08-octal-hex.sh
if does not require a test construct:
dir=/home/bozo Hides (consumes) error message
if cd "$dir" 2>/dev/null
then
echo "Now in $dir." Arithmetic (( ))
else
echo "Can't change to $dir."
02-09-arithmetic.sh
fi
57
FILE TEST OPERATORS.
TRUE, IF FILE …
-e exists; -r read permission;
-s size is not zero; -w write permission;
-f is a regular file; -x execute permission;
-d is a directory; -O you are owner;
-b is a block device; -G group same as yours;
-c is a character device; -N modified after last read;
-h/L is a symbolic link; f1 -nt f2 f1 is newer than f2;
-c is a character device; f1 -ot f2 f1 is newer than f2;
-t is associated w/terminal f1 -ot f2 f1 & f2 hard links to
same file;
device.
stdin? [ -t 0 ], stdout [ -t 1 ] ! not—reverses test.
58
BINARY COMPARISON
OPERATORS
Integer String
-eq is equal to = is equal to
-ne is not equal to != is not equal to
-lt is less than < is before in dictionary order
-le is less than or equal to > is after in dictionary order
-gt is greater than Unary (string)
-ge is greater than or equal to -z is null (zero length)
< (( is less than )) -n is not null
<= (( is less than or equal Compound logical
to)) [[ && ]] logical AND
> (( is greater than )) [[ || ]] logical OR
>= (( is greater than or equal
to ))
02-10-comparisons.sh 02-11-null-string.sh
59
FOR LOOPS
for arg in [list]
do
command(s)...
done
NB: During each pass through the loop, arg takes
on the value of each successive variable in the list.
for arg in "$var1" "$var2" "$var3" ... "$varN"
# In pass 1 of the loop, arg = $var1
# In pass 2 of the loop, arg = $var2
# In pass 3 of the loop, arg = $var3
# ...
# In pass N of the loop, arg = $varN
# Args in [list] may need to be quoted to
# prevent possible word splitting.
60
LOOP TOPICS & SCRIPTS
(BG 10.1)
for loops
02-12-for-loops.sh
02-13-for-fileinfo.sh
02-14-for-files.sh
02-15-for-cmd-subst.sh
02-16-for-users.sh
02-17-for-func.sh
02-18-for-symlinks.sh
02-19-for-c-style.sh
while loops
02-20-while-loop.sh
02-21-while-c-style.sh
02-22-continue-break.sh
02-23-nested-break.sh
61
TEXT PROCESSING
GREP & SED &
AWK
62
GAWK: Effective AWK Programming by Arnold D.
Robbins [8]
sed & awk by Dougherty & Robbins (ISBN: 978-1-56592-
225-5)
GREP-SED-AWK
grep is only good for simple text matching and printing
sed offers additional text transformation commands like
substitution
awk is a scripting language that offers a multitude of
features
63
HOW SED/AWK WORK
sed/awk instructions have two parts:
regular expression pattern + procedure
For each line of input, patterns are evaluated
The procedures of matched patterns are executed
64
USING SED (STREAM
EDITOR)
sed 'instruction' input_file
sed –f sed_script input_file
Most useful sed scripts are one liners and do something simple.
If you need something complex, go elsewhere
65
SED EXAMPLE
66
SED EXAMPLE
Replace all “sed” text with DrNur
Command: sed ‘s/sed/DrNur/g’ sedTest
/g is for global.
/1 Replace first match, /2 Replace second match
67
SED EXAMPLE
Parenthesize first character of each word
Command: echo “Hello Class How Are
You" | sed 's/\(\b[A-Z]\)/\(\1\)/g'
68
SED ONE LINERS BY
EXAMPLE …
external:
http://sed.sourceforge.net/sed1line.txt
https://catonmat.net/sed-one-liners-explained-part-one
https://linuxhint.com/50_sed_command_examples/
69
AWK BY EXAMPLE
An awk program is a sequence of pattern-action statements:
BEGIN { action } initialization (optional)
pattern{ action } script (optional)
END { action } finalization (optional)
70
[9]
AWK BY EXAMPLE
Command: awk {print} stringSample
Print file
71
AWK BY EXAMPLE
72
AWK BY EXAMPLE
awk 'BEGIN { for(i=1;i<10;i++) print "square of", i, “=", i*i;}'
73
AWK (QUESTION FROM
STACKOVERFLOW
[10] 74
[10] 75
REFERENCES
[1] https://en.wikipedia.org/wiki/System_administrator
[2] https://www.wired.com/2016/08/linux-took-web-now-taking-world/
[3] https://webtribunal.net/blog/linux-statistics/
[4] https://www.geeksforgeeks.org/linux-from-the-beginning-history-and-evolution/
[5] https://itsfoss.com/25-years-linux/
[6] https://en.wikipedia.org/wiki/History_of_Linux
[7] https://hangar118.sdf.org/p/bash-scripting-guide-home/
[8] https://www.gnu.org/software/gawk/manual/gawk.pdf
[9] https://www.geeksforgeeks.org/awk-command-unixlinux-examples/
[10] https://stackoverflow.com/questions/25544974/awk-after-grep-print-value-
when-grep-returns-nothing
76