Linux-CenOS Miscellaneous Notes <1> Common Command Record

Linux-CenOS Miscellaneous Notes <1> Common Command Record

1. Firewall
1) Turn on after restart
: chkconfig iptables on
Turn off: chkconfig iptables off
2) Take effect immediately, fail after restart
Turn on: service iptables start
Turn off: service iptables stop
Check status service iptables status


2. Shutdown
# shutdown -h now #shutdown immediately
# shutdown -h 2:30 'halt' #shutdown at 2:30, pay attention to the local time
# halt -p #shutdown immediately
# init 0 #run the system immediately The level is switched to 0, that is, shutdown
# shutdown -r now #Restart immediately


3. Reboot
# shutdown -k now 'reboot' # Issue a warning message, but no real shutdown.
# shutdown -t10 -r now # Reboot immediately, but delay 10 seconds between warning and deleting processes.
# init 6 # Immediately switch the system run level to 6, that is, restart
# shutdown -r +10 'reboot' # Restart after 10 minutes
# shutdown -c # Cancel the previous shutdown command
# shutdown now # Switch to single-person operation mode (without adding any option)

4. Modify the system time
date time window
date -s '2017-08-01 10:10:00' Change the year, month, day, hour, minute and second
date -s 10:00:02 Only change the time without changing the year, month
clock -w write System time
hwclock Synchronize bios time
service ntpdate start Enable network time synchronization
systemctl start ntpdate.service Enable network time synchronization Red Hat 7
chkconfig --leves 2345 ntpdate on boot

Timezone:
date -R
tzselect

5. Port
1, lsof -i: port number
2, netstat -tunlp|grep port number
3, netstat -anp|grep 80
port enable, stop
1, open port (take port 80 as an example)

Method 1:
/sbin/iptables -I INPUT -p tcp --dport 80 -j ACCEPT Write and modify
/etc/init.d/iptables save Save and modify
service iptables restart Restart the firewall, the modification will take effect
Method 2:
vi /etc/sysconfig/iptables Open the configuration file and add the following Statement:
-A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT Restart the firewall, the modification is completed
2. Close the port

Method 1:
/sbin/iptables -I INPUT -p tcp --dport 80 -j DROP Write and modify
/etc/init.d/iptables save Save and modify
service iptables restart Restart the firewall, the modification will take effect
Method 2:
vi /etc/sysconfig/iptables Open the configuration file and add the following statement:
-A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j DROP Restart the firewall, the modification is completed


6. View CPU and memory configuration
Hard disk: df -h
CPU, memory: top
##CPU model view: cat /proc/cpuinfo | grep name | cut -f2 -d: | uniq -c
##From the application point of view Say, available memory = system free memory+buffers+cached.
##Check how many cores the cpu has, you can also use the command to display, 4 means 4 cores
cat /proc/cpuinfo |grep "cores"|uniq cpu cores: 4
##Check the number of physical CPUs, 1 means there is only one physical CPU CPU
cat /proc/cpuinfo |grep "physical id"|sort |uniq|wc -l

Check the graphics card: lspci |grep -i vga

7. Modify the terminal garbled code
# vim /etc/sysconfig/i18n
to:
LANG="en_US.UTF-8"
SYSFONT="latarcyrheb-sun16"

9. Top interface introduction
The first line:
13:42:59 The current system time is
6 days, 9:29 The system has been running for 6 days, 6 hours and 29 minutes (no restart during this period)
3 users There are currently 3 users logged in to the system
load average: 3.06, 3.01, 1.79 The three numbers behind the load average are the load conditions of 1 minute, 5 minutes, and 15 minutes, respectively.
The load average data is to check the number of active processes every 5 seconds, and then calculate the value according to a specific algorithm. If this number is divided by the number of logical CPUs, a result above 5 indicates that the system is overloaded.
The second line:
Tasks task (process), the system now has a total of 131 processes, of which 3 are running, 127 are sleeping (sleep), 0 are in the stopped state, and 1 is in the zombie state (zombie). .
The third line: cpu status
10.6% us The percentage of CPU occupied by user space.
2.2% sy The percentage of CPU that the kernel space occupies.
0.0% ni Percentage of CPU occupied by processes that have changed priority
84.5% id Percentage of idle CPU
2.5% wa Percentage of CPU occupied by IO waiting
0.1% hi Percentage of CPU occupied by hardware IRQs
0.0% si Soft interrupts (Software Interrupts) The percentage
of CPU occupied The CPU usage ratio here is different from the concept of windows. If you don't understand user space and kernel space, you need to charge it up.
Fourth line: memory status
8300124k total total physical memory (8GB)
5979476k used total memory in use (5.7GB)
2320648k free total free memory (2.2G)
455544k buffers cache memory (434M)
Fifth line: swap swap partition
8193108k total swap area Total (8GB)
41568k used Total used swap area (40.6M)
8151540k free Total free swap area (8GB)
4217456k cached buffered total swap area (4GB)
****Total memory (used) refers to It is the amount of memory currently controlled by the system kernel, and the total amount of free memory (free) is the amount that the kernel has not yet brought under its control. The memory managed by the kernel is not necessarily in use, and
it also includes the memory that has been used in the past and can now be reused. The kernel does not return these reusable memory to free, so free memory on linux will be Less and less, but don't worry about it.
****Approximate calculation formula of available memory: free in the fourth row + buffers in the fourth row + cached in the fifth row, according to this formula, the available memory of this server: 2320648+455544 +4217456 = 6.6GB.
****In top, we need to monitor the used of the swap partition of the fifth row at all times. If this value is constantly changing, it means that the kernel is constantly exchanging data between memory and swap, which is the real memory is not enough.
Below the seventh line: status monitoring of each process (task)
PID process id
USER process owner
PR process priority
NI nice value. Negative values ​​indicate high priority, and positive values ​​indicate
the total amount of virtual memory used by low-priority VIRT processes, in kilobytes. VIRT=SWAP+RES
The size of the physical memory used by the RES process but not swapped out, in kb. RES=CODE+DATA
SHR Shared memory size, unit kb
S Process status. D=uninterruptible sleep state R=running S=sleep T=tracking/stopping Z=zombie process
%CPU on+——The percentage of CPU time used by the last update to the present
%MEM The percentage of physical memory
used by the process TIME+ The CPU used by the process Total time, unit 1/100 seconds
COMMAND process name (command name/command line)
********************Multi-U multi-core CPU monitoring******* ********************************************
in top basic view , press the number 1 on the keyboard to monitor the status of each logical CPU:
when entering the top by default, the processes are sorted according to the CPU usage. In [top view 01], the mysqld process with process ID 3527 is ranked first. (CPU occupies 2%), the java process with process ID 26955 ranks second (cpu occupies 1%).
You can change the sorting field through keyboard commands. For example, if you want to monitor which process occupies the most MEM, I generally use it as follows:
1. Press the keyboard b (to turn on/off the highlighting effect), and the top view changes as follows:
we find the process id The top process of 20517 is highlighted, and generally only the running process is highlighted. You can close or open the highlighting effect of the running process by hitting the y key.
2. Press the keyboard x (to turn on/off the highlighting effect of the sorting column), the view of top changes as follows:
You can see that the default sorting column of top is %CPU.
3. The sorting sequence can be changed to the right or left by shift + > or shift + <. The following picture shows the effect of pressing shift + > once: the
view is now sorted according to %MEM.
To change the process display fields
1. Hit the f key, top enters another view, where you can arrange the display fields in the basic view:
this lists all the process fields that can be displayed in the top basic view, with * and marked in uppercase Fields with letters are displayable, fields without * and lowercase letters are not.
If you want to display the two fields CODE and DATA in the basic view, you can press the r and s keys:
2. Press Enter to return to the basic view, you can see two more fields of CODE and DATA:
The supplementary
top command of the top command is: The preferred command for system monitoring on Linux, but sometimes it does not meet our requirements. The smallest unit of monitoring of the top command is a process, so the number of threads and client connections of the program cannot be seen. Usually, two commands, ps and netstate, can be used. to make up for the lack of top.
Monitor the number of java threads:
ps -eLf | grep java | wc -l
Monitor the number of network client connections:
netstat -n | grep tcp | grep listening port | wc -l
The above two commands can change the parameters of grep to achieve more Detailed monitoring requirements.
Under the guidance of the idea that everything is a file in the Linux system, the running status of all processes can be obtained by using files. In the system root directory /proc, the name of each digital subdirectory is the PID of the running process, enter any process directory,
The various running indicators of the process can be observed through the files or directories in it. For example, the task directory is used to describe the threads in the process.
Therefore, the number of threads running in a process can also be obtained by the following method (PID refers to the process ID. ):
ls /proc/PID/task | wc -l
There is also a command pmap in linux to output the status of the process memory, which can be used to analyze the thread stack:
pmap PID
%CPU=(The life cycle of the process occupies the CPU Time)*100/(The length of time represented by the life cycle of the process)
For example, the length of the life cycle of a process is 1000s, and then the time occupied by the CPU in these 1000s is 500s, then
%CPU=500*100/1000=50
That is, the final result represents 50%


10. Modify the IP address
vim /etc/sysconfig/network-scripts/ifcfg-eth0
service network restart


11. Process management
check process Use the
ps command to find the PID number related to the process:
ps a Display all programs under the current terminal, including programs of other users.
ps -A shows all programs.
When ps c lists programs, display the actual command name of each program, without the path, parameter, or resident service designation.
ps -e This parameter has the same effect as specifying the "A" parameter.
ps e When listing programs, display the environment variables used by each program.
ps f Use ASCII characters to display a tree structure, expressing the relationship between programs.
ps -H Displays a tree-like structure, representing the interrelationships between programs.
ps -N displays all programs, except the programs under the ps command terminal.
ps s Displays program status in program signal format.
ps S When listing programs, include interrupted subroutine data.
ps -t <terminal number> Specifies the terminal number and lists the status of programs belonging to that terminal.
ps u Displays program status in a user-focused format.
ps x displays all programs, regardless of terminal.
The most common method is ps aux, and then use the grep command to filter through the pipeline to find a specific process, and then operate on the specific process.
ps aux | grep program_filter_word,ps -ef |grep tomcat
ps -ef|grep java|grep -v grep Displays all java processes and removes the current grep process.
Kill the process
Use the kill command to end the process: kill xxx
Commonly used: kill -9 324
Linux also provides a killall command, which can directly use the name of the process instead of the process identification number, for example: # killall -9 NAME
Enter the path where the execution file of the process is located, and execute the file ./file name


12. Apache restart on Centos, mysql restart, nginx restart method
1. Restart apache
  service httpd restart
  /etc/init.d/httpd stop
  /etc/init.d/httpd start
  2. Restart mysql
  service mysql restart
  /etc/init. d/mysqld stop
  /etc/init.d/mysqld start
  3. Restart nginx
  service nginx restart
  /etc/init.d/nginx stop
  /etc/init.d/nginx start

13. Check the linux version
: lsb_release -a cat /etc/issue uname -a
Bit: getconf LONG_BIT file /bin/ls

14. Linux network card monitoring
watch more /proc/net/dev

15. Packing and compressing (Note: many compression programs in Linux can only compress one file)
01-.tar format
Unpacking: [*******]$ tar xvf FileName.tar
Packing: [** *****]$ tar cvf FileName.tar DirName (Note: tar is for packing, not for compression!)

02-.gz format
Decompression 1: [*******]$ gunzip FileName.gz
Decompression 2: [ *******]$ gzip -d FileName.gz
Compression: [*******]$ gzip FileName

03-.tar.gz format
decompression: [*******]$ tar zxvf FileName.tar.gz
compression: [*******]$ tar zcvf FileName.tar.gz DirName

04-.bz2 format
Decompression 1: [*******]$ bzip2 -d FileName.bz2
decompression 2: [*******]$ bunzip2 FileName.bz2
compression: [*******]$ bzip2 -z FileName

05-.tar.bz2 format
decompression: [*******] $ tar jxvf FileName.tar.bz2
Compression: [*******]$ tar jcvf FileName.tar.bz2 DirName

06-.bz format
Unzip 1: [*******]$ bzip2 -d FileName.bz
Unzip 2: [*******]$ bunzip2 FileName.bz
./bin/pika -c conf/pika.conf
07- .tar.bz format
Decompression: [*******]$ tar jxvf FileName.tar.bz

08-.Z format
Decompression: [*******]$ uncompress FileName.Z
Compression: [** *****]$ compress FileName

09-.tar.Z format
Decompression: [*******]$ tar Zxvf FileName.tar.Z
Compression: [*******]$ tar Zcvf FileName .tar.Z DirName

10-.tgz format
Decompression: [*******]$ tar zxvf FileName.tgz

11-.tar.tgz format
decompression: [*******]$ tar zxvf FileName. tar.tgz
Compression: [*******]$ tar zcvf FileName.tar.tgz FileName

12-.zip format
Decompression: [*******]$ unzip FileName.zip
Compression: [*** ****]$ zip FileName.zip DirName

13-.lha format
Decompression: [*******]$ lha -e FileName.lha
Compression: [*******]$ lha -a FileName.lha FileName

14-.rar format
decompression: [***** **]$ rar a FileName.rar
Compressed: [*******]$ rar e FileName.rar

16. View the path of the process
cd /proc/29565 ll

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326059668&siteId=291194637