Common linux commands for operation and maintenance

a, batch modify the string in the file

     sed -is/192.168.10.1/192.168.12.100/g ./file.text

b. Check whether the program is running

      ps -el |grep program name

      ps -ef |grep program name

# Delete existing czht user

userdel -rf czht

# Create a new czht user

useradd -m -U -G wheel czht

//Set new user password

echo "123456" | passwd --stdin czht

//Detect a process and kill it

kill -9 `ps -el |grep czht_face |awk '{print $4}'`

1. The linux startup process

Turn on the power --> BIOS power-on self-test --> boot program lilo or grub --> kernel boot (kernel boot) --> execute init (rc.sysinit, rc) --> mingetty (build terminal) --> Shell

2. Network card binding multiple ip
# ifconfig eth0:1 192.168.1.99 netmask 255.255.255.0
3. Set DNS, gateway
# echo "nameserver 202.16.53.68" >> /etc/resolv.conf
# route add default gw 192.168.1.1
4 , Eject, retract the optical drive
# eject
# eject -t
5. Query yesterday's date with date
# date --date=yesterday
6. Query the line number of the blank line in file1
# grep ^$ file
7. Query file1 ending with abc Line
# grep abc$ file1
8. Print out the first to third lines of file1
# sed -n '1,3p' file1
# head -3 file1
9. Clear the file
# true> 1.txt
# echo ""> 1. txt
#> 1.txt
# cat /dev/null> 1.txt
10. Delete all empty directories
# find /data -type d -empty -exec rm -rf {};
11. Batch delete empty files (size equal to 0 files) method
# find /data -type f -size 0c -exec rm -rf {};
# find /data -type f -size 0c|xargs rm -f
12. Delete files five days ago
# find /data -mtime +5- type f -exec rm -rf {};
13. Delete the duplicated parts of the two files and print others
# cat 1.txt 3.txt |sort |uniq
14. Attack the host name of the remote server
# echo `ssh $IP cat / etc/sysconfig/network|awk -F ='/hostname/ {print $2}'`
15. Real-time monitoring of network card traffic (install iftop)
# /usr/local/iftop/sbin/iftop -i eth1 -n
16. View system Version
# lsb_release -a
17. Forcibly kick out the logged-in user
# pkill -kill -t pts/1
18, tar increase backup, restore
# tar -g king -zcvf kerry_full.tar.gz kerry
# tar -g king -zcvf kerry_diff_1 .tar.gz kerry
# tar -g king -zcvf kerry_diff_2.tar.gz kerry
# tar -zxvf kerry_full.tar.gz
# tar -zxvf kerry_diff_1.tar.gz
# tar -zxvf kerry_diff_2.tar.gz
19. Forward the local 80 port request to 8080 port, the current host’s external IP is 202.96.85.46
-A PREROUTING -d 202.96.85.46 -p tcp -m tcp --dport 80 -j DNAT --to-destination 192.168.9.10:8080
20. In November, every day from 6 am to 12 am, execute /usr/bin/httpd every 2 hours .sh
# crontab -e
0 6-12/2 * 11 * /usr/bin/httpd.sh
21. View the process occupying port 8080
# netstat -tnlp | grep 8080
lsof -i:8080
22. In the Shell environment, How to check how long the remote Linux system is running?
# ssh user@monitored host ip "uptime"
23. The command to check the CPU usage is
refreshed every 5 seconds, and there is the CPU usage data on the far right
# vmstat 5

top Then press Shift+P to sort by process processor usage
# top
24.
Command to view memory usage Use free command to view memory usage
# free -m

top Then press Shift+M, sort by process memory usage
# top
25. View disk i/o
Use iostat to view disk i/o of disk /dev/sdc3, refresh every two seconds
# iostat -d -x /dev /sdc3 2
26, repair the file system
# fsck -yt ext3 /
-t specify the file system
-y automatically answer yes to the questions found
27, automatically exit after 5 seconds of the read command
# read -t 5
28, grep -E -P Yes What does
-E, --extended-regexp use extended regular expressions.
-P, --perl-regexp uses perl regular expressions
29, vi editor (involving modification, addition, search)
insert mode
i Insert before the
cursor I Insert at the
beginning of the cursor a Insert after the
cursor A Insert at the end of the cursor
o Insert a line under the line where the cursor is, insert a line at the beginning of the line
O Insert a line at the line where the cursor is, insert
G at the
beginning of the line, move to the beginning of the last line nG
Move to the beginning of the nth line, move down n lines,
and move the beginning n- move up n Line, the
beginning of the line : /str/ move from the current to the right to the place with str
:?str? Move to the left from the current line to the place with str
: s/str1/str2/ replace the first str1 found with str2  
: s/str2/str2/g replace all str1 found in the current line with str2
: n1,n2s/str1/str2/g replace all str1 found from lines n1 to n2 with str2
: 1, .s/str1/str2/g replace all str1 from line 1 to the current line with str2
: .,$s/str1/str2/g will replace all str1 from the current line to the last line with str2
30, copy files between linux servers
copy local files 1.sh to the /data/ directory of the remote 192.168.9.10 server
# scp /etc/1.sh [email protected]:/data/ 

Copy the remote 192.168.9.10 server/data/2.sh file to the local /data/ directory
# scp [email protected]:/data/2.sh /data/
31. Use the sed command to put the 23rd line of the test.txt file Replace the TEST with TSET.
# sed -i '23s/TEST/TSET/' test.txt
# sed -i '23 s/TEST/TSET/' test.txt
32. Enable the history command to display the time
# export HISTTIMEFORMAT=" %F %T "
33. How to check the open ports of the target host 192.168.0.1
# nmap -ps 192.168.0.1
34. How to check the network connection
# netstat -n | awk'/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}'
35. How to check which library files the current system uses
# ldconfig -v
36. How to check the driver version of the network card
# ethtool -i eth0
37. Use tcpdump to Monitor the
tcp port 80 of the host 192.168.0.1 # tcpdump tcp port 80 host 192.168.0.1
38. How to read the mailing lists of other users
# mial -u king
39. Cut large files by
1000 lines per file
# split -l 1000 httperr8007.log httperr

Split according to 5m for each file
# split -b 5m httperr8007.log httperr
40. Combine files
Take out the union of two files (only one copy of repeated lines)
# cat file1 file2 | sort | uniq

Take out the intersection of two files (leave only the files that exist in both files)
# cat file1 file2 | sort | uniq -d

Delete the intersection and leave the other lines
# cat file1 file2 | sort | uniq –u
41. Service running in print text mode
# chkconfig --list|awk'$5~/on/{print $1,$5}'
42. Delete 0-byte file
# find -type f -size 0 -exec rm -rf {};
43. View the process, sort by memory from largest to smallest
# ps -e -o "%C: %p: %z: %a "|sort -k5 -nr
44. View the number of concurrent http requests and their TCP connection status
# netstat -n | awk'/^tcp/ {++S[$NF]} END {for(a in S) print a , S[a]}'
45. Get IP address
# ifconfig eth0|sed -n '2p'|awk'{print $2}'|cut -c 6-30

Perl achieves obtaining IP address:
# ifconfig -a | perl -ne'if (m/^s*inet (?:addr:)?([d.]+).*?cast/) {print qq($1n) ; exit 0; }
'46、Get memory size
# free -m |grep "Mem" | awk'{print $2}'
47、Check the number of CPU cores
# cat /proc/cpuinfo |grep -c processor
48、Check disk usage Situation
# df -hl
49. Check how many active PHP-cgi processes are there
# netstat -anp | grep php-cgi | grep ^tcp | wc -l
50. Check hardware manufacturer
# dmidecode -s system-product-name

Guess you like

Origin blog.csdn.net/qq_30264689/article/details/99323017