
如今网络无疑是很重要的,linux系统中提供了丰富的网络测试与管理命令。我们来一起看看它们。
内容如下:
1、ping 发送TCMP回显请求报文,并等待返回TCMP回显应答。
ping [OPTIONS]... destination
这里的目标destination可以是目的IP地址或者域名/主机名
选项-c指定发送请求报文的次数,当ping没有任何选项时,在linux中默认将一直发送请求报文直到手动终止。
[root@centos7 ~]# ping -c 3 www.baidu.com PING www.a.shifen.com (61.135.169.121) 56(84) bytes of data. 64 bytes from 61.135.169.121: icmp_seq=1 ttl=52 time=1.35 ms 64 bytes from 61.135.169.121: icmp_seq=2 ttl=52 time=1.32 ms 64 bytes from 61.135.169.121: icmp_seq=3 ttl=52 time=1.22 ms --- www.a.shifen.com ping statistics --- 3 packets transmitted, 3 received, 0% packet loss, time 2003ms rtt min/avg/max/mdev = 1.225/1.303/1.359/0.064 ms首先,ping程序会向域名服务器(DNS)发送请求,解析域名www.baidu.com的IP地址。DNS返回域名的一个别名www.a.shifen.com以及对应的IP地址61.135.169.121。之后ping程序开始向这个地址发送请求报文,每1s发送一个,ping收到TCMP回显应答并将结果显示在终端上,包括ICMP序列号(icmp_seq),生存时间(ttl)和数据包往返时间(time)。最后,给出汇总信息,包括报文总收发情况,总时间,往返时间最小值、平均值、最大值、平均偏差(越大说明网络越不稳定)。
[root@centos7 ~]# ping www.a.comping: unknown host www.a.com当目的域名无法解析出IP地址时,会报未知主机的错
[root@centos7 ~]# ping 192.168.0.1PING 192.168.0.1 (192.168.0.1) 56(84) bytes of data.^C #这里按CTRL+C键手动终止了进程--- 192.168.0.1 ping statistics ---6 packets transmitted, 0 received, 100% packet loss, time 4999ms当目的IP地址没有路由时不会收到任何ICMP回显报文
[root@centos7 ~]# ping -c2 10.0.1.2PING 10.0.1.2 (10.0.1.2) 56(84) bytes of data.From 10.0.1.254 icmp_seq=1 Destination Host UnreachableFrom 10.0.1.254 icmp_seq=2 Destination Host Unreachable--- 10.0.1.2 ping statistics ---2 packets transmitted, 0 received, +2 errors, 100% packet loss, time 999mspipe 2当有目的IP的路由但无法达到时显示目标不可达错误(Destination Host Unreachable)。
[root@centos7 temp]# hostnamecentos7[root@centos7 temp]#这个主机名是系统的gethostname(2)函数返回的。
[root@centos7 temp]# hostname NAME[root@centos7 temp]# hostnameNAME这个临时修改实际上是修改了linux kernel中一个同为hostname的内核参数,它保存在/proc/sys/kernel/hostname中。如果需要永久修改则需要修改配置文件/etc/sysconfig/network,centos7中需要修改/etc/hostname。需要注意的是,如果配置文件中的主机名是localhost或localhost.localdomain时,系统会取得网络接口的IP地址,并用这个地址找出/etc/hosts文件中对应的主机名,然后将其设置成最终的hostname。
[root@centos7 temp]# host www.baidu.comwww.baidu.com is an alias for www.a.shifen.com.www.a.shifen.com has address 61.135.169.121www.a.shifen.com has address 61.135.169.1254、dig DNS
[root@centos7 ~]# dig www.baidu.com; <<>> DiG 9.9.4-RedHat-9.9.4-29.el7_2.2 <<>> www.baidu.com;; global options: +cmd;; Got answer:;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 22125;; flags: qr rd ra; QUERY: 1, ANSWER: 3, AUTHORITY: 0, ADDITIONAL: 0;; QUESTION SECTION:;www.baidu.com. INA;; ANSWER SECTION:www.baidu.com. 113INCNAME www.a.shifen.com.www.a.shifen.com.113INA61.135.169.125www.a.shifen.com.113INA61.135.169.121;; Query time: 2 msec;; SERVER: 223.5.5.5#53(223.5.5.5);; WHEN: 四 11月 10 12:31:20 CST 2016;; MSG SIZE rcvd: 90[root@centos7 ~]#如只查询域名的A记录并以短格式显示:
[root@centos7 ~]# dig www.baidu.com A +shortwww.a.shifen.com.61.135.169.12561.135.169.121[root@centos7 ~]#或者:
[root@centos7 ~]# dig +nocmd www.baidu.com A +noall +answerwww.baidu.com. 252INCNAME www.a.shifen.com.www.a.shifen.com.252INA61.135.169.125www.a.shifen.com.252INA61.135.169.121还可以用@server的方式指定DNS服务器:
[root@centos7 ~]# dig +noall +answer www.baidu.com A @8.8.8.8www.baidu.com. 21INCNAME www.a.shifen.com.www.a.shifen.com.263INA61.135.169.125www.a.shifen.com.263INA61.135.169.121更多的命令及选项请自行man
[root@centos7 ~]# tracepath www.baidu.com1?: [LOCALHOST]pmtu 1500 1: 10.0.1.1030.396ms 1: 10.0.1.1030.350ms 2: 210.51.161.11.187ms asymm 3 3: 210.51.161.18.186ms 4: 210.51.175.81 1.117ms 5: 61.148.142.61 8.554ms asymm 12 6: 61.148.147.13 1.694ms asymm 12 7: 123.126.8.117 3.934ms asymm 10 8: 61.148.155.46 2.703ms asymm 10 ....这里只列出部分输出,表示跟踪到目的地址的路由,每一跳都返回。
[root@centos7 ~]# ifconfigens32: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500inet 172.20.71.254 netmask 255.255.255.0 broadcast 172.20.71.255inet6 fe80::250:56ff:fea4:fe34 prefixlen 64 scopeid 0x20<link>ether 00:50:56:a4:fe:34 txqueuelen 1000 (Ethernet)RX packets 11996157 bytes 775368588 (739.4 MiB)RX errors 0 dropped 0 overruns 0 frame 0TX packets 12 bytes 888 (888.0 B)TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500inet 10.0.1.254 netmask 255.255.255.0 broadcast 10.0.1.255inet6 fe80::250:56ff:fea4:a09 prefixlen 64 scopeid 0x20ether 00:50:56:a4:0a:09 txqueuelen 1000 (Ethernet)RX packets 20941185 bytes 1307830447 (1.2 GiB)RX errors 0 dropped 0 overruns 0 frame 0TX packets 147552 bytes 11833605 (11.2 MiB)TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536inet 127.0.0.1 netmask 255.0.0.0inet6 ::1 prefixlen 128 scopeid 0x10loop txqueuelen 1 (Local Loopback)RX packets 0 bytes 0 (0.0 B)RX errors 0 dropped 0 overruns 0 frame 0TX packets 0 bytes 0 (0.0 B)TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0[root@centos7 ~]#本例中显示了两个网卡ens32和ens33以及环回口lo的信息,包括mtu,ip地址,掩码,mac地址,传输和接收数据量等等。
[root@idc-v-71253 ~]# ifconfig -s ens32IfaceMTU RX-OK RX-ERR RX-DRP RX-OVR TX-OK TX-ERR TX-DRP TX-OVR Flgens321500 1199695100 0 12000 BMRU如给ens33增加一个新地址10.0.1.4:
[root@centos7 ~]# ifconfig ens33:0 10.0.1.4/24 up[root@centos7 ~]# ifconfig ens33:0ens33:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500inet 10.0.1.4 netmask 255.255.255.0 broadcast 10.0.1.255ether 00:50:56:a4:0a:09 txqueuelen 1000 (Ethernet)命令中/24表明接口地址的掩码,up表示启用此接口。注意如果ip地址已经被使用,这里依然会被设置成功,但此地址被访问时,可能会有冲突。
[root@idc-v-71253 ~]# arp -a? (10.0.1.1) at 68:8f:84:01:f1:ff [ether] on ens33? (10.0.1.102) at 00:50:56:a4:18:9a [ether] on ens33? (10.0.1.254) at 00:50:56:a4:a9:16 [ether] on ens33? (10.0.1.10) at 00:50:56:a4:d2:e4 [ether] on ens33? (10.0.1.104) at 00:50:56:a4:37:a7 [ether] on ens33?表示未知域名,最后的网卡名表示arp表项对应的网络接口
[root@centos7 ~]# arping 10.0.1.252 -I ens33ARPING 10.0.1.252 from 10.0.1.254 ens33Unicast reply from 10.0.1.252 [00:50:56:A4:65:71] 0.843msUnicast reply from 10.0.1.252 [00:50:56:A4:0A:09] 1.034ms这里两条返回信息中的MAC地址不同,说明有两块网卡配置了相同的IP地址。选项-I指定发送arp请求的网络接口。
[root@centos7 ~]# arping -c3 -I ens33 -s 10.0.1.254 10.0.1.1ARPING 10.0.1.1 from 10.0.1.254 ens33Unicast reply from 10.0.1.1 [68:8F:84:01:F1:FF] 19.466msUnicast reply from 10.0.1.1 [68:8F:84:01:F1:FF] 2.358msUnicast reply from 10.0.1.1 [68:8F:84:01:F1:FF] 24.305msSent 3 probes (1 broadcast(s))Received 3 response(s)-c指定发送arp请求次数,-s指定源地址,最后的IP表示发送目标(这里是网关地址)。
[root@centos7 ~]# routeKernel IP routing tableDestinationGateway Genmask Flags Metric Ref Use Iface10.0.1.00.0.0.0 255.255.255.0 U000 ens33link-local0.0.0.0 255.255.0.0U1002 00 ens32link-local0.0.0.0 255.255.0.0U1003 00 ens33172.20.71.00.0.0.0 255.255.255.0 U000 ens32192.168.78.0 10.0.1.104255.255.255.0 UG 000 ens33其中Destination表示目的网段或目标主机;Gateway表示网关地址;Genmask表示目的网段的掩码;Flags表示路由标志:U表示路由是启用(up)的、G表示网关;Metric表示目标距离,通常用跳数表示;Ref表示路由的引用数;Use表示路由查找计数;Iface表示此条路由的出口。
[root@centos7 ~]# telnet 10.0.1.251 80Trying 10.0.1.251...Connected to 10.0.1.251.Escape character is "^]".^] #这里按了CTRL+],也可以按CTRL+C强行退出。telnet> quitConnection closed.这里对方的80端口是开启并允许通信的。当对端端口没有开启时:
[root@centos7 ~]# telnet 10.0.1.251 81Trying 10.0.1.251...telnet: connect to address 10.0.1.251: No route to host当对端拒绝连接时:
[root@centos7 ~]# telnet 10.0.1.251 8085Trying 10.0.1.251...telnet: connect to address 10.0.1.251: Connection refused10、ssh 远程登录程序
[root@centos7 ~]# ssh 10.0.1.253The authenticity of host "10.0.1.253 (10.0.1.253)" can"t be established.ECDSA key fingerprint is 96:bd:a3:a7:87:09:1b:53:44:4c:9b:b9:5f:b2:97:89.Are you sure you want to continue connecting (yes/no)? yes #这里输入yesWarning: Permanently added "10.0.1.253" (ECDSA) to the list of known hosts.root@10.0.1.253"s password: #这里输入密码Last login: Fri Nov 11 09:04:01 2016 from 192.168.78.137[root@idc-v-71253 ~]# #已登录当命令ssh后直接跟主机IP时表示使用默认用户root登录,如果是首次登录,需要确认添加该主机的认证key,当输入yes后,即会在本机/root/.ssh/known_hosts中增加一条该主机的记录,下一次登录时就不用再次确认了。然后需要输入用户密码,通过验证之后,我们就获得了目的主机的一个shell,我们就可以在这个shell中执行命令了。
[root@centos7 ~]# ssh-keygen -t rsaGenerating public/private rsa key pair.Enter file in which to save the key (/root/.ssh/id_rsa): #回车Enter passphrase (empty for no passphrase): #回车Enter same passphrase again: #回车Your identification has been saved in /root/.ssh/id_rsa. #私钥Your public key has been saved in /root/.ssh/id_rsa.pub. #公钥The key fingerprint is:be:c3:d0:02:50:35:35:fe:60:d6:2f:26:96:f0:e1:e6 root@centos7The key"s randomart image is:+--[ RSA 2048]----+| ...o.o|| . o o|| . . * .|| . * = .||. .S + . ||o=.o .||+E||o.||..|+-----------------+[root@centos7 ~]#[root@centos7 ~]# ssh-copy-id 10.0.1.253/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keysroot@10.0.1.253"s password:Number of key(s) added: 1Now try logging into the machine, with: "ssh "10.0.1.253""and check to make sure that only the key(s) you wanted were added.[root@centos7 ~]#其中命令ssh-keygen用来生成公钥私钥,选项-t指明密钥类型。之后使用命令ssh-copy-id将公钥发送至目标主机,这里需要输入目标主机用户密码。然后就可以免密码登录了:
[root@centos7 ~]# ssh 10.0.1.253Last login: Fri Nov 11 11:08:37 2016 from 10.0.1.254[root@idc-v-71253 ~]#还可以通过ssh远程执行命令:
[root@centos7 ~]# ssh 10.0.1.252 "hostname"root@10.0.1.252"s password: #输入密码idc-v-71252 #显示命令结果[root@centos7 ~]# #并不登录或者手动将公钥拷贝至目标主机:
[root@centos7 ~]# cat /root/.ssh/id_rsa.pub | ssh 10.0.1.252 "cat - >> /root/.ssh/authorized_keys"root@10.0.1.252"s password: #输入密码[root@centos7 ~]# ssh 10.0.1.252#免密登录Last login: Thu Nov 10 14:42:11 2016 from 192.168.78.135[root@idc-v-71252 ~]#选项-p为登录指定端口:
[root@centos7 temp]# ssh -p22 10.0.1.252Last login: Fri Nov 11 11:44:31 2016 from 10.0.1.254[root@idc-v-71252 ~]#端口设置在服务端配置文件/etc/ssh/sshd_config中,默认端口号为22,如更改需将#Port 22去掉注释并将22更改为需要的端口,然后重启sshd服务service sshd restart或systemctl restart sshd。
tar zc /home/temp | ssh user@host "tar xz" #本地temp目录备份到远程ssh user@host "tar cz /home/temp" | tar xz #远程temp目录备份到本地选项-L [bind_address:]port:host:hostport设置本地端口转发
[root@centos7 ~]# ssh -L 2222:10.0.1.252:22 10.0.1.253Last login: Mon Nov 14 10:34:43 2016 from 10.0.1.254[root@idc-v-71253 ~]# #注意如果这里exit断开连接,则此转发也将终止。此命令的意思是绑定本地端口2222,并将所有发送至此端口的数据通过中间主机10.0.1.253转发至目标主机10.0.1.252的22端口,此时如果用ssh登录本机的2222端口,则实际登录的是主机10.0.1.252
[root@centos7 ~]# ssh -p 2222 127.0.0.1Last login: Mon Nov 14 10:34:56 2016 from 10.0.1.253[root@idc-v-71252 ~]#这里默认绑定的是本机的环回口127.0.0.1,如绑定到其他地址,则根据语法设置bind_address。
[root@centos7 ~]# ssh -N -L 2222:10.0.1.252:22 10.0.1.253 &[1] 12432[root@centos7 ~]#命令最后的符号&表示此命令将在后台执行,返回的信息中[1]表示后台命令编号,12432表示命令的PID。(关于shell后台命令,以后的文章中会有叙述)
[root@centos7 ~]# ssh -p 2222 localhostLast login: Mon Nov 14 10:40:44 2016 from 10.0.1.253[root@idc-v-71252 ~]#这里的意思是使远程主机10.0.1.254(相对10.0.1.253来说)监听端口2222,然后将所有发送至此端口的数据转发至目标主机10.0.1.252的端口22。之后再在10.0.1.254登录本地(localhost)的2222端口时,实际通过中间主机10.0.1.253登录目标主机10.0.1.252。
[root@centos7 ~]# scp root@10.0.1.251:/root/a.txt ./root@10.0.1.251"s password:a.txt100% 1250.1KB/s 00:00[root@centos7 ~]#命令会显示传输状态(传输百分比,大小,速度,用时)。
[root@centos7 ~]# netstat -aActive Internet connections (servers and established)Proto Recv-Q Send-Q Local Address Foreign Address Statetcp00 0.0.0.0:ssh0.0.0.0:*LISTENtcp00 localhost:smtp 0.0.0.0:*LISTENtcp052 10.0.1.254:ssh 192.168.78.143:49583 ESTABLISHEDtcp600 [::]:commplex-main[::]:* LISTENtcp600 [::]:4243[::]:* LISTENtcp600 [::]:ssh[::]:* LISTENtcp600 localhost:smtp [::]:* LISTENraw600 [::]:ipv6-icmp [::]:* 7raw600 [::]:ipv6-icmp [::]:* 7Active UNIX domain sockets (servers and established)Proto RefCnt FlagsTypeState I-Node Pathunix 2[ ACC ]STREAMLISTENING12807 /run/systemd/privateunix 2[ ACC ]STREAMLISTENING12815 /run/lvm/lvmpolld.socketunix 2[ ] DGRAM 12818 /run/systemd/shutdowndunix 2[ ACC ]STREAMLISTENING16403 /var/run/dbus/system_bus_socket....这里只显示部分信息
[root@centos7 ~]# netstat -antpActive Internet connections (servers and established)Proto Recv-Q Send-Q Local Address Foreign Address StatePID/Program nametcp00 0.0.0.0:220.0.0.0:*LISTEN1358/sshdtcp00 127.0.0.1:25 0.0.0.0:*LISTEN2162/mastertcp052 10.0.1.254:22 192.168.78.143:49583 ESTABLISHED 12044/sshd: root@pttcp600 :::5000 :::* LISTEN17222/docker-proxytcp600 :::4243 :::* LISTEN16983/docker tcp600 :::22 :::* LISTEN1358/sshdtcp600 ::1:25 :::* LISTEN2162/master[root@centos7 ~]#其中Proto表示协议(包括TCP、UDP等);Recv-Q和Send-Q表示接收和发送队列,一般都为0,如果非0则表示本地的接收或发送缓存区有数据等待处理;Local Address和Foreign Address分别表示本地地址和远端地址;State表示连接状态,对应于TCP各种连接状态;PID/Program name表示进程号和程序名。
[root@centos7 ~]# netstat -ntlActive Internet connections (only servers)Proto Recv-Q Send-Q Local Address Foreign Address Statetcp00 0.0.0.0:220.0.0.0:*LISTENtcp00 127.0.0.1:25 0.0.0.0:*LISTENtcp600 :::5000 :::* LISTENtcp600 :::4243 :::* LISTENtcp600 :::22 :::* LISTENtcp600 ::1:25 :::* LISTEN[root@centos7 ~]#选项-u表示显示UDP连接信息
[root@centos7 ~]# netstat -rKernel IP routing tableDestinationGateway Genmask Flags MSS Window irtt Ifacedefault 10.0.1.1030.0.0.0 UG0 0 0 ens3310.0.1.00.0.0.0 255.255.255.0 U 0 0 0 ens33172.20.71.00.0.0.0 255.255.255.0 U 0 0 0 ens32192.168.78.0 10.0.1.104255.255.255.0 UG0 0 0 ens33选项-i显示接口信息
[root@centos7 ~]# netstat -iKernel Interface tableIfaceMTU RX-OK RX-ERR RX-DRP RX-OVR TX-OK TX-ERR TX-DRP TX-OVR Flgens321500 13196107077 0 3246000 BMRUens331500 25312388088 02516050000 BMRUlo65536 250358900 02503589000 LRU13、tcpdump 网络抓包工具
[root@idc-v-71253 ~]# tcpdump -i ens33tcpdump: verbose output suppressed, use -v or -vv for full protocol decodelistening on ens33, link-type EN10MB (Ethernet), capture size 65535 bytes15:41:59.121948 IP 10.0.1.108.3693 > 239.100.1.1.websm: UDP, length 5815:41:59.122191 IP 10.0.1.109.35673 > 239.100.1.1.websm: UDP, length 57 15:41:59.128282 IP 10.0.1.253.ssh > 192.168.78.143.51694: Flags [P.], seq 749565300:749565496, ack 3522345564, win 255, length 196 15:41:59.134127 IP 192.168.78.143.51694 > 10.0.1.253.ssh: Flags [.], ack 196, win 3977, length 015:41:59.140319 ARP, Request who-has 10.0.1.31 tell 10.0.1.102, length 4615:41:59.168328 ARP, Request who-has 10.0.1.37 tell 10.0.1.102, length 4615:41:59.262235 ARP, Request who-has 192.168.10.150 tell 192.168.10.151, length 4615:41:59.622090 IP 10.0.1.108.3693 > 239.100.1.1.websm: UDP, length 5815:41:59.622178 IP 10.0.1.109.35673 > 239.100.1.1.websm: UDP, length 57....启动命令之后显示出可以使用-v或-vv显示更详细的信息,开始从ens33捕获数据包。输出显示出各个发送或接收数据包包头信息(包括ARP、IP、TCP、UDP等等协议)。此命令并未指定expression,所以默认将捕获所有数据包。
tcpdump -i ens33 dst host 10.0.1.251#监视所有从端口ens33发送到主机10.0.1.251的数据包,主机也可以是主机名tcpdump -i eth0 host ! 211.161.223.70 and ! 211.161.223.71 and dst port 80#监听端口eth0,抓取不是来自或去到主机211.161.223.70和211.161.223.71并且目标端口为80的包tcpdump tcp port 23 host 210.27.48.1#获取主机210.27.48.1接收或发出的telnet包tcpdump "tcp port 80 and (((ip[2:2] - ((ip[0]&0xf)<<2)) - ((tcp[12]&0xf0)>>2)) != 0) and src net (183.60.190 or 122.13.220)" -s0 -i eth0 -w ipdump#抓取源或目的端口是80,且源网络是(183.60.190.0/24 或者 122.13.220.0/24),并且含有数据,而不是SYN,FIN以及ACK-only等不含数据的TCP数据包写入文件ipdump#注意这里表达式使用单引号引起来以避免其中的特殊字符被shell解析而造成语法错误tcpdump "tcp[tcpflags] & (tcp-syn|tcp-fin) != 0 and ! src and dst net 10.0.0"#只打印TCP的开始和结束包(SYN和FIN标记),并且源和目标网段均不是10.0.0.0/24tcpdump "gateway 10.0.1.1 and ip[2:2] > 576"#表示抓取发送至网关10.0.1.1并且大于576字节的IP数据包网络相关命令内容较多,希望能够帮到你。