14/180: Linux powerful Commands for System administration
Today is 14th day of my challenge of 180 days streak. Very tired today. It’s 11:30 already, just got free from office. Hectic day, but then this challenge must go on. Today I am going to discuss more on Linux commands, I have already covered Linux basic commands in my previous blog. Today we are going to learn the stuff from a system administrator perspective.
Let’s begin with few Filters / Text Processor Commands
awk: is for data extraction. Let’s say I have a file having list of names and the names consists of first and last name like
- Navneet Ojha
- Jeni Kaur
- Kenny Parker …..
- …………….and so on
awk '{print $1}' filename
The above command will print the first column in the file. i.e. the first names from the file.
//it will print the send column
awk '{print $2}' filename// this command file print first and 3rd column of the list
ls -l | awk '{print $1, $3}' //print last column
ls -l | awk '{print $NF}' //awk can do search for you, it will give you that particular row
awk '/keyword to be searched/{print}' filename
grep/egrep: It process text line by line and show the line which matches.
grep keyword filename
grep -c keyword filename // -c will show the count of occurances
grep -i KEYword filename //-i ignore case
grep -n keyword filename //give line numbers also
grep -vi keword filename | aw '{print $1}' //will give only first col
ls -l | grep Desktop //give matched keyword from list
egrep -i "keyword1|keyword2" filename
sort/uniq -> filters out repeated lines
sort -> sorts in alphabetical order.
sort file | uniq //alone uniq will not work, it is required to use sort with this.
sort file | uniq -c //give counts for each.
wc: Read input newline count, word count, byte count.
Linux File editor: Though there are many file editors, but I will basically talk about vi editor. Usage of vi editor:
- Supplies command for Inserting and Deleting text
- Replacing Text
- Moving around the file
- Finding and Substituting strings
- Cutting and Pasting
Basic vi command:
- i -> insert
- r -> replace
- d -> delete
- :q! -> quit without write
- :wq! -> quit with save
Another very powerful command is sed
“sed” command
- Substitute command
- Replace a string a file with new string
- Find and Delete
- Remove empty lines
- To replace tab with spaces
- Show defined lines from a file
sed 's/keyword/replacewith' filename
sed 's/keyword/replacewith/g' filename //replace globally
sed -i 's/keyword/replacewith' filename //if want to insert changes in file
sed 's/keyword/d' filename //delete all lines having keword
sed 's/^$/d' filename //remove empty lines
sed 's/\t/ /g' filename //remove tabs
User Account Management
- useradd
- groupadd
- groupdel
- usermod
Files: /etc/passwd, /etc/group, /etc/shadow
Monitor User
- who -> to see how many people are logged in to system
- last -> last time user logged in
- w -> almost same as who, with some more information
- id -> give information about the user
Talking to User
- users ->users who are logged in
- wall -> Write message to all users.
- write -> write message to particular user
Linux Directory Service, linux account authentication
- local account
- Domain / Directory Account
- Windows = Active Directory
- Linux = LDAP -> protocol to authenticate against directory.
- IDM -> Identity manager
System Utility Commands
- date -> shows date
- uptime -> how long system was up for
- hostname
- uname
- which -> location of command
- cal -> calender
- bc -> binary calculator
Processes And Jobs
Application = Service
- Script -> written in files and packages
- Process -> when your application generates process id
- Daemon -> also process run always
- Threads -> Service Process -> thread 1, thread 2 …
- systemctl or service
- ps -> allows you to see the process
- top -> system resources for monitoring
- kill -> stop process by process id
- crontab -> schedule processes
- at -> like crontab
Process Management (bg, fg, nice)
- bg -> background
- fg -> forground
- nice -> prioritize the process
System Monitoring
- df -> disk partition info
- df -h -> disk partition info in human readable format
- iostat -> input out statistics {iostat 1 (refresh every 1 second)}
- free -> gives physical memory
- dmesg -> issues related to system hardware
Log Monitoring
Log Directory = /var/log
- boot -> when system boots up logs are generated
- chronyd = NTP
- cron
- maillog
- secure -> logging in logout activity
- message -> to monitor system messages
- httpd
- cat /proc/cpuinfo -> get cpu info
- cat /proc/meminfo -> get memory info