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 packetsping
发送icmp数据包google.com
is the destination of the icmp packetsgoogle.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 thegoogle.com
and seeing this means our system can resolve domain names在第2行中
216.58.212.46
是google.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 connectiontelnet
是建立TCP连接的工具google.com
is the destination systemgoogle.com
是目标系统80
is port number of the system80
是系统的端口号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
命令并获取所有接口。 接口编号容易识别。
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
.
接口名称后,接口的状态出现。 有不同的选项,但最重要的是UP
和DOWN
选项。 如果状态为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 addressadd
指定我们要添加一个IP地址192.168.122.107/24
is ip address and netmask192.168.122.107/24
是IP地址和网络掩码dev ens3
is the interface we want to set ip addressdev ens3
是我们要设置ip地址的接口
删除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 submoduleroute
意味着我们将使用路由子模块show
lists the routing table contentshow
列出路由表的内容
添加静态路由(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 entryadd
用于添加新的路线条目10.0.0.0/24
specifies destination network10.0.0.0/24
指定目标网络via 192.168.122.1
sets next hoop to destination networkvia 192.168.122.1
将下一环设置到目标网络dev ens3
is the network interface where packet go out for the destination networkdev ens3
是数据包传出到目标网络的网络接口
删除静态路由(Remove Static Route)
Removing static route is easier than adding it.
删除静态路由比添加静态路由容易。
# ip route del 10.0.0.0/24
del
issued for removeldel
发布的del
10.0.0.0/24
specifies the destination network which route will be removed10.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 networksdefault
表示目的地是所有网络via 192.168.122.1
specifies default gateway ip addressvia 192.168.122.1
指定默认网关IP地址
删除默认网关(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 routedel
删除这条路线default
route is default gatewaydefault
路由是默认网关via 192.168.122.1
the ip address of default gatewayvia 192.168.122.1
默认网关的IP地址
翻译自: https://www.poftut.com/linux-network-administration-ping-ip-commands/