SlideShare a Scribd company logo
Linux Commands
• A command is a program which interacts with the kernel to
provide the environment and perform the functions called for by
the user.
• A command can be: a built-in shell command; an executable
shell file, known as a shell script; or a source compiled, object
code file.
• The shell is a command line interpreter. The user interacts with
the kernel through the shell. You can write ASCII (text) scripts to
be acted upon by a shell.
• When the shell starts up it reads its startup files and may set
environment variables, command search paths, and command
aliases, and executes any commands specified in these files.
• Every Unix platform will either have the Bourne shell, or a Bourne
compatible shell available.
• The default prompt for the Bourne shell is $ (or #, for the root user).
• Another popular shell is C Shell. The default prompt
for the C shell is %.
Shell programming
• You can write shell programs by creating scripts containing a series of shell
commands.
• The first line of the script should start with #! Which indicates to the kernel
that the script is directly executable.
• The first line is followed by commands
• Within the scripts # indicates a comment from that point until
the end of the line, with #! being a special case if found as the
first characters of the file.
#!/bin/bash
cd /tmp
mkdir t
• Command Syntax
Command <options> <Arguments>
Multiple commands are separated by ; can be executed one after the
another
We can get help for command using “command --help” or “man
command”
• Pipes
An important early development in Unix was the invention of "pipes,"
a way to pass the output of one tool to the input of another.
eg. $ who | wc −l
By combining these two tools, giving the wc command the output
of who, you can build a new command to list the number of users
currently on the system
Basic commands
• Cal
• Print the calander
• Date
• Print the current date
• Who
• Print the details of the users who logged into the system
• Clear
• Clear the screen
• Free
• Print the ram details
• whoami
• Which
• Print the location of the command file
• Ex: which <command>
• Pwd
• Print present working directory
• sleep
Linux File management and viewing
• cd
• Change directory. With no arguments “cd” changes to user home directory.
• Ls
• List the contents of the directory
• Ex: ls , ls –l, ls –al, ls –ld, ls –R
• Mkdir
• Make a directory
• Ex: mkdir –p <directory name>
• Cp
• Copy files or folder to another location
• Ex: cp –r <file/directory> <destination>
• Mv
• Move or rename a file or directory
• Ex: mv <source> <destination>
• Touch
• Create an empty file
• We can also change the timestamp for existing file
• Ex: touch <file>
• Vi Editor
• Used to edit the files
• We can do find and replace , search like normal text editors.
• Head
• Print first ten lines in a file
• Print all lines except first lines (-n-1)
• Ex: head –n <10> filename
• Cat
• View the content of a file
• Ex: cat filename
• Echo
• Display the text with new line
• Ex: Echo “content”
• Printf
• Display the text without new line
• Ec: printf “content”
• Df
• Show the disk free space for one or more file systems
• Ex: df –h , df –g
• Du
• Show how much disk space a directory or file is used
• Ex: du –sh <directory/file>
• Grep
• Search content in a file
• Ex: grep <string> filename
• Less , more
• View the content of file
• Wc
• Count the number of lies or characters or words
• Find
• Search for a file in a location
• Find <location> -name <name of file>
• Rm
• Remove a file or directory
• Rmdir
• Remove empty directory
• Locate
• Locate a file or directory
• This gets the information from system without actually searching
• Chown
• Change the ownership of group or folder
• Ex:- chown user:group file/folder
File Access Rights
• Types of Users: CHOWN
• Owner
• Group
• All/Other
• Types of Permissions: CHMOD
• Read
• Write
• Execute
• Types of Files
• Directories
• Other files
Table 8.1 Summary of File Permissions in LINUX
Directory Permissions
• read = list files in the directory
• write = add new files to the directory
• execute = access files in the directory
Table 8.2 Permission Values
Table 8.3 Permissions for Access to courses, labs, and temp
Changing the Access Rights
Purpose – to set/change permissions in files
• chmod [options] octal-mode filelist
• chmod [options] symbolic-mode filelist
Options
• -R recursively process subdirectories
Table 8.4 Values for Symbolic Mode Components
Table 8.5 Examples of the chmod Commands and Their Purposes
Table 8.5 Examples of the chmod Commands and Their Purposes
Linux File compression and archiving
• Tar
• Used to archive multiple files
• Zip
• Compress the files
• Ex: zip <file name>
• Unzip
• Un-compress file files
• Ex: unzip <filename.(zip)>
• Gzip
• Compress the files
• Ex: gzip <filename>
• Gunzip
• Uncompress the files
• Ex: gunzip <filename.gzip>
Network Management commands
• Hostname
• Show or set the name for the host
• Ex: hostname -I
• Ifconfig
• Configure or show the network interface
• Ex: ifconfig -a
• Ifdown
• Shutdown a network service
• Ex: ifdown <ethernet card>
• Ifup
• Brings up a network service
• Ex: ifup <ethernet card>
• Nslookup
• Query dns server for information about hosts
• Ex: nslookup <ipaddress/hostname>
• Ping
• Send ICMP ECHO REQUEST packets to network hosts
• Ex: ping <remote-hosts/ip>
• Nfs
• Show the information about the NFS server
• Ssh
• Secure login (ssh remote host)
• Ex: ssh user@ipaddress/hostname
• Scp
• Secure copy
• Scp –r <file/directory> user@destip:<destination location>
• telnet
• Test the port opened or not
• Ex: telnet <ipaddress> <port>
• Mail/send mail/mailx
• Send mail to email address
• Mailx <email>
• Su
• Switch user
• Awk
• Print column of a file
• Awk ‘{print $1}’ filename
• Cut
• Process the files and words
• Ex: cut –d ‘,’ –f1
• Ln – softlink
• Ex: ln –s <file> link
System Management Commands
Runtime level management
• exit Terminates the shell.
• halt Stop the system.
• init Process control initialization. (init 3)
• logout Log the user off the system.
• poweroff Brings the system down.
• reboot Reboot the system.
• runlevel List the current and previous runlevel.
Group and user management
• Groupadd
• Create a group
• Useradd
• Create user
• Useradd <username>
• Passwd
• Assign password to a user
• Passwd <username>
• Userdel
• Delete the user
• Userdel <username>
• Groupdel
• Remove a group same as userdel
• /etc/passwd
• /etc/group
• /etc/hosts
• Systemctl , service
• Manage the services in Linux
• Repository management
• Yum repository in redhat/centos/fedora
Process management
• Ps
• Print the processes
• Ex: ps –ef | grep postgres
• Top
• Like task manager in Windows. List the processes running.
• Kill pid
• Stop the process
• Ex: kill -9 pid
• Killall process – named process
• Ex: Killall --user postgres
• Ex: Killall --process-group postgres
• Bg – list stopped or background jobs, resume a stopped job in the
background
• Ex: bg
• Fg brings the most recent job to foreground
• Ex: fg
• Fg n – brings a job number to foreground
• Ex: fg pid
• Nohup , & run a process in background
• Ex: nohup sh script &
Redirecting output
• >
• Redirect output to file
• Ex: cmd > file
• >>
• Append output to file
• Ex: cmd >> file
• <
• Take input from file
• Ex: cmd < file
• |
• Pipe output from previous command
• Cmd1 | cmd2
Environment variables
• What is computing environment ?
• The Computing environment is the Platform(Platform = Operating System+
Processor) where a user can run programs.
• What is variable ?
• In computer science, a variable is a location for storing a value which can be
a filename, text, number or any other data. It is usually referred to with its
Symbolic name which is given to it while creation. The value thus stored can be
displayed, deleted, edited and re-saved.
• What are Environment variables?
• Environment variables are dynamic values which affect the processes or programs
on a computer. They exist in every operating system, but types may vary.
Environment variables can be created, edited, saved, and deleted and give
information about the system behavior.
• Example of environment variables
• PATH: This variable contains a colon (:)-separated list of directories in which
your system looks for executable files.
• USER: The username
• HOME: Default users home directory
• SHELL: Default shell for user
• Access variables
• Echo $VARIABLE
• Env command list all the environment variables.
• Setting variables
• Export VARIALBE=VALUE (This is for session)
• .bash_profile or .bashrc (Set every time a user logins)
Logical Volume management
• What is LVM ?
• Stands for Logical Volume Management
• Common problems
• Resize partition ?
• Use the space on separate disks ?
• Use speed of multiple disks ?
• Write same data on more disks ?
• Backup used filesystem ?
• LVM structure
• Physical Volume (PV) : physical partition from disk.
• Volume Group (VG) : collection of physical disks, like a single virtual
disk.
• Logical Volume (LV) : A partition in a volume group.
• File system(FS) : Create file system on logical volume.
• Example
[root@localhost ~]# fdisk -l
Disk /dev/sda: 214.7 GB, 214748364800 bytes, 419430400 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x00036652
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 1026047 512000 83 Linux
/dev/sda2 1026048 419430399 209202176 8e Linux LVM
Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk /dev/sdc: 21.5 GB, 21474836480 bytes, 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Create physical volumes
[root@localhost ~]# pvcreate /dev/sdb
Physical volume "/dev/sdb" successfully created
[root@localhost ~]# pvcreate /dev/sdc
Physical volume "/dev/sdc" successfully created
Display physical volumes
Pvdisplay
"/dev/sdb" is a new physical volume of "20.00 GiB"
--- NEW Physical volume ---
PV Name /dev/sdb
VG Name
PV Size 20.00 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID n2I1P7-IYY7-uIXv-NV7L-aT4z-wOqO-4A1LsB
"/dev/sdc" is a new physical volume of "20.00 GiB"
--- NEW Physical volume ---
PV Name /dev/sdc
VG Name
PV Size 20.00 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID 1rpm03-3urJ-HYt9-pWhy-KNFk-boEe-rr1iOA
• Create volume group
[root@localhost ~]# vgcreate myvg /dev/sdb /dev/sdc
Volume group "myvg" successfully created
How to extend volume group
Vgextend <volume group> <disk>
Create logical volumes
[root@localhost ~]# lvcreate -L 500 -n vol1 myvg
Logical volume "vol1" created.
[root@localhost ~]#
[root@localhost ~]# lvcreate -L 500 -n vol2 myvg
Logical volume "vol2" created.
[root@localhost ~]#
[root@localhost ~]# lvcreate -L 500 -n vol3 myvg
Logical volume "vol3" created.
[root@localhost ~]#
[root@localhost ~]# lvcreate -L 1000 -n vol4 myvg
Logical volume "vol4" created.
• To display the list of logical volumes “lvdisplay”
• To display the list of volume groups use “vgdisplay”
• Create file system on logical volumes
[root@localhost ~]# mkfs.xfs /dev/myvg/vol1
meta-data=/dev/myvg/vol1 isize=256 agcount=4, agsize=32000 blks
= sectsz=512 attr=2, projid32bit=1
= crc=0 finobt=0
data = bsize=4096 blocks=128000, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=0
log =internal log bsize=4096 blocks=853, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
[root@localhost ~]# mkfs.xfs /dev/myvg/vol2
meta-data=/dev/myvg/vol2 isize=256 agcount=4, agsize=32000 blks
= sectsz=512 attr=2, projid32bit=1
= crc=0 finobt=0
data = bsize=4096 blocks=128000, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=0
log =internal log bsize=4096 blocks=853, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
[root@localhost ~]#
[root@localhost ~]# mkfs.xfs /dev/myvg/vol3
meta-data=/dev/myvg/vol3 isize=256 agcount=4, agsize=32000 blks
= sectsz=512 attr=2, projid32bit=1
= crc=0 finobt=0
data = bsize=4096 blocks=128000, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=0
log =internal log bsize=4096 blocks=853, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
[root@localhost ~]# mkfs.xfs /dev/myvg/vol4
meta-data=/dev/myvg/vol4 isize=256 agcount=4, agsize=64000 blks
= sectsz=512 attr=2, projid32bit=1
= crc=0 finobt=0
data = bsize=4096 blocks=256000, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=0
log =internal log bsize=4096 blocks=853, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
• Mount the file systems
[root@localhost ~]# mkdir /pgdata
[root@localhost ~]# mkdir /archive
[root@localhost ~]# mkdir /pg_xlog
[root@localhost ~]# mkdir /backups
[root@localhost ~]# mount /dev/myvg/vol1 /pgdata
[root@localhost ~]# mount /dev/myvg/vol2 /archive
[root@localhost ~]# mount /dev/myvg/vol3 /pg_xlog
[root@localhost ~]# mount /dev/myvg/vol4 /backups
df –h
……
……
/dev/mapper/myvg-vol1 497M 26M 472M 6% /pgdata
/dev/mapper/myvg-vol2 497M 26M 472M 6% /archive
/dev/mapper/myvg-vol3 497M 26M 472M 6% /pg_xlog
/dev/mapper/myvg-vol4 997M 33M 965M 4% /backups
Make entry in /etc/fstab
• Extend logical volumes
Syntax: Lvextend –L <size in MB> <logical volume name>
• Resize the filesystem
Syntax: resize2fs <logical volume name>
xfs_growfs /dev/mapper/pgclass-pgvol2 (XFS)
• Remove logical colume
• Syntax: lvremove <logical volume name>
Network File system
• NFS (Network File System) is basically developed for sharing
of files and folders between Linux/Unix systems by Sun Microsystems in 1980. It allows you to
mount your local file systems over a network and remote hosts to interact with them as they are
mounted locally on the same system. With the help of NFS, we can set up file sharing
between Unix to Linux system and Linux to Unix system.
• Steps for nfs
• Install the required packages for the NFS on both the server and client
nfs-utils-1.0.9-42.el5.x86_64.rpm
nfs-utils-lib-1.0.8-7.6.el5.x86_64.rpm
nfs-utils-lib-devel-1.0.8-7.6.el5.x86_64.rpm
nfs4-acl-tools-0.3.3-1.el5.x86_64.rpm
portmap-4.0-65.2.2.1.x86_64.rpm
• On NFS Server
• make an entry about the directory we want to share in the /etc/exports file
like below.
/archive 192.9.1.103(rw,sync,fsid=0)
• Start the services
Systemctl nfs start
Systemctl portmap start
Systemctl enable nfs
Systemctl enable portmap
• ON NFS Client
• execute the below command as root user.
• Mkdir /archive
• mount 192.9.1.101:/archive /archive
• Make below entry in /etc/fstab of nfs client to mount the filesystem
automatically after reboot.
• 192.9.1.103:/archive /archive nfs4 soft,intr,rsize=8192,wsize=8192,nosuid
Job scheduling
• Crontab
• The crontab is a list of commands that you want to run on a regular schedule,
and also the name of the command used to manage that list.
• Linux crontab format
• MIN HOUR DOM MON DOW CMD
• Field Description Allowed Value
MIN Minute field 0 to 59
HOUR Hour field 0 to 23
DOM Day of Month 1-31
MON Month field 1-12
DOW Day Of Week 0-6 (Sunday to Saturday)
CMD Command Any command to be executed.
• SELINUX
• Selinux is an enhanced security in Linux.
• To enable or disable the selinux set the parameter in file “/etc/selinux/config”
• Uname
• Uname is a command which is used to find the linux version and kernel
version.
• Ulimit
• The ulimit and sysctl programs allow to limit system-wide resource use. This
can help a lot in system administration, e.g. when a user starts too many
processes and therefore makes the system unresponsive for other users.
• How to set ulimit in Linux for a user
• Open file “/etc/security/limits.conf” and set the parameters for user
<user> soft nproc <value>
<user> hard nproc <value>
<user> hard cpu <value>
<user> soft cpu <value>
• Logout and login as the user again to see the changed parameters
• Monitoring in Linux
• There are different tools in market to monitor Linux operating system. Below are
the commands used to monitor in Linux
• For every process there is a file in /proc file system which is used to monitor the
memory utilization of a process
• “top” command is like task manager in Windows which used to monitor the overall
system level and cpu, memory utilization also process level can be monitored.
• Examples:
• enter top command and press “shift+O” to sort by a field
• To monitor processes of a user “top –u postgres”
• Press c to see the command with full path
• To kill a process “press k and type the process id”
• Save top command output to a file “top –n 1 –b > outputfile”
• Vmsstat is a command used to monitor the virtual memory stats, cpu stats, swap memory, disks.
• Ex: vmstat 2 10  This will print the vmstat details for every 2 sec up to 10 times.
• IOSTAT is used to print the disk level or lvm level stats about the iops and usage of disk
• Ex: iostat 1 5  This will print iostat detail for every 1 second up to 5 times.
• Tools to monitor
• There are lot of tools in market to monitor in Linux
• Nagios
• Nmon
• etc
Linux shell scripting
• Shell scripting
• Conditions
• Loops
• Variables (export)
• Symbols (` , $ )

More Related Content

Similar to Basic Linux Administration - 3.pptxon server (20)

PPT
linux-lecture4.pptuyhbjhbiibihbiuhbbihbi
YajnadattaPattanayak
 
PPT
LINUX
ARJUN
 
PPT
Linux commands and file structure
Sreenatha Reddy K R
 
PDF
3.1.a linux commands reference
Acácio Oliveira
 
PDF
Unix _linux_fundamentals_for_hpc-_b
Mohammad Reza Beygi
 
PPTX
Linux administration training
iman darabi
 
PDF
Basics of Linux Commands, Git and Github
Devang Garach
 
PPT
3. intro
Harsh Shrimal
 
PPTX
Linux Fundamentals
DianaWhitney4
 
PDF
Quick guide of the most common linux commands
Carlos Enrique
 
PPTX
Linux Systems Programming: Ubuntu Installation and Configuration
RashidFaridChishti
 
PDF
Information about linux operating system
PriyankaMate4
 
PPTX
Linux week 2
Vinoth Sn
 
PDF
Basics of Linux
SaifUrRahman180
 
PPTX
Linux
Srinivas Reddy
 
PPTX
Linux Commands all presentation file .pptx
AshutoshPrajapati30
 
DOCX
232623113-Basic-Linux-Commands.docx linux unix
mahakbhogill
 
PPT
cisco
edomaldo
 
PPTX
Unix Trainning Doc.pptx
KalpeshRaut7
 
PDF
Shell intro
Srikanth Learner
 
linux-lecture4.pptuyhbjhbiibihbiuhbbihbi
YajnadattaPattanayak
 
LINUX
ARJUN
 
Linux commands and file structure
Sreenatha Reddy K R
 
3.1.a linux commands reference
Acácio Oliveira
 
Unix _linux_fundamentals_for_hpc-_b
Mohammad Reza Beygi
 
Linux administration training
iman darabi
 
Basics of Linux Commands, Git and Github
Devang Garach
 
3. intro
Harsh Shrimal
 
Linux Fundamentals
DianaWhitney4
 
Quick guide of the most common linux commands
Carlos Enrique
 
Linux Systems Programming: Ubuntu Installation and Configuration
RashidFaridChishti
 
Information about linux operating system
PriyankaMate4
 
Linux week 2
Vinoth Sn
 
Basics of Linux
SaifUrRahman180
 
Linux Commands all presentation file .pptx
AshutoshPrajapati30
 
232623113-Basic-Linux-Commands.docx linux unix
mahakbhogill
 
cisco
edomaldo
 
Unix Trainning Doc.pptx
KalpeshRaut7
 
Shell intro
Srikanth Learner
 

Recently uploaded (20)

PDF
4 Tier Teamcenter Installation part1.pdf
VnyKumar1
 
PPTX
cybersecurityandthe importance of the that
JayachanduHNJc
 
PDF
NOISE CONTROL ppt - SHRESTH SUDHIR KOKNE
SHRESTHKOKNE
 
PDF
MRI Tool Kit E2I0500BC Plus Presentation
Ing. Ph. J. Daum GmbH & Co. KG
 
PPTX
00-ClimateChangeImpactCIAProcess_PPTon23.12.2024-ByDr.VijayanGurumurthyIyer1....
praz3
 
PPTX
Introduction to Fluid and Thermal Engineering
Avesahemad Husainy
 
PDF
Farm Machinery and Equipments Unit 1&2.pdf
prabhum311
 
PPTX
Online Cab Booking and Management System.pptx
diptipaneri80
 
PDF
Web Technologies - Chapter 3 of Front end path.pdf
reemaaliasker
 
PPTX
ENSA_Module_7.pptx_wide_area_network_concepts
RanaMukherjee24
 
PPT
IISM Presentation.ppt Construction safety
lovingrkn
 
PDF
The Complete Guide to the Role of the Fourth Engineer On Ships
Mahmoud Moghtaderi
 
PDF
勉強会資料_An Image is Worth More Than 16x16 Patches
NABLAS株式会社
 
PPTX
MULTI LEVEL DATA TRACKING USING COOJA.pptx
dollysharma12ab
 
PDF
Zero Carbon Building Performance standard
BassemOsman1
 
PDF
Zero carbon Building Design Guidelines V4
BassemOsman1
 
PPTX
Ground improvement techniques-DEWATERING
DivakarSai4
 
PDF
CAD-CAM U-1 Combined Notes_57761226_2025_04_22_14_40.pdf
shailendrapratap2002
 
PDF
2025 Laurence Sigler - Advancing Decision Support. Content Management Ecommer...
Francisco Javier Mora Serrano
 
PDF
Introduction to Robotics Mechanics and Control 4th Edition by John J. Craig S...
solutionsmanual3
 
4 Tier Teamcenter Installation part1.pdf
VnyKumar1
 
cybersecurityandthe importance of the that
JayachanduHNJc
 
NOISE CONTROL ppt - SHRESTH SUDHIR KOKNE
SHRESTHKOKNE
 
MRI Tool Kit E2I0500BC Plus Presentation
Ing. Ph. J. Daum GmbH & Co. KG
 
00-ClimateChangeImpactCIAProcess_PPTon23.12.2024-ByDr.VijayanGurumurthyIyer1....
praz3
 
Introduction to Fluid and Thermal Engineering
Avesahemad Husainy
 
Farm Machinery and Equipments Unit 1&2.pdf
prabhum311
 
Online Cab Booking and Management System.pptx
diptipaneri80
 
Web Technologies - Chapter 3 of Front end path.pdf
reemaaliasker
 
ENSA_Module_7.pptx_wide_area_network_concepts
RanaMukherjee24
 
IISM Presentation.ppt Construction safety
lovingrkn
 
The Complete Guide to the Role of the Fourth Engineer On Ships
Mahmoud Moghtaderi
 
勉強会資料_An Image is Worth More Than 16x16 Patches
NABLAS株式会社
 
MULTI LEVEL DATA TRACKING USING COOJA.pptx
dollysharma12ab
 
Zero Carbon Building Performance standard
BassemOsman1
 
Zero carbon Building Design Guidelines V4
BassemOsman1
 
Ground improvement techniques-DEWATERING
DivakarSai4
 
CAD-CAM U-1 Combined Notes_57761226_2025_04_22_14_40.pdf
shailendrapratap2002
 
2025 Laurence Sigler - Advancing Decision Support. Content Management Ecommer...
Francisco Javier Mora Serrano
 
Introduction to Robotics Mechanics and Control 4th Edition by John J. Craig S...
solutionsmanual3
 
Ad

Basic Linux Administration - 3.pptxon server

  • 2. • A command is a program which interacts with the kernel to provide the environment and perform the functions called for by the user. • A command can be: a built-in shell command; an executable shell file, known as a shell script; or a source compiled, object code file. • The shell is a command line interpreter. The user interacts with the kernel through the shell. You can write ASCII (text) scripts to be acted upon by a shell.
  • 3. • When the shell starts up it reads its startup files and may set environment variables, command search paths, and command aliases, and executes any commands specified in these files. • Every Unix platform will either have the Bourne shell, or a Bourne compatible shell available. • The default prompt for the Bourne shell is $ (or #, for the root user). • Another popular shell is C Shell. The default prompt for the C shell is %.
  • 4. Shell programming • You can write shell programs by creating scripts containing a series of shell commands. • The first line of the script should start with #! Which indicates to the kernel that the script is directly executable. • The first line is followed by commands • Within the scripts # indicates a comment from that point until the end of the line, with #! being a special case if found as the first characters of the file. #!/bin/bash cd /tmp mkdir t
  • 5. • Command Syntax Command <options> <Arguments> Multiple commands are separated by ; can be executed one after the another We can get help for command using “command --help” or “man command”
  • 6. • Pipes An important early development in Unix was the invention of "pipes," a way to pass the output of one tool to the input of another. eg. $ who | wc −l By combining these two tools, giving the wc command the output of who, you can build a new command to list the number of users currently on the system
  • 7. Basic commands • Cal • Print the calander • Date • Print the current date • Who • Print the details of the users who logged into the system • Clear • Clear the screen • Free • Print the ram details • whoami
  • 8. • Which • Print the location of the command file • Ex: which <command> • Pwd • Print present working directory • sleep
  • 9. Linux File management and viewing • cd • Change directory. With no arguments “cd” changes to user home directory. • Ls • List the contents of the directory • Ex: ls , ls –l, ls –al, ls –ld, ls –R • Mkdir • Make a directory • Ex: mkdir –p <directory name> • Cp • Copy files or folder to another location • Ex: cp –r <file/directory> <destination>
  • 10. • Mv • Move or rename a file or directory • Ex: mv <source> <destination> • Touch • Create an empty file • We can also change the timestamp for existing file • Ex: touch <file> • Vi Editor • Used to edit the files • We can do find and replace , search like normal text editors. • Head • Print first ten lines in a file • Print all lines except first lines (-n-1) • Ex: head –n <10> filename
  • 11. • Cat • View the content of a file • Ex: cat filename • Echo • Display the text with new line • Ex: Echo “content” • Printf • Display the text without new line • Ec: printf “content” • Df • Show the disk free space for one or more file systems • Ex: df –h , df –g
  • 12. • Du • Show how much disk space a directory or file is used • Ex: du –sh <directory/file> • Grep • Search content in a file • Ex: grep <string> filename • Less , more • View the content of file • Wc • Count the number of lies or characters or words • Find • Search for a file in a location • Find <location> -name <name of file>
  • 13. • Rm • Remove a file or directory • Rmdir • Remove empty directory • Locate • Locate a file or directory • This gets the information from system without actually searching • Chown • Change the ownership of group or folder • Ex:- chown user:group file/folder
  • 14. File Access Rights • Types of Users: CHOWN • Owner • Group • All/Other • Types of Permissions: CHMOD • Read • Write • Execute • Types of Files • Directories • Other files
  • 15. Table 8.1 Summary of File Permissions in LINUX
  • 16. Directory Permissions • read = list files in the directory • write = add new files to the directory • execute = access files in the directory
  • 18. Table 8.3 Permissions for Access to courses, labs, and temp
  • 19. Changing the Access Rights Purpose – to set/change permissions in files • chmod [options] octal-mode filelist • chmod [options] symbolic-mode filelist Options • -R recursively process subdirectories
  • 20. Table 8.4 Values for Symbolic Mode Components
  • 21. Table 8.5 Examples of the chmod Commands and Their Purposes
  • 22. Table 8.5 Examples of the chmod Commands and Their Purposes
  • 23. Linux File compression and archiving • Tar • Used to archive multiple files • Zip • Compress the files • Ex: zip <file name> • Unzip • Un-compress file files • Ex: unzip <filename.(zip)> • Gzip • Compress the files • Ex: gzip <filename> • Gunzip • Uncompress the files • Ex: gunzip <filename.gzip>
  • 24. Network Management commands • Hostname • Show or set the name for the host • Ex: hostname -I • Ifconfig • Configure or show the network interface • Ex: ifconfig -a • Ifdown • Shutdown a network service • Ex: ifdown <ethernet card> • Ifup • Brings up a network service • Ex: ifup <ethernet card> • Nslookup • Query dns server for information about hosts • Ex: nslookup <ipaddress/hostname>
  • 25. • Ping • Send ICMP ECHO REQUEST packets to network hosts • Ex: ping <remote-hosts/ip> • Nfs • Show the information about the NFS server • Ssh • Secure login (ssh remote host) • Ex: ssh user@ipaddress/hostname • Scp • Secure copy • Scp –r <file/directory> user@destip:<destination location> • telnet • Test the port opened or not • Ex: telnet <ipaddress> <port>
  • 26. • Mail/send mail/mailx • Send mail to email address • Mailx <email> • Su • Switch user • Awk • Print column of a file • Awk ‘{print $1}’ filename • Cut • Process the files and words • Ex: cut –d ‘,’ –f1 • Ln – softlink • Ex: ln –s <file> link
  • 27. System Management Commands Runtime level management • exit Terminates the shell. • halt Stop the system. • init Process control initialization. (init 3) • logout Log the user off the system. • poweroff Brings the system down. • reboot Reboot the system. • runlevel List the current and previous runlevel.
  • 28. Group and user management • Groupadd • Create a group • Useradd • Create user • Useradd <username> • Passwd • Assign password to a user • Passwd <username> • Userdel • Delete the user • Userdel <username>
  • 29. • Groupdel • Remove a group same as userdel • /etc/passwd • /etc/group • /etc/hosts • Systemctl , service • Manage the services in Linux • Repository management • Yum repository in redhat/centos/fedora
  • 30. Process management • Ps • Print the processes • Ex: ps –ef | grep postgres • Top • Like task manager in Windows. List the processes running. • Kill pid • Stop the process • Ex: kill -9 pid • Killall process – named process • Ex: Killall --user postgres • Ex: Killall --process-group postgres
  • 31. • Bg – list stopped or background jobs, resume a stopped job in the background • Ex: bg • Fg brings the most recent job to foreground • Ex: fg • Fg n – brings a job number to foreground • Ex: fg pid • Nohup , & run a process in background • Ex: nohup sh script &
  • 32. Redirecting output • > • Redirect output to file • Ex: cmd > file • >> • Append output to file • Ex: cmd >> file • < • Take input from file • Ex: cmd < file • | • Pipe output from previous command • Cmd1 | cmd2
  • 33. Environment variables • What is computing environment ? • The Computing environment is the Platform(Platform = Operating System+ Processor) where a user can run programs. • What is variable ? • In computer science, a variable is a location for storing a value which can be a filename, text, number or any other data. It is usually referred to with its Symbolic name which is given to it while creation. The value thus stored can be displayed, deleted, edited and re-saved. • What are Environment variables? • Environment variables are dynamic values which affect the processes or programs on a computer. They exist in every operating system, but types may vary. Environment variables can be created, edited, saved, and deleted and give information about the system behavior.
  • 34. • Example of environment variables • PATH: This variable contains a colon (:)-separated list of directories in which your system looks for executable files. • USER: The username • HOME: Default users home directory • SHELL: Default shell for user • Access variables • Echo $VARIABLE • Env command list all the environment variables. • Setting variables • Export VARIALBE=VALUE (This is for session) • .bash_profile or .bashrc (Set every time a user logins)
  • 35. Logical Volume management • What is LVM ? • Stands for Logical Volume Management • Common problems • Resize partition ? • Use the space on separate disks ? • Use speed of multiple disks ? • Write same data on more disks ? • Backup used filesystem ?
  • 37. • Physical Volume (PV) : physical partition from disk. • Volume Group (VG) : collection of physical disks, like a single virtual disk. • Logical Volume (LV) : A partition in a volume group. • File system(FS) : Create file system on logical volume.
  • 38. • Example [root@localhost ~]# fdisk -l Disk /dev/sda: 214.7 GB, 214748364800 bytes, 419430400 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk label type: dos Disk identifier: 0x00036652 Device Boot Start End Blocks Id System /dev/sda1 * 2048 1026047 512000 83 Linux /dev/sda2 1026048 419430399 209202176 8e Linux LVM Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk /dev/sdc: 21.5 GB, 21474836480 bytes, 41943040 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes
  • 39. Create physical volumes [root@localhost ~]# pvcreate /dev/sdb Physical volume "/dev/sdb" successfully created [root@localhost ~]# pvcreate /dev/sdc Physical volume "/dev/sdc" successfully created Display physical volumes Pvdisplay "/dev/sdb" is a new physical volume of "20.00 GiB" --- NEW Physical volume --- PV Name /dev/sdb VG Name PV Size 20.00 GiB Allocatable NO PE Size 0 Total PE 0 Free PE 0 Allocated PE 0 PV UUID n2I1P7-IYY7-uIXv-NV7L-aT4z-wOqO-4A1LsB "/dev/sdc" is a new physical volume of "20.00 GiB" --- NEW Physical volume --- PV Name /dev/sdc VG Name PV Size 20.00 GiB Allocatable NO PE Size 0 Total PE 0 Free PE 0 Allocated PE 0 PV UUID 1rpm03-3urJ-HYt9-pWhy-KNFk-boEe-rr1iOA
  • 40. • Create volume group [root@localhost ~]# vgcreate myvg /dev/sdb /dev/sdc Volume group "myvg" successfully created How to extend volume group Vgextend <volume group> <disk> Create logical volumes [root@localhost ~]# lvcreate -L 500 -n vol1 myvg Logical volume "vol1" created. [root@localhost ~]# [root@localhost ~]# lvcreate -L 500 -n vol2 myvg Logical volume "vol2" created. [root@localhost ~]# [root@localhost ~]# lvcreate -L 500 -n vol3 myvg Logical volume "vol3" created. [root@localhost ~]# [root@localhost ~]# lvcreate -L 1000 -n vol4 myvg Logical volume "vol4" created.
  • 41. • To display the list of logical volumes “lvdisplay” • To display the list of volume groups use “vgdisplay” • Create file system on logical volumes [root@localhost ~]# mkfs.xfs /dev/myvg/vol1 meta-data=/dev/myvg/vol1 isize=256 agcount=4, agsize=32000 blks = sectsz=512 attr=2, projid32bit=1 = crc=0 finobt=0 data = bsize=4096 blocks=128000, imaxpct=25 = sunit=0 swidth=0 blks naming =version 2 bsize=4096 ascii-ci=0 ftype=0 log =internal log bsize=4096 blocks=853, version=2 = sectsz=512 sunit=0 blks, lazy-count=1 realtime =none extsz=4096 blocks=0, rtextents=0 [root@localhost ~]# mkfs.xfs /dev/myvg/vol2 meta-data=/dev/myvg/vol2 isize=256 agcount=4, agsize=32000 blks = sectsz=512 attr=2, projid32bit=1 = crc=0 finobt=0 data = bsize=4096 blocks=128000, imaxpct=25 = sunit=0 swidth=0 blks naming =version 2 bsize=4096 ascii-ci=0 ftype=0 log =internal log bsize=4096 blocks=853, version=2 = sectsz=512 sunit=0 blks, lazy-count=1 realtime =none extsz=4096 blocks=0, rtextents=0 [root@localhost ~]#
  • 42. [root@localhost ~]# mkfs.xfs /dev/myvg/vol3 meta-data=/dev/myvg/vol3 isize=256 agcount=4, agsize=32000 blks = sectsz=512 attr=2, projid32bit=1 = crc=0 finobt=0 data = bsize=4096 blocks=128000, imaxpct=25 = sunit=0 swidth=0 blks naming =version 2 bsize=4096 ascii-ci=0 ftype=0 log =internal log bsize=4096 blocks=853, version=2 = sectsz=512 sunit=0 blks, lazy-count=1 realtime =none extsz=4096 blocks=0, rtextents=0 [root@localhost ~]# mkfs.xfs /dev/myvg/vol4 meta-data=/dev/myvg/vol4 isize=256 agcount=4, agsize=64000 blks = sectsz=512 attr=2, projid32bit=1 = crc=0 finobt=0 data = bsize=4096 blocks=256000, imaxpct=25 = sunit=0 swidth=0 blks naming =version 2 bsize=4096 ascii-ci=0 ftype=0 log =internal log bsize=4096 blocks=853, version=2 = sectsz=512 sunit=0 blks, lazy-count=1 realtime =none extsz=4096 blocks=0, rtextents=0
  • 43. • Mount the file systems [root@localhost ~]# mkdir /pgdata [root@localhost ~]# mkdir /archive [root@localhost ~]# mkdir /pg_xlog [root@localhost ~]# mkdir /backups [root@localhost ~]# mount /dev/myvg/vol1 /pgdata [root@localhost ~]# mount /dev/myvg/vol2 /archive [root@localhost ~]# mount /dev/myvg/vol3 /pg_xlog [root@localhost ~]# mount /dev/myvg/vol4 /backups df –h …… …… /dev/mapper/myvg-vol1 497M 26M 472M 6% /pgdata /dev/mapper/myvg-vol2 497M 26M 472M 6% /archive /dev/mapper/myvg-vol3 497M 26M 472M 6% /pg_xlog /dev/mapper/myvg-vol4 997M 33M 965M 4% /backups Make entry in /etc/fstab
  • 44. • Extend logical volumes Syntax: Lvextend –L <size in MB> <logical volume name> • Resize the filesystem Syntax: resize2fs <logical volume name> xfs_growfs /dev/mapper/pgclass-pgvol2 (XFS) • Remove logical colume • Syntax: lvremove <logical volume name>
  • 45. Network File system • NFS (Network File System) is basically developed for sharing of files and folders between Linux/Unix systems by Sun Microsystems in 1980. It allows you to mount your local file systems over a network and remote hosts to interact with them as they are mounted locally on the same system. With the help of NFS, we can set up file sharing between Unix to Linux system and Linux to Unix system.
  • 46. • Steps for nfs • Install the required packages for the NFS on both the server and client nfs-utils-1.0.9-42.el5.x86_64.rpm nfs-utils-lib-1.0.8-7.6.el5.x86_64.rpm nfs-utils-lib-devel-1.0.8-7.6.el5.x86_64.rpm nfs4-acl-tools-0.3.3-1.el5.x86_64.rpm portmap-4.0-65.2.2.1.x86_64.rpm • On NFS Server • make an entry about the directory we want to share in the /etc/exports file like below. /archive 192.9.1.103(rw,sync,fsid=0) • Start the services Systemctl nfs start Systemctl portmap start Systemctl enable nfs Systemctl enable portmap
  • 47. • ON NFS Client • execute the below command as root user. • Mkdir /archive • mount 192.9.1.101:/archive /archive • Make below entry in /etc/fstab of nfs client to mount the filesystem automatically after reboot. • 192.9.1.103:/archive /archive nfs4 soft,intr,rsize=8192,wsize=8192,nosuid
  • 48. Job scheduling • Crontab • The crontab is a list of commands that you want to run on a regular schedule, and also the name of the command used to manage that list. • Linux crontab format • MIN HOUR DOM MON DOW CMD • Field Description Allowed Value MIN Minute field 0 to 59 HOUR Hour field 0 to 23 DOM Day of Month 1-31 MON Month field 1-12 DOW Day Of Week 0-6 (Sunday to Saturday) CMD Command Any command to be executed.
  • 49. • SELINUX • Selinux is an enhanced security in Linux. • To enable or disable the selinux set the parameter in file “/etc/selinux/config” • Uname • Uname is a command which is used to find the linux version and kernel version. • Ulimit • The ulimit and sysctl programs allow to limit system-wide resource use. This can help a lot in system administration, e.g. when a user starts too many processes and therefore makes the system unresponsive for other users.
  • 50. • How to set ulimit in Linux for a user • Open file “/etc/security/limits.conf” and set the parameters for user <user> soft nproc <value> <user> hard nproc <value> <user> hard cpu <value> <user> soft cpu <value> • Logout and login as the user again to see the changed parameters
  • 51. • Monitoring in Linux • There are different tools in market to monitor Linux operating system. Below are the commands used to monitor in Linux • For every process there is a file in /proc file system which is used to monitor the memory utilization of a process • “top” command is like task manager in Windows which used to monitor the overall system level and cpu, memory utilization also process level can be monitored. • Examples: • enter top command and press “shift+O” to sort by a field • To monitor processes of a user “top –u postgres” • Press c to see the command with full path • To kill a process “press k and type the process id” • Save top command output to a file “top –n 1 –b > outputfile”
  • 52. • Vmsstat is a command used to monitor the virtual memory stats, cpu stats, swap memory, disks. • Ex: vmstat 2 10  This will print the vmstat details for every 2 sec up to 10 times. • IOSTAT is used to print the disk level or lvm level stats about the iops and usage of disk • Ex: iostat 1 5  This will print iostat detail for every 1 second up to 5 times. • Tools to monitor • There are lot of tools in market to monitor in Linux • Nagios • Nmon • etc
  • 53. Linux shell scripting • Shell scripting • Conditions • Loops • Variables (export) • Symbols (` , $ )