使用ping和ip命令进行Linux网络管理

Linux network stack is very powerful. Network stack provides a lot of functionalities where most of the enterprise network products can not offer. Here we will look basic and everyday network administration commands.

Linux网络堆栈非常强大。 网络堆栈提供了大多数企业网络产品无法提供的许多功能。 在这里,我们将介绍基本的日常网络管理命令。

检查网络和Internet连接 (Check Network and Internet Connectivity)

Network connectivity can be checked easily with ping and telnet tools. One method to check internet connectivity is ping to Google.

使用ping和telnet工具可以轻松检查网络连接。 一种检查Internet连接的方法是ping Google。

# ping google.com 
PING google.com (216.58.212.46) 56(84) bytes of data. 
64 bytes from sof02s18-in-f46.1e100.net (216.58.212.46): icmp_seq=1 ttl=54 time=45.0 ms 
64 bytes from sof02s18-in-f46.1e100.net (216.58.212.46): icmp_seq=2 ttl=54 time=45.1 ms
  • ping sends icmp packets

    ping发送icmp数据包

  • google.com is the destination of the icmp packets

    google.com是icmp数据包的目的地

  • line 3 and4 shows that google.com responds to our icmp packets

    第3和4行显示google.com响应了我们的icmp数据包

  • In line 2 216.58.212.46 is the ip address of the google.com and seeing this means our system can resolve domain names

    在第2行中216.58.212.46google.com的IP地址,看到这意味着我们的系统可以解析域名

Some  times ping can not be a feasible method to check internet connectivity. Because some network administrators disables icmp traffic in the network for security reasons. Here our alternative is telnet

有时ping并不是检查Internet连接的可行方法。 因为某些网络管理员出于安全原因禁用了网络中的icmp通信。 在这里,我们的替代方法是telnet

# telnet google.com 80 
Trying 216.58.212.14... 
Connected to google.com. 
Escape character is '^]'. 


  • telnet is the tool to make tcp connection

    telnet是建立TCP连接的工具

  • google.com is the destination system

    google.com是目标系统

  • 80 is port number of the system

    80是系统的端口号

  • Line 3 means there is no problem to make tcp connection to the google.com

    第3行表示将tcp连接到google.com没问题

If we see other than Line 3 it means there is a problem.

如果我们看到的不是3号线,则表示存在问题。

列出网络接口 (List Network Interfaces)

Linux system can have multiple interfaces. Listing them can be done different ways but most used and modern way is using ip commands.

Linux系统可以具有多个接口。 列出它们可以用不同的方法完成,但是最常用和现代的方法是使用ip命令。

# ip link 
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 
2: ens3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP mode DEFAULT group default qlen 1000 
    link/ether 52:54:00:ea:3f:9c brd ff:ff:ff:ff:ff:ff 
3: br-b7a05d7f1b53: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN mode DEFAULT group default  
    link/ether 02:42:32:dc:12:91 brd ff:ff:ff:ff:ff:ff 
4: docker0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP mode DEFAULT group default  
    link/ether 02:42:19:73:82:9a brd ff:ff:ff:ff:ff:ff 
6: [email protected]: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master docker0 state UP mode DEFAULT group default  
    link/ether 22:fe:82:84:d4:d5 brd ff:ff:ff:ff:ff:ff link-netnsid 0

We issue ip link command and get all interfaces. Interfaces are numbered to be easily indentified.

我们发出ip link命令并获取所有接口。 接口编号容易识别。

LEARN MORE  Linux ip Command With Examples- Network Management
通过示例了解更多Linux ip命令-网络管理

After the interface name came like ens .

接口名称之后是ens

After interface name the status of the interface cames. There are different options but most important is UP and DOWN option. If there is DOWN status it shows there is a problem or the interface is not set UP .

接口名称后,接口的状态出现。 有不同的选项,但最重要的是UPDOWN选项。 如果状态为DOWN则表明存在问题或接口未设置为UP

And there is MAC address like 52:54:00:ea:3f:9c .

还有MAC地址,例如52:54:00:ea:3f:9c

设置界面上/下 (Setting Interface UP/DOWN)

We can set an interface with ip link command by using set .

我们可以使用set使用ip link命令设置接口。

# ip link set down dev docker0  
# ip link show docker0 
4: docker0: <BROADCAST,MULTICAST> mtu 1500 qdisc noqueue state DOWN mode DEFAULT group default  
    link/ether 02:42:19:73:82:9a brd ff:ff:ff:ff:ff:ff

We have set down the interface and than list the status of the interface. Now we can set up the interface again. Keep in mind if an interface is set down the network communication about the interface will be down.

我们已经设置了界面,然后列出了界面的状态。 现在,我们可以再次设置界面。 请记住,如果设置了接口,则有关该接口的网络通信将关闭。

# ip link set up dev docker0      
# ip link show docker0        
4: docker0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP mode DEFAULT group default  
    link/ether 02:42:19:73:82:9a brd ff:ff:ff:ff:ff:ff

Good, everything works as what is expected. We simple used up argument instead of down . If there is an dhcp server in the interface network it will get an ip address as soon as possible.

很好,一切正常。 我们简单地用up而不是down 。 如果接口网络中有一个dhcp服务器,它将尽快获得一个IP地址。

列出IP地址 (List IP Adress)

Listing ip address is easy. All interfaces ip addresses will be listed.

列出IP地址很容易。 将列出所有接口的IP地址。

# ip address show 
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 
    inet 127.0.0.1/8 scope host lo 
       valid_lft forever preferred_lft forever 
    inet6 ::1/128 scope host  
       valid_lft forever preferred_lft forever 
2: ens3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000 
    link/ether 52:54:00:ea:3f:9c brd ff:ff:ff:ff:ff:ff 
    inet 192.168.122.106/24 brd 192.168.122.255 scope global dynamic ens3 
       valid_lft 3399sec preferred_lft 3399sec 
    inet6 fe80::c85b:e028:c080:c51e/64 scope link  
       valid_lft forever preferred_lft forever 
3: br-b7a05d7f1b53: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default  
    link/ether 02:42:32:dc:12:91 brd ff:ff:ff:ff:ff:ff 
    inet 172.18.0.1/16 scope global br-b7a05d7f1b53 
       valid_lft forever preferred_lft forever 
4: docker0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default  
    link/ether 02:42:19:73:82:9a brd ff:ff:ff:ff:ff:ff 
    inet6 fe80::42:19ff:fe73:829a/64 scope link  
       valid_lft forever preferred_lft forever 
6: [email protected]: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master docker0 state UP group default  
    link/ether 22:fe:82:84:d4:d5 brd ff:ff:ff:ff:ff:ff link-netnsid 0

We may want to list specific ip address. Because in enterprise systems there will be a lot of interface and ip addreses.

我们可能要列出特定的IP地址。 因为在企业系统中会有很多接口和ip地址。

# ip address show ens3 
2: ens3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000 
    link/ether 52:54:00:ea:3f:9c brd ff:ff:ff:ff:ff:ff 
    inet 192.168.122.106/24 brd 192.168.122.255 scope global dynamic ens3 
       valid_lft 3293sec preferred_lft 3293sec 
    inet6 fe80::c85b:e028:c080:c51e/64 scope link  
       valid_lft forever preferred_lft forever

设定IP位址 (Setting IP Address)

We want to set ip address statically. If dhcp is enable in the interface network it will get automatically an ip address.

我们要静态设置IP地址。 如果在接口网络中启用了dhcp,它将自动获得一个IP地址。

# ip address add 192.168.122.107/24 dev ens3 
# ip address show ens3 
2: ens3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000 
    link/ether 52:54:00:ea:3f:9c brd ff:ff:ff:ff:ff:ff 
    inet 192.168.122.106/24 brd 192.168.122.255 scope global dynamic ens3 
       valid_lft 1130sec preferred_lft 1130sec 
    inet 192.168.122.107/24 scope global secondary ens3 
       valid_lft forever preferred_lft forever 
    inet6 fe80::c85b:e028:c080:c51e/64 scope link  
       valid_lft forever preferred_lft forever

Our interface has all ready an ip address.

我们的界面已经准备好一个IP地址。

  • add specifies that we want to add an ip address

    add指定我们要添加一个IP地址

  • 192.168.122.107/24 is ip address and netmask

    192.168.122.107/24是IP地址和网络掩码

  • dev ens3 is the interface we want to set ip address

    dev ens3是我们要设置ip地址的接口

LEARN MORE  Nmap Host Discovery
了解更多Nmap主机发现

删除IP地址(Remove IP Address)

Removing ip address is similar to adding just add command is substituted with del command.

删除ip地址类似于添加IP地址,只是用del命令代替add命令。

# ip address del 192.168.122.107/24 dev ens3    
# ip address show ens3 
2: ens3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000 
    link/ether 52:54:00:ea:3f:9c brd ff:ff:ff:ff:ff:ff 
    inet 192.168.122.106/24 brd 192.168.122.255 scope global dynamic ens3 
       valid_lft 3065sec preferred_lft 3065sec 
    inet6 fe80::c85b:e028:c080:c51e/64 scope link  
       valid_lft forever preferred_lft forever

As we see previously added ip address is removed.

如我们所见,先前添加的IP地址已删除。

列出路由表 (List Routing Table)

Routing table is a crucial part of the networking because the network packets are sent to the destination according to routing table. We can check routing table like below

路由表是网络连接的关键部分,因为网络数据包根据路由表发送到目的地。 我们可以如下检查路由表

# ip route show 
default via 192.168.122.1 dev ens3  proto static  metric 100  
172.18.0.0/16 dev br-b7a05d7f1b53  proto kernel  scope link  src 172.18.0.1 linkdown  
192.168.122.0/24 dev ens3  proto kernel  scope link  src 192.168.122.106  metric 100
  • route means we will use routing submodule

    route意味着我们将使用路由子模块

  • show lists the routing table content

    show列出路由表的内容

添加静态路由(Add Static Routing)

Static route is a new route that is inserted by system administrator. Normally routing entries are inserted by system itself.

静态路由是系统管理员插入的新路由。 通常,路由条目由系统本身插入。

# ip route add 10.0.0.0/24 via 192.168.122.1 dev ens3
  • add used to add new route entry

    add用于添加新的路线条目

  • 10.0.0.0/24 specifies destination network

    10.0.0.0/24指定目标网络

  • via 192.168.122.1 sets next hoop to destination network

    via 192.168.122.1将下一环设置到目标网络

  • dev ens3 is the network interface where packet go out for the destination network

    dev ens3是数据包传出到目标网络的网络接口

删除静态路由(Remove Static Route)

Removing static route is easier than adding it.

删除静态路由比添加静态路由容易。

# ip route del 10.0.0.0/24
  • del issued for removel

    del发布的del

  • 10.0.0.0/24 specifies the destination network which route will be removed

    10.0.0.0/24指定将删除路由的目标网络

添加默认网关(Adding Default Gateway)

Default gateway is a route too. But default gateway is special because if there is no entry on the route table about destination network, packets are forwarded through default gateway.

默认网关也是一条路由。 但是默认网关是特殊的,因为如果路由表上没有有关目标网络的条目,则数据包将通过默认网关转发。

# ip route add default via 192.168.122.1
  • default means the destination is all networks

    default表示目的地是所有网络

  • via 192.168.122.1 specifies default gateway ip address

    via 192.168.122.1指定默认网关IP地址

LEARN MORE  ARPANET and Its History
了解更多ARPANET及其历史

删除默认网关(Removing Default Gateway)

Default gateway can be removed easyli just changing add in the adding command to del like below

只需将add命令中的add更改为如下所示的del即可删除默认网关easyli

# ip route del default via 192.168.122.1
  • del delete this route

    del删除这条路线

  • default route is default gateway

    default路由是默认网关

  • via 192.168.122.1 the ip address of default gateway

    via 192.168.122.1默认网关的IP地址

翻译自: https://www.poftut.com/linux-network-administration-ping-ip-commands/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值