linux基础命令介绍六:网络

Posted 51reboot运维开发

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了linux基础命令介绍六:网络相关的知识,希望对你有一定的参考价值。

不 断 前 行,方 可 不 被 淘 汰


本文将讲述网络相关命令,作者假定读者具备TCP/IP协议栈的基础知识。对于相关命令及其输出只介绍它的基本的使用方法和大概的描述,具体协议将不作详细解释。

如今网络无疑是很重要的,linux系统中提供了丰富的网络测试与管理命令。我们来一起看看它们。

1、ping 发送TCMP回显请求报文,并等待返回TCMP回显应答。

ping [OPTIONS]... destination

选项-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
[root@centos7 ~]# ping www.a.com
ping: unknown host www.a.com
[root@centos7 ~]# ping 192.168.0.1
PING 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
[root@centos7 ~]# ping -c2 10.0.1.2
PING 10.0.1.2 (10.0.1.2) 56(84) bytes of data. From 10.0.1.254 icmp_seq=1 Destination Host Unreachable From 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 999ms
pipe 2

当有目的IP的路由但无法达到时显示目标不可达错误(Destination Host Unreachable)。
ICMP回显应答还包括超时(request time out)等其他类型。

2、hostname显示或设置系统主机名

hostname [OPTIONS]... [NAME]

直接执行命令hostname时将显示主机名:

[root@centos7 temp]# hostname
centos7 [root@centos7 temp]#

这个主机名是系统的gethostname(2)函数返回的。
可以通过执行命令hostname NAME来临时改变主机名:

[root@centos7 temp]# hostname NAME
[root@centos7 temp]# hostname
NAME

3、host DNS查询

host name
[root@centos7 temp]# host www.baidu.com
www.baidu.com is an alias for www.a.shifen.com. www.a.shifen.com has address 61.135.169.121
www.a.shifen.com has address 61.135.169.125

4、dig DNS查询

dighost命令的语法一致,但提供了更详细的信息和更多的选项:

[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.                 IN      A

;; ANSWER SECTION:
www.baidu.com.          113     IN      CNAME   www.a.shifen.com. www.a.shifen.com.       113     IN      A       61.135.169.125
www.a.shifen.com.       113     IN      A       61.135.169.121

;; Query time: 2 msec ;; SERVER: 223.5.5.5#53(223.5.5.5)
;; WHEN:1110 12:31:20 CST 2016
;; MSG SIZE  rcvd: 90

[root@centos7 ~]#

如只查询域名的A记录并以短格式显示:

[root@centos7 ~]# dig www.baidu.com A +short
www.a.shifen.com.
61.135.169.125
61.135.169.121
[root@centos7 ~]#

或者:

[root@centos7 ~]# dig +nocmd www.baidu.com A +noall +answer     
www.baidu.com.          252     IN      CNAME   www.a.shifen.com. www.a.shifen.com.       252     IN      A       61.135.169.125
www.a.shifen.com.       252     IN      A       61.135.169.121

还可以用@server的方式指定DNS服务器:

[root@centos7 ~]# dig +noall +answer www.baidu.com A @8.8.8.8
www.baidu.com.          21      IN      CNAME   www.a.shifen.com. www.a.shifen.com.       263     IN      A       61.135.169.125
www.a.shifen.com.       263     IN      A       61.135.169.121

更多的命令及选项请自行man

5、traceroutetracepath 路由跟踪

[root@centos7 ~]# tracepath www.baidu.com 1?: [LOCALHOST]                                         pmtu 1500 1:  10.0.1.103                                            0.396ms 1:  10.0.1.103                                            0.350ms 2:  210.51.161.1                                          1.187ms asymm  3 3:  210.51.161.1                                          8.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 ....

6、ifconfig 配置网络接口

当命令没有任何参数时显示所有网络接口的信息:

[root@centos7 ~]# ifconfig
ens32: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500        inet 172.20.71.254  netmask 255.255.255.0  broadcast 172.20.71.255        inet6 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 0        TX packets 12  bytes 888 (888.0 B)        
       TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500        inet 10.0.1.254  netmask 255.255.255.0  broadcast 10.0.1.255        inet6 fe80::250:56ff:fea4:a09  prefixlen 64  scopeid 0x20<link>        ether 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 0        TX 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 65536        inet 127.0.0.1  netmask 255.0.0.0        inet6 ::1  prefixlen 128  scopeid 0x10<host>        loop  txqueuelen 1  (Local Loopback)        
       RX packets 0  bytes 0 (0.0 B)        
       RX errors 0  dropped 0  overruns 0  frame 0        TX packets 0  bytes 0 (0.0 B)        
       TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0[root@centos7 ~]#
[root@idc-v-71253 ~]# ifconfig -s ens32
Iface      MTU    RX-OK RX-ERR RX-DRP RX-OVR    TX-OK TX-ERR TX-DRP TX-OVR Flg ens32     1500 11996951      0      0 0            12      0      0      0 BMRU
[root@centos7 ~]# ifconfig ens33:0 10.0.1.4/24 up
[root@centos7 ~]# ifconfig ens33:0  
ens33:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500        inet 10.0.1.4  netmask 255.255.255.0  broadcast 10.0.1.255        ether 00:50:56:a4:0a:09  txqueuelen 1000  (Ethernet)
[root@centos7 ~]# ifconfig ens33:0 down

7、arparping

命令arp显示系统的arp缓存,命令arping给邻居主机发送ARP请求。

[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
[root@centos7 ~]# arping 10.0.1.252 -I ens33
ARPING 10.0.1.252 from 10.0.1.254 ens33 Unicast reply from 10.0.1.252 [00:50:56:A4:65:71]  0.843ms Unicast reply from 10.0.1.252 [00:50:56:A4:0A:09]  1.034ms
[root@centos7 ~]# arping -c3 -I ens33 -s 10.0.1.254 10.0.1.1
ARPING 10.0.1.1 from 10.0.1.254 ens33
Unicast reply from 10.0.1.1 [68:8F:84:01:F1:FF]  19.466ms
Unicast reply from 10.0.1.1 [68:8F:84:01:F1:FF]  2.358ms
Unicast reply from 10.0.1.1 [68:8F:84:01:F1:FF]  24.305ms
Sent 3 probes (1 broadcast(s))
Received 3 response(s)

linux基础命令介绍六:网络


相关链接:

1、

2、

3、

4、

5

6、

7、

8、

9、



课程信息:

课程:第13期Python 实战班

时间:2017年 2月19日(周日)

大纲:http://51reboot.com/course/actual/  (点击阅读原文)

咨询QQ:279312229     979950755 (春节前报名可享优惠)



长按关注——Reboot

技术交流群:238757010



戳原文,更有料!

以上是关于linux基础命令介绍六:网络的主要内容,如果未能解决你的问题,请参考以下文章

linux基础-第十六单元 yum管理RPM包

Linux操作基础

Linux系统下基础命令介绍

Linux系统下基础命令介绍

Linux系统下基础命令介绍

#导入Word文档图片# Linux系统与终端基础命令介绍