System Administration

System Administration
This course covers the main or important points from the Udemy Course
File/Text Editor
A text editor is a program that enables you to create and manipulate data in a text file.
There are many standard text editors available by default on most Linux Systems
- vi - Visual Editor
- ed - Standard Line Editor
- ex - Extended Line Editor
- emacs - A Full-screen editor
- pico - Beginner’s editor
- vim - Advanced version of vi
vi vs vim
- Both the editors are the same in terms of functionality, but Vim has more features and an easier learning curve.
- vim is based on vi, when you learn Vim you also learn vi
- vim has a lot of customization and help available online.
- some features that Vim has are,
- spell check
- completion
- comparison
- merging
- regular expressions
- folding
- syntax highlighting
- vimdiff
vi and vim once learnt are very powerful for editing and programming in the console.
sed
sed (Stream Editor) is a command line utility that can be used to replace a string with a new string in other file operations listed below and much more
- find and delete the 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
| command | description |
|---|---|
sed 's/search-word/replace-word/g filename' |
replaces all occurences of the search-word with the replace word |
sed 's/search-word/d filename |
deletes all occurences of the search word |
sed -i '/^$/d' filename |
removes all the empty lines, -i flag is for inplace edit |
sed -i '1,2d' filename |
remove lines 1 and 2 from the line |
sed -i 's/\t/ /g' filename |
replaces tabspace with space |
sed -n '12,18p' filename |
show lines from 12-18 |
sed 12,18d filename |
show all lines except 12-18 |
User Account Management
User Account management is an integral part of Linux, here are some commands to manage user and groups
- useradd
- groupadd
- userdel
- groupdel
- usermod
here is an example command that adds the user spiderman to the group spiderman, defines the default shell for the user and also creates a home directory and defines the path for it as well.
useradd -g superheros -s "/bin/bash" -c "user description" -m -d "/home/spiderman spiderman
groupadd and groupdel are the commands to create and delete groups
usermod -aG spiderman superheros` This command adds the user spiderman to superheroes group
there are a few commands that help us with managing users
- who
- last
- w
- finger
- id
- users
- wall
- write
who
this command gives information on who is logged into the system at any given time
last
it gives information about all users from the time of installation, logs all the login, crashes and reboots, and helps with diagnosing when and who was logged in when the event occurred
w
this command is very similar to who gives us more information on idle time, login time and what processes are being run by which user.
id
id command gives information on what the ID of the user is, and the IDs groups the user belongs to.
users
this command gives us the list of all the users logged in
wall
using the wall command we can communicate with all the logged-in users / send a broadcast message
usage - run the command wall
Password Aging
password aging and other parameters related to passwords can be enabled from the config values present in /etc/login.defs file, some parameters are
- PASS_MAX_DAYS
- PASS_MIN_LEN
- PASS_WARN_AGE
chage
the command chage stands for change age, using this we can change the password aging on a per-user basis, the command has a few flags/options that are of significance
It has d days since the password was last changed.
-mthe minimum number of days required between password changes --Mthe maximum number of days that the password is valid.-Wthe number of days before the password is to expire the user needs to be warned about it-lgives information about the password age and other information
System Utility Commands
some system utility commands are frequently used are
- date
- uptime
- cal
- bc - (binary calculator)
- hostname
- uname
- which - gives the location of the command
- ps
ps
ps stands for process status and displays all the processes running in the linux system.
usage: ps = shows the processes for the current shell
PID - process ID TTY - terminal type the user is logged into TIME - the amount of CPU in minutes that the process has been running for CMD - the name of the command
| command | description |
|---|---|
ps -e |
shows all the running processes |
ps aux |
shows all the running processes in BSD format |
ps -ef |
shows all running processes in full format listing |
ps -u username |
shows all the proesses running for a user |
top
- PID - unique process ID
- USER - username of the owner of the task
- PR - the “PR” field shows the scheduling priority of the process from the perspective of the kernel
- NI - Represents the nice value of a task, lower number = higher priority
- VIRT - total virtual memory used by the task
- RES - memory consumed by the task
- SHR - shows the shared memory used by a task
- S - shows the process state in single-letter form
- %CPU - represents CPU usage
- %MEM - shows the memory usage of the task
- TIME+ - CPU Time, same as TIME but has more granularity to the millisecond
kill
kill command is used to terminate the processes manually, it sends a signal to terminate or kill a process or group of processes.
usage - kill [OPTION] [PID]\
[OPTION] - signal name or signal number / PID
most commonly used signals are
| command | description |
|---|---|
kill -l |
gives the list of signal names or numbers |
kill PID |
kills the process of the given PID |
kill -1 |
restart |
kill -2 |
interrupt from the keyboard just like CTRL+C |
kill -9 |
forcefully kill the process |
kill -15 |
gracefully kill the process |
at
this is a scheduling command just like cronjob, but at command only runs once. when the command is run the console/terminal enters interactive mode and you have to use CTRL+D to exit it.
usage:
at HH:MM PM= schedule a jobatq= list all the entriesatrm= remove at entryatd= to manage theatdaemonat HH:MM PM DDMMYY= schedule a job on a specific date and time.at 4PM + 4 days= schedule a job at 4PM 4 days from nowat now + hours= schedule a job 5 hours from nowat 8:00 AM Sun= schedule a job on the coming sunday at 8AM
additonal crontabs
there are a few additional ways to run services on a schedule using cron, there are 4 directories for cron - hourly, daily, weekly, and monthly.
all these above files are present in the /etc/cron.____ directory, moving our scripts to these folders will run them without needing to add them to the crontab
Process Management
To send a process to the background which has occupied the current terminal use CTRL+Z to stop the process and keep it in the background and then use jobs to view it in the list of the processes that are running or stopped in the background.
bg, fg, &
to start the stopped process in the background to running use the bg command, and use fg to bring it back to the foreground.
System Monitoring
dmesg
gives information on the system information and warnings, is very important to the system hardware and helps diagnose information.
iostat
gives information on disk usage, use iostat 1 to see the disk information which refreshes every second.
netstat
netstat helps with all the information in the network. use man netstat to learn more about it.
cpu and mem info
the files located at /proc/cpuinfo and /proc/meminfo have all the information related to the CPU and memory.
script
usage script filename This starts recording all the commands typed in the screen to the file, use the exit command to stop the recording process