Ch02 Host MGMT

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 134

LECTURE TWO

Host Management in Linux

1 / 73
Werkneh E. (MSc in Computer Networking)
Linux History

Background of Linux Operating System

o Every computer had a different operating system.


o software for one given system didn't run on another system.
o It was difficult, both for the users and the system administrators
o In 1969, a team of developers in the Bell Labs laboratories started working on a
solution for the software problem, to address these compatibility issues.
o They developed new operating system and calls their project “UNIX” called
kernel
o The operating system and all other functions were built around this kernel and
could run on many different types of hardware

2 / 73
Werkneh E. (MSc in Computer Networking)
Linux History

Background of Linux Operating System..


o All modern operating systems have their roots in 1969 when Dennis Ritchie
and Ken Thompson developed the C language and the Unix operating
system at AT&T Bell Labs
o UNIX was initially found only in very large environments (gov’t, large
financial corporation) it was totally commercial
o 1991 ,Linus Torvalds a CS graduate student develops a free OS called
Linux.
o Linux is a full UNIX clone, fit for use on workstations
o Developed under the GNU General Public License , the source code for
Linux is freely available to everyone.

3 / 73
Werkneh E. (MSc in Computer Networking)
Linux History...

Background of Linux Operating System...


 Today more than 90% of supercomputers (including the complete top 10),
more than half of all smartphones, many millions of desktop computers,
around 70 percent of all web servers, a large chunk of tablet computers, and
several appliances (DVD Players, Washing Machines, DSL Modems,
Routers, ...) run Linux
 About 200 commercial companies including Red Hat, Intel, Broadcom, Texas
Instruments, IBM, Novell, Qualcomm, Samsung, Nokia, Oracle, Google and
even Microsoft are contributing their many developers to enhance the
performance of different Linux versions

4 / 73
Werkneh E. (MSc in Computer Networking)
Installation of Linux Operating Systems

Installation of Debian Operating System


Let us see it on the Lab Session 1 Manual

5 / 73
Werkneh E. (MSc in Computer Networking)
The Linux File Tree and Managing Directories…

Unix/Linux file Structure

6 / 73
Werkneh E. (MSc in Computer Networking)
The Linux File Tree and Managing Directories…

Unix/Linux file Structure…


The Unix file system looks like an inverted tree structure.
You start with the root directory, denoted by /, at the top and work
down through sub-directories underneath it.
Each node is either a file or a directory of files, where the latter can
contain other files and directories.
You specify a file or directory by its path name, either the full, or
absolute, path name or the one relative to a location.
The full path name starts with the root, /, and follows the branches of
the file system, each separated by /, until you reach the desired file,
e.g.: /home/condron/source/xntp

7 / 73
Werkneh E. (MSc in Computer Networking)
The Linux File Tree and Managing Directories…

Unix/Linux file Structure…


o A relative path name specifies the path relative to another, usually the
current working directory that you are at. Two special directories :
o . the current directory
o .. the parent of the current directory
o So if I'm at /home/frank and wish to specify the path above in a
relative fashion I could use:
o ../condron/source/xntp
o This indicates that I should first go up one directory level, then come
down through the condron directory, followed by the source directory
and then to xntp.

8 / 73
Werkneh E. (MSc in Computer Networking)
The Linux File Tree and Managing Directories…

Unix/Linux file Structure…


o Unlike window File names are case sensitive
o File1, file1, filE1, fIle1, etc. are different files and treated differently

9 / 73
Werkneh E. (MSc in Computer Networking)
The Linux File Tree and Managing Directories…

The Root Directory /


o There are some differences in the filesystems between Linux distributions
o All Linux systems have a directory structure that starts at the root directory
o The root directory is represented by a forward slash, /
o Everything that exists on your Linux system can be found below this root
directory
o Let’s take a brief look at the contents of the root directory

10 / 73
Werkneh E. (MSc in Computer Networking)
The Linux File Tree and Managing Directories…

The Root Directory /...

/bin
This directory contains a number of essential commands that are available to
unprivileged users (such as cat, chmod, chown, etc)

The /bin directory also houses the shells (such as bash)

In this directory, basic binary files are exist

11 / 73
Werkneh E. (MSc in Computer Networking)
The Linux File Tree and Managing Directories…

The Root Directory /...

/sbin
o The /sbin directory is similar to the /bin directory.

o It contains essential binaries that are generally intended to be run by the root
user for system administration.

12 / 73
Werkneh E. (MSc in Computer Networking)
The Linux File Tree and Managing Directories…

The Root Directory /...

/lib
 This is where all kernel modules needed for system boot libraries that are
required by root system commands (commands found in /bin and /sbin)

 Binaries found in /bin and /sbin often use shared libraries located in /lib

13 / 73
Werkneh E. (MSc in Computer Networking)
The Linux File Tree and Managing Directories…

The Root Directory /...

/opt
 The purpose of /opt is to store optional software
 In many cases these software from outside the distribution repository
 It may an empty /opt directory on many systems
 A large package can install all its files in /bin, /lib, /etc, subdirectories
within /opt/ $packagename/
 For example the package is called wp, then it installs in /opt/wp, putting
binaries in /opt/wp/bin and man pages in /opt/wp/man

14 / 73

Werkneh E. (MSc in Computer Networking)


The Linux File Tree and Managing Directories…

Configuration Directories

/boot
 The /boot directory contains all files needed to boot the computer, Linux
kernels are stored here
 These files don’t change very often
 On Linux systems you typically find the /boot/grub directory here; /boot/grub
contains /boot/grub/grub.cfg (older systems may still have
/boot/grub/grub.conf) which defines the boot menu that is displayed before
the kernel starts

15 / 73
Werkneh E. (MSc in Computer Networking)
The Linux File Tree and Managing Directories…

Configuration Directories...

/etc
 All of the machine-specific configuration files should be located in /etc
 Historically /etc stood for etcetera, today people often use the Editable Text
Configuration acronym
 Many times the name of a configuration files is the same as the application,
daemon, or protocol with .conf added as the extension

16 / 73
Werkneh E. (MSc in Computer Networking)
The Linux File Tree and Managing Directories…

Data Directories

/home
 Users can store personal or project data under /home
 Represented by tile (~) in terminal
 It is common practice to name the users home directory after the user name
in the format /home/$USERNAME. For example:

17 / 73
Werkneh E. (MSc in Computer Networking)
The Linux File Tree and Managing Directories…

Data Directories...

/ro ot
 On many systems /root is the default location for personal data and profile of
the root user

 If it does not exist by default, then some administrators create it

18 / 73
Werkneh E. (MSc in Computer Networking)
The Linux File Tree and Managing Directories…
Data Directories...

/media
 The /media directory serves as a mount point for removable media devices such
as CD-ROM’s, digital cameras, and various usb-attached devices
 Most Linux distributions today mount all removable media in /media

19 / 73

Werkneh E. (MSc in Computer Networking)


The Linux File Tree and Managing Directories…

Data Directories...

/mnt
 The /mnt directory should be empty and should only be used for temporary
mount points
 Unix and Linux administrators used to create many directories here, like
/mnt/ something/
 You likely will encounter many systems with more than one directory created
and/or mounted inside /mnt to be used for various local and remote
filesystems

20 / 73
Werkneh E. (MSc in Computer Networking)
The Linux File Tree and Managing Directories…

Data Directories...

/tmp
 Applications and users should use /tmp to store temporary data when
needed
 Data stored in /tmp may use either disk space or RAM. Both of which are
managed by the operating system
 Never use /tmp to store data that is important or which you wish to archive

21 / 73
Werkneh E. (MSc in Computer Networking)
The Linux File Tree and Managing Directories…

Memory Directories

/dev
 Device files in /dev appear to be ordinary files, but are not actually/standard
file
 The /dev directory is populated with files as the kernel is recognizing
hardware
 Common hardware such as hard disk devices are represented by device files in
/dev; SATA or SCSI or USB

22 / 73

Werkneh E. (MSc in Computer Networking)


The Linux File Tree and Managing Directories…

Memory Directories...

/dev/null
 /dev/null which can be considered as a black hole
 It has unlimited storage, but nothing can be retrieved from it; Technically
speaking, anything written to /dev/null will be discarded
 It can be useful to discard unwanted output from commands; Thus, it is not a
good location to store your backups

23 / 73
Werkneh E. (MSc in Computer Networking)
The Linux File Tree and Managing Directories…
Memory Directories...

/p roc
 It is another special directory, appearing to be ordinary files, but not taking up
disk space
 It is actually a view of the kernel, or better, what the kernel manages, and is a
means to interact with it directly
 It contains special files that represent system and process
information

24 / 73
Werkneh E. (MSc in Computer Networking)
The Linux File Tree and Managing Directories…

Memory Directories...

/usr- user binaries & read only data


 It is pronounced like user, contains applications and files used by users

 The /usr hierarchy should contain shareable, read only data

 The /usr contains many directories and files. Such as, /usr/bin and /usr/include

25 / 73
Werkneh E. (MSc in Computer Networking)
The Linux File Tree and Managing Directories…

Memory Directories...

/usr/bin
It is a directory which contains a lot of commands

26 / 73
Werkneh E. (MSc in Computer Networking)
The Linux File Tree and Managing Directories…

Memory Directories...

/usr/lib
 Libraries for each are located inside the /usr/lib directory

27 / 73
Werkneh E. (MSc in Computer Networking)
The Linux File Tree and Managing Directories…

Memory Directories...

/var variable data


 It is a directory which is unpredictable in size, such as log, cache and spool
files
 It has sub directories and files. Such as, /var/log and /var/cache

28 / 73
Werkneh E. (MSc in Computer Networking)
The Linux File Tree and Managing Directories…

Memory Directories...

/var/log
 It is a directory which serves as a central point to contain all log files

29 / 73
Werkneh E. (MSc in Computer Networking)
The Linux File Tree and Managing Directories…

Memory Directories...

/var/cache
 It is a directory which can contain cache data for several applications

30 / 73
Werkneh E. (MSc in Computer Networking)
The Linux File Tree and Managing Directories…

Terminal
 To issue any commands and it is possible to open it by using
shortcuts Cntrl + Alt + T
 The shell is where commands are invoked, A command is typed at a
shell prompt
 The command prompt contains the following:
 Commands are case-sensitive, usually lower-case

31 / 73
Werkneh E. (MSc in Computer Networking)
The Linux File Tree and Managing Directories…

Logging out
To exit from the shell, use the exit command
Pressing CTRL + D at the shell prompt will also quit the shell
Quitting all programs should log you out
If in a text-only single-shell environment, exiting the shell should be
sufficient
In a window environment, the window manager should have a log out
command for this purpose
After logging out, a new login prompt should be displayed

32 / 73
Werkneh E. (MSc in Computer Networking)
The Linux File Tree and Managing Directories…

Managing Directories

pwd
 Means Print/present working directory
 It is a tool not directory which can displays the current directory

33 / 73
Werkneh E. (MSc in Computer Networking)
The Linux File Tree and Managing Directories…

Managing Directories...

pwd...
 It is a tool not directory which can displays the current directory

34 / 73
Werkneh E. (MSc in Computer Networking)
The Linux File Tree and Managing Directories…

Managing Directories...

Cd <directory1>
 It is a tool not directory which can change the current directory
 It stands for Change Directory

35 / 73
Werkneh E. (MSc in Computer Networking)
The Linux File Tree and Managing Directories…

Managing Directories...

Cd or cd ~
 To return to home directory

36 / 73
Werkneh E. (MSc in Computer Networking)
The Linux File Tree and Managing Directories…

Managing Directories...


CdTo/ return to root directory, don’t forget to put space b/n cd and
forward slash (not window uses backward slash)
 Remember that root directory is represented as / in Linux

37 / 73
Werkneh E. (MSc in Computer Networking)
The Linux File Tree and Managing Directories…

Managing Directories...

Cd directory1/directory2
 To navigate to multiple directories at the same time

38 / 73
Werkneh E. (MSc in Computer Networking)
The Linux File Tree and Managing Directories…

Managing Directories...

Cd ..
This tool used to go to the parent directory (the one just above the current
directory in the directory tree)

39 / 73
Werkneh E. (MSc in Computer Networking)
The Linux File Tree and Managing Directories…

Managing Directories...

Absolute and Relative Paths


 When you type a path starting with a slash (/), then the root of the file tree is
assumed
 If you don’t start your path with a slash, then the current directory is the
assumed starting point

40 / 73
The Linux File Tree and Managing Directories…

Managing Directories...

Command/File Name Completion


 The tab key can help you in typing a path without errors
 Typing cd /et followed by the tab key will expand the command line to
cd /etc/
 When typing cd / Et followed by the tab key, nothing will happen because you
typed the wrong path (upper case E)
 For an ambiguous name (there are several possible completions), the
shell can list the options:
● For Bash, type Tab twice in succession
● For C shells, type Ctrl+D

41 / 73
Werkneh E. (MSc in Computer Networking)
The Linux File Tree and Managing Directories…
Managing Directories...

ls
 ls is stands for list
 You can list the contents of a directory with ls

42 / 73
Werkneh E. (MSc in Computer Networking)
The Linux File Tree and Managing Directories…
Managing Directories...

ls -l
 Many times you will be using options with ls to display the contents of the
directory in different formats or to display different parts of the directory
 Typing just ls gives you a list of files in the directory. Typing ls -l (that is a
letter l, not the number 1) gives you a long format

43 / 73

Werkneh E. (MSc in Computer Networking)


The Linux File Tree and Managing Directories…

Managing Directories...

ls -l
The information specified in detailed list are: File/Directory
name

File/ Access Memory Size Date


directory permission block
owner groups (byte) /time

44 / 73
Werkneh E. (MSc in Computer Networking)
The Linux File Tree and Managing Directories…

Managing Directories...

ls -R
 To List all the directories, subdirectories and files

45 / 73
Werkneh E. (MSc in Computer Networking)
The Linux File Tree and Managing Directories…

Managing Directories...

ls -a

To List all including the hidden files and represented as “ .“

46 / 73
Werkneh E. (MSc in Computer Networking)
The Linux File Tree and Managing Directories…

Managing Directories...

mkdir
 mkdir is stands for make directory
 mkdir is used to create a new directory on Linux file tree

47 / 73
Werkneh E. (MSc in Computer Networking)
The Linux File Tree and Managing Directories…

Managing Directories...

rmdir
 rmdir is stands for remove directory
 rmdir is used to remove the existed directory if it is empty

48 / 73
Werkneh E. (MSc in Computer Networking)
File Management and Manipulation h

Characteristics of Linux Files

All Files are Case Sensitive


 In Linux, all files are case sensitive
 Linux is case sensitive; this means that etc is different from Etc

49 / 73
Werkneh E. (MSc in Computer Networking)
File Management and Manipulation

Characteristics of Linux Files...

Everything is a File
 A directory is a special kind of file, but it is still a (case sensitive!) file
 Even a terminal window (/dev/pts/4) or a hard disk (/dev/sdb) is represented
somewhere in the file system as a file

50 / 73
Werkneh E. (MSc in Computer Networking)
File Management and Manipulation

Characteristics of Linux Files...

File Extension is not an Issue


 The file utility determines the file type
 Linux does not use extensions to determine the file type Your editor does

not care whether a file ends in .TXT or .DOC


 As a system administrator, you should use the file command to determine the
file type

51 / 73
Werkneh E. (MSc in Computer Networking)
File Management and Manipulation

File Management Important Commands

touch
 One easy way to create a file is with touch

52 / 73
Werkneh E. (MSc in Computer Networking)
File Management and Manipulation…

File Management Important Commands...

rm
 When you no longer need a file, use rm to remove it
 Unlike some graphical user interfaces, the command line in general does not
have a waste bin or trash can to recover files
 Therefore, be careful when removing files!

53 / 73
Werkneh E. (MSc in Computer Networking)
File Management and Manipulation…

File Management Important Commands...

rm -i
 To prevent yourself from accidentally removing a file, you can type rm -i

54 / 73
Werkneh E. (MSc in Computer Networking)
File Management and Manipulation…

File Management Important Commands...

cp <source> <target>
 To copy a file, use cp with a source and a target argument
 If the target is a directory, then the source files are copied to that target
directory

55 / 73
Werkneh E. (MSc in Computer Networking)
File Management and Manipulation…

File Management Important Commands...

cp -i
 To prevent copy from overwriting existing files, use the cp –I (for interactive)
command

56 / 73
Werkneh E. (MSc in Computer Networking)
File Management and Manipulation…

File Management Important Commands...

mv
 Use mv to rename a file or to move the file to another directory

57 / 73
Werkneh E. (MSc in Computer Networking)
File Management and Manipulation…

File Management Important Commands...

File Ownership
 In Linux, there are two basic owners; User owner and Group owner
respectively
 The users and groups of a system can be locally managed in /etc/passwd
and /etc/ group, or they can be in a NIS, LDAP, or Samba domain
 These users and groups can own files. Actually, every file has a user owner
and a group owner, as can be seen in the following screenshot

58 / 73
Werkneh E. (MSc in Computer Networking)
File Management and Manipulation…

File Management Important Commands...

File Ownership...

 User nkengr owns file11, three of those are also owned by the group
nkengr
 By default group is created by a user name and it is a member of that
group by default

59 / 73
Werkneh E. (MSc in Computer Networking)
File Management and Manipulation…

File Management Important Commands...

chgrp
 chgrp stands for change group
 It can change the group owner of a file using the chgrp command

60 / 73
Werkneh E. (MSc in Computer Networking)
File Management and Manipulation…

File Management Important Commands...

chown
 chown stands for change owner
 The user owner of a file can be changed with chown command

61 / 73
Werkneh E. (MSc in Computer Networking)
File Management and Manipulation…

File Management Important Commands...

List of Special Files


 When you use ls -l, for each file you can see ten characters
 before the user and group owner
 The first character tells us the type of file. Regular files get a -, directories get a
d, symbolic links are shown with an l
 - normal file
 d directory
 l symbolic link

62 / 73
Werkneh E. (MSc in Computer Networking)
File Management and Manipulation…

File Management Important Commands...

Permissions
 The nine characters following the file type denote the permissions in
three triplets
 Permission can be r for read access, w for write access, and x for
execute
 You need the r permission to list (ls) the contents of a directory
 You need the x permission to enter (cd) a directory
 You need the w permission to create files in or remove files from a directory

63 / 73
Werkneh E. (MSc in Computer Networking)
File Management and Manipulation…

File Management Important Commands...

Permission on a File on a Directory


 r - (read) read file contents (cat), read directory contents (ls)
 w - (write) change file contents (nano), create files in (touch)
 x - (execute) execute the file, enter the directory (cd)

64 / 73
Werkneh E. (MSc in Computer Networking)
File Management and Manipulation…
File Management Important Commands...

Three Sets of rwx


 This screenshot shows a regular file (because the first character is a -)

 - This is a regular file


 rw- Permissions for the user owner
 r- - Permissions for the group owner
 r- - Permissions for others
65 / 73
Werkneh E. (MSc in Computer Networking)
File Management and Manipulation…
File Management Important Commands...

Setting Permissions (chmod)


 Permissions can be changed with chmod
 The following example gives the user owner execute permissions

66 / 73
Werkneh E. (MSc in Computer Networking)
File Management and Manipulation…
File Management Important Commands...

Setting Octal Permissions


Number Permission Type Symbol
0 No permission  ---
1 Execute --x
2 write -w-
3 Execute+write -wx
4 Read r--
5 Read+Execute r- x
6 Read+write rw-
Read+write +
7 Execute rwx
67 / 73
Werkneh E. (MSc in Computer Networking)
File Management and Manipulation…
File Management Important Commands...

Setting Octal Permissions…


e.g. To give all permission for all users use 777

68 / 73
Werkneh E. (MSc in Computer Networking)
File Management and Manipulation…

Working with File Contents

head
 It is used to display the first ten lines of a file

69 / 73
Werkneh E. (MSc in Computer Networking)
File Management and Manipulation…

Working with File Contents...

tail
 It is similar to head, the tail command will display the last ten lines of a file

70 / 73
Werkneh E. (MSc in Computer Networking)
File Management and Manipulation…
Working with File Contents...

cat
 It is used to Display, copy, combine, and create text file
 If the file is longer than the screen, it will scroll to the end

71 / 73
Werkneh E. (MSc in Computer Networking)
File Management and Manipulation…
Working with File Contents...

tac
 It copies standard input to standard output; It is used to display a file on the
screen from bottom to top
 It is the opposite of cat

72 / 73
Werkneh E. (MSc in Computer Networking)
File Management and Manipulation…

Working with File Contents...

more and less


 The more command is useful for displaying files that take up more than one
screen
 More will allow you to see the contents of the file page by page
 Use the space bar to see the next page, or q to quit. Some people prefer the
less command to more

73 / 73
Werkneh E. (MSc in Computer Networking)
File Management and Manipulation…

Working with File Contents...

Searching & sorting the Contents of a file


 grep – print lines matching a pattern
Syntax: grep [options] pattern [file]
 Example: search for the word localhost in the file /etc/hosts
grep localhost /etc/hosts
 (Use –i option to ignore case)
 sort command is used to sort files contents alphabetically
sort <options> filename
Example: (sort the content of fruit reversibly )
sort –r fruits

74 / 73
Werkneh E. (MSc in Computer Networking)
File Management and Manipulation…
Working with File Contents...

pip-run two commands consecutively


 A pipe channels the output of one program to the input of another
 Allows programs to be chained together
 Use the vertical bar: |
 E.g.1 use both cat and less to display a screen per time as
follows and use ↓↑ to go to the reset of content of file
output input
cat fruits | less

 E.g2 you can use grep with cat for search from output which
filters only the word
cat fruits | grep ^a
 E.g3 three commands as
cat fruits | grep –v a | sort -r
75 / 73
Werkneh E. (MSc in Computer Networking)
File Management and Manipulation…
Working with File Contents...

Search Text File using Regular expression (Regex)


 Regex is set of characters to check patterns in string, used mostly for
writing scripts
 Basic Regex are
 . Replaces any character
 ^ matches start of string
 $ matches end of string
Ex: cat fruits | grep t displays file contents that contains t
cat fruits | grep t$ displays file contents that ends with t
 Extended regular Regex are:
 \+ matches one or more occurrence of the previous character
 \? matches zero or more occurrence of the previous character
 Interval regular Regex are: No of occurrence of characters in a string
 {n} matches the preceding character appearing n times exactly
76 / 73
Werkneh E. (MSc in Computer Networking)
File Management and Manipulation…
Working with File Contents...

Search Text File using Regular expression (Regex)…


 {n, m} matches the preceding character appearing n times but not
more than m
 {n, } matches the preceding character when it appears only n times
or more

77 / 73
Werkneh E. (MSc in Computer Networking)
File Management and Manipulation…

Working with File Contents...

Search Text File using Regular expression (Regex)…

78 / 73
Werkneh E. (MSc in Computer Networking)
File Management and Manipulation…
Working with File Contents...

Search Text File using Regular expression (Regex)…


 Creating strings using brace expansions

79 / 73
Werkneh E. (MSc in Computer Networking)
File Management and Manipulation…

Working with File Contents...

Editors
 Several choices available:
 vi Standard UNIX editor
 gedit graphical text editor
 nano Simple display-oriented text editor
 Pico Simple display-oriented text editor

80 / 73
Werkneh E. (MSc in Computer Networking)
File Management and Manipulation…

Working with File Contents...

Redirection in Linux
 Most programs/commands reads input(stdin) form Keyboard, produce output to
screen/terminal (stdout and stderr)
 Standard error is similar to standard output, but used for error and warning
messages and both are displayed in terminal, so they get mixed
 More than “>” symbol is used for output (stdout) redirection and the less
than < (stderr)symbol is input redirection
 For example you can redirect the output of ls –al command to a file see latter if output is
redirected to a file output.txt file using cat command shown below

81 / 73
Werkneh E. (MSc in Computer Networking)
File Management and Manipulation…

Working with File Contents...

Redirection in Linux…

82 / 73
Werkneh E. (MSc in Computer Networking)
File Management and Manipulation…

Working with File Contents...

Redirection in Linux…
 If we redirect an output to existing file, then it’s content is overwritten
 For this purpose we use append (>>) in order not to replace the existing file as
follows. The following e.g. also shows input redirections

83 / 73
Werkneh E. (MSc in Computer Networking)
File Management and Manipulation…

Working with File Contents...

Redirection in Linux…
 Standard input (stdin) used to read data shown as follows
 We can use stdin to attach email file as

mail –s “news today” [email protected] < newsflash

84 / 73
Werkneh E. (MSc in Computer Networking)
File Management and Manipulation…

Working with File Contents...

Advanced Redirection in Linux


 Every Linux file has descriptors called file descriptors (FD)
 Whenever a program/command opened, stdin, stdout and stderr is opened
together which has FD0, FD1, FD2 File Descriptor
 Most system admins redirect errors not to mix it with the standard output which
clutters the output shown in the following e.g.

85 / 73
Werkneh E. (MSc in Computer Networking)
File Management and Manipulation…

Working with File Contents...

Advanced Redirection in Linux…


Every Linux file has descriptors called file descriptors (FD)
Whenever a program/command opened, stdin, stdout and stderr is opened
together which has FD0, FD1, FD2 File Descriptor
Most system admins redirect errors not to mix it with the standard output
which clutters the output shown in the following e.g.

86 / 73
Werkneh E. (MSc in Computer Networking)
File Management and Manipulation…

Working with File Contents...

Advanced Redirection in Linux…


 Some sys admin redirect both error and stdout using >&

87 / 73
Werkneh E. (MSc in Computer Networking)
Users, Groups and Process Management…

Identify Yourself

whoami
 The whoami command is used to tells you your username

who
 The who command will give you info about who is logged on the system

88 / 73
Werkneh E. (MSc in Computer Networking)
Users, Groups and Process Management…

Identify Yourself...

w
 The w command shows you who is logged on and what they are doing

id
 The id command will give you your user id, primary group id, and a list of the
groups that you belong to

89 / 73
Werkneh E. (MSc in Computer Networking)
Users, Groups and Process Management…

User Management

root
 The local user database on Linux (and on most Unixes) is /etc/passwd
 The root user also called the superuser, is the most powerful account on your
Linux system
 This user can do almost anything, including the creation of other users
 The root user always has userid 0 (regardless of the name of the account)

90 / 73
Werkneh E. (MSc in Computer Networking)
Users, Groups and Process Management…

User Management...

useradd- Adding new user


 The useradd command is used to add users on the system
 Syntax: sudo useradd [OPTIONS] USERNAME
 Options:
 -d home directory  -g (primary group assigned to the user)
 -s starting program (shell)  -G (other groups the user belongs to )
 -p password  -m (auto Create the user’s home directory)
 -u UID  -gid Group ID
 For Example: To add a new user with
• A primary group of users home directory of user1
• A second group mgmt create home directory
• Starting shell /bin/bash a login name of user1
• Password of xxxx
useradd –g users –G mgmt –s /bin/shell –pxxxx –d /home/user1 –m user1
91 / 73
Werkneh E. (MSc in Computer Networking)
Users, Groups and Process Management…

User Management...

useradd- Adding new user…

92 / 73
Werkneh E. (MSc in Computer Networking)
Users, Groups and Process Management…

User Management...

useradd- Adding new user…


 Add a User with non-default Home Directory, Custom Comment, UID, GID,
multiple Groups, with Account Expiry Date, password expiry date, and default
shell /bin/bash
Sudo useradd -d /data/projects –c “Secretary Account”-u 999 -g 500 –G mgmtsec,
accountingsec, financesec -e 2014-03-27 –f 45 –s /bin/bash user2

 You can see your comments in ‘/etc/passwd‘ file in comments section.


tail -1 /etc/passwd

93 / 73
Werkneh E. (MSc in Computer Networking)
Users, Groups and Process Management…

User Management...

userdel
 The userdel command is used to delete users from the system
 The -r option of userdel will also remove the home directory

Example: to remove user1 and his home directory


userdel –r user1

94 / 73
Werkneh E. (MSc in Computer Networking)
Users, Groups and Process Management…

User Management...

usermod
 The usermod command is used to modify existing user
 Options:
 -d home directory
-  -s starting program (shell)
 -p password
 -g (primary group assigned to the user)
 -G (other group the user belongs to )
 To add the group ‘others’ to the user user2
usermod –G others user2

95 / 73
Werkneh E. (MSc in Computer Networking)
Users, Groups and Process Management…

Password

passwd
 Passwords of users can be set with the passwd command
 Options: user’s name (only required if your are root and want to change another
user’s password
 User passwords are encrypted and kept in /etc/shadow. The
 /etc/shadow file is read only and can only be read by root
 Example: To change the password for the account your are currently
logged as…
Passwd
Enter existing password
Enter new password
Enter new password again
96 / 73
Werkneh E. (MSc in Computer Networking)
Users, Groups and Process Management…

Password

Passwd…
 To change the password for user1 created above:

97 / 73
Werkneh E. (MSc in Computer Networking)
Users, Groups and Process Management…

Password...

su to another user/root and sudo


 The su command allows a user to run a shell as another user
 The su command also allows a user to become root
 When no username is provided to su or su -, the command will assume root
is the target
 Quicker than logging off and back on again
 The sudo program allows a user to start a program with the credentials of
another user-root

98 / 73
Werkneh E. (MSc in Computer Networking)
Users, Groups and Process Management…

Password...

su to another user/root and sudo…


 e.g. login as user2 and nstat is started as starting program
 The su command also allows a user to become root

 When no username is provided to su or su -, the command will assume root is


the target
 The sudo program allows a user to start a program with the
 credentials of another user

99 / 73
Werkneh E. (MSc in Computer Networking)
Users, Groups and Process Management…
Group Management

groupadd
 Users can be listed in groups. Groups allow you to set permissions on the
group level instead of having to set permissions for every individual user
 Groups can be created with the groupadd command. The example below shows
the creation of (empty) groups

100 / 73
Werkneh E. (MSc in Computer Networking)
Users, Groups and Process Management…

Group Management...

groupdel
 The groupdel command can permanently remove a group from the
system

101 / 73
Werkneh E. (MSc in Computer Networking)
Users, Groups and Process Management…

Where user & group information stored?


 User name and primary group is stored in /etc/passwd, can be directly
edited using vi but not recommended
 Format of the file is:
 user name(lower case)
 Password (encrypted- only letter ‘x’)
 Primary GID
 Comment (usually person full name )
 Home directory (usually /home/<username>
 Default shell(normally /bin/bash)
 Password for each user are stored in /etc/shadow and only be changed
using passwd command
 Group information is stored in /etc/group
 Format:
 Group name, Group password(rarely used), GID, and user names
(separated by comma )
102 / 73
Werkneh E. (MSc in Computer Networking)
Users, Groups and Process Management…

User and Group Management Takeaway


Syntax /Example
Command Description

useradd Adds accounts to the system. useradd [OPTIONS] USERNAME

usermod Modifies account attributes. usermod [OPTIONS] USERNAME

userdel Deletes accounts from the system. userdel username

groupadd [-g gid [-o]] [-r] [-f]


groupadd Adds groups to the system. groupname

$groupmod –n newgroupname
groupmod Modifies group attributes. oldgroupame

groupdel Removes groups from the system $Goupdel groupname

103 / 73
Werkneh E. (MSc in Computer Networking)
Users, Groups and Process Management…

User and Group configuration files Takeaway


/etc/passwd Keeps user account and password information.
This file holds the majority of information about
accounts on the Unix system.

etc/shadow Holds the encrypted password of the corresponding


account. Not all the system support this file.

/etc/group This file contains the group information for each


account.
/etc/gshadow This file contains secure group account information.

104 / 73
Werkneh E. (MSc in Computer Networking)
Users, Groups and Process Management…

Process Management

What is process
 Kernel considers each program running on your system to be a process

 Any command that you give to your Linux machine starts a new process

 When you launch Mozilla it creates the following process

105 / 73
Werkneh E. (MSc in Computer Networking)
Users, Groups and Process Management…

Process Management…

Type of process
 Foreground process
 They seen on the screen needs inputs from users
 You can start them from desktop menu or from terminal
 e.g. office
 Background process
 Run in the background, usually don’t need inputs from the user
 E.g. Antivirus

106 / 73
Werkneh E. (MSc in Computer Networking)
Users, Groups and Process Management…

Process Management…

PID
 All processes have a process id or PID
 kernel identifies each process by a PID

PPID
 Every process has a parent process (with a PPID). The child process is often
started by the parent process

init
 The init process always has process ID 1. The init process is started by the
kernel itself so technically it does not have a parent process
107 / 73
Werkneh E. (MSc in Computer Networking)
Users, Groups and Process Management…
Process Management …

bg and fg
 To send the process to background use bg <process>
 To send the process back to from use fg <process>

Top utility
 Displays all the running process in the machine in the form of
• PID-process ID • S-status (D(uninteraptable sleep, R(running),
• user-task owner T (traced/stoped), s (sleep), Z(zombie)
• PR-priority value( 20, -20) • %CPU-cpu percentage used
• NI-nice value • %MEM-Memory percentage used
• VIRT-virtual memory used (kb) • time-cpu time
• RES-physical memory used (kb) • command- command name
• SHR-share memory used (kb)

108 / 73
Werkneh E. (MSc in Computer Networking)
Users, Groups and Process Management…

Process Management …

ps utility
 Stands for process status, it is like window task manager
 To check all the process under user use ps ux
 To check process status of a single process use ps PID
 To get PID use pidof <processname>
 E.g. pidof firefox

kill
 Terminate running process, use kill PID

109 / 73
Werkneh E. (MSc in Computer Networking)
Users, Groups and Process Management…

Process Management …

Niceness
 Used for prioritize process which called niceness value which is
between -20 and 19
 Lower niceness value, higher the priority
 Default value of all process is 0
 Syntax: nice –n ‘nice value’ process name or
renice ‘nice value’ –p PID (existing processes)

110 / 73
Werkneh E. (MSc in Computer Networking)
Users, Groups and Process Management…
Maintaining Log Files

syslog
 Many events that occur on a Linux system should be logged for
administrative purposes
 syslog facility in Linux allows to log such events
 syslog can be configured to log different events (severity (‘level’) and
service/facility) to different places (on same machine or on remote syslog
server)

/etc/syslog.conf
 It stores syslog’s configuration, stored in the form of
facility. Level destination
 Facility: the creator of the message (user,kernel, mail, security etc.)
 Level: is a severity threshold - (from lowest to highest): debug, info,
notice, warning, err, crit, alert, emerg
 Destination:
111 / 73 where messages will be sent
Werkneh E. (MSc in Computer Networking)
Users, Groups and Process Management…
Maintaining Log Files…

Reconfiguring syslog
 If you change /etc/syslog.conf, you need to tell syslog to re-read the
configuration
 Accomplished by sending the syslogd process a SIGHUP signal

Examining Logs: less and grep


 to manually scan log files for notable activity
 To review the entire contents of a log file:
# less /var/log/messages
 To look for messages on a certain topic:
# grep -i sshd /var/log/messages

112 / 73
Werkneh E. (MSc in Computer Networking)
Users, Groups and Process Management…
Maintaining Log Files…

Examining Logs in Real Time: tail


 The -f option to tail will watch the file forever:
# tail -f /var/log/messages
 Kill it with Ctrl+C when you’re done

Log Rotation
 syslog will normally allow log files to grow without bound until you run out of
disk space. . .
 The solution is to use log rotation: a scheme whereby existing log files are
periodically renamed and ultimately deleted
 logrotate can be configured with /etc/logrotate.conf

113 / 73
Werkneh E. (MSc in Computer Networking)
Users, Groups and Process Management…

File System Repair, Backup and Restoration

Reasons for Backup


 Disks fail
 Bugs in software can cause corruption
 mistakes by administrator
 Accidental deletion or overwriting (e.g., with rm, mv or cp)
 Malicious deletion or virus attack
 Theft of machines with hard drives in
 Fire, or other disasters which can destroy hardware

114 / 73
Werkneh E. (MSc in Computer Networking)
Users, Groups and Process Management…

File System Repair, Backup and Restoration…

Backup Media
 Traditionally, backups have been made onto tapes
 Can store lots of data on reasonably cheap tapes
 Copying to a different hard disk
 There is a risk of losing the backup along with the original
 Better if on a remote computer
 CD writers can be used to store backups on CDs
 Convenient for long-term storage
 Handy to remove to remote locations

115 / 73
Werkneh E. (MSc in Computer Networking)
Users, Groups and Process Management…
File System Repair, Backup and Restoration…

Types of Backup
 Full backup — includes everything of importance
 Might not include system files, can include a lot of files, many of which
hardly ever change
 Differential backup — only includes changes since last full backup
 Nightly backup only needs to include files changed since the last full backup
 Recovery requires the full backup on which it was based
 Incremental backup — only includes changes since last backup
 Nightly backup only includes files changed in the last 24 hours
 Recovery requires the last full backup and a complete sequence of
incremental backups after that

116 / 73
Werkneh E. (MSc in Computer Networking)
Users, Groups and Process Management…
File System Repair, Backup and Restoration…

Difference Between Full, Differential and Incremental Backups


Full Differential Incremental
Storage Space High Medium to High Low
Backup Speed Slowest Fast Fastest
Restoration Speed Fastest Fast Slowest
Most recent full
Most recent full
backup & all
Media Required Most recent backup & most
incremental
for Recovery backup only recent differential
backups since full
backup
backup
Stores a lot of Stores duplicate
Duplication No duplicate files
duplicate files files
117 / 73
Werkneh E. (MSc in Computer Networking)
Users, Groups and Process Management…
File System Repair, Backup and Restoration…

Backup Strategy
 The backup schedule should be regular and well known by those who rely
on it
 It must be decided what to backup and what can be left out
 Typically a full backup is done once a week or once a month
 Daily changes are recorded in a differential or incremental backup each
night
 Large sites might have more than these two levels to their strategy
 Monthly tapes might be kept for a long time, in case a really old file
becomes important

118 / 73
Werkneh E. (MSc in Computer Networking)
Users, Groups and Process Management…
File System Repair, Backup and Restoration…

Archiving Files with tar


 tar can package up files for distribution or backup
 Encapsulates many files in a single file, Known as a tar archive or a tarball

Creating Archives with tar


 Use the c option to create an archive
 For example, to create an archive called docs.tar.gz containing
everything in the documents directory:
tar czf docs.tar.gz Documents
 f specifies the archive’s filename, The z option compresses the
archive with gzip
119 / 73
Werkneh E. (MSc in Computer Networking)
Users, Groups and Process Management…
File System Repair, Backup and Restoration…

Listing the Files in tar Archives


 To check that a tar file has been made correctly, use the t operation (for ‘list
’):
$ tar tzf docs.tar.gz

Extracting Files from tar Archives


 use the x operation to extract files from an archive:
$ tar xzvf docs.tar.gz

120 / 73
Werkneh E. (MSc in Computer Networking)
Users, Groups and Process Management…
File System Repair, Backup and Restoration…

Deciding what to backup


 Being selective about what is included in the backups can drastically
reduce the time and space taken
 For example, /bin, /sbin, /lib and /usr could be restored from an installation
CD
 But it might still be worth backing them up to make restoration simpler
 The things which are most likely to be important in backups are:
 /home
 The CVS repository, or other places where project work is stored
 Some directories under /var (particularly email)

121 / 73
Werkneh E. (MSc in Computer Networking)
Users, Groups and Process Management…
File System Repair, Backup and Restoration…

what Not to Backup


 Some other areas which shouldn’t be backed up are:
 /tmp — usually doesn’t contain anything of lasting value
 /proc — automatically generated by the kernel
 /dev — if using devfs this is also generated automatically
 /mnt — some media mounted here, like CD ROMS, typically aren’t backed
up
 Filesystems mounted remotely whose backup is taken care of elsewhere

122 / 73
Werkneh E. (MSc in Computer Networking)
Users, Groups and Process Management…
File System Repair, Backup and Restoration…

Scripting Backup
 It is common to have a script to perform backups each night
 Might perform different types of backup, e.g., a full backup on Saturday
night and a differential one on other nights
 Such a script can be run with cron, making backup automatic
 Example scripts are readily available on the WWW

Other Backup Software


 cpio — alternative archiving program
 afio — similar to cpio, but allows files to be compressed individually
 dump and restore — access the filesystem directly, rather than through the
kernel

123 / 73
Werkneh E. (MSc in Computer Networking)
Users, Groups and Process Management…

File System Repair, Backup and Restoration…

gzip - gunzip
 For efficient utilization of space
 gzip used to compress while gunzip is used for
decompressing
 For example to compress file1.txt use
gzip file1.txt
 To uncompress file1.gz use
gunzip file1.gz

124 / 73
Werkneh E. (MSc in Computer Networking)
Users, Groups and Process Management…

File System Repair, Backup and Restoration…

bzip2 -bunzip2
 Files can also be compressed with bzip2 which takes a little
more time than gzip, but compresses better.
 For example to compress text.txt use
$ bzip2 text.txt
 Files can be uncompressed again with bunzip2.
$ bunzip2 text.txt.bz2

125 / 73
Werkneh E. (MSc in Computer Networking)
Users, Groups and Process Management…

Handling Man page /Help system

man
 Most Linux commands have an associated manual page,
called manpage
 Syntax : man <command>
 Example: $ man ls
Shows manual entry for the command ls
 Press q to quit and return to the shell prompt
 man uses the less viewer
 Use the cursor keys for scrolling

126 / 73
Werkneh E. (MSc in Computer Networking)
Users, Groups and Process Management…

Handling Man page /Help system…

man…
 Other common keystrokes:
 Space jump down a page
 b jump back up a page
 /word search for the next occurrence of “word”
 n repeat the previous search
 g go to the top

127 / 73
Werkneh E. (MSc in Computer Networking)
Users, Groups and Process Management…

Package Management

apt-get
 Advanced Packaging Tool (APT) performing such functions
as
 installation of new software packages,
 upgrade of existing software packages,
 updating of the package list index, and
 even upgrading the entire Ubuntu system.

128 / 73
Werkneh E. (MSc in Computer Networking)
Users, Groups and Process Management…

Package Management…

apt-get…
 apt-get update – refresh available updates
 apt-get upgrade – upgrade all packages
 apt-get dist-upgrade – upgrade with package replacements
 apt-cache policy pkg-check weather the pkg installed or not
 apt-cache search pkg-check the avialability of the pkg
 apt-get install pkg – install pkg
 apt-get purge pkg – uninstall pkg
 apt-get autoremove – remove obsolete packages
 apt-get -f install – try to fix broken packages
 dpkg --configure -a – try to fix broken packages

129 / 73
Werkneh E. (MSc in Computer Networking)
Users, Groups and Process Management…

Kernel Customization

 Kernel is Core of OS which manages system resources such as


process, memory, storage management
 The main reason for making changes to the kernel is to fix bugs and
to upgrade system software, such as support for new hardware;
performance gains can also be achieved, to remove unwanted
capability to reduce it memory size, and etc

130 / 73
Werkneh E. (MSc in Computer Networking)
Users, Groups and Process Management…

Kernel Customization…

kernel customization steps includes:


 Installing the kernel source code package (if necessary)
 Applying any patches, adding new device driver code, and/or
making any other source code changes you may require.
 Saving the current kernel and its associated configuration files.
 Modifying the current system configuration as needed.
 Building a new kernel executable image.
 Building any associated kernel modules (if applicable).
 Installing and testing the new kernel

131 / 73
Werkneh E. (MSc in Computer Networking)
Users, Groups and Process Management…

System performance tuning

What is system performance issues


 rang from sluggish interactive response time, to a job that
takes too long to complete or is unable to run at all
because of insufficient resources, like CPU, memory, disk space,
I/O devices etc.
 For solving such resource shortage obtaining more of it (if that is
possible), reducing job or system requirements to desire less of it,
having its various consumers share the amount that is available
by dividing it between them, having them take turns using it
 For example if you have shortage of CPU you may add more of it,
scheduling some jobs to run after a wile, move some job to
another computer, allocate priority, etc..

132 / 73
Werkneh E. (MSc in Computer Networking)
Users, Groups and Process Management…

System performance tuning…

Mechanisms
Resources Control Mechanisms
CPU  Nice numbers
 Process priorities
 Batch queues
 Schedule parameters
Memory  Process resource limit
 Memory management- related parameters.
 Paging(swap) space.
Disk I/O  File system organization accross physical.
 Disk and controllers.
 File placement on disk.
 I/O related parameters.
Network I/O  Network memory buffers.
 Network related parameters.
 Network infrastructure.
133 / 73
Werkneh E. (MSc in Computer Networking)
h

73 / 73

Werkneh E. (MSc in Computer Networking)

You might also like