Linux Training : Section 5 (Part-1)
Module 5 - System Administration
Linux File Editor(vi)
A text editor is a program which enables you to create and manipulate data (text) in a Linux File
vi supplies commands for:
Inserting and Deleting text
Replacing text
Moving around the file
Finding and substituting strings
Cutting and pasting text
Most common keys-
i
= insertesc
= escape out of any moder
= replaced
= deletedd
= delete current linex
= delete the current characteru
= undo:q!
= quit without saving/<name>
= to search:wq!
ORSHIFT+Z,Z
\= quit and save:set number
= display no. of lines:set nonumber
= turns off numbering of lines:%s/aditya/shiv
= replace aditya with shiv
vi vs vim editor
Vi is known for its modal editing, where the meaning of typed keys depends on which mode the editor is in.
Vim is an improved clone of Vi that offers additional features and functionality.
Vim is designed for use both from a command-line interface and as a standalone application in a graphical user interface.
Vi is typist-friendly, with short commands that don't require removing hands from the keyboard.
Vim is the most commonly used implementation of the Vi standard.
“sed” Command
Replace a string in a file with a newstring
Find and delete a line
Remove empty lines
Remove the first or n lines in a file
To replace tabs with spaces
Show defined lines from a file
Substitute within vi editor
sed ‘s/aditya/shiv/g’ seinfeld-characters
- by this cmd aditya will get replace with shiv but this reflection will not occur in actual filesed -i ‘s/aditya/shiv/g’ seinfeld-characters
- this will reflect in the filesed ‘/aditya/d’ seinfeld-characters
- It will delete the aditya keyword from file and it will just display it on the screen, not inside the file.sed ‘/^$/d’ seinfeld-characters
- To delete the empty lines inside the filesed ‘1d’ seinfeld-characters
- To delete 1st linesed ‘1,2d’ seinfeld-characters
- to delete 1st & 2nd linesed ‘s/\t/ /g’ seinfeld-characters
- Replace tab from spacesed -n 12,18p seinfeld-characters
- Display define lines from bottomsed 12,18d seinfeld-characters
- Display define lines from topsed G seinfeld-characters
- Enter after every linesed ‘8!s/shiv/RAM/’ seinfeld-characters
- Substitute the RAM over shiv after line 8.
User Account Management
Commands
useradd
groupadd
userdel
groupdel
usermod
Files
/etc/passwd
/etc/group
/etc/shadow
Example
useradd -g superheros -s /bin/bash -c “user desc“ -m -d /home/spiderman spiderman
Hands-On
Create a super using
useradd
-Create a group-
Delete a user-
Delete the group-
Modify the user-
NOTE: use the
passwd <filename>
to setup the password.
Enable Password Aging
The /etc/login.defs File
The
chage
command - per userExample
chage [-m mindays] [-M maxdays] [-d lastday] [-I inactive] [-E expiredate] [-W warndays] user
File =
/etc/login.def
PASS_MAX_DAYS 99999
PASS_MIN_DAYS 0
PASS_MIN_LEN 5
PASS_WARN_AGE 7
Switch Users and sudo Access
Commands
su - username
sudo
commandvisudo
File
/etc/sudoers
Monitor Users(who, last, w, id)
Commands
who
- Used to check how many persons are logged into the system.last
- Check for the login historyw
- Just like who with some extra detailsfinger
ORpinky
id
Talking to Users
Commands-
users
- print the user names of users currently logged into the current hostwall
- write a message to all users…..cntl+d
to send the msg.write
- write a message to specific user — CMD-write <username>
Linux Account Authentication
Types of Accounts
Local accounts
Domain/Directory accounts
Active Directory = Microsoft
IDM = Identity Manager
WinBIND = Used in Linux to communicate with Windows(Samba)
OpenLDAP(open source) = it’s a protocol
IBM Directory Server
JumpCloud
LDPA = Lightweight Directory Access Protocol
System Utility Commands
date
= shows date and timeuptime
= tell from how much time the system is runninghostname
= shows hostnameuname
= print system informationwhich
= shows full path of cmdcal
= display a calendarbc
= binary calculator
Processes and Jobs
Application
= ServiceScript
= Shell Scripts or cmd are list of instructions e.g., adduser, cd, pwd etcProcess
= Every Service has ProcessDaemon
= Runs till interruptedThreads
= Every Service has Process and it has threadsJob
= Job or workorder- Run a service or process at a schedule time
Commands-
systemctl
orservice
systemctl
command is a new tool to control system servicesExamples:
systemctl start|stop|status servicename.service
systemctl enable servicename.service
systemctl restart|reload servicename.service
systemctl list-units --all
To add a service under systemctl management:
- Create a unit file in
/etc/systemd/system/servicename.service
- Create a unit file in
To control system with systemctl
systemctl poweroff
systemctl halt
systemctl reboot
ps
ps
command stands for process status and it displays all the currently running process in the Linux system.Example:
ps
= Shows the processed of the current shellps -e
= Shows all running processesps aux
= Shows all running processes in BSD formatps -ef
= Shows all running process in full format listingps -u username
= shows all processes by the username
top
top command is used to show the Linux processes and it provides a real-view of the running system
This command shows the summary information of the system and the list of processes or threads which are currently managed by the Linux Kernel.
When the top command is executed then it goes into interactive mode and you can exit out by hitting q.
Example:
top -u username
= shows tasks/processes by user ownedtop then press c
= shows commands absolute pathtop then press k
= kill a process by PID within top sessiontop then M and P
= to sort all Linux running processes by Memory usage
kill
this command is used to terminate processes manually
It sends a signal which ultimately terminates or kills a particular process or group of processes
Example:
kill [OPTION] [PID]
OPTION
\= Signal name or signal number/IDPID
= Process ID
kill PID
= Kill a process with default signalkill -1
= Restartkill -2
= Interrupt from the keyboard just like ctrl+Ckill -9
= Forcefully kill the processkill -15
= Kill a process gracefullykillall
= Kill Allpkill
= Kill by process name
crontab
Crontab command is used to schedule tasks
Usage:
crontab -e
= edit the crontabcrontab -l
= list the crontab entriescrontab -r
= remove the crontabcrond
= crontab daemon/service that manages schedulingsystemctl status crond
= to manage the crond service
at
at command is like crontab which allows you to schedule jobs but only once
When the command is run it will enter interactive mode and you can get out by pressing
CTRL+D
Usage:
at HH:MM PM
= Schedule a jobatq
= List the at entriesatrm #
= Remove at entryatd
= at daemon/service that manages schedulingsystemctl status atd
= To manage the atd service
Other future scheduling format:
at 2:45 AM 101621
= schedule a job to run on Oct 16th, 2021 at 2:45amat 4PM + 4 days
= schedule a job at 4pm four days from nowat now +5 hours
= schedule a job to run five hours from nowat 8:00 AM Sun
= schedule a job to 8am on coming Sundayat 10:00 AM next month
= schedule a job to 10am next month
Additional Cron Jobs
by default there are 4 different types of cronjobs
Hourly
Daily
Weekly
Monthly
All the above crons are setup in
/etc/cron.____
(directory)
The timing for each are set in
/etc/anacrontab - -
except hourly
For hourly
/etc/cron.d/0hourly
Process Signals in Linux
Types-
SIGNIT
(Signal Interrupt) —> Used to interrupt process:Kill -SIGINT PID
SIGTERM
(Signal Terminate) —> Process to terminate gracefully:kill -SIGTERM PID
SIGKILL
(Signal Kill) —> Forcefully kill process:kill -SIGKILL PID
SIGSTOP
(Signal Stop) —> Pause Running process:kill -SIGSTOP PID
SIGCONT
(Signal Continue) —> Resume Stopped Process:kill -SIGCONT PID
SIGSEGV
(Signal Segmentation Fault) —> Access Invalid Memory Location:kill -SIGSEGV PID