分享最新优惠信息
购买主机更加划算

12个Linux系统网络配置和故障排除命令汇总整理

计算机在网络中是被连接起来的,从而相互交换信息或资源。两台或多台计算机通过网络介质连接被称为计算机网络,有许多网络设备或媒体参与形成的网络就叫做互联网。

安装Linux操作系统的计算机/服务器也可以通过其多任务和多用户性质成为网络的一部分,无论它是小型网络还是大型网络。当然,维护系统和网络的正常运行是Linux系统/网络管理员的主要工作任务。

在本文中,小编将汇总和整理12个Linux系统中常用的网络配置和故障排除命令,无论是初次建站的站长,还是Linux服务器管理人员都可以看看。

1、ifconfig命令

ifconfig ( interface configurator ) 命令用于初始化网络接口,为接口分配IP地址,并按需求合理启用或禁用接口。

使用此命令可以查看分配给接口的IP地址和硬件/MAC地址以及MTU(最大传输单元)大小。

# ifconfig

eth0      Link encap:Ethernet  HWaddr 00:0C:29:28:FD:4C
          inet addr:192.168.50.2  Bcast:192.168.50.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fe28:fd4c/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:6093 errors:0 dropped:0 overruns:0 frame:0
          TX packets:4824 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:6125302 (5.8 MiB)  TX bytes:536966 (524.3 KiB)
          Interrupt:18 Base address:0x2000

lo        Link encap:Local Loopback
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:8 errors:0 dropped:0 overruns:0 frame:0
          TX packets:8 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:480 (480.0 b)  TX bytes:480 (480.0 b)

ifconfig带有interface ( eth0 ) 命令只显示特定的接口详细信息,如IP地址、MAC 地址等。如果ifconfig带有 -a选项的话,将显示所有可用的接口详细信息。

# ifconfig eth0

eth0      Link encap:Ethernet  HWaddr 00:0C:29:28:FD:4C
          inet addr:192.168.50.2  Bcast:192.168.50.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fe28:fd4c/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:6119 errors:0 dropped:0 overruns:0 frame:0
          TX packets:4841 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:6127464 (5.8 MiB)  TX bytes:539648 (527.0 KiB)
          Interrupt:18 Base address:0x2000

在Linux系统中设置IP地址和网关

动态地为接口分配IP地址和网关,如果系统重新启动,该设置将被删除。

# ifconfig eth0 192.168.52.5 netmask 255.255.255.0

启用或禁用特定接口

要启用或禁用特定接口,使用以下命令即可:

启用eth0
# ifup eth0
禁用eth0
# ifup eth0

设置MTU大小

默认MTU大小为1500,可以使用以下命令设置所需的MTU大小,自行替换掉XXX

# ifconfig eth0 mtu XXX

在混杂模式下设置接口

网络接口只接收属于该特定NIC的数据包。如果将接口置于混杂模式,它将接收所有数据包。这对于获取数据包并稍后分析它们非常有用,或许需要超级用户权限才能访问:

# ifconfig eth0 - promisc

注意目前在大多数Linux发行版中,ifconfig命令被IP命令替换。参考:简单分享整理10个配置网络接口的“IP”命令

2. ping命令

Ping Packet Internet Groper)命令是测试两个节点之间连通性的最佳方式。无论是局域网( LAN)还是广域网( WAN)。

Ping使用ICMP(Internet 控制消息协议)与其他设备通信,可以使用以下命令ping主机名或IP地址。

# ping 5.4.4.4

PING 5.4.4.4 (5.4.4.4) 56(84) bytes of data.
64 bytes from 5.4.4.4: icmp_seq=1 ttl=44 time=203 ms
64 bytes from 5.4.4.4: icmp_seq=2 ttl=44 time=201 ms
64 bytes from 5.4.4.4: icmp_seq=3 ttl=44 time=201 ms

或者

# ping www.baidu.com

PING baidu.com (220.181.38.251) 56(84) bytes of data.
64 bytes from 220.181.38.251: icmp_seq=1 ttl=47 time=284 ms
64 bytes from 220.181.38.251: icmp_seq=2 ttl=47 time=287 ms
64 bytes from 220.181.38.251: icmp_seq=3 ttl=47 time=285 ms

在Linux系统中,ping命令会一直执行直到手动中断。-cN次请求(成功或错误响应)后用选项退出Ping 。

# ping -c 5 www.baidu.com

PING baidu.com (220.181.38.251) 56(84) 字节数据。
来自 220.181.38.251 的 64 个字节:icmp_seq=1 ttl=47 time=285 ms
来自 220.181.38.251 的 64 个字节:icmp_seq=2 ttl=47 time=285 ms
来自 220.181.38.251 的 64 个字节:icmp_seq=3 ttl=47 time=285 ms
来自 220.181.38.251 的 64 个字节:icmp_seq=4 ttl=47 time=285 ms
来自 220.181.38.251 的 64 个字节:icmp_seq=5 ttl=47 time=285 ms

--- tecmint.com ping statistics ---
发送 5 个数据包,接收 5 个数据包,0% 数据包丢失,时间 4295ms

3、traceroute 命令

traceroute是一种网络故障排除实用程序,它显示到达目的地所用的跳数也决定了数据包的传输路径。下面将跟踪到全局DNS服务器IP地址的路由,并且能够到达目的地还显示了该数据包的路径。

# traceroute 4.2.2.2

traceroute to 4.2.2.2 (4.2.2.2), 30 hops max, 60 byte packets
 1  192.168.50.1 (192.168.50.1)  0.217 ms  0.624 ms  0.133 ms
 2  227.18.106.27.mysipl.com (27.106.18.227)  2.343 ms  1.910 ms  1.799 ms
 3  221-231-119-111.mysipl.com (111.119.231.221)  4.334 ms  4.001 ms  5.619 ms
 4  10.0.0.5 (10.0.0.5)  5.386 ms  6.490 ms  6.224 ms
 5  gi0-0-0.dgw1.bom2.pacific.net.in (203.123.129.25)  7.798 ms  7.614 ms  7.378 ms
 6  115.113.165.49.static-mumbai.vsnl.net.in (115.113.165.49)  10.852 ms  5.389 ms  4.322 ms
 7  ix-0-100.tcore1.MLV-Mumbai.as6453.net (180.87.38.5)  5.836 ms  5.590 ms  5.503 ms
 8  if-9-5.tcore1.WYN-Marseille.as6453.net (80.231.217.17)  216.909 ms  198.864 ms  201.737 ms
 9  if-2-2.tcore2.WYN-Marseille.as6453.net (80.231.217.2)  203.305 ms  203.141 ms  202.888 ms
10  if-5-2.tcore1.WV6-Madrid.as6453.net (80.231.200.6)  200.552 ms  202.463 ms  202.222 ms
11  if-8-2.tcore2.SV8-Highbridge.as6453.net (80.231.91.26)  205.446 ms  215.885 ms  202.867 ms
12  if-2-2.tcore1.SV8-Highbridge.as6453.net (80.231.139.2)  202.675 ms  201.540 ms  203.972 ms
13  if-6-2.tcore1.NJY-Newark.as6453.net (80.231.138.18)  203.732 ms  203.496 ms  202.951 ms
14  if-2-2.tcore2.NJY-Newark.as6453.net (66.198.70.2)  203.858 ms  203.373 ms  203.208 ms
15  66.198.111.26 (66.198.111.26)  201.093 ms 63.243.128.25 (63.243.128.25)  206.597 ms 66.198.111.26 (66.198.111.26)  204.178 ms
16  ae9.edge1.NewYork.Level3.net (4.68.62.185)  205.960 ms  205.740 ms  205.487 ms
17  vlan51.ebr1.NewYork2.Level3.net (4.69.138.222)  203.867 ms vlan52.ebr2.NewYork2.Level3.net (4.69.138.254)  202.850 ms vlan51.ebr1.NewYork2.Level3.net (4.69.138.222)  202.351 ms
18  ae-6-6.ebr2.NewYork1.Level3.net (4.69.141.21)  201.771 ms  201.185 ms  201.120 ms
19  ae-81-81.csw3.NewYork1.Level3.net (4.69.134.74)  202.407 ms  201.479 ms ae-92-92.csw4.NewYork1.Level3.net (4.69.148.46)  208.145 ms
20  ae-2-70.edge2.NewYork1.Level3.net (4.69.155.80)  200.572 ms ae-4-90.edge2.NewYork1.Level3.net (4.69.155.208)  200.402 ms ae-1-60.edge2.NewYork1.Level3.net (4.69.155.16)  203.573 ms
21  b.resolvers.Level3.net (4.2.2.2)  199.725 ms  199.190 ms  202.488 ms

4、netstat命令

Netstat(网络统计)命令显示连接信息、路由表信息等。要显示路由表信息,请使用选项为-r

# netstat -r

Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
192.168.50.0    *               255.255.255.0   U         0 0          0 eth0
link-local      *               255.255.0.0     U         0 0          0 eth0
default         192.168.50.1    0.0.0.0         UG        0 0          0 eth0

注意目前在大多数现代Linux发行版当中,netstat命令已被ss(socket statistics)命令取代。

5、Dig Command

Dig(域信息搜索器)命令用于查询DNS相关信息,如A记录、CNAME记录、MX记录等,该命令主要用于对DNS 相关查询进行故障排除。

# dig www.idccoupon.com; <<>> DiG 9.8.2rc1-RedHat-9.8.2-0.10.rc1.el6 <<>> www.idccoupon.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<

6、Nslookup 命令

nslookup命令也用于找出与DNS相关的查询,以下示例显示了baidu.com 的A记录(IP地址)。

# nslookup www.baidu.com
Server:         4.2.2.2
Address:        4.2.2.2#53

Non-authoritative answer:
www.baidu.com  canonical name = baidu.com.
Name:  baidu.com 
Address: 220.181.38.251

7、Route命令

route命令还显示和操作ip路由表,要查看Linux系统中的默认路由表,请输入以下命令。

# route

Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.50.0    *               255.255.255.0   U     0      0        0 eth0
link-local      *               255.255.0.0     U     1002   0        0 eth0
default         192.168.50.1    0.0.0.0         UG    0      0        0 eth0

使用以下命令添加、删除路由和默认网关。

在Linux中添加路由
# route add -net 10.10.10.0/24 gw 1​​92.168.0.1
在Linux中删除路由
# route del -net 10.10.10.0/24 gw 1​​92.168.0.1
在Linux中添加默认网关
# route add default gw 192.168.0.1

8、Host命令

Host命令来查找一个域名对应的IP或IP对应的域名,适用于IPv4或IPv6地址,还可以查询DNS记录。

# host www.baidu.com

www.baidu.com has address 220.181.38.148
www.baidu.com has address 220.181.38.251
www.baidu.com has address 220.181.38.253
www.baidu.com has IPv6 address 2404:6800:4003:802::1214

使用-t选项找出DNS资源记录,如CNAMENSMXSOA等。

# host -t CNAME www.redhat.com
www.redhat.com is an alias for wildcard.redhat.com.edgekey.net.

9、arp命令

ARP(地址解析协议)对于查看/添加内核ARP表的内容很有用,要查看默认表,请使用以下命令:

# arp -e

Address                  HWtype  HWaddress           Flags Mask            Iface
192.168.50.1             ether   00:50:56:c0:00:08   C                     eth0

10、Ethtool命令

ethtool是mii-tool的替代品。用于查看、设置网络接口卡( NIC ) 的速度和duplex(双工),可以使用ETHTOOL_OPTS变量在/etc/sysconfig/network-scripts/ifcfg-eth0 中永久设置双工。

# ethtool eth0

Settings for eth0:
        Current message level: 0x00000007 (7)
        Link detected: yes

11、Hostname命令

执行hostname命令以查看你的机器的主机名,可以在/etc/sysconfig/network中永久设置主机名,设置正确的主机名后需要重新启动系统。

# hostname 

idccoupon.com

12、Nmcli和Nmtui工具

Nmcli和Nmtui工具用于配置网络设置,并还可以用来管理Linux系统中的网络设备,创建,修改,激活/停用和删除网络连接。

# nmcli
# nmtui

Nmtui网络管理器

总之,以上就是汇总整理的12个Linux系统网络配置和故障排除命令,仅供参考。其实在对Linux服务器配置和管理网络的时候以上这些命令经常会用到,而且非常有用。

未经允许不得转载:惠主机 » 12个Linux系统网络配置和故障排除命令汇总整理

相关文章