0% found this document useful (0 votes)
11 views61 pages

OSA Manual

The document is a certificate for Sanket Belgundkar from AITM Polytechnic, Belagavi, confirming his completion of the Operating Systems and Administration laboratory course. It includes a list of experiments related to operating systems, installation methods, boot processes, file systems, and post-installation tasks. Additionally, it describes the installation and configuration of Virtual Box for running multiple operating systems on a single machine.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views61 pages

OSA Manual

The document is a certificate for Sanket Belgundkar from AITM Polytechnic, Belagavi, confirming his completion of the Operating Systems and Administration laboratory course. It includes a list of experiments related to operating systems, installation methods, boot processes, file systems, and post-installation tasks. Additionally, it describes the installation and configuration of Virtual Box for running multiple operating systems on a single machine.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 61

Operating Systems and Administration-20CS42P

Certificate

Name : Sanket Belgundkar Class: IV Semester Register


Number :
Institution : AITM Polytechnic, Belagavi

This is certified to be the bonafide work of the student in the Operating System and Administration –
20CS42P Laboratory during the academic year 202 – 202

Course Coordinator

Examiner Signature

1.

2.

AITM Polytechnic, Belagavi Dept Of Computer Science


Operating Systems and Administration-20CS42P

List of Experiments

Sl. No. Name of Experiment Remarks

1 Operating System (OS) and Administration

2 Install and Configure Virtual Machine – Virtual box

3 File and Directory Commands

4 Process Creation and Management

5 Process Synchronization

6 Memory Management

7 Shell Programming

8 Automation of System Tasks

9 Network Management

10 User Authentication

11 System/Log monitoring commands and System


Information/Maintenance Commands

12 Domain Name Service (DNS)

13 Storage Management

AITM Polytechnic, Belagavi Dept Of Computer Science


Operating Systems and Administration-20CS42P

01 – Operating System and Administration

An Operating system (OS) is system software that manages computer hardware, software resources, and
provides common services for computer programs. In other words, an Operating System (OS) is software that
acts as an interface between computer hardware components and the user.

1. Types of OS installation
Attended installation:
 An installation process usually needs a user who attends it to make choices, such as accepting or
declining an end-user license agreement (EULA), specifying preferences such as the installation
location, supplying passwords or assisting in product activation.
 In graphical environments, installers that offer a wizard-based interface are common.
 Attended installers may ask users to help mitigate the errors.
o For example, if the disk in which the computer program is being installed was full, the
installer may ask the user to specify another target path or clear enough space in the disk.

Silent installation:
 An installation that does not display messages or windows during its progress.
 "Silent installation" is NOT the same as "unattended installation". [All silent installations are
unattended but not all unattended installations are silent.]
 In bigger organizations where thousands of users work, deploying the applications becomes a typical
task and for that reason silent installation is performed so that the application is installed in background
without affecting the work of user.

Unattended installation:
 An installation that is performed without user interaction during its progress or with no user present
at all.
 One of the reasons to use this approach is to automate the installation of a large number of systems.
 An unattended installation either does not require the user to supply anything or has received all
necessary input prior to the start of installation. Such input may be in the form of command line
switches or an answer file, a file that contains all the necessary parameters.
o For example, if the installation medium was faulty, the installer should fail the installation, as
there is no user to fix the fault or replace the medium. Unattended installers may record errors
in a computer log for later review.

Headless installation:
 Installation performed without using a computer monitor connected.
 In attended forms of headless installation, another machine connects to the target machine (for
example, via a local area network) and takes over the display output.

Scheduled or Automated installation:


 An installation process that runs on a preset time or when a predefined condition meet the
requirements, as opposed to an installation process that starts explicitly on a user's command.
o For example, a system administrator willing to install a later version of a computer program
that is being used can schedule that installation to occur when that program is not running.
 An operating system may automatically install a device driver for a device that the user connects.

Clean installation:
 A clean installation is one that is done in the absence of any interfering elements such as old versions
of the computer program being installed or leftovers from a previous installation.
 The clean installation of an operating system is an installation in which the target disk partition is
erased before installation.
 Since the interfering elements are absent, a clean installation may succeed where an unclean
installation may fail or may take significantly longer.
AITM Polytechnic, Belagavi Dept Of Computer
Science
Operating Systems and Administration-20CS42P

Network installation:
Network installation (netinstall), is an installation of a program from a shared network resource that may be
done by installing a minimal system before proceeding to download further packages over the network.
This may simply be a copy of the original media but software publishers which offer site licenses for
institutional customers may provide a version intended for installation over a network.

2. Boot methods
Booting is the process of starting a computer as initiated via hardware such as a button or by a software command.
Types of Booting:
 Warm Booting: The Warm Booting is that in which system starts from the starting or from initial
state means.
o In the Warm Booting the system will be started from its beginning state means, first, the user
will press the Power Button, then this will read all the instructions from the ROM and the
Operating System will be automatically gets loaded into the System (RAM).
 Cold Booting: The Cold Booting is that in which System automatically starts when the system is in a
running state.
o For example, due to Light Fluctuation, the system will automatically restarts. In this, chances
of damaging of system are more. The system will now be start from its initial state, so some
files may be damaged because they are not properly stored into the system.

3. File System and Formatting:


File System:
 A file system is a process of managing how and where data on a storage disk, which is also referred
to as file management or FS.
 It is a logical disk component that compresses files separated into groups, which is known as
directories.
 The file system enables user to view a file in the current directory as files are often managed in
a hierarchy.
 It is abstract to a human user and related to a computer; hence, it manages a disk's internal
operations.
 NTFS is the most common file system in modern times (Windows OS).
 Without file management, it would be impossible for a file with the same name to exist and also
impossible to remove installed programs and recover specific files.

Examples of File Systems:


The examples of file systems are given below:
 FAT: FAT is a type of file system, which is developed for hard drives. It stands for File Allocation
Table. On hard drives and other computer systems, it helps to manage files on Microsoft operating
systems. In devices like digital cameras, flash memory, and other portable devices, it is also often
found that is used to store file information. It also helps to extend the life of a hard drive as it
minimizes the wear and tears on the hard disc. Now a days later versions of Microsoft Windows like
Windows XP, Vista, 7, and 10 as use NTFS.
o The FAT8, FAT12, FAT32, FAT16 are all the different types of FAT (for file allocation
table).
 GFS: A GFS is a file system, which stands for Global File System. It has the ability to make enable
multiple computers to act as an integrated machine. When the physical distance of two or more
computers is high, and they are unable to send files directly with each other, a GFS file system makes
them capable of sharing a group of files directly. A computer can organize its I/O to preserve file
systems with the help of a global file system.
 HFS: HFS (Hierarchical file system) is the file system that is used on a Macintosh computer for
creating a directory at the time a hard disk is formatted. Generally, its basic function is to organize or
hold the files on a Macintosh hard disk. Apple is not capable of supporting to write to or format HFS
AITM Polytechnic, Belagavi Dept Of Computer
Science
Operating Systems and Administration-20CS42P

disks since when OS X came on the market. Also, HFS-formatted drives are not recognized by
Windows computers as HFS is a Macintosh format. With the help of WIN32 or NTFS file systems, Windows
hard drives are formatted.
 NTFS: NTFS is the file system, which stands for NT file system and stores and retrieves files on
Windows NT operating system and other versions of Windows like Windows 2000, Windows XP,
Windows 7, and Windows 10. Sometimes, it is known as the New Technology File System. As
compared to the FAT and HFS file system, it provides better methods of file recovery and data
protection and offers a number of improvements in terms of extendibility, security, and performance.
 UDF: A UDF is a file system, stands for Universal Disk Format and used first developed by OSTA
(Optical Storage Technology Association) for ensuring consistency among data written to several
optical media. It is used with CD-ROMs and DVD-ROMs and is supported on all operating systems.
Now, it is used in the process of CD-R's and CD-RW's, called packet writing.

Formatting:
Formatting is a process of preparing the storage device to store the data. Formatting storage device will
erase the earlier contents of the device.

4. Post installation tasks:


Post Installation task is the set of steps to be carried out to ensure that the installation is complete and went
smoothly.
Post Installation Tasks for Ubuntu Operating System:
 Online accounts
The first step allows user to configure online accounts, in case user want to integrate the desktop with
different services.
 Livepatch
Livepatch is a service that allows the installation of some updates that would generally require a system
reboot, such as those of the kernel.
 Help improve Ubuntu
In this step user can choose whether or not to send data from his system to Ubuntu. The option is
activated by default. The user can verify the secrecy of the data being sent beforehand. The results are
used to improve Ubuntu.
 Privacy
If required, a user can enable location services so that apps can determine user geographic location.
All the applications installed by default on Ubuntu are free software.
 You are ready to start!
The last screen shows some featured applications -some of which are not free software with the option
to open the software center to install them.

AITM Polytechnic, Belagavi Dept Of Computer


Science
Operating Systems and Administration-20CS42P

02 – Install and configure virtual machine – Virtual box

Virtual Box:
Virtual Box is a cross-platform virtualization application. It installs on existing operating systems and also it
extends the capabilities of the existing computer so that it can run multiple operating systems (it means, inside
multiple virtual machines) at the same time.
 Host operating system (host OS): the operating system of the physical computer on which Virtual Box
was installed.
 Guest operating system (guest OS): the operating system that is running inside the virtual machine.
 Virtual machine (VM). When running, a VM is the special environment that VirtualBox creates for guest
operating system.

Virtual Box’s main features:

 Portability  Great hardware support


 No hardware virtualization required o Guest multiprocessing (SMP)
 o USB 2.0 device support
Guest Additions: shared folders, o Hardware compatibility
seamless windows, 3D virtualization o Full ACPI support (Advanced
Configuration and Power Interface)
 Multigeneration branched snapshots. o Multiscreen resolutions
 Clean architecture; unprecedented o Built-in iSCSI support
modularity.  Remote machine display

Host operating systems:


o Windows hosts (Windows XP, Windows 7, Windows Server 2003, 2008, Vista etc)
o Mac OS X hosts (Leopard 32-bit, Snow Leopard 32/64 - bit)
o Linux hosts (Ubuntu, Debian, Fedora, Mandriva etc)

Installing Virtual Box on Hosts:


 Open Terminal
 Be sure about Internet Connection is there.
 Type the command sudo apt-get install virtual box
 Type virtual box in terminal or select virtual box from installed programs list.

Installing Virtual Box from OFFLINE software:


 get the Virtual Machine image for linux distribution from Oracle Website
 Example: for Ubuntu 18.3 – Get the Image for Ubuntu 16.04
 https://download.virtualbox.org/virtualbox/6.1.32/virtualbox-6.1_6.1.32-
149290~Ubuntu~xenial_amd64.deb
o (Choose appropriate image from repository according to current Host Linux OS distribution)
 Go to the Downloads  Select the Downloaded Virtual Box Software  Open  Check for the
Compatibility (if not compatible, download the suitable image form oracle website)  Install the
Virtual Machine Software.

Creating Virtual Machine:


 Open the Virtual box and click on New to create a new VM, give name of user choice, select Linux &
version as 32 bit or 64 bit depending upon user system architecture
 Give RAM Memory to the VM. Generally 1024 Mbytes

AITM Polytechnic, Belagavi Dept Of Computer Science -6-


Operating Systems and Administration-20CS42P

 Select the hard disk space, Go for 20.00 GB.


 Create New Hard Drive or Select one form the list or from another location. Go for Create a Virtual
hard drive now.
 Select the type of the file form the new virtual hard drive. Go for VDI (Virtual Box Disk Image)
 Choose whether the new virtual disk file should be allocated as it is used (Dynamic) or if it should be
created fully allocated at its maximum size (Fixed Size). Go for Dynamically Allocated.
 Summary: Will display the Virtual disk parameters. If these settings are correct, press the Create
button and it will create a new virtual disk file.

Installing Guest Operating System:


 Make sure the location of Guest OS iso in host OS.
 Select the Virtual disk in Virtual Box Manager (VBM), then select Settings.
 Select Storage Tab, Select Controller IDE, the select ⨀Empty. Then click on CD symbol at Attributes
frame, and then select the Guest OS iso File (Choose Virtual Optical Disk File).
 Click the Start Button on VBM toolbar.
 This wizard will help us to perform the steps necessary for installing an Operating System onto this virtual
machine. Follow the steps for installing the Guest OS.
o Click “Ok”
o Go to Guest OS. And assign one valid IP Address to the Guest OS (Say 192.100.100.6)
o Use Ping command to check the Communication between 192.100.100.5 and 192.100.100.6.

Screen Shots (working Virtual Box in Linux Operating System – Host is Linux):

AITM Polytechnic, Belagavi Dept Of Computer Science -7-


Operating Systems and Administration-20CS42P

AITM Polytechnic, Belagavi Dept Of Computer Science -8-


Operating Systems and Administration-20CS42P

Download and Install a Terminal Emulator:


 A Terminal emulator is a computer program that reproduces a video terminal within some other display
structure (i.e., remote machine desktop will be appeared in local machine as a terminal).
o In other words, the Terminal emulator has the ability to make a dumb machine appear like a client
computer networked to the server.
 The terminal emulator allows an end-user to access the console as well as its applications such as text user
interface and command-line interface.
 Examples for Terminal Emulator: Terminator, ROXTerm, Eterm, Tilix, LXTerminal, Konsole, Kitty, st,
Gnome-Terminal, Terminology, Deepin Terminal, xterm, LilyTerm, Extraterm, mate-terminal, DomTerm,
TermKit

Example 1: xterm
o The xterm terminal application is a standard terminal emulator for the X Window System
o Installation:
sudo apt-get update
sudo apt-get install xterm

Example 2: mate-terminal
o MATE Terminal is a terminal emulation application to access a UNIX shell in the MATE environment.
With it, one can run any application that is designed to run on VT102, VT220, and xterm terminals.
o MATE Terminal also has the ability to use multiple terminals in a single window (tabs) and supports
management of different configurations (profiles).
o MATE Terminal is a fork of GNOME-Terminal.
o Installation:
sudo apt-get update -y
sudo apt-get install -y mate-terminal

Significance of man Command


 man command in Linux is used to display the user manual of any command that run on the terminal.
 It provides a detailed view of the command which includes NAME, SYNOPSIS, DESCRIPTION,
OPTIONS, EXIT STATUS, RETURN VALUES, ERRORS, FILES, VERSIONS, EXAMPLES,
AUTHORS and SEE ALSO.

AITM Polytechnic, Belagavi Dept Of Computer Science -9-


Operating Systems and Administration-20CS42P

 Every manual is divided into the following sections:


o Executable programs or shell commands
o System calls (functions provided by the kernel)
o Library calls (functions within program libraries
o Games
o Special files (usually found in /dev)
o File formats and conventions eg /etc/passwd
o Miscellaneous (including macro packages and conventions),
o System administration commands
 Syntax
o man [SECTION-NUM] [COMMAND NAME]

03 – File and Directory Commands

ls (list)
 Use ls without any arguments to display current directory contents.
 ls with the -a option. files all begin with a "dot", which indicates they are "hidden" files.
o ls -a
 ls with -F, this command is useful for distinguishing between directories, ordinary files, and executable
files.
o ls -F
 ls with -l to obtain a "long" listing of files. An explanation of the information it provides appears below.
o ls -l
-rwxr-xr-x 1 jsmith staff 43 Mar 23 18:14 prog1
-rw-r--r-- 1 jsmith staff 10030 Mar 22 20:41 sample.f

drwxr-sr-x 2 jsmith staff 512 Mar 23 18:07 subdir1

drwxr-sr-x 2 jsmith staff 512 Mar 23 18:06 subdir2

drwxr-sr-x 2 jsmith staff 512 Mar 23 18:06 subdir3

1 2 3 4 5 6 7
1 = access modes/permissions 5 = size (in bytes)
2 = number of links 6 = date/time of last modification
3 = owner 7 = name of file
4 = group
 ls with R, Recursive listings which will display files and folders inside the folders recursively.
o ls -R
o ls -Rl
pwd (Present Working Directory)

 Will give the present working directory path informatoin

mkdir (Make Directory)

AITM Polytechnic, Belagavi Dept Of Computer Science - 10 -


Operating Systems and Administration-20CS42P

 mkdir will create a new directory (folder)


 Syntax: mkdir directoryname(folder name)
Example 1: mkdir polytechnic // will create directory with name polytechnic
Example 2: Create some additional subdirectories within polytechnic. List new directories after the
command completes.
mkdir polytechnic/mechanical polytechnic/computer polytechnic/civil ls polytechnic
Example 3: Try to create a directory in a location where user doesn’t have permission.
mkdir /etc/mydir
Output: mkdir: cannot create directory ‘/etc/mydir’: Permission denied
cd (Change Directory)
 cd is used to Change Directory
 Change to home directory
o cd // Change to default home directory
o cd ~ // Change to default home directory
 Change to a subdirectory within user home directory
o cd polytechnic/computer
 Go up one level back to the current directory's parent directory
o cd ..
 Change to the root (top-most) directory
o cd /
 Change to another directory
o cd ~/polytechnic
 Change to another one of subdirectories
o cd ~/polytechnic/computer

rmdir (Remove Directory)


 Will remove (delete) the directory
 Make sure that folder is empty before issuing rmdir command.
 Make sure current directory is not part of the directory being deleted.
o rmdir civil (assume current directory is /home/polytechnic)
o rmdir /polytechnic/computer (assume /polytechnic/computer folder exists)

File Manipulation Commands:


Creating File:
 touch: will create empty files
o Example: touch a.txt b.txt
 Create file and add contents at the end press enter key and Ctrl+z
o Example:
cat > a.txt
Welcome to Operating System Lab Enjoy
Typing commands
Ctrl+z cat
a.txt

Display the Contents of Files:


 Syntax: cat filename
cat /path/filename
 Example:
o cat a.txt

AITM Polytechnic, Belagavi Dept Of Computer Science - 11 -


Operating Systems and Administration-20CS42P

rm (Remove File)

 Will remove (delete) the files


 Syntax: rm FileName
o rm path/Filename
 Example:
o rm a.txt // Assume a.txt is present in current directory
o rm polytechnic/civil/a.txt // Assume polytechnic/civil folder has file a.txt
o rm -i * // will delete all the files but interactively, asking user about confirmation of
deleting the files
o rm polytechnic/civil/* // will delete all files in polytechnic/civil folder

cp (Copy)

 Used to create a copy of a existing file.


 Copy an existing file in current directory to another file in the current directory.
 Syntax: cp file1 file2
 cp path1/file1 path2/file2 // creates copy of file1 in current directory or in specified directory
 cp a.txt b.txt (Make sure file a.txt exists in current directory)
o new file b.txt is created and contents of b.txt is same as a.txt
 In order to avoid accidental overwriting, -i option can be used. It will alter the user when file2 is
already exists.
o cp -i file1 file2 (to test, make sure both files exists)
 Use the recursive option to copy an entire subdirectory to a new subdirectory and then list both directories
to prove that it worked:
o cp -R subdir1 subdir4
 Copying a file from another location to the current directory and want its name to remain the same, user
can use the shorthand "." to indicate the current directory.
o cp polytechnic/civil/a.txt .
mv (Move)
 Used to rename the file (in other words, used to move the files from one location to another
location)
 It can be used to rename the Directory (Folder) also.
 Example 1:
o mv OldFileName NewFileName
 Example 2:
o mv OldFolderName NewFolderName

Pipes: The pipe command lets user sends the output of one command to another. Piping, as the term suggests, can
redirect the standard output, input, or error of one process to another for further processing. A traditional pipe is
“unnamed” and lasts only as long as the process.
A named pipe, however, can last as long as the system is up, beyond the life of the process. It can be deleted if no
longer used.

Named Pipe:
AITM Polytechnic, Belagavi Dept Of Computer Science - 12 -
Operating Systems and Administration-20CS42P

 In computing, a named pipe (also known as a FIFO) is one of the methods for inter- process
communication.
 It is an extension to the traditional pipe concept on Unix.
 Usually a named pipe appears as a file and generally processes attach to it for inter- process
communication.
 A FIFO file is a special kind of file on the local storage which allows two or more processes to
communicate with each other by reading/writing to/from this file.
 A FIFO special file is entered into the file system by calling mkfifo() in C.
 Once a FIFO special file is created in this way, any process can open it for reading or writing, in the same
way as an ordinary file. However, it has to be open at both ends simultaneously before it can proceed to do
any input or output operations on it.
 Creating a FIFO file: In order to create a FIFO file, a function calls i.e. mkfifo is used.
 Example: a.c and b.c communication

more
 Use the more command to read a file:
o more filename
 Press Space bar / Return Key to read the remaining parts of the file.
 Press q to quit viewing contents

less
 less is the opposite of more command
 Use the more command to read a file:
o less filename
 Searching can be done by typing /searchkey, will highlights the searched words
 Example: less test.c
o /printf

cmp (Compare)
 compare two files byte by byte
 Syntax: cmp -b file1 file2

File Permissions
 There are three users in linux namely owner (u), group(g), others(o).
o Note: For all users can be identified with letter a
 All three uses will have three possible permissions namely read(r), write(w), execute(e)
 Numbers assigned for permissions are read(4), write(2), execute(1)
 Command used to change permission of file is chmod
 Syntax: chmod filePermissionPattern filename
 Permission Table:
o Octal Table:

binary octal permissions binary octal permissions


AITM Polytechnic, 000
Belagavi 0 --- 100 4 Dept Of Computer
r-- Science - 13 -
001 1 --x 101 5 r-x
Operating Systems and Administration-20CS42P

Output:
-rw-rw-r-- 1 admincs admincs 805 Mar 28 13:42 b.c
drwx---------2 admincs admincs 4.0K Mar 28 09:12 OS
-rwxrw-r-- 1 admincs admincs 1.9M Mar 28 08:45 output.pdf
drwxrwxr-x 2 admincs admincs 4.0K Jan 23 2021 Scratch Projects
Explanation
First letter (-) indicates file (in this example b.c, output.pdf are files)
First letter (d) indicates item is directory
Next 9 characters indicates File Permissions for different uses
In this example, file output.pdf has permission r w x r w – r – – which indicates
o Owner has r w x Permissions, means file owner can read, write and execute the fileGroup users has r
w – Permissions, means Group users can only read and write the File contents
o Other users has r – – Permissions, means Other users can only read the file contents

Examples
 add execution permission to group and other users
o chmod go+x output.pdf
 remove write permissions to group users
o chmod g-w output.pdf
 Keep only read permission to all users
o chmod a=r output.pdf

 Adding execution to all users


o chmod a+x output.pdf
 giving only writing permission to other users (all settings may be not valid)
o chmod o=w output.pdf
 give user all permission, group users read and write, and other user only read permissions
o chmod u=rwx,g=rw,o=r output.pdf

Working with numbers in chmod


Syntax: chmod chmodnumber filename
Example 1: if user wants rwxrw-r-- format set number to 764
o 777 = rwxrwxrwx
o 765 = rwxrw-r-x
o 654 = rw-r-xr--
Example 2:
o chmod 777 output.pdf will set all permissions to all users
o chmod 765 output.pdf will set rwxrw-r-x to users
o chmod 654 output.pdf will set rw-r-xr-- to users

umask
 While creating a file or directory, by default a set of permissions are applied. These default
permissions are viewed by umask command.
 For safety reasons all Unix systems doesn't provide execution permission to newly created files.
o umask
Output: 0002

File Compression and Decompression:


Compression reduces the size of an application or document for storage or transmission. Compressed files are smaller,
download faster, and easier to transport. Decompression or expansion restores the document or application to its original

AITM Polytechnic, Belagavi Dept Of Computer Science - 14 -


Operating Systems and Administration-20CS42P

size.

Compressing files:

Syntax Description Example(s)

 Gzip compress the size of the given files gzip mydata.doc


using Lempel-Ziv coding (LZ77). gzip *.jpg
gzip {filename}  Whenever possible, each file is ls -l
replaced by one with the extension .gz.

 bzip2 compresses files using the


Burrows-Wheeler block sorting text
compression algorithm, and
Huffman coding.
 Compression is generally considerably bzip2 mydata.doc
better than that achieved by bzip bzip2 *.jpg
bzip2 {filename} command (LZ77/LZ78-based ls -l
compressors).
 Whenever possible, each file is
replaced by one with the extension
.bz2.

 zip is a compression and file packaging zip mydata.zip mydata.doc zip


zip {.zip-filename} {filename- utility for Unix/Linux. data.zip *.doc
to-compress}  Each file is stored in single .zip {.zip- ls -l
filename} file with the extension .zip.

 The GNU tar is archiving utility but it


can be use to compressing large
file(s). tar -zcvf data.tgz *.doc
 GNU tar supports both archive tar -zcvf pics.tar.gz *.jpg *.png tar -
compressing through gzip and bzip2. jcvf data.tbz2 *.doc
tar -zcvf {.tgz-file} {files}  If user have more than 2 files then it is ls -l
tar -jcvf {.tbz2-file} {files} recommended to use tar instead of gzip
or bzip2.
-z: use gzip compress
-j: use bzip2 compress

AITM Polytechnic, Belagavi Dept Of Computer Science - 15 -


Operating Systems and Administration-20CS42P

Syntax
Decompressing files Description Example(s)
 Decompressed a file that is
gzip -d {.gz file} created using gzip command. gzip -d mydata.doc.gz
gunzip {.gz file}  File is restored to their original gunzip mydata.doc.gz
form using this command.

 Decompressed a file that is


bzip2 -d {.bz2-file} created using bzip2 command. bzip2 -d mydata.doc.bz2
bunzip2 {.bz2-file}  File is restored to their original gunzip mydata.doc.bz2
form using this command.

Extract compressed files in a ZIP unzip file.zip


unzip {.zip file}
archive. unzip data.zip resume.doc
Untar or decompressed a file(s) that is tar -zxvf data.tgz
tar -zxvf {.tgz-file}
created using tar compressing through tar -zxvf pics.tar.gz *.jpg
tar -jxvf {.tbz2-file}
gzip and bzip2 filter tar -jxvf data.tbz2

List the contents of an archive/compressed file


Some time user just wanted to look at files inside an archive or compressed file. Then all of the above command supports
file list option.
Syntax Description Example(s)

gzip -l {.gz file} List files from a GZIP archive gzip -l mydata.doc.gz

unzip -l {.zip file} List files from a ZIP archive unzip -l mydata.zip

tar -ztvf {.tar.gz} tar List files from a TAR archive tar -ztvf pics.tar.gz tar
-jtvf {.tbz2} -jtvf data.tbz2

Important options in tar manual


 -c : create archive
 -x : extract
 -j : use bzip2 compress
 -z : use gzip compress
 -v : verbose mode
 -f : use archive file or device ARCHIVE

Text Processing Commands


Commands affecting text and text files

sort
 sort command is used to sort a file, arranging the records in a particular order.
 By default, the sort command sorts file assuming the contents are ASCII.
AITM Polytechnic, Belagavi Dept Of Computer Science - 16 -
Operating Systems and Administration-20CS42P

 Using options in the sort command can also be used to sort numerically.
Syntax:
sort [OPTIONS] filename
Options used:
 -o : us ed to save sorted content in specified file
o sort -o output.txt inputfile.txt
 r : sort the file contents in descending order
o sort -r inputfile.txt
 -n : if the file contains numbers, based on number values, file contents can be sorted.
o sort -n inputfile.txt
 -k : sort the file contents based on kth field values
o sort -k 2 inputfile.txt // sorts file contents based on values in 2nd field.
 -u : will remove duplicate entries in file while sorting.

uniq
 uniq filter removes duplicate lines from a sorted file. It is often seen in a pipe coupled
with sort.
 uniq -c filename
 removes the duplicates lines as well as displays frequency of duplicate lines.

cut
 A tool for extracting fields from files.
 Important options are -d, which specifies delimiter to separate fields, -f which indicates field numbers
 Example 1: cut -d” “ -f1-4,6,8 FileName
o here contains information separated by space. This command displays contents in fields 1,2,3,4
and 6 and 8
 Example 2: cut -d: -f1-4,6,8 FileName
o Same as above but the fields in file are seperated by : (colon)
join
 Join allows merging two files in a meaningful fashion, which essentially creates a simple
version of a relational database.
 The join command operates on exactly two files, but pastes together only those lines with a common tagged
field (usually a numerical label), and writes the result to stdout.
 The files to be joined should be sorted according to the tagged field for the matchups to work properly.
head and tail

 head/tail: output the first/last part of files (by Default 10 lines)


o head FileName
o tail FileName
 Example 1: head -5 a.txt will display first 5 lines of a.txt tail -5
a.txt will display last 5 lines of a.txt

grep
 A multi-purpose file search tool that uses Regular Expressions. (Global Regular
Expression Pring).
o grep pattern [FileName...]
 options used with grep are:

AITM Polytechnic, Belagavi Dept Of Computer Science - 17 -


Operating Systems and Administration-20CS42P

o The -i option causes a case-insensitive search.


o The -w option matches only whole words.
o The -r (recursive) option searches files in the current working directory and all subdirectories
below it.
o The -n option lists the matching lines, together with line numbers.
o The -c (--count) option gives a numerical count of matches.
 Example 1:
o To see every process on the system using BSD syntax:
o ps ax
o But search only word swap associated with the processes.
 Output:
o ps ax | grep swap
o 55 ? S 0:00 [kswapd0]
o 4206 pts/0 S+ 0:00 grep --color=auto swap

wc
 wc gives a "word count" on a file or I/O stream:
 Important Options Used:
o wc -l gives only the line count.
o wc -w gives only the word count.
o wc -c gives only the byte count.
 Syntax: wc -lwc FileName
 Example:
o wc -lwc temp2.txt
 Output:
o 3 6 18 temp2.txt
o Says there 3 Lines, 6 Wrods and 18 Characters altogher in a file temp2.txt
tr
 character translation filter.
 Example 1: tr "A-Z" "*" <filename
o will translate all the UPPER CASE characters to * in a file
 Example 2: tr "0-9" "A" <filename
o will translate all the DIGITS to ‘A’ in a file
 The -c "complement" option inverts the character set to match.
 Example 3: echo "acfdeb123" | tr -c b-d +
o +c+d+b++++
 Example 4: echo "abcd2ef1" | tr '[:alpha:]' -
o--------2--1
 same as echo "abcd2ef1" | tr 'a-zA-Z' -
o ----2-----1

fold
 A filter that wraps lines of input to a specified width. This is especially useful with the -s option, which
breaks lines at word spaces.
 Example 1: fold -w 3 FileName
o Will wrap each line for every three characters
 Example 2: fold - -s -w 3
o Will wait for the user to enter text, after return key, the text will be wraped to every three
AITM Polytechnic, Belagavi Dept Of Computer Science - 18 -
nl
Operating Systems and Administration-20CS42P

characters.
 Line numbering filter: nl filename lists filename to stdout, but inserts consecutive
numbers at the beginning of each non-blank line.
 If filename omitted, operates on stdin.

AITM Polytechnic, Belagavi Dept Of Computer Science - 19 -


Operating Systems and Administration-20CS42P

04 – Process Creation and Management

Process
 A process is any active (running) instance of a program. In other words, process is a
program in execution.
 A new process can be created by the fork() system call.
 The new process consists of a copy of the address space of the original process. fork() creates new
process from existing process.
 Existing process is called the parent process and the process is created newly is called
child process.

 Each process is given a unique process identification number (PID).


 PID is usually five-digit number.
o This number is used to manage each process.
o user can also use the process name to manage process.
A process executes in two ways they are,
Starting a Process
 Foreground Processes
 Background Processes

Foreground Processes
 Every process by default runs in Foreground (which means the output is printed on the
screen.) The best example for the foreground process is the ls command which prints the output on the
screen by listing the files and directories.
 When a program is running in the foreground, user cannot start another process without completing the
previous process. [ It is because of this reason foreground process is considered a time-consuming process.]
Background Processes
 When a process starts running and it is not visible on the screen it is called a background process.
 User can simultaneously run ‘n’ number of commands in the background process.
 To enable the background process, provide ampersand symbol (&) at the end of the command.
 Example : ls &

ps
The default output of ps is a simple list of the processes running in current terminal.
 Example: ps
PID TTY TIME CMD
23989 pts/0 00:00:00 bash
24148 pts/0 00:00:00 ps
 Every running process (-e) and a full listing (-f) could be obtained with these options.
Demonstrate fork() system call
AITM Polytechnic, Belagavi Dept Of Computer Science
- 20 -
Operating Systems and Administration-20CS42P

Example 1: else //Parent process


#include <unistd.h> {
#include <sys/types.h>
#include <errno.h> local = 10;
#include <stdio.h> global = 20;
#include <sys/wait.h>
#include <stdlib.h> printf("\n Parent process :: local = %d,
global %d\n", local, global);
int global; // A global variable }
}
int main(void) else // fork failed
{ {
pid_t childPID; printf("\n Fork failed, quitting!!!!!!\n");
int local = 0; return 1;
}
childPID = fork();
return 0;
if(childPID >= 0) // fork was successful }
{
if(childPID == 0) // child process
{
local++;
global++; Output:
printf("\n Child Process :: local = %d, Parent process :: local = 10, global 20
global %d\n", local, global);
} Child Process :: local = 1, global 1

AITM Polytechnic, Belagavi Dept Of Computer Science


- 21 -
Operating Systems and Administration-20CS42P

Demonstrate exec( ) system call


The exec() family of functions replaces the current process image with a new process image. exec command in
Linux is used to execute a command from the bash itself. This command does not create a new process it just
replaces the bash with the command to be executed. If the exec command is successful, it does not return to the
calling process.
exec > output.txt
 Redirect all output to the file output.txt for the current shell process.
 Redirections are a special case, and exec does not destroy the current shell process, but
bash will no longer print output to the screen, writing it to the file instead.
Example : (try this command in sudo bash)

$ exec > output.txt Output:


$ cal cat output.txt
$ echo i am trying command exec March 2022
$ date Su Mo Tu We Th Fr Sa 1
$ pwd 2 3 4 5
$ echo no commands display output on screen 6 7 8 9 10 11 12
$ echo enjoy exec command 13 14 15 16 17 18 19
$ exit 20 21 22 23 24 25 26
exit 27 28 29 30 31
i am trying command exec
Tue Mar 29 14:35:47 IST 2022
/home/admincs
no commands display output on screen enjoy
exec command

bg: background
 bg used to place foreground jobs in background. [used to send a process running in the foreground to
the background in the current shell.]
Syntax: bg [ job_spec]
 Place the jobs identified by each job_spec in the background, as if they has been started with ‘&’.
 Job_spec may be
o %n : Refer to job number n.
o %% or %+ : Refer to the current job.
%- : Refer to the previous job.
 Example: sleep 500 is a command which is used to create a dummy job which runs for 500 seconds.
o Use jobs command to list all jobs
o Create a process using sleep command, get job id as 1
o Put that process in background using id
 $ jobs // Display current jobs running and displayed nothing
 $ sleep 500 // Create one job, which sleeps for 500 seconds.
 ^z // Terminate the job by pressing Ctrl + z
o [1]+ Stopped sleep 500
 $ jobs // Display current jobs running and displayed one job stopped
o [1]+ Stopped sleep 500
 $ bg %1 // Refer the process by job number place it in background
o [1]+ sleep 500 &
 $ jobs // Display current jobs running and displayed one job which is running.

AITM Polytechnic, Belagavi Dept Of Computer Science


- 22 -
Operating Systems and Administration-20CS42P

o [1]+ Running sleep 500


fg: forground
 bg is a command that moves a background process on current Linux shell to the foreground.
Syntax: bg [ job_spec]
 Place the jobs identified by each job_spec in the foreground, making it the current job. Example: sleep
500 is a command which is used to create a dummy job which runs for 500 seconds.
 $ jobs
 $ sleep 500
 ^z
o [1]+ Stopped sleep 500
 $ jobs

o [1]+ Stopped sleep 500


 $ bg %1

o [1]+ sleep 500 &


 $ jobs

o [1]+ Running sleep 500


 $ fg %1 // Brings the background process (referred by its number) to foreground

 sleep 500 // waiting for the process to terminate.

nohup
 nohup, short for No Hang Up is a command in Linux systems that keep processes running even
after exiting the shell or terminal.
 nohup prevents the processes or jobs from receiving the SIGHUP (Signal Hang UP) signal.
 This is a signal that is sent to a process upon closing or exiting the terminal.
 Every command in Linux starts a process at the time of its execution, which automatically gets terminated
upon exiting the terminal.
 Suppose, user executing programs over SSH and if the connection drops, the session will be terminated, all
the executed processes will stop, and user may face a huge accidental crisis.
o In such cases, running commands in the background can be very helpful to the user and
this is where nohup command comes into the picture. Syntax: nohup command [command-
argument ...]
 nohup command can be used to run multiple commands in the background.
Syntax: nohup bash -c 'commands'

Example 1:
 nohup bash -c 'cal && ls'

AITM Polytechnic, Belagavi Dept Of Computer Science


- 23 -
Operating Systems and Administration-20CS42P

 Here, the output will be by default stored in nohup.out. To redirect it, type:
 nohup bash -c 'commands' > filename.txt

Stopping a process

kill

 kill is used to send a signal to a process. The most commonly used signal is "terminate"
(SIGTERM) or "kill" (SIGKILL). The full list can be shown with kill –L

o 1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL 5) SIGTRAP


o 6) SIGABRT 7) SIGBUS 8) SIGFPE 9) SIGKILL 10)
SIGUSR1
o 11) SIGSEGV 12) SIGUSR2 13) SIGPIPE 14) SIGALRM 15)
SIGTERM

 Signal number nine is SIGKILL. The default signal is 15, which is SIGTERM.
o Issue a command such as kill -9 20896.

pkill
 command used to send signal to kill process by process name.
 while issuing pkill, make sure that selected process name is the correct process to be stopped, verify
it by its full path by issuing pgrep –f processname.
 Syntax: pkill -f ProcessName
nice
 nice command in Linux helps in execution of a program/process with modified scheduling priority.
 It launches a process with a user-defined scheduling priority.
 The renice command allows user to change and modify the scheduling priority of an already running
process.
 Linux Kernel schedules the process and allocates CPU time accordingly for each of them.
 The kernel stores a great deal of information about processes including process priority which is simply
the scheduling priority attached to a process.
 Processes with a higher priority will be executed before those with a lower priority, while processes with
the same priority are scheduled one after the next, repeatedly.
 There are a total of 140 priorities and two distinct priority ranges implemented in Linux.
 The first one is a nice value (niceness) which ranges from -20 (highest priority value) to 19 (lowest
priority value) and the default is 0.
 The other is the real-time priority, which ranges from 1 to 99 by default, then 100 to 139

AITM Polytechnic, Belagavi Dept Of Computer Science


- 24 -
Operating Systems and Administration-20CS42P

are meant for user-space.


Check Nice Value of Linux Processes

ps -eo pid, ppid, ni, comm

 pid process-id, ppid parent process-id, ni niceness of process, comm command for that
process
 Alternatively, user can use top or htop utilities to view Linux processes nice values
 in htop command output, PRI – is the process’s actual priority, as seen by the Linux kernel.
 rt value in PRI fields, means the process is running under real-time scheduling priority

Syntax:
niceness-value [command args] OR
Change the Scheduling Priority of a Process
 Linux allows dynamic priority-based scheduling. Therefore, if a program is already
running, user can change its priority with the renice command in this form:
 $ renice -n -12 -p 1055 //Mention process with id (-p)
 $ renice -n -2 -u apache // Mention user process with
name (-u)
// -g for group process priority
 Output Verification:
o New priority for process 1055 is 20 – 12 = 8
o New priority for process apache is 20 – 2 = 18
Observe new priority by issuing top or htop command
$ sudo nice -n 5 tar -czf backup.tac

AITM Polytechnic, Belagavi Dept Of Computer Science


- 25 -
Operating Systems and Administration-20CS42P

top
 The top command has been around a long time and is very useful for viewing details of running
processes and quickly identifying issues such as memory hogs.
 Its default view is shown below.
Example 1: top
top - 11:56:28 up 1 day, 13:37, 1 user, load average: 0.09, 0.04, 0.03
Tasks: 292 total, 3 running, 225 sleeping, 0 stopped, 0 zombie
%Cpu(s): 0.1 us, 0.2 sy, 0.0 ni, 99.7 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem : 16387132 total, 10854648 free, 1859036 used, 3673448 buff/cache
KiB Swap: 0 total, 0 free, 0 used. 14176540 avail Mem

PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND


17270 alan 20 0 3930764 247288 98992 R 0.7 1.5 5:58.22 gnome-shell

cron
 The cron is a software utility, offered by a Linux-like operating system that automates the scheduled task at
a predetermined time.
 It is a daemon process, which runs as a background process and performs the specified operations at the
predefined time when a certain event or condition is triggered without the intervention of a user.
 Dealing with a repeated task frequently is an intimidating task for the system administrator and thus he
can schedule such processes to run automatically in the background at regular intervals of time by
creating a list of those commands using cron.
 It enables the users to execute the scheduled task on a regular basis unobtrusively like doing the
backup every day at midnight, scheduling updates on a weekly basis, synchronizing the files at some
regular interval.
 Cron checks for the scheduled job recurrently and when the scheduled time fields match the current time
fields, the scheduled commands are executed.
 It is started automatically from /etc/init.d on entering multi-user run levels.
Syntax: cron [-f] [-l] [-L loglevel]

Permitting users to run cron jobs:


 The user must be listed in this file to be able to run cron jobs if the file exists.
o /etc/cron.allow
 If the cron.allow file doesn’t exist but the cron.deny file exists, then a user must not be listed in this file
to be able to run the cron job.
o /etc/cron.deny

at
 at command is a command-line utility that is used to schedule a command to be executed at a
particular time in the future.
 Jobs created with at command are executed only once.
 The at command can be used to execute any program or mail at any time in the future.
 It executes commands at a particular time and accepts times of the form HH:MM to run a job at a specific
time of day.
 In the command, expression like noon, midnight, teatime, tomorrow, next week, next Monday, etc.
could be used with at command to schedule a job.

Syntax: at [OPTION...] runtime


Working with at command
 Command to list the user’s pending jobs:
o at -l
OR
AITM Polytechnic, Belagavi Dept Of Computer Science
- 26 -
Operating Systems and Administration-20CS42P

o atq
 Schedule a job for the coming Monday at a time twenty minutes later than the current time:

o at Monday +20 minutes


 Schedule a job to run at 1:45 Aug 12 2020:
o at 1:45 081220
 Schedule a job to run at 3pm four days from now:
o at 3pm + 4 days
 Schedule a job to shutdown the system at 4:30 today:
o echo "shutdown -h now" | at -m 4:30
 Schedule a job to run five hour from now:
o at now +5 hours

05 – Process Synchronization

Commands to exhibit thread concepts:


 Threads are a popular programming abstraction for parallel execution on modern
operating systems.
 When threads are forked inside a program for multiple flows of execution, these threads share certain
resources (e.g., memory address space, open files) among themselves to minimize forking overhead and
avoid expensive IPC (inter-process communication) channel.
 These properties make threads an efficient mechanism for concurrent execution.
 In Linux, threads (also called Lightweight Processes (LWP)) created within a program will have the same
"thread group ID" as the program's PID.
 Each thread will then have its own thread ID (TID).
 Threads are nothing more than standard processes which happen to share certain resources.
AITM Polytechnic, Belagavi Dept Of Computer Science
- 27 -
Operating Systems and Administration-20CS42P

 Classic command-line tools such as ps or top, which display process-level information by default, can be
instructed to display thread-level information.

Using the ps

The "-T" option for the ps command enables thread views.


ps -T -p <pid>
Example: List the threads for the mate-terminal process:
 First find the all process containing word terminal

 The "SPID" column represents thread IDs, and "CMD" column shows thread names.

Using the top


The top command can show a real-time view of individual threads.
To enable thread views in the top output, invoke top with "-H" option. This will list all Linux threads.
User can also toggle on or off thread view mode while top is running, by pressing 'H' key.

Example 1: top

To restrict the top output to a particular process and check all threads running inside the process:
Example 2: top -H -p 1572

AITM Polytechnic, Belagavi Dept Of Computer Science


- 28 -
Operating Systems and Administration-20CS42P

Using htop
A more user-friendly way to view threads per process is via htop, an ncurses-based interactive process viewer.
This program allows user to monitor individual threads in tree views.
To enable thread views in htop, launch htop, and press F2 to enter htop setup menu.
sChoose "Display option" under "Setup" column, and toggle on "Tree view" and "Show custom thread names"
options. Presss F10 to exit the setup.

AITM Polytechnic, Belagavi Dept Of Computer Science


- 29 -
Operating Systems and Administration-20CS42P

06 – Memory Management

Commands to view memory consumption:

1. free command

 Syntax: free –m
 Output:

total used free shared buffers cached


Mem: 7976 6459 1517 0 865 2248
-/+ buffers/cache: 3344 4631
Swap: 1951 0 1951
 The m option displays all data in MBs.
 The total os 7976 MB is the total amount of RAM installed on the system, that is 8GB.
 The used column shows the amount of RAM that has been used by linux, in this case around 6.4 GB. The
second line tells that 4.6 GB is free. This is the free memory in first line added with the buffers and cached
amount of memory (1517 + 865 + 2248 around 4631 MB= 4.6GB).

2. /proc/meminfo
 Check memory usage by reading the file at /proc/meminfo.
 Know that the /proc file system does not contain real files.
o They are rather virtual files that contain dynamic information about the kernel and the system.
Portion of output is
$ cat /proc/meminfo MemTotal:
8167848 kB
MemFree: 1409696 kB
Buffers: 961452 kB
Cached: 2347236 kB
SwapCached: 0 kB
SwapTotal: 1998844 kB
SwapFree: 1998844 kB

3. vmstat
 vmstat –s
 Portion of output is
8167848 K total memory
7449376 K used memory
718472 K free memory
1154464 K buffer memory
2422876 K swap cache
1998844 K total swap
0 K used swap
1998844 K free swap
The top command is generally used to check memory and cpu usage per process.
4. top command
 It also reports total memory usage and can be used to monitor the total RAM usage.
 The header on output has the required information.
 Check the KiB Mem and KiB Swap lines on the header. They indicate total, used and free amounts of the
AITM Polytechnic, Belagavi Dept Of Computer Science
- 30 -
Operating Systems and Administration-20CS42P

memory. The buffer and cache information is present here too.


 Portion of output is
top - 15:20:30 up 6:57, 5 users, load average: 0.64, 0.44, 0.33
Tasks: 265 total, 1 running, 263 sleeping, 0 stopped, 1 zombie
%Cpu(s): 7.8 us, 2.4 sy, 0.0 ni, 88.9 id, 0.9 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem: 8167848 total, 6642360 used, 1525488 free, 1026876 buffers
KiB Swap: 1998844 total, 0 used, 1998844 free, 2138148 cached
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
2986 enlighte 20 0 584m 42m 26m S 14.3 0.5 0:44.27 yakuake
1305 root 20 0 448m 68m 39m S 5.0 0.9 3:33.98 Xorg
7701 enlighte 20 0 424m 17m 10m S 4.0 0.2 0:00.12 kio_thumbnail

5. htop
Similar to the top command, the htop command also shows memory usage along with
various other details.
 The header on top shows cpu usage along with RAM and swap usage with the corresponding figures.
6. RAM Information
 To find out hardware information about the installed RAM, use the demidecode command.
 Portion of output is
sudo dmidecode –t 17 #
dmidecode 2.11
SMBIOS 2.4 present.
Handle 0x0015, DMI type 17, 27 bytes
Memory Device
Array Handle: 0x0014
Error Information Handle: Not Provided Total
Width: 64 bits
Data Width: 64 bits Size: 2048 MB
Form Factor: DIMM
Set: None
Locator: J1MY
Bank Locator: CHAN A DIMM 0 Type: DDR2 Type Detail: Synchronous
Speed: 667 MHz Manufacturer: 0xFF00000000000000 Serial Number: 0xFFFFFFFF
07 – Shell Programming

Script 1: Largest of three numbers using if-elif-fi


echo Enter Three Numbers read a b c
if (( $a>=$b && $a>=$c )) then
big=$a
elif(( $b>=$a && $b>=$c )) then
big=$b
elif(( $c>=$a && $c>=$b )) then
big=$c
fi
echo Biggest among $a $b $c is $big

Script 2: Check whether word is present in a sentence or not.


echo Enter Main String Separated by space read str
echo Enter Search String read
key
for word in $str do
AITM Polytechnic, Belagavi Dept Of Computer Science
- 31 -
Operating Systems and Administration-20CS42P

if [ $word == $key ] then


echo Element Present
exit
fi
done
echo Element NOT Present

Script 3: Count number of lines, words, and characters in given file.


echo Enter the filename
read file
c=`cat $file | wc -c`
w=`cat $file | wc -w`
l=`grep -c "." $file`
echo Number of characters in $file is $c echo
Number of words in $file is $w echo Number
of lines in $file is $l

AITM Polytechnic, Belagavi Dept Of Computer Science


- 32 -
Operating Systems and Administration-20CS42P

Script 4: Check the permissions of a given file and display the status
echo -e "Enter the name of the file : \c" read
filename
echo File Permission
`ls -l $filename`
if [ -r $filename ]
then
echo "$filename is readable" echo
else "$filename is not readable"
fi
if [ -w $filename ]
then
echo "$filename is writable" echo
else "$filename is not writable"
fi
if [ -x $filename ]
then
echo "$filename is executable" echo
else "$filename is not executable"
fi
if [ -s $filename ]
then
echo "$filename has size>0"
else
fi
echo "$filename has size= 0"

Script 5: Join two strings

string1="Linux"
string2="Hint"
echo "$string1$string2"
string3=$string1+$string2
string3+=" is a good tutorial blog site"
echo $string3

Output

AITM Polytechnic, Belagavi Dept Of Computer Science - 33 -


Operating Systems and Administration-20CS42P

08 – Automation of System Tasks

Illustrate automation of basic tasks like monitoring memory consumption, check


connectivity, etc., at different frequencies.
Sometimes, user may have tasks that need to be performed on a regular basis or at certain predefined intervals. Such
tasks include backing up databases, updating the system, performing periodic reboots and so on. Such tasks in linux
are referred to as cron jobs (Crontab). Cron jobs are used for automation of tasks that come in handy and help in
simplifying the execution of repetitive and sometimes everyday tasks.

Write a Shell Script to display a menu consisting of options to display disk space, the current users logged in,
total memory usage, etc.

$cat menu1
#!/bin/bash
#simple script menu

function diskspace
{
clear
df -k
}

function whoseon
{
clear
who
}

function memusage
{
clear
cat /proc/meminfo
}

function menu
{
clear
echo
echo -e "\t System admin menu\n"
echo -e "\t 1. Display disk space\n"
echo -e "\t 2. Display logged on users\n"
echo -e "\t 3. Display memory usage\n"
echo -e "\t 0. Exit program\n\n"
echo -en "\t Enter option : "
read -n 1 option
}

while [ 1 ]
do
menu
case $option in
0)
break ;;
1)
diskspace ;;
2)
whoseon ;;

AITM Polytechnic, Belagavi Dept Of Computer Science - 34 -


Operating Systems and Administration-20CS42P

3)
memusage ;;
*)

clear
echo "Sorry wrong selection";;
esac
echo -en "\n\t Hit any key to continue"

read -n 1 line
done
clear

OUTPUT

btps123@ubuntu:~$ ./14b.sh

System admin menu


1. Display disk space
2. Display logged on users
3. Display memory usage
0. Exit program

Enter option : 1

Filesystem 1K-blocks Used Available Use% Mounted on


/dev/sda6 110160452 10027748 94536868 10% /
tmpfs 1531032 0 1531032 0% /lib/init/rw
varlock 1531032 0 1531032 0% /var/lock
udev 1531032 148 1530884 1% /dev
lrm 1531032 2192 1528840 1% /lib/modules/2.6.2819-generic/volatile

Hit any key to continue


System admin menu
1. Display disk space
2. Display logged on users
3. Display memory usage
0. Exit program

AITM Polytechnic, Belagavi Dept Of Computer Science - 35 -


Operating Systems and Administration-20CS42P

09 – Network Management

Enable internet on Linux VM.


 Communicate between Guest and Host using ping command.
Host Virtual Box Manager Settings:
In Virtual Box Manager, Select “File”, → “Preferences” → Network → Host only
Networks (Tab).
Select icon for adding “New Host only Networks” (See vboxnet0 gets added”
Change the properties of vboxnet0
By default network CIDR is 10.0.2.0/24. Click OK.

ifconfig:
 ifconfig(interface configuration) is used to configure the kernel-resident network interfaces.
 It is used at the boot time to set up the interfaces as necessary.
 It is used to assign the IP address and netmask to an interface or to enable or disable a given interface.

Example 1: ifconfig -a
 -a : This option is used to display all the interfaces available, even if they are down.
 Output: ifconfig -a
enp1s0 Link encap:Ethernet HWaddr 94:c6:91:f6:37:56
inet addr:172.16.20.107 Bcast:172.16.20.255 Mask:255.255.255.0
inet6 addr: fe80::ef4b:1b1d:5c61:d9c6/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1

Example 2: ifconfig –s
 -s : Display a short list, instead of details.

iwconfig:
 iwconfig command in Linux is like ifconfig command, in the sense it works with kernel- resident network
interface but it is dedicated to wireless networking interfaces only.

 It is used to set the parameters of the network interface that are particular to the wireless operation like SSID,
frequency etc.
 iwconfig may also be used to display the parameters, and the wireless statistics which are extracted from
/proc/net/wireless.
 Usage Examples:

AITM Polytechnic, Belagavi Dept Of Computer Science - 36 -


Operating Systems and Administration-20CS42P

o nwid: This option sets the network ID, user may disable or enable the Network ID.
 iwconfig [Interface] nwid on/off
o mode: Set the operating mode of the device, which depends on the network topology. The modes can
be Ad-Hoc, Managed, Master, Repeater, Secondary, and Monitor.
 iwconfig [Interface] mode Managed
o commit: This option forces the card to apply all pending changes.
 iwconfig [Interface] commit

ethtool:
 ethtool utility is used to view and change the ethernet device parameters.
 Before executing the command first identify the name of the nic using command ifconfig. (Here it is, say enp1s0 )

Example 1: List Ethernet Device Properties


 When user executes ethtool command with a device name, it displays the following information about the
Ethernet device.
 ethtool enp1s0
Settings for enp1s0:
Supported ports: [ TP MII ]
Supported link modes: 10baseT/Half 10baseT/Full 100baseT/Half
100baseT/Full 1000baseT/Half 1000baseT/Full

arpwatch:
 arpwatch is an open source computer software program that helps user to monitor Ethernet traffic activity (like
Changing IP and MAC Addresses) on network and maintains a database of ethernet/ip address pairings.

Example 1: To watch a specific interface, type the following command with ‘-i’ and device name.
 arpwatch -i enp1s0
o So, whenever a new MAC is plugged or a particular IP is changing his MAC address on the
network, user can notice syslog entries at ‘/var/log/syslog‘ or ‘/var/log/message‘ file.

AITM Polytechnic, Belagavi Dept Of Computer Science - 37 -


Operating Systems and Administration-20CS42P

bmon:
 bmon is a simple yet powerful, text-based network monitoring and debugging tool for Unix-like systems, which
captures networking related statistics and displays them visually in a human friendly format.
 It is a reliable and effective real-time bandwidth monitor and rate estimator.
Example 1: bmon -p enp1s0
To view more detailed graphical statistics/information of bandwidth usage, press d key Press [Shift + ?] to
view the quick reference. To exit the interface, press [Shift + ?] again.

ssh:
 Secure Shell, sometimes referred to as Secure Socket Shell, is a protocol which allows user to connect securely to
a remote computer or a server by using a text-based interface.
 When a secure SSH connection is established, a shell session will be started and user will be able to manipulate
the server by typing commands within the client on local computer.
 Prerequisite:
o Install openssh-client and openssh-server through software manager
o assign valid ip address to two machines, for connection
o Assure, both the system have user account with password.
 Settings:

Machine 1: Machine 2:
name: admincsm1 admincsm2
login: admincsl1 admincsl2
192.100.100.1 192.100.100.2

Example 1: machine 1 is connecting machine 2


 ssh -l admincsl2 admincsm2@192.100.100.2 21
o Connect to the computer with name admincsm2 and ipaddress 192.100.100.2 to the port number 21
through login admincsl2
o It will ask for admin login password.
o The successful connection lead to remote machine terminal. User can issue any Linux commands to
handle remote machine locally.

telnet:

 The telnet command is used to create a remote connection with a system over a TCP/IP
network.
 It allows us to administrate other systems by the terminal.
 User can run a program to conduct administration.
 It uses a TELNET protocol. However, this protocol has some security defects, but it is one of the most used
networking protocols due to its simplicity.
 It is not a secure protocol because it transfers data in unencrypted form.
 Often Linux user prefers ssh over telnet because ssh transfers data in encrypted form.
 Prerequisite:
o Install telnet (client) and telnetd (server) through software manager
o assign valid ip address to two machines, for connection
o Assure, both the system have user account with password.
 Settings:

AITM Polytechnic, Belagavi Dept Of Computer Science - 38 -


Operating Systems and Administration-20CS42P

Machine 1: Machine 2:
name: admincsm1 admincsm2
login: admincsl1 admincsl2
192.100.100.1 192.100.100.2

Example 1: machine 1 is connecting machine 2


 telnet -l admincsl2 192.100.100.2 23
o Connect to the computer with ipaddress 192.100.100.2 to the port number 23 through login admincsl2
o It will ask for admin login password.
o The successful connection lead to remote machine terminal. User can issue any Linux commands
handle remote machine lcoally.

ftp:
 ftp is the user interface to the Internet standard File Transfer Protocol.
 The program allows a user to transfer files to and from a remote network site.
 Prerequisites:
 Install Ftp (FTP-Client) and vsftpd (FTP-Server) using Software Manager.
 Change /etc/vsftpd.conf file to enable write permission (other settings can be done according to
requirement.
◦ nano /etc/vsftpd.conf
 Search for # write_enable=YES (around 30th line in that file)
 Uncommnent and Save and Exit
 Restart the Service: systemctl restart vsftpd

Example 1: Get the connection to ftp


 ftp 192.100.100.2 21
 Enter user name and password for connection, this will take user to ftp utility and then issue ftp command sets
 For more command take the help by issuing ? and to quit utility type quit command.

curl:
 curl is a command-line tool to transfer data to or from a server, using any of the supported protocols (HTTP, FTP,
IMAP, POP3, SCP, SFTP, SMTP, TFTP, TELNET, LDAP, or FILE).
 curl is powered by Libcurl.
 This tool is preferred for automation since it is designed to work without user interaction.
 curl can transfer multiple files at once.

Example 1: Download the file from ftp Server


 -u option: indicates username and separated with password for that account (user authenticated FTP servers).
 -O option: will download file with same name as original file, here ftp://
192.100.100.2/a.txt downloaded with the same in current machine.
 -# option: indicates downloading progress bar.
 curl -# -u admincs:admincs -O ftp:// 192.100.100.2/a.txt
o Here user name and password are same (admincs)

wget:
 wget is the non-interactive network downloader which is used to download files from the
AITM Polytechnic, Belagavi Dept Of Computer Science - 39 -
Operating Systems and Administration-20CS42P

server even when the user has not logged on to the system and it can work in the background
without hindering the current process.
 Examples:
 wget --ftp-user=admincs --ftp-password=admincs ftp://
192.100.100.2/Desktop/test.c

rcp:
 The rcp command is used to copy files between different computers without starting an
FTP session or logging into the remote system explicitly.
 It uses ssh for data transfer, and uses the same authentication and provides the same security as ssh.
 To simply use the rcp command, just provide the source and destination to rcp command with a colon used to
separate the host and the data.
 Syntax Example:
o /* using rcp command to send a file from local host to remote host */
 rcp /mydirectory/a.txt admin:remotedir/kt.txt
o /* the example above is to send a file not to receive a file from remote host */

scp:
scp allows user to securely copy files and directories between two local/remote
machines.
 It uses ssh for data transfer, and uses the same authentication and provides the same security as ssh.
Working with scp:
Note: Assume, Source machine and Destination machine have same username, so identify different machine
user names with ipaddress.
Example 1: Copy file from local machine to remoter machine.
 scp index.html admincs@192.100.100.2:/home/admincs/test.html
 This will copy file index.html from current user to remote user admincs@192.100.100.2 and file will be copied
to remote user /home/admincs with the file name test.html
rsync:
 rsync is a fast and extraordinarily versatile file copying tool.
 It can copy locally, to/from another host over any remote shell, or to/from a remote rsync daemon.
 It offers a large number of options that control every aspect of its behavior and permit very flexible specification
of the set of files to be copied.
 It is famous for its delta-transfer algorithm, which reduces the amount of data sent over the network by sending
only the differences between the source files and the existing files in the destination.
 rsync is widely used for backups and mirroring and as an improved copy command for everyday use.

Example 1: synchronize files (copy file locally)


 rsync /home/admincs/Documents/temp.txt /home/admincs/Desktop/test.txt
This will copy file temp.txt at local/admincs/Documents to local machine desktop with new name
test.txt

sftp:
 sftp works on a client-server model. It is a subsystem of SSH and supports all SSH authentication mechanisms.
 To open an SFTP connection to a remote system, use the sftp command followed by the remote server username
and the IP address or domain name (then provide password for the user):
o sftp admincs@172.16.20.116

Example 1: Download the file from ftp server


 get a.txt /home/admincs/Desktop/test.txt
will download the file a.txt from ftp server to current user (local user /home/admincs/ Desktop
with file name test.txt
AITM Polytechnic, Belagavi Dept Of Computer Science - 40 -
Operating Systems and Administration-20CS42P

netstat:
 netstat is a command line utility for Linux that prints network connections, routing tables, interface statistics,
masquerade connections, and multicast memberships.
 netstat can be used to diagnose network issues and service problems.
 Important Options used:
o -a: all listening and non-listening ports, -t tcp ports
o -u udp ports, -l listening ports
o -s Statistics of ports, -r Kernel Routing Information

Example 1: netstat -at | head // To list all tcp ports.

ping:
 ping (Packet Internet Groper) command is used to check the network connectivity between host and server/host.
 This command takes as input the IP address or the URL and sends a data packet to the specified address with the
message “PING” and get a response from the server/host this time is recorded which is called latency.
 Fast ping low latency means faster connection.
 Ping uses ICMP(Internet Control Message Protocol) to send an ICMP echo message
to the specified host if that host is available then it sends ICMP reply message.
 Ping is generally measured in millisecond.
 Important options used:
o -c : Number of packets to be transfered
o -w : deadline, with in this seconds, continuesly send the packets ICMP Packets.
o -s : Packe size
Example 1: ping -c 5 -s 100 172.16.20.116
o Will send 5 ICMP Packets to test wheter machine 100.172.16.116 is alive or not, and each packet size
data is 100 bytes, total packet size is 108 (8 bytes of header).
Example 2: ping -w 5 172.16.20.115
o Will continuously send the ICMP packets within 5 seconds.

traceroute:
o traceroute command in Linux prints the route that a packet takes to reach the host.
o This command is useful when user want to know about the route and about all the hops that a packet takes.

Example 1: traceroute www.google.com


traceroute to www.google.com (142.250.195.196), 30 hops max, 60 byte packets 1
172.16.20.1 (172.16.20.1) 0.761 ms 1.407 ms 1.973 ms
2 172.16.1.100 (172.16.1.100) 0.137 ms 0.137 ms 0.140 ms
 The first column corresponds to the hop count. The second column represents the address of that hop and after
that, three space-separated time in milliseconds.

iftop:
 The iftop command listens to network traffic on a named network interface, or on the first interface, it can find
AITM Polytechnic, Belagavi Dept Of Computer Science - 41 -
Operating Systems and Administration-20CS42P

which looks like an external interface if none is specified, and displays a table of current bandwidth usage by pairs
of hosts.
 The iftop is a perfect tool for remote Linux server over an ssh based session.
 iftop must be run by the root or the user who has sufficient permissions to monitor all network traffic on the
network interface.

Example 1: iptop -f icmp -i enp1s0


 filters only icmp packets type ping command in some other terminal to see the effects.
 Example 3: iftop -F 192.168.1.0/24
 Display or analyses packet flowing in and out of the 192.168.1.0/24 network:

nload:
 nload is a Linux command-line tool used to monitor network traffic and bandwidth usage in real time, using
insightful graphs and traffic statistics.
 Output of nload is in paragraph, one for each device.
 A device is anything which sends and/or receives internet packets on the same network, but usually, it
represents a network interface device.
 It does not necessarily need to be a separate physical device, but can even be on the same machine!
 nload –m // -m for multiple devices.
 Output:
Device enp1s0 [172.16.20.107] (1/2):
Incoming: Outgoing:
Curr: 1.61 kBit/s Curr: 0.00 Bit/s
Avg: 1.82 kBit/s Avg: 456.00 Bit/s
Min: 0.00 Bit/s Min: 0.00 Bit/s
Max: 34.95 kBit/s Max: 15.07 kBit/s
Ttl: 76.13 MByte Ttl: 8.84 MByte

ss:
 The ss command is a tool used to dump socket statistics and displays information in
similar fashion (although simpler and faster) to netstat.
 The ss command can also display even more TCP and state information than most other tools.
 The ss command can also display even more TCP and state information than most other tools. Because ss is the
new netstat, the ss command-line utility can display stats for the likes of PACKET, TCP, UDP, DCCP, RAW, and
Unix domain sockets.

Example 1: ss -t

State Recv-Q Send-Q Local Address:Port Peer Address:Port


ESTAB 0 0 172.16.20.107:57818 142.250.182.46:https
ESTAB 0 0 172.16.20.107:48336 34.107.221.82:http
ESTAB 0 0 172.16.20.107:48334 34.107.221.82:http
ESTAB 0 0 172.16.20.107:49542 34.213.33.47:https

tcpdump:

 tcpdump is a packet sniffing and packet analyzing tool for a System Administrator to troubleshoot connectivity
issues in Linux.
 It is used to capture, filter, and analyze network traffic such as TCP/IP packets going through system.
 It is many times used as a security tool as well. It saves the captured information in a pcap file, these pcap files
AITM Polytechnic, Belagavi Dept Of Computer Science - 42 -
Operating Systems and Administration-20CS42P

can then be opened through Wireshark or through the command tool itself.
 tcpdump -i enp1s0
13:50:29.798343 IP 172.16.20.116 > 172.16.20.107: ICMP echo request, id 29434, seq 2, length 64
13:50:30.822415 IP 172.16.20.116 > 172.16.20.107: ICMP echo request, id 29434, seq 3, length 64
13:50:31.846476 IP 172.16.20.116 > 172.16.20.107: ICMP echo request, id 29434, seq 4, length 64
4 packets captured
4 packets received by filter 0
packets dropped by kernel

dstat:
 dstat is a tool that is used to retrieve information or statistics form components of the system such as network
connections, IO devices, or CPU, etc.
 It is generally used by system administrators to retrieve a handful of information about the above-mentioned
components of the system. It itself performs like vmstat, netstat, iostat, etc.
 By using this tool one can even see the throughput for block devices that make up a single filesystem or storage
system.
 dstat allows user to view all of system resources instantly, for example, user can compare disk usage in
combination with interrupts from IDE controller, or compare the network bandwidth numbers directly with the
disk throughput (in the same interval).

AITM Polytechnic, Belagavi Dept Of Computer Science - 43 -


Operating Systems and Administration-20CS42P

10 – User Authentication

Work on user accounts:

useradd

 useradd is a command in Linux that is used to add user accounts to system.


 It is just a symbolic link to adduser command in Linux and the difference between both of them is that useradd is a
native binary compiled with system whereas adduser is a Perl script which uses useradd binary in the background.
 When we run the ‘useradd‘ command in the Linux terminal, it performs the following major things:
o It edits /etc/passwd, /etc/shadow, /etc/group and /etc/gshadow files for the newly created user
accounts.
o Creates and populates a home directory for the new user.
o Sets permissions and ownerships to the home directory.
 Syntax:
o useradd -d /home/newusername –p passwordstring newusername
o This creates user with name newusername
 -d option will create directory for new user at /home directory
 -p option allows specifying password while creating user with the value given in place
of passwordstring
o User can change the password later by typing command
 passwd newusername
 This asks the user to enter the new password and confirm password

passwd
 The passwd command changes passwords for user accounts.
 A normal user may only change the password for their own account, while the superuser may change the
password for any account.
 passwd also changes the account or associated password validity period.
 Important options used along with passwd:
o -d, --delete Delete a user's password (make it empty). This is a quick way to disable a
password for an account. It will set the named account passwordless.
o -e, --expire Immediately expire an account's password. This in effect can force a user to
change his/her password at the user's next login.

userdel
 userdel command in Linux system is used to delete a user account and related files.
 This command basically modifies the system account files, deleting all the entries which refer to the username
LOGIN.
 It is a low-level utility for removing the users.
 Syntax: sudo userdel -f –r –Z neuser
 Important Options used along with userdel:
o -f: This option forces the removal of the specified user account. It doesn’t matter that the user is
still logged in. It also forces the userdel to remove the user’s home directory and mail spool, even
if another user is using the same home directory or even if the mail spool is not owned by the
specified user.
o -r: Whenever we are deleting a user using this option then the files in the user’s home directory
will be removed along with the home directory itself and the user’s mail spool. All the files
located in other file systems will have to be searched for and deleted manually.
o -Z : This option remove any SELinux(Security-Enhanced Linux) user mapping for the user’s
AITM Polytechnic, Belagavi Dept Of Computer Science
- 44 -
Operating Systems and Administration-20CS42P

login.

usermod

 usermod command or modify user is used to change the properties of a user.


 After creating a user we have to sometimes change their attributes like password or login directory etc.
 To change the home directory of a user
o usermod -d /home/newfolder existinguser
 To change the group of a user
o usermod -g newgroupname existinguser
 To change user login name
o usermod -l usernewname useroldname
 To lock a user
o usermod -L existinguser // -U for unlocking user
 To set an unencrypted password for the user
o usermod -p newpassword existinguser

Groups: Groups in Linux refer to the user groups. In Linux, there can be many users of a single system, (normal user
can take uid from 1000 to 60000, and one root user (uid 0) and 999 system users (uid 1 to 999)).

groupadd
 groupadd command is used to create a new user group.
 Syntax: groupadd newgroupname
 Every new group created is registered in the file “/etc/group“. To verify that the group has been created, enter the
command
o sudo tail /etc/group
 Adding user while creating newuser
o useradd -d /home/newusername –p passwordstring -g
existinggroupname newusername
 Adding existing user to group // Explained once again in next topic
o usermod -g existinggroupname existinguser

groupmod
 groupmod command is used to modify or change the existing group.
 It can be handled by superuser or root user.
 Basically, it modifies a group definition on the system by modifying the right entry in the database of the group.
 Syntax: groupmod [option] groupname
o Change the group name to new name.
o groupmod -n groupnewname groupoldname

gpasswd
 gpasswd command is used to administer the /etc/group and /etc/gshadow.
 gpasswd command assigns a user to a group with some security criteria.
 gpasswd command is called by a group administrator with a group name only which prompts for the new
password of the group.
 System administrators can use the -A option to define group administrator(s) and -M
option to define members.
 Syntax: gpasswd [option] group
 Important Options: Here only -A and -M options can be combined.
o -a, –add : This option is used to add a user to the named group.
o -d, –delete : It is used to remove a user from the named group.
AITM Polytechnic, Belagavi Dept Of Computer Science
- 45 -
Operating Systems and Administration-20CS42P

o -r, –remove-password : It is used to remove the password from the named group.
o -A, –administrators : Set the list of administrators for the group.
o -M, –members : set the list of members of the group.

Example:
 add the user to group:
o gpasswd -a existinguser existinggroup
 Deleting the created user from group geeks.
o gpasswd -d existinguser existinggroup

groupdel
 groupdel command is used to delete a existing group.
 It will delete all entry that refers to the group, modifies the system account files, and it is handled by superuser or
root user.
 Syntax: groupdel –f existinggroup
 Option used: -f –force: It used to delete a group even if it is the primary group of a user
LDAP server and client configuration:

Step 1: Installing and Configuring the LDAP Server


 Install the LDAP server and some associated utilities.
 sudo apt-get update
 sudo apt-get install slapd ldap-utils
 During the installation,
o Enter administrator password for LDAP.
o Install and fill the fields asked during configuration:
 sudo dpkg-reconfigure slapd
There are quite a few new questions to answer in this process. Accept most of the defaults. Questions are:
 Omit OpenLDAP server configuration? No
 DNS domain name?
 This option will determine the base structure of directory path. Read the message to understand
exactly how this will be implemented. This installation use example.com .
 Organization name?
 For this guide, use example as the name of organization. (user may choose anything which is
appropriate. )
 Administrator password? enter a secure password twice
 Database backend? MDB
 Remove the database when slapd is purged? No
 Move old database? Yes
 Allow LDAPv2 protocol? No
At this point, LDAP server is configured and running. Open up the LDAP port on firewall so external clients can
connect:
 sudo ufw allow ldap

Test LDAP connection with ldapwhoami, which should return the username as:
 ldapwhoami -H ldap:// -x
 Output
o anonymous
o anonymous is the result expected, since running ldapwhoami without logging in to the LDAP
server. This means the server is running and answering queries.
AITM Polytechnic, Belagavi Dept Of Computer Science
- 46 -
Operating Systems and Administration-20CS42P

AITM Polytechnic, Belagavi Dept Of Computer Science


- 47 -
Operating Systems and Administration-20CS42P
20CS42P
11 – System/Log Monitoring Commands and System Information/Maintenance Commands

top
 The top command has been around a long time and is very useful for viewing details of
running processes and quickly identifying issues such as memory hogs. Its default view is shown below.
top - 11:56:28 up 1 day, 13:37, 1 user, load average: 0.09, 0.04, 0.03
Tasks: 292 total, 3 running, 225 sleeping, 0 stopped, 0 zombie
%Cpu(s): 0.1 us, 0.2 sy, 0.0 ni, 99.7 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
 To make it easier to monitor required processes, user can call top and pass the PID(s) using the -p
option.
 top -p20881 -p20882 -p20895 -p20896
Tasks: 4 total, 0 running, 4 sleeping, 0 stopped, 0 zombie
%Cpu(s): 2.8 us, 1.3 sy, 0.0 ni, 95.9 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem : 16387132 total, 10856008 free, 1857648 used, 3673476 buff/cache
KiB Swap: 0 total, 0 free, 0 used. 14177928 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND

df
 The df command (short for disk free), is used to display information related to file systems about
total space and available space.
 Syntax : df [OPTION]... [FILE]...
 If no file name is given, it displays the space available on all currently mounted file systems.

Example 1: df
Portion of output:
Filesystem 1K-blocks Used Available Use% Mounted on udev
3996816 0 3996816 0% /dev
tmpfs 804624 10020 794604 2% /run
/dev/sda9 68117056 18036160 46597712 28% /

iostat
 The iostat command in Linux is used for monitoring system input/output statistics for
devices and partitions.
 It monitors system input/output by observing the time the devices are active in relation to their average
transfer rates.
 The iostat produce reports may be used to change the system configuration to raised balance the
input/output between the physical disks.
 Note: iostat is being included in sysstat package. If user doesn’t have it, user need to install first. (apt-
get install sysstat)

 Syntax: iostat
 Options used:
o -x: This command shows more details statistics information. iostat command gives I/O devices report
utilization as a result. So it’s possible to extend the statistic result for a diagnose in depth with the -x
option.
o -c: This command show only the CPU statistic. It is possible to show the statistic information and
report of our cpu with -c option.
o -d: This command displays only the device report. It is possible to only show the status of the device
utilization with the help of -d option. It will be going to list information for each connected device.
AITM Polytechnic, Belagavi Dept Of Computer Science
Operating Systems and Administration-20CS42P
20CS42P
o -k: This command captures the statistics in kilobytes or megabytes. By default, iostat measure the I/O
system with the bytes unit.
o -c 2 2: To show CPU only report with 2 seconds interval and 2 times reports.

free
 free command is used to view memory consumption
 free -m
total used free shared buffers cached
Mem: 7976 6459 1517 0 865 2248
-/+ buffers/cache: 3344 4631
Swap: 1951 0 1951
 The m option displays all data in MBs.
 The total os 7976 MB is the total amount of RAM installed on the system, that is 8GB.

cat /proc/cpuinfo
 The file /proc/cpuinfo displays what type of processor the user system is running
including the number of CPUs present.
 Portion of the Output: #
cat /proc/cpuinfo
processor 0
vendor_id : GenuineIntel
cpu family 6
model 45
model name : Intel(R) Xeon(R) CPU E5-2660 0 @ 2.20GHz

cat /proc/meminfo
 On Linux, user can use the command cat /proc/meminfo to determine how much memory the computer
has.
 This command displays the information stored in the meminfo file located in the /proc directory.
 The total amount of memory will be displayed as MemTotal
 Know that the /proc file system does not contain real files.
o They are rather virtual files that contain dynamic information about the kernel and the system.
 Portion of output is
$ cat /proc/meminfo MemTotal:
8167848 kB
MemFree: 1409696 kB
Buffers: 961452 kB
Cached: 2347236 kB

Work on log directory:


/var/log
 It is essential that user know where the log files are located, and what is contained in them. Such files
are usually in /var/log. Logging is controlled by the associated .conf file.
 Some log files are distribution specific and this directory can also contain applications such as samba,
apache, lighttpd, mail etc.
 Generated log files will be important for system administration and troubleshooting.
 Learn and understand the content of various log files, which will help user when there is a crisis and user
have to look though the log files to identify the issue.

System maintenance commands:


Shutdown
AITM Polytechnic, Belagavi Dept Of Computer Science
Operating Systems and Administration-20CS42P
20CS42P
 “Shutdown” refers to the process of stopping and shutting down a computer or server.
 This involves cutting the power to the main components of the system using a controlled process.
 Applications are closed, active processes and protocols are saved to the hard drive, device drivers are
removed, and user settings are saved in the process.
 Linux operating systems can easily be stopped, shut down, and restarted using the shutdown command
and its various options.
 Standard command for shutting down Linux
o shutdown –h
 Linux will shut down in under a minute. The “-h” option explicitly stands for the
shutting down or powering off of a system.
o shutdown
 User can usually produce the same results by just entering the shutdown command on
its own.
 Standard command for restarting Linux
o shutdown -r
 Linux will be restarted in under a minute.
 The “-r” option stands for reboot or restart.
 Command for shutting down Linux immediately
o shutdown -h 0 // time Specification 0
o shutdown now
 Another common command for shutting down Linux immediately:
 Command for restarting Linux immediately
o shutdown -r 0 // time Specification 0
o shutdown -r now

Rebooting

 Booting is starting a computer's operating system, so rebooting is to start it for a


second or third time.
 Rebooting is usually necessary after a computer crashes, meaning it stops working because of a
malfunction.
 sudo reboot
 sudo systemctl reboot
 sudo shutdown -r

halt
 This command in Linux is used to instruct the hardware to stop all the CPU
functions.
 Basically, it reboots or stops the system.
 halt [OPTION]
 Options used:
o -f, –force It does not invoke shutdown
o -w, –wtmp-only It will not call shutdown or the reboot system call but writes the shutdown
record to /var/log/wtmp file.
o -p, –poweroff To behave as poweroff
init
 init is parent of all Linux processes with PID or process ID of 1.
 It is the first process to start when a computer boots up and runs until the system shuts down.
 init stands for initialization.
 The role of init is to create processes from script stored in the file /etc/inittab which is a configuration
file which is to be used by initialization system.
AITM Polytechnic, Belagavi Dept Of Computer Science
Operating Systems and Administration-20CS42P
20CS42P

System update & repositories

Update the Repositories

 sudo apt-get update


 This command refreshes local list of software, making a note of any newer revisions and updates.
 If there’s a newer version of the kernel, the command will find it and mark it for download and
installation.

Run the upgrade

 sudo apt-get dist-upgrade


 The “dist-upgrade” switch asks Ubuntu to handle any dependencies intelligently.
o That is, if a particular software package is dependent on another software package to run, this
command will make sure that the second package is upgraded before upgrading the first one.
 This method is a safe way to upgrade Ubuntu Linux kernel.
 The kernel updates accessible through this utility have been tested and verified to work with version of
Ubuntu.

yum

 Yum and RPM are both package managers for Linux systems.
 Yum stands for Yellowdog Updater Modified. They are packaging managers for RPM- based Linux
systems.
 They are a high-level front end management package managers for Linux distributions that are RPM-based.
 It can sense and resolve dependencies.
 Yum can only install the packages available in its repository.
 Yum can also scan and upgrade the packages to the latest versions. It also entirely relies on online
repositories.
rpm
 RPM stands for Redhat Packaging Manager.
 It can be considered one of the oldest packaging managers that do basic functions like uninstalling,
updating, archiving the packages received by the Linux systems.
 It cannot sense and resolve dependencies on its own.
 It can install multiple packages with the condition that we give the correct file name with the .rpm
extension.
 RPM does not depend on online repositories for any of its services and it cannot scan or upgrade itself or
its packages to the latest versions.

AITM Polytechnic, Belagavi Dept Of Computer Science


Operating Systems and Administration-20CS42P
20CS42P
12 – Domain Name Service (DNS)

Domain Name Service (DNS)

 DNS is an Internet service that maps IP addresses and fully qualified domain names
Page | 52 (FQDN) to one another.
o In this way, DNS alleviates the need to remember IP addresses.
 Computers that run DNS are called name servers.
 Ubuntu ships with BIND (Berkley Internet Naming Daemon), the most common program used for
maintaining a name server on Linux.
o BIND is the most common program used for maintaining a name server on Linux.
Prerequisites: Install the following packages.
Installation
 sudo apt-get update
 sudo apt-get install bind9
 sudo apt-get update
 sudo apt-get install dnsutils
 sudo apt-get update

Configuration
There are many ways to configure BIND9. Some of the most common configurations are a
caching nameserver, primary server, and secondary server.
 When configured as a caching nameserver BIND9 will find the answer to name queries and remember the
answer when the domain is queried again.
 As a primary server, BIND9 reads the data for a zone from a file on its host and is authoritative for
that zone.
 As a secondary server, BIND9 gets the zone data from another nameserver that is authoritative for
the zone.

Caching Nameserver

The default configuration acts as a caching server. Simply uncomment and edit
/etc/bind/named.conf.options to set the IP addresses of ISP’s DNS servers:

nano /etc/bind/named.conf.options

Before After

// forwarders { forwarders {
// 1.2.3.4; 192.168.1.10;

// 5.6.7.8; };

// };

Assume:
Name Server: 192.168.1.10

Primary Server: 192.168.1.10

AITM Polytechnic, Belagavi Dept Of Computer Science


Operating Systems and Administration-20CS42P
20CS42P
Secondary Server 192.168.1.11
To enable the new configuration, restart the DNS server. From a terminal prompt:
sudo systemctl restart bind9.service

Primary Server
In this section BIND9 will be configured as the Primary server for the domain example.com. Simply replace
Page | 53
example.com with FQDN (Fully Qualified Domain Name).
Forward Zone File
To add a DNS zone to BIND9, turning BIND9 into a Primary server, first edit
/etc/bind/named.conf.local:

nano /etc/bind/named.conf.local
zone "example.com" {
type master;
file "/etc/bind/db.example.com";
};

Secondary Server Settings


Once a Primary Server has been configured a Secondary Server is highly recommended in order to maintain the
availability of the domain should the Primary become unavailable.
First, on the Primary server, the zone transfer needs to be allowed. Add the allow-transfer option to the example
Forward and Reverse zone definitions in /etc/bind/named.conf.local:

nano /etc/bind/named.conf.local
zone "example.com" {
type master;
file "/etc/bind/db.example.com";
allow-transfer { 192.168.1.11; };
};
zone "1.168.192.in-addr.arpa" {
type master;
file "/etc/bind/db.192";
allow-transfer { 192.168.1.11; };
};

AITM Polytechnic, Belagavi Dept Of Computer Science


Operating Systems and Administration-20CS42P
20CS42P

Common Record Types used during DNS Setups:


This section covers some of the most common DNS record types.
 A record: This record maps an IP Address to a hostname. www
IN A 192.168.1.12
Page | 54  CNAME record: Used to create an alias to an existing A record. User cannot create a CNAME record
pointing to another CNAME record.
Web IN CNAME www
 MX record: Used to define where email should be sent to. Must point to an A record, not a CNAME.
@ IN MX 1 mail.example.com. mail
IN A 192.168.1.13
 NS record: Used to define which servers serve copies of a zone. It must point to an A record, not a
CNAME. This is where Primary and Secondary servers are defined.
@ IN NS ns.example.com. @
IN NS ns2.example.com. ns
IN A 192.168.1.10
ns2 IN A 192.168.1.11

FTP server on LINUX and transfer files to demonstrate its working.

ftp

ftp is the user interface to the Internet standard File Transfer Protocol. The program allows a
user to transfer files to and from a remote network site.

Examples below are to upload and download files using ftp service.
Prerequisites:
 Install Ftp (FTP-Client) and vsftpd (FTP-Server) using Software Manager.
 Change /etc/vsftpd.conf file to enable write permission (other settings can be done according to
requirement.
◦ nano /etc/vsftpd.conf
 Search for # write_enable=YES (around 30th line in that file)
 Uncommnent and Save and Exit
 Restart the Service: systemctl restart vsftpd

Example 1: Get the connection to ftp


 ftp 172.16.20.116 21
o Enter user name and password for connection, this will take user to ftp utility and then issue ftp
set of command
o For more command take the help by issuing? And to quit utility type quit
command.

Install and configure Apache web server and create virtual hosts:
 The Apache web server is the most popular way of serving web content on the internet.
 It accounts for more than half of all active websites on the internet and is extremely powerful and
flexible.
 Apache breaks its functionality and components into individual units that can be customized and
configured independently. The basic unit that describes an individual site or domain is called a virtual host.
 These designations allow the administrator to use one server to host multiple domains or sites off of a
single interface or IP by using a matching mechanism. This is relevant to anyone looking to host more than

AITM Polytechnic, Belagavi Dept Of Computer Science


Operating Systems and Administration-20CS42P
20CS42P
one site off of a single server.
 Each domain that is configured will direct the visitor to a specific directory holding that site’s information,
never indicating that the same server is also responsible for other sites.
 This scheme is expandable without any software limit as long as server can handle the load.

Prerequisites:
Page | 55  For all commands use sudo bash
 Install apache Server: apt-get install apache2

Step 1: Creating the Directory Structure

o Make a directory structure that will hold the site data that we will be serving to visitors.
o Document root (the top-level directory that Apache looks at to find content to serve) will be set to
individual directories under the /var/www directory.
o Create a directory here for virtual host.
o Within this directory, create a public_html folder that will hold our actual files. This gives some flexibility
in hosting.
o mkdir -p /var/www/example.com/public_html
Step 2: Granting Permissions
 Directory structure for host files owned by our root user. If user want regular user to be able to modify
files in web directories, can change the ownership by doing this:
 chown -R $USER:$USER /var/www/example.com/public_html
 By doing this, regular user now owns the public_html subdirectories where we will be storing our
content.
 Also modify permissions a little bit to ensure that read access is permitted to the general web directory
and all of the files and folders it contains so that pages can be served correctly:
 chmod -R 755 /var/www

AITM Polytechnic, Belagavi Dept Of Computer Science


Operating Systems and Administration-20CS42P
20CS42P
Step 3: Creating Demo Pages for Each Virtual Host
 Once directory structure in place then create some content to serve.
 Design a very simple Web page.
 nano /var/www/example.com/public_html/index.html
o In this file, create a simple HTML document that indicates the site it is connected to. The file
looks like this:
Page | 56 o Save and close the file when finished.

<html>
<head>
<title>Welcome to Apache Server Experiment</title>
</head>
<body>
<h1>Success! The virtual host is working Fine!</h1>
</body>
</html>

Step 4: Creating New Virtual Host Files


 Virtual host files are the files that specify the actual configuration of our virtual hosts and dictate how the
Apache web server will respond to various domain requests.
 Apache comes with a default virtual host file called 000-default.conf that can used as a jumping off
point.
 Now copy it over to create a virtual host file for domains.
 The default Ubuntu configuration requires that each virtual host file end in .conf.

Creating the First Virtual Host File


 Start by copying the file for the domain:
sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/example.com.conf
 The file will look something like this (Comments have been removed here to make the file more
approachable): After removing the comments the file contents look like (First Column).

AITM Polytechnic, Belagavi Dept Of Computer Science


Operating Systems and Administration-20CS42P
20CS42P

/etc/apache2/sites-available/example.com.conf

Before After
<VirtualHost *:80> <VirtualHost *:80>
ServerAdmin webmaster@localhost ServerAdmin admin@example.com
Page | 57 DocumentRoot /var/www/html ServerName example.com ServerAlias
ErrorLog ${APACHE_LOG_DIR}/error.log www.example.com DocumentRoot
CustomLog /var/www/example.com/public_html ErrorLog $
${APACHE_LOG_DIR}/access.log combined {APACHE_LOG_DIR}/error.log CustomLog
</VirtualHost> ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

 Add few contents to the above file. And also make few changes to the file such that it finally looks
like (Second Column):
 Save and close the file.

Step 5: Enabling the New Virtual Host Files


 Now virtual host files have been created, Enable them. Apache includes some tools that allow us to do
this.
 Ue the a2ensite tool to enable each of our sites like this:
o sudo a2ensite example.com.conf
 Next, disable the default site defined in 000-default.conf:
o sudo a2dissite 000-default.conf
 When above task finished, Restart Apache to make these changes take effect:
o sudo systemctl restart apache2

Step 6: Setting Up Local Hosts File (Optional)


 If admin have not been using actual domain names to test this procedure and have been using some
example domains instead, admin can at least test the functionality of this process by temporarily
modifying the hosts file on local computer.
 edit local file with administrative privileges by typing:
o sudo nano /etc/hosts
Testing working of Web Server
 Go to the other machines /etc/hosts and do the above settings (step 6) like
o 172.16.20.107 example.com
 Try http://example.com, this will display the web page of virtual host.

AITM Polytechnic, Belagavi Dept Of Computer Science


Operating Systems and Administration-20CS42P

13 – Storage Management

Basic Commands for Storage Partitions

fdisk

 fdisk is used to check the partitions on a disk.


 The fdisk command can display the partitions and details like file system type.
 However it does not report the size of each partitions.
o sudofdisk -l
 Each device is reported separately with details about size, seconds, id and individual partitions.
 sfdisk utility purpose similar to fdisk, but with more features.
It can display the size of each partition in MB.
sfdisk

cfdisk
o sudo sfdisk -l -uM

 cfdisk is a linux partition editor with an interactive user interface based on ncurses.
 It can be used to list out the existing partitions as well as create or modify them.
 An example of how to use cfdisk to list the partitions.

 cfdisk works with one partition at a time.


o So if user need to see the details of a particular disk, then pass the device name to cfdisk.
o sudo cfdisk /dev/sdb

parted

 parted utility is to list out partitions and modify them if needed.


o sudo parted –l
df

 df is not a partitioning utility, but prints out details about only mounted file systems.
 The list generated by df even includes file systems that are not real disk partitions.
o df -h
o df -h | grep ^/dev
 Note:df shows only the mounted file systems or partitions and not all.

AITM Polytechnic, Belagavi Dept Of Computer Science- 58 -


Operating Systems and Administration-20CS42P

pydf

 pydf is an improved version of df, written in python.


 Prints out all the hard disk partitions in a easy to read manner.
o pydf
 pydf is limited to showing only the mounted file systems.

lsblk

 Lists out all the storage blocks, which includes disk partitions and optical drives.
 Details include the total size of the partition/block and the mount point if any.
 Does not report the used/free disk space on the partitions.
o lsblk
 If there is no MOUNTPOINT, then it means that the file system is not yet mounted. For cd/dvd this
means that there is no disk.
 lsblk is capable of displaying more information about each device like the label and model. Check out the
man page for more information
 Display UUID and Model of device
o The "-o" option can be used to specify the columns to display.
 lsblk -o PATH,SIZE,RO,TYPE,MOUNTPOINT,UUID,MODEL
o The above output has all the necessary information about all the storage devices present on the
system or connected via usb.
o This is the best command to see all information about storage devices together in one place.
 Prints the block device (partitions and storage media) attributes like uuid and file system type. Does not
report the space on the partitions.
blkid
o sudo blkid

hwinfo
 The hwinfo is a general purpose hardware information tool and can be used to print out
the disk and partition list.
 The output however does not print details about each partition like the above commands.
o hwinfo --block –short

inxi

 inxi command display information about various hardware components present on the system.
 To display information about the disk drives and storage devices use the "-D" option with inxi.
o inxi -D -xx

Logical Volume Management (LVM)


 LVM, or Logical Volume Management, is a storage device management technology that
gives users the power to pool and abstract the physical layout of component storage devices for easier and
flexible administration.
 The main advantages of LVM are increased abstraction, flexibility, and control.
 Logical volumes can have meaningful names like “databases” or “root-backup”.
 Volumes can be resized dynamically as space requirements change and migrated between physical devices

AITM Polytechnic, Belagavi Dept Of Computer Science- 59 -


Operating Systems and Administration-20CS42P

within the pool on a running system or exported easily.


 LVM also offers advanced features like snapshotting, striping, and mirroring.

LVM Storage Management Structures


LVM functions by layering abstractions on top of physical storage devices. The basic layers that
LVM uses, starting with the most primitive, are.
 Physical Volumes:
o LVM utility prefix: pv...
o Description: Physical block devices or other disk-like devices (for example, other devices created
by device mapper, like RAID arrays) are used by LVM as the raw building material for higher
levels of abstraction. Physical volumes are regular storage devices. LVM writes a header to the
device to allocate it for management.
 Volume Groups:
o LVM utility prefix: vg...
o Description: LVM combines physical volumes into storage pools known as volume groups.
Volume groups abstract the characteristics of the underlying devices and function as a unified
logical device with combined storage capacity of the component physical volumes.
 Logical Volumes:
o LVM utility prefix: lv... (generic LVM utilities might begin with lvm...)
o Description: A volume group can be sliced up into any number of logical volumes. Logical
volumes are functionally equivalent to partitions on a physical disk, but with much more
flexibility. Logical volumes are the primary component that users and applications will interact
with.
Each volume within a volume group is segmented into small, fixed-size chunks called extents. The size of
the extents is determined by the volume group (all volumes within the group conform to the same extent
size).

AITM Polytechnic, Belagavi Dept Of Computer Science- 60 -


Operating Systems and Administration-20CS42P
Format and Mount the Logical Volumes
 Now user can use logical volumes as normal block devices.
 The logical devices are available within the /dev directory just like other storage devices.
 User can access them in two places:
/dev/volume_group_name/logical_volume_name
[OR /dev/mapper/volume_group_name-logical_volume_name ]

 So to format our four logical volumes with the Ext4 filesystem, user can type:
1. sudo mkfs.ext4 /dev/LVMVolGroup/projects
2. sudo mkfs.ext4 /dev/LVMVolGroup/www
3. sudo mkfs.ext4 /dev/LVMVolGroup/db
4. sudo mkfs.ext4 /dev/LVMVolGroup/workspace
]
 After formatting, user can create mount points:
sudomkdir /mnt/projects sudomkdir -p
/mnt/www sudomkdir -p /mnt/db
sudomkdir -p /mnt/workspace

 Then mount the logical volumes to the appropriate location using commands:

1. sudomount /dev/LVMVolGroup/projects /mnt/projects


2. sudomount /dev/LVMVolGroup/www /mnt/www

3. sudomount /dev/LVMVolGroup/db /mnt/db

4. sudomount /dev/LVMVolGroup/workspace /mnt/workspace


 To make the mounts persistent, add them to /etc/fstab just like user would with normal block devices:
sudonano /etc/fstab
Editor will be opened for /etc/fstab. Add the below entries at the end of the file, save and exit.
/dev/LVMVolGroup/projects /mnt/projects ext4 defaults, nofail 0 0
/dev/LVMVolGroup/www /mnt/www ext4 defaults, nofail 0 0
/dev/LVMVolGroup/db /mnt/db ext4 defaults, nofail 0 0
/dev/LVMVolGroup/workspace /mnt/workspace ext4 defaults, nofail 0 0

 The operating system should now mount the LVM logical volumes automatically at boot.

Resizing a Physical Volume


 If user wants to change the size of an underlying block device for any reason, can use the
pv resize command to update LVM with the new size.
 User can execute this command while LVM is using the physical volume
61
Page

AITM Polytechnic, Belagavi Department of Computer Science and


Engineering

You might also like