Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Common commands for daily maintenance of Linux

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the knowledge of "Linux daily maintenance commands". In the operation of actual cases, many people will encounter such a dilemma. Then let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Network commands:

# ifconfig# displays network information, including IP address, packet reception / transmission

# ifconfig eth0 up# starts eth0

# vi / etc/sysconfig/network-scripts/ifcfg-eth0 # change the eth0 configuration file

# iwconfig # displaying wireless network information

# sudo / etc/init.d/network restart | start | stop# restart the network | start the network | stop the network

# service network start | service networking restart# ditto

# / etc/network/interfaces# manually configure network files

# ifup interface# enable network interface

# ifdown interface# disables network interface

# wget http://xxx.com/xx.tar.gz # download

# route-n # enumerates the kernel IP routing table to solve related problems

# sudo dhclient# gets the IP address from the DHCP server

# sudo dhclient-r # obtain the IP address from the specified network interface

# cat / etc/resolv.conf# View DNS Server

# iwlist scan# displays the wireless network in the area

# lshw-C network# displays network card and network device driver information (needs to be run with administrator privileges)

Other common commands:

# cat# connects the file string to the basic output (screen)

# more# is similar to cat and will be displayed page by page, next page, previous page, help

# grep# looks for strings that match the criteria in the file. If so, which column will be displayed?

# tar-zxpf xx.tar.gz# decompression

# hostname | cat / etc/sysconfig/network# to view hostname

# lspci-nn# displays PCI vendor and device codes connected to the PCI bus in digits and hardware names

# lsusb# displays USB devices

# lshw-C usb# displays additional information on USB-related hardware (for USB dongles)

# yum install XX# yum installation commands

# yum list installed | wc-l | rpm-qa# to view all installed software packages

# chkconfig-- list # list all system services

# grep on# lists all started system services

# service XX (Service) restart | stop | start# restart | stop | start the XX service

# echo $LANG $LANGUAGE | cat / etc/sysconfig/i18n# to view the system default language

Hard disk mount:

# mount-t hard disk mount point

(hard disk type: FAT is msdos,FAT32, vfat,NTFS is ntfs)

# fdisk-l # View the hard drive and all usage

# mkdir / mnt/win_disk# establish a mount point in the / mnt directory

# mount-t ntfs / dev/sda6 / mnt/win_disk# mount sda6 to / mnt/win_disk

# unmount / mnt/win_disk# uninstall the hard disk

# mount-t iso9660 / dev/cdrom / mnt/cdrom# mount CD

# mount / dev/fd0 / mnt/floppy# mount floppy disk

Common commands are classified:

System

# uname-a | uname-r # View kernel / operating system / CPU information

# lsb_release-a # View system version information

# head-n 1 / etc/issue # View operating system version

# cat / proc/cpuinfo | more / proc/cpuinfo | grep "model name" # View CPU information

# hostname # View computer name

# lspci-tv # list all PCI devices

# lsusb-tv # list all USB devices

# lsmod # list the loaded kernel modules

# env # View environment variables

# getconf LONG_BIT # View the number of cpu bits (32 or 64)

# more / etc/redhat-release | cat / etc/redhat-release# to view the current linux version information

# passwd # modify a user's password

# reboot | Ctrl+Alt+Del | init restart

# shutdown-h now | halt | init shutdown

Resources

# free-m # View memory usage and swap area usage

# df-h # View the usage of each partition

# du-sh # View the size of the specified directory

# grep MemTotal / proc/meminfo # check the total memory

# grep MemFree / proc/meminfo # check the amount of free memory

# uptime # View system running time, number of users, and load

# cat / proc/loadavg # View system load

# pwd # View the directory

# ls-l # Show directory file list (- a: show hidden)

Disks and partitions

# mount | column-t # to view the status of attached partitions

# fdisk-l | df-h # View all partitions

# swapon-s # View all swap partitions

# hdparm-I / dev/hda # View disk parameters (for IDE devices only)

# dmesg | grep IDE # check the detection status of IDE devices at startup

The network

# ifconfig # View the properties of all network interfaces

# iptables-L # View firewall settings

# route-n # View the routing table

# netstat-lntp # View all listening ports

# netstat-antp # View all established connections

# netstat-s # View network statistics

# cat / etc/sysconfig/network# View Gateway

# cat / etc/resolv.conf # View DNS

Process

# ps-ef # View all processes

# top # Real-time display of process status

# ps-auxw # enumerate the current process ID

# kill XX process ID # terminating a single process

# killall program name # terminates all processes of the program

# xkill # terminate the X-Window program

User

# w # View active users

# id # View specified user information

# last # View user login log

# cut-d:-F1 / etc/passwd # View all users of the system

# cut-d:-F1 / etc/group # View all groups of the system

# crontab-l # View the current user's scheduled tasks

# startx # launch X Desktop

# switchdesk gnome | KDE |.. # switch desktop

Service

# chkconfig-- list # list all system services

# chkconfig-- list | grep on # lists all started system services

program

# rpm-qa | wc-l # View all installed software packages

# yum list installed # ditto

# cat-n / root/install.log# to view the software packages installed during system installation

# more / root/install.log # ditto

# / .configure # configuration

# make # compilation

# make install # installation

1.-- df-hP does not break the output regardless of the name of the file system, which is useful when writing to monitor disk utilization

Df-hP-P,-- portability use the POSIX output format

2.-- filter yesterday's and today's error logs in / var/log/messages

Cat / var/log/messages | grep-iE 'warning | panic | error | exception | fail' | grep "$(date | awk' {print $2"$3}')" > > $FILENAME

Cat / var/log/messages | grep-iE 'warning | panic | error | exception | fail' | grep "$(date-d yesterday | awk' {print $2"$3}')" > > $FILENAME

3.-- sort sorts by field,-t followed by delimiter,-k n (n represents number) by column, and-n sorts string number

Sort-t':'- k 3-n / etc/passwd / / A pair of uid of / etc/passwd file is sorted from smallest to largest, as follows:

Station90.example.com# sort-n-t ":"-k 3 / etc/passwd | head-n 3

Root:x:0:0:root:/root:/bin/bash

Bin:x:1:1:bin:/bin:/bin/bash

Daemon:x:2:2:Daemon:/sbin:/bin/bash

4.-- bc mathematical operation

Station90.example.com# echo "scale=3;1/5" | bc / / is accurate to a few decimal places, and scale is required. The default value of scale is 0.

. 200

Station90.example.com# echo "1.2: 8" | bc

9.6

[root@station90] # bc-Q / /-Q-- quiet don't print initial banner

Three and four

twelve

3/4

0

Scale=5

3/4

. 75000

[root@station90 ~] # var1=2

[root@station90 ~] # echo "scale=5;$var1/3" | bc / / bc supports variables

. 66666

[root@station90 ~] # var1=3

[root@station90 ~] # var2=$ (echo "scale=3;$var1/3" | bc)

[root@station90 ~] # echo $var2

1.000

5.-- take out the data in the second row, column 2. NR take out the data in which row.

Oracle@Station90.example.com/1211 > cat 1.txt

1 4

2 5

3 6

Oracle@Station90.example.com/1211 > awk 'NR==2 {print $2}' 1.txt

five

Df-h is executed every 2s

Watch-n 2 "df-h"

Humanized display output of ls-ltrh

Rm * an error was reported when deleting a file. You can delete a file in the following ways

Find. /-type f > / tmp/0110.txt

For an in $(cat / tmp/0110.txt); do rm $a done

In the tar command, the parameter-files-from is easy to use, such as:

Extract specific files in tar, and use-C to specify the decompression path.-- the file 0110.txt after files-from stores the name of the file to be extracted, and fuzzy matching can be performed.

Tar-zxvf xxxxx.tar.gz-C xxxx/-- files-from 0110.txt

-- files-from is also very useful when compressing files, such as:

Keep the files under the current path for ten days and delete the rest.

Find. /-type f-name "emcm*"-mtime + 10 > / tmp/0110.txt

Tar-zcvpf xxxxx.tar.gz-files-from / tmp/0110.txt

For an in $(cat / tmp/0110.txt); do rm $a done

5. Split split files and composite files

Split-b 70m xxx.tar.gz xxx.tar.gz_

For network reasons, split the exported file into 70m, and after you have downloaded all the files, execute the following command to synthesize the file:

Cat xxx.tar.gz_aa xxx.tar.gz_ab xxx.tar.gz_ac xxx.tar.gz_ad > xxx.tar.gz

If you need to synthesize on windows, if you download the file to d disk, execute the following command:

C:\ Users\ Administrator > d:

D:\ > copy / b "xxx.tar.gz_aa" + "xxx.tar.gz_ab" + "xxx.tar.gz_ac" + "xxx.tar.gz_ad" xxx.tar.gz

6. Check how many file handles are opened by the process, and take the first 3

[root@squid ~] # lsof-n | awk'{print $2}'| sort | uniq-c | sort-nr | head-n 3

128 6618

58 9945

55 9883

7. Change the operating system user password in batch

Chpasswd

< 1.txt cat 1.txt test1:$%uK83Re test2:$#rK8e78 test3:#&rH9e3R 8、当时同事正在做一些操作,可能导致存储有问题,只要能在存储上创建文件,则说明存储还是OK的,这时候的工作就是时不时的去多台服务器上去创建文件,简单重复但很累,就写下面的脚本代替: 脚本作用:若不能创建文件则发出响声报警echo $'\a' #!/bin/bash for i in {1..10000} do sleep 3 echo "xxxxxxxxxxxxxxxxxxxxx">

Lhf_$i.txt

If [$?-eq 0]; then

Echo "ok"

Else

Echo "failure"

Echo $'\ a'

Fi

Done

9. Tac looks at the text backwards. It is convenient to see the latest message log.

Tac / var/log/message | less

10. Grep parameters-A _

-A 1 takes the current row and the last row of the filtered value

-B1 takes the current row and the previous row of the filtered values

-C 1 takes the current row and the upper and lower rows of the filtered values

Station90.example.com:~/1205 # cat grep.txt

one

two

three

four

five

six

Station90.example.com:~/1205 # cat grep.txt | grep-B 1 4 / / take the current line and the previous line of the filtered value

three

four

Station90.example.com:~/1205 # cat grep.txt | grep-A 1 4 / / take the current line and the last line of the filtered value

four

five

Station90.example.com:~/1205 # cat grep.txt | grep-C 1 4 / / take the current line and the upper and lower lines of the filtered values

three

four

five

11. Grep-l-r parameters

Search for files with keyword keywords,-l list only file names containing keywords, and-r recursion

Grep-l-r keyword. /

12. Grep parameter-x-w

Cat 1.txt | grep-x 192.168.1.1 matches rows, and matches only when this line is 192.168.1.1

Cat 1.txt | grep-w 192.168.1.1 matching phrase, as long as this line has 192.168.1.1 matching

13. Grep-f is followed by the file name, such as:

Cat 1.txt | every line in the grep-f 2.txt 2.txt content will be matched by 1.txt. If there is a match in the 1.txt file, the 1.txt file will display the matching content. If the 2.txt and 1.txt files are both large, it will consume performance.

14. Change the user's uid and the group's gid

Usermod-u uid user name

Groupmod-g gid group name

15. Usage of sed

1. Sed replaces the 86 before the number with an empty one.

Sed-I's / ^ 86 / / g '1.txt

2. Insert xxxxx above line 115 of the text

Sed-I "115i\ xxxxx" 1.txt

3. Add "at the beginning, and replace the space with\ + space.

Sed-I-e "s / ^ /\" / g "- e" sUniverse /\ "/ g"-e "s / /\ / g" mdsp_filepath.txt

4. Write step by step, then:

Sed-I "s / ^ /\" / g "1.txt

Sed-I "s / /\ / g" 1.txt

5. Add "at the end of the document"

Sed-I "sCompact 1.txt /\" / g "

16. Xargs usage

1. Batch rename files

Ls | xargs-I mv {}. Bak

2. Query the keyword information in the file

Find. -name'* .CDR'| xargs grep 'keyword'

17. Awk usage

Awk-F specifies the delimiter, which is a space by default, as shown in

Take the ninth column with | as the delimiter

Awk-F'|'{print $9} '1.txt

18. Batch fetch the absolute path dirname where the file is located, and the file name must be without spaces.

Station90.example.com~ # cat 1.txt

/ etc/ATAEV100R002C03SPC200/sles10sp2_x64.properties

/ etc/ATAEV100R002C03SPC200/1206/task.properties

/ etc/DIR_COLORS

Station90.example.com:~ # for i in $(cat 1.txt); do dirname $idone

/ etc/ATAEV100R002C03SPC200

/ etc/ATAEV100R002C03SPC200/1206

/ etc

19. Batch fetch the file name basename

Station90.example.com~# for i in $(cat 1.txt); do basename $iTX done

Sles10sp2_x64.properties

Task.properties

DIR_COLORS

Netcontrol_global_hooks

20. Rsync transfers files

Rsync-avH-n-delete. / oracle@192.168.15.28:/home/oracle/

-n doesn't really transmit, it's just a test.

-a shows that the file is transferred recursively and all file properties are maintained

-- delete delete if the remote file is different from the transferred directory file (use with caution)

This is the end of the content of "Linux daily maintenance commands". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report