iostat工具 : 相似sar,用于监控cpu和磁盘IO性能linux
安装:ios
[root@localhost ~]# yum -y install sysstat
*与sar工具在同一个包中,如已安装sar,便可直接使用iostatweb
使用:shell
[root@localhost ~]# iostat -x Linux 3.10.0-957.el7.x86_64 (localhost.localdomain) 2019年09月24日 _x86_64_ (1 CPU) avg-cpu: %user %nice %system %iowait %steal %idle 0.08 0.00 0.14 0.01 0.00 99.77 Device: rrqm/s wrqm/s r/s w/s rkB/s wkB/s avgrq-sz avgqu-sz await r_await w_await svctm %util sda 0.00 0.01 0.07 0.11 2.63 4.52 80.50 0.00 3.17 0.97 4.68 0.99 0.02 scd0 0.00 0.00 0.01 0.00 0.11 0.00 21.98 0.00 1.43 1.43 0.00 1.37 0.00 dm-0 0.00 0.00 0.07 0.12 2.49 4.50 75.86 0.00 3.30 1.02 4.64 0.95 0.02 dm-1 0.00 0.00 0.00 0.00 0.03 0.00 54.67 0.00 0.24 0.24 0.00 0.20 0.00
-x 参数:输出更详细的IO设备信息(主要是最后一列:util) avg-cpu:cpu使用状况统计 iowait列:表示cpu用于等待IO请求的完成时间(值超过50%,或者明显大于%system、%user以及%idle,表示IO可能存在问题。) rkB/s:每秒读取数据量(KB) wkB/s:每秒写入数据量(KB) util:IO操做占用cpu的时间,表示磁盘忙碌状况(若是值超过50%但每秒读写数据量又不高表示磁盘可能存在问题)vim
iotop: 相似top,区别在于top监控进程的cpu和内存使用率,iotop监控程序的IO使用情况缓存
安装:网络
[root@localhost dev]# yum -y install iotop
IO列:表示进程的IO使用情况 按o键:只显示有IO操做的进程多线程
free命令:查看内存和swap使用状况dom
使用:ssh
[root@localhost dev]# free total used free shared buff/cache available Mem: 1005628 133616 411228 7652 460784 668556 Swap: 2097148 0 2097148 [root@localhost dev]# free -h total used free shared buff/cache available Mem: 982M 130M 401M 7.5M 449M 653M Swap: 2.0G 0B 2.0G
-h 参数:以MB为单位显示 total:内存总量 used:已使用量 free:空闲内存 buff:缓冲空间 cache:缓存空间 available:可以使用空间
cache与buffer的区别:
磁盘--->内存(cache)--->cpu 当cpu须要读取磁盘数据时,数据会先到内存中,供cpu读取、处理、计算等操做,此时数据存在内存中的cache缓存空间中
cpu--->内存(buffer)--->磁盘 当cpu处理的数据要被写入磁盘时,数据会先进入内存的buffer空间中,在写入磁盘
补充: total=used+free+buff/cache avaliable=free+buff/cache中剩余未使用的空闲部分
ps命令:输出进程信息(静态显示) -aux 参数:显示完整信息 -ef 参数:显示信息没有aux详细(通常用于查找进程的PID,kill进程)
使用:
USER:启动进程的用户 PID:进程号 STAT:进程状态 START:启动时间 TIME:运行了多久 COMMAND:进程名
根据进程号杀进程:
[root@localhost dev]# ps -ef|grep auditd root 91 2 0 9月23 ? 00:00:00 [kauditd] root 5080 1 0 9月23 ? 00:00:00 /sbin/auditd root 16496 15918 0 20:49 pts/1 00:00:00 grep --color=auto auditd [root@localhost dev]# kill 5080
STAT进程状态: D:不能中断的进程 R:run,运行状态的进程 S:休眠状态的进程(不运行时进入休眠状态的进程,运行时变为S+) T:暂停的进程 Z:僵尸进程 <:高优先级进程 N:低优先级进程 L:内存中被锁内存分页 s:主进程 l:多线程进程(sl包含多个线程的主进程) +:前台运行的进程
参数: -t : 指明显示TCP端口 -u : 指明显示UDP端口 -l : 仅显示监听套接字 -p : 显示进程标识符和程序名称,每个套接字/端口都属于一个程序。 -n : 不进行DNS轮询,显示IP(能够加速操做)
查看网络监听端口:-lnp参数
[root@localhost dev]# netstat -lnp Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 6078/sshd tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 6231/master tcp6 0 0 :::22 :::* LISTEN 6078/sshd tcp6 0 0 ::1:25 :::* LISTEN 6231/master udp 0 0 127.0.0.1:323 0.0.0.0:* 5366/chronyd udp6 0 0 ::1:323 :::* 5366/chronyd raw6 0 0 :::58 :::* 7 5594/NetworkManager
只查看tcp类型的端口:-lntp参数
[root@localhost dev]# netstat -lntp Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 6078/sshd tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 6231/master tcp6 0 0 :::22 :::* LISTEN 6078/sshd tcp6 0 0 ::1:25 :::* LISTEN 6231/master
查看系统端口使用状况:
[root@localhost dev]# netstat -anp
ss命令:与netstat -an相似(不能显示PID和程序名)
[root@localhost dev]# ss -an
显示tcp各类链接状态的数量:netstat -an | awk '/^tcp/ {++sta[$NF]} END {for(key in sta) print key,"\t",sta[key]}'
[root@localhost dev]# netstat -an | awk '/^tcp/ {++sta[$NF]} END {for(key in sta) print key,"\t",sta[key]}' LISTEN 4 ESTABLISHED 1
安装:
[root@localhost ~]# yum -y install tcpdump
用法:
[root@localhost ~]# tcpdump -nn -i ens33
-nn:表示将两个(源地址/目标地址)hostname以ip显示 -i:指定网卡名称
指定端口:
[root@localhost ~]# tcpdump -nn -i ens33 port 80
*只抓80端口的数据包
取反:(只抓除了80端口的全部包)
[root@localhost ~]# tcpdump -nn -i ens33 not port 80
多条件:
[root@localhost ~]# tcpdump -nn -i ens33 not port 80 and host 192.168.234.1
*只抓 ip为192.168.234.1的地址除了80端口的包
-c参数:指定抓包数量
[root@localhost ~]# tcpdump -nn -i ens33 -c 10
-w参数:将数据包写入指定文件
[root@localhost ~]# tcpdump -nn -i ens33 -c 5 -w /tmp/1.cap tcpdump: listening on ens33, link-type EN10MB (Ethernet), capture size 262144 bytes 5 packets captured 5 packets received by filter 0 packets dropped by kernel
查看数据包文件:tcpdump -r
[root@localhost ~]# tcpdump -r /tmp/1.cap reading from file /tmp/1.cap, link-type EN10MB (Ethernet) 23:04:16.355507 IP localhost.localdomain.ssh > 192.168.234.1.52014: Flags [P.], seq 2733320790:2733320922, ack 2721249905, win 266, length 132 23:04:16.355793 IP 192.168.234.1.52014 > localhost.localdomain.ssh: Flags [.], ack 132, win 8211, length 0 23:04:22.622548 IP 192.168.234.1.52014 > localhost.localdomain.ssh: Flags [P.], seq 1:69, ack 132, win 8211, length 68 23:04:22.622708 IP localhost.localdomain.ssh > 192.168.234.1.52014: Flags [P.], seq 132:184, ack 69, win 266, length 52 23:04:22.667220 IP 192.168.234.1.52014 > localhost.localdomain.ssh: Flags [.], ack 184, win 8210, length 0
补充: tshark工具: 查看指定网卡80端口的web访问状况
安装:
[root@localhost ~]# yum -y install wireshark
命令:
[root@localhost ~]# tshark -n -t a -R http.request -T fields -e "frame.time" -e "ip.src" -e "http.host" -e "http.request.method" -e "http.request.uri"
*能够查看访问时间、访问IP、访问的域名、访问的文件、HTTP请求类型(相似web访问日志)
ifconfig命令用于查看或更改网卡配置信息,启动或关闭网卡
安装:
[root@localhost ~]# yum -y install net-tools
使用:
[root@localhost ~]# ifconfig -a ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.234.128 netmask 255.255.255.0 broadcast 192.168.234.255 inet6 fe80::777d:eb3e:9e03:3457 prefixlen 64 scopeid 0x20<link> ether 00:0c:29:10:01:94 txqueuelen 1000 (Ethernet) RX packets 253157 bytes 67090793 (63.9 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 343338 bytes 103040732 (98.2 MiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 inet6 ::1 prefixlen 128 scopeid 0x10<host> loop txqueuelen 1000 (Local Loopback) RX packets 120 bytes 10496 (10.2 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 120 bytes 10496 (10.2 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
-a参数:显示全部网卡信息,如未加-a参数,只显示已激活网卡的信息
ifdown:关闭网卡
[root@localhost ~]# ifdown ens33
ifup:启动网卡
[root@localhost ~]# ifup ens33
经常使用于在修改了网卡配置后重启单个指定的网卡,而不须要重启网络服务影响全部网卡:
[root@localhost ~]# ifdown ens33 && ifup ens33
添加虚拟网卡:ens33:01
[root@localhost ~]# cd /etc/sysconfig/network-scripts/ [root@localhost network-scripts]# cp ifcfg-ens33 ifcfg-ens33\:01
修改虚拟网卡配置文件:
[root@localhost network-scripts]# vim ifcfg-ens33:01
修改如下内容:NAME/DEVICE/IPADDR
重启网卡:
[root@localhost ~]# ifdown ens33 && ifup ens33
虚拟网卡已在运行:
[root@localhost ~]# ifconfig -a ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.234.128 netmask 255.255.255.0 broadcast 192.168.234.255 inet6 fe80::777d:eb3e:9e03:3457 prefixlen 64 scopeid 0x20<link> ether 00:0c:29:10:01:94 txqueuelen 1000 (Ethernet) RX packets 271863 bytes 87467742 (83.4 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 349091 bytes 103525345 (98.7 MiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 ens33:01: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.234.129 netmask 255.255.255.0 broadcast 192.168.234.255 ether 00:0c:29:10:01:94 txqueuelen 1000 (Ethernet)
mii-tool命令:查看网络接口状态
[root@localhost ~]# mii-tool ens33 ens33: negotiated 1000baseT-FD flow-control, link ok
*最后一列 link值为ok表示网卡已链接
ethtool命令:与mii-tool命令相似
[root@localhost ~]# ethtool ens33
*最后一行 Link detected值为yes表示网卡已链接
linux下更改hostname:
临时更改,终端从新链接后生效,系统重启后失效
[root@localhost ~]# hostname test
永久更改:
[root@test ~]# hostnamectl set-hostname linux [root@linux ~]# cat /etc/hostname linux
DNS配置文件:
[root@linux ~]# cat /etc/resolv.conf # Generated by NetworkManager nameserver 192.168.234.2
*该文件中的DNS配置信息来自于网卡配置文件中定义的DNS地址,能够临时更改
域名重定向文件:
[root@linux ~]# cat /etc/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
*可使单个或多个域名在访问时指向指定的 IP地址
[root@linux ~]# echo '192.168.234.129 www.baidu.com' >> /etc/hosts
*将访问百度时的地址指向虚拟网卡地址192.168.234.129
[root@linux ~]# ping www.baidu.com PING www.baidu.com (192.168.234.129) 56(84) bytes of data. 64 bytes from www.baidu.com (192.168.234.129): icmp_seq=1 ttl=64 time=0.154 ms 64 bytes from www.baidu.com (192.168.234.129): icmp_seq=2 ttl=64 time=0.111 ms
*域名重定向只在本机生效