L66.linux命令每日一练 -- 第十章 Linux网络管理命令 -- arp和ip

10.5 arp:管理系统的arp缓存

10.5.1 命令详解

【命令星级】 ★★★★☆

【功能说明】

​ arp命令用于操作本机的Aarp缓存区,它可以显示arp缓存区中的所有条目、删除指定的条目或者添加静态的IP地址与MAC地址的对应关系。

​ 什么是arp?即地址解析协议(ARP,Address Resolution Protocol),其主要功能是根据IP地址获取物理地址(MAC地址)。

【语法格式】

arp [option]
arp [选项]

​ **说明:**在arp命令及后面的选项里,每个元素直接都至少要有一个空格。

【选项说明】

​ 表10-4针对该命令的参数选项进行了说明。

​ 表10-4 arp命令的参数选项及说明

在这里插入图片描述

0.5.2 使用范例

​ **范例10-13:**显示arp缓存区的所有条目。

[root@centos7 ~]# arp	#显示arp缓存区的所有条目。
Address                  HWtype  HWaddress           Flags Mask            Iface
gateway                  ether   00:50:56:e7:33:4d   C                     eth0
10.0.0.1                 ether   00:50:56:c0:00:08   C                     eth0
[root@centos7 ~]# arp -n	#使用-n选项以数字的形式显示arp缓存区的所有条目。
Address                  HWtype  HWaddress           Flags Mask            Iface
10.0.0.254               ether   00:50:56:e7:33:4d   C                     eth0
10.0.0.1                 ether   00:50:56:c0:00:08   C                     eth0

​ 命令说明具体如下。

  • Address:主机地址。

  • HWtype:硬件类型。

  • HWaddress:硬件地址。

  • Flags Mask:记录标志,“C”表示arp高速缓存中的条目,“M”表示静态的arp条目。

  • Iface:网络接口。

    ​ **范例10-14:**查询指定主机的arp条目。

    [root@centos7 ~]# arp -n 10.0.0.1	#指定查询10.0.0.1的arp信息。
    Address                  HWtype  HWaddress           Flags Mask            Iface
    10.0.0.1                 ether   00:50:56:c0:00:08   C                     eth0
    

    ​ **范例10-15:**静态绑定IP地址与MAC地址。

    [root@centos7 ~]# arp -s 10.0.0.100 00:0c:29:c0:5a:ef	#绑定IP地址和MAC地址。
    [root@centos7 ~]# arp -n
    Address                  HWtype  HWaddress           Flags Mask            Iface
    10.0.0.254               ether   00:50:56:e7:33:4d   C                     eth0
    10.0.0.1                 ether   00:50:56:c0:00:08   C                     eth0
    10.0.0.100               ether   00:0c:29:c0:5a:ef   CM                    eth0
    [root@centos7 ~]# arp -d 10.0.0.100	#删除静态ARP绑定。
    [root@centos7 ~]# arp -n
    Address                  HWtype  HWaddress           Flags Mask            Iface
    10.0.0.254               ether   00:50:56:e7:33:4d   C                     eth0
    10.0.0.1                 ether   00:50:56:c0:00:08   C                     eth0
    #提示:局域网有arp病毒时,就可以用上述方法绑定MAC地址,以防止中毒。
    

10.6 ip:网络配置工具

10.6.1 命令详解

【命令星级】 ★★★★★

【功能说明】

​ ip命令是iproute软件包中的一个强大的网络配置工具,用于显示或管理Linux系统的路由、网络设备、策略路由和隧道。

【语法格式】

ip [option] [object] [command]
ip [选项] [网络对象] [操作命令]

​ **说明:**在ip命令及后面的选项、网络对象和操作命令里,每个元素直接都至少要有一个空格。

【选项说明】

​ 表10-5针对该命令的参数选项进行了说明。

​ 表10-5 ip命令的参数选项及说明

在这里插入图片描述
在这里插入图片描述

10.6.2 使用范例

​ **范例10-16:**显示网络设备属性。

[root@centos7 ~]# ip link show dev eth1	#显示eth1网卡属性。
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
    link/ether 00:0c:29:23:c8:84 brd ff:ff:ff:ff:ff:ff
[root@centos7 ~]# ip -s link show dev eth1	#显示详细属性。
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
    link/ether 00:0c:29:23:c8:84 brd ff:ff:ff:ff:ff:ff
    RX: bytes  packets  errors  dropped overrun mcast   
    5054       54       0       0       0       0       
    TX: bytes  packets  errors  dropped carrier collsns 
    2948       31       0       0       0       0    #显示每个网络设备上数据包的统计信息。  
[root@centos7 ~]# ip -s -s link show dev eth1	#使用两个-s显示更详细的属性。
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
    link/ether 00:0c:29:23:c8:84 brd ff:ff:ff:ff:ff:ff
    RX: bytes  packets  errors  dropped overrun mcast   
    5054       54       0       0       0       0       
    RX errors: length   crc     frame   fifo    missed
               0        0       0       0       0       
    TX: bytes  packets  errors  dropped carrier collsns 
    2948       31       0       0       0       0       
    TX errors: aborted  fifo   window heartbeat transns
               0        0       0       0       2   

​ **范例10-17:**关闭和激活网络设备。

[root@centos7 ~]# ip link show dev eth1
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
    link/ether 00:0c:29:23:c8:84 brd ff:ff:ff:ff:ff:ff
[root@centos7 ~]# ip link set eth1 up	#激活eth1网卡。
[root@centos7 ~]# ip link show dev eth1
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
    link/ether 00:0c:29:23:c8:84 brd ff:ff:ff:ff:ff:ff
[root@centos7 ~]# ip link set eth1 down	#关闭eth1网卡。
[root@centos7 ~]# ip link show dev eth1
3: eth1: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast state DOWN mode DEFAULT group default qlen 1000
    link/ether 00:0c:29:23:c8:84 brd ff:ff:ff:ff:ff:ff

​ **范例10-18:**修改网卡MAC地址。

[root@centos7 ~]# ip link show dev eth1
3: eth1: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast state DOWN mode DEFAULT group default qlen 1000
    link/ether 00:0c:29:23:c8:84 brd ff:ff:ff:ff:ff:ff
[root@centos7 ~]# ip link set eth1 address 00:0c:29:23:c8:11	#修改MAC地址。
[root@centos7 ~]# ip link show dev eth1
3: eth1: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast state DOWN mode DEFAULT group default qlen 1000
    link/ether 00:0c:29:23:c8:11 brd ff:ff:ff:ff:ff:ff

​ **范例10-19:**查看网卡信息。

[root@centos7 ~]# ip a	#效果与ip address一样,显示的结果包括激活和未激活的网卡。
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    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: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:23:c8:7a brd ff:ff:ff:ff:ff:ff
    inet 10.0.0.201/24 brd 10.0.0.255 scope global noprefixroute eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::4122:b9af:997b:a6ff/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
3: eth1: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast state DOWN group default qlen 1000
    link/ether 00:0c:29:23:c8:11 brd ff:ff:ff:ff:ff:ff

​ 与ip link(简写ip l)命令结果的对比,可以发现多出了IP地址信息。

​ **范例10-20:**添加和删除IP地址。

[root@centos7 ~]# ip a show eth1	#显示eth1的IP地址,没有配置。
3: eth1: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast state DOWN group default qlen 1000
    link/ether 00:0c:29:23:c8:11 brd ff:ff:ff:ff:ff:ff
[root@centos7 ~]# ip a add 172.16.1.12/24 dev eth1	#使用add选项添加一个IP地址172.16.1.12,子网掩码255.255.255.0,简写为172.16.1.12/24,使用dev选项指定网络设备为eth1。
[root@centos7 ~]# ip a show eth1	#显示eth1的IP地址为172.16.1.12/24,但是网卡状态还是DOWN。
3: eth1: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast state DOWN group default qlen 1000
    link/ether 00:0c:29:23:c8:11 brd ff:ff:ff:ff:ff:ff
    inet 172.16.1.12/24 scope global eth1
       valid_lft forever preferred_lft forever
[root@centos7 ~]# ip link set eth1 up	#激活网卡。
[root@centos7 ~]# ip a show eth1	#现在eth1网卡正常运行。
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:23:c8:11 brd ff:ff:ff:ff:ff:ff
    inet 172.16.1.12/24 scope global eth1
       valid_lft forever preferred_lft forever
[root@centos7 ~]# ip a add 172.16.1.13/24 dev eth1	#可以添加多个IP地址,这种称为辅助IP,前面ifconfig命令创建的别名称为IP。现在常用的高可用软件如heartbeat、keepalive都采用了辅助IP。
[root@centos7 ~]# ip a show eth1
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:23:c8:11 brd ff:ff:ff:ff:ff:ff
    inet 172.16.1.12/24 scope global eth1
       valid_lft forever preferred_lft forever
    inet 172.16.1.13/24 scope global secondary eth1
       valid_lft forever preferred_lft forever
[root@centos7 ~]# ip a del 172.16.1.12/24 dev eth1	#删除IP,删除IP命令配置的IP地址,直接将前面的添加命令中的add选项改为del即可。
[root@centos7 ~]# ip a show eth1	#可以看到删除了主IP,辅助IP还在。
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:23:c8:11 brd ff:ff:ff:ff:ff:ff
    inet 172.16.1.13/24 scope global eth1
       valid_lft forever preferred_lft forever

小结:(CentOS 7)

  • 删除网卡的主IP地址,辅助IP地址会自动变为主IP地址
[root@centos6 ~]# ip a show eth1	#显示eth1的IP地址,没有配置。
3: eth1: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast state DOWN qlen 1000
    link/ether 00:0c:29:a3:21:30 brd ff:ff:ff:ff:ff:ff
[root@centos6 ~]# ip a add 172.16.1.15/24 dev eth1	#使用add选项添加一个IP地址172.16.1.15,子网掩码255.255.255.0,简写为172.16.1.15/24,使用dev选项指定网络设备为eth1。
[root@centos6 ~]# ip a show eth1	#显示eth1的IP地址为172.16.1.15/24,但是网卡状态还是DOWN。
3: eth1: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast state DOWN qlen 1000
    link/ether 00:0c:29:a3:21:30 brd ff:ff:ff:ff:ff:ff
    inet 172.16.1.15/24 scope global eth1
[root@centos6 ~]# ip link set eth1 up	#激活网卡。
[root@centos6 ~]# ip a show eth1	#现在eth1网卡正常运行。
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:a3:21:30 brd ff:ff:ff:ff:ff:ff
    inet 172.16.1.15/24 scope global eth1
    inet6 fe80::20c:29ff:fea3:2130/64 scope link 
       valid_lft forever preferred_lft forever
[root@centos6 ~]# ip a add 172.16.1.16/24 dev eth1	#可以添加多个IP地址,这种称为辅助IP,前面ifconfig命令创建的别名称为IP。现在常用的高可用软件如heartbeat、keepalive都采用了辅助IP。
[root@centos6 ~]# ip a show eth1
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:a3:21:30 brd ff:ff:ff:ff:ff:ff
    inet 172.16.1.15/24 scope global eth1
    inet 172.16.1.16/24 scope global secondary eth1
    inet6 fe80::20c:29ff:fea3:2130/64 scope link 
       valid_lft forever preferred_lft forever
[root@centos6 ~]# ip a del 172.16.1.15/24 dev eth1	#删除IP,删除IP命令配置的IP地址,直接将前面的添加命令中的add选项改为del即可。
[root@centos6 ~]# ip a show eth1	#IP地址全部被删除。
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:a3:21:30 brd ff:ff:ff:ff:ff:ff
    inet6 fe80::20c:29ff:fea3:2130/64 scope link 
       valid_lft forever preferred_lft forever
[root@centos6 ~]# ip a add 172.16.1.15/24 dev eth1	#添加2个IP地址。
[root@centos6 ~]# ip a add 172.16.1.16/24 dev eth1
[root@centos6 ~]# ip a show eth1
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:a3:21:30 brd ff:ff:ff:ff:ff:ff
    inet 172.16.1.15/24 scope global eth1
    inet 172.16.1.16/24 scope global secondary eth1
    inet6 fe80::20c:29ff:fea3:2130/64 scope link 
       valid_lft forever preferred_lft forever
[root@centos6 ~]# ip a del 172.16.1.16/24 dev eth1	#删除辅助IP地址。
[root@centos6 ~]# ip a show eth1
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:a3:21:30 brd ff:ff:ff:ff:ff:ff
    inet 172.16.1.15/24 scope global eth1	#保留主IP。
    inet6 fe80::20c:29ff:fea3:2130/64 scope link 
       valid_lft forever preferred_lft forever

小结:(CentOS 6)

  • 删除网卡的主IP地址,同时会删除该网卡的所有IP地址。
  • 删除网卡的辅助IP地址,不会影响该网卡的其他IP地址。

​ **趣味知识点:**如何用ip命令创建别名IP?

[root@centos7 ~]# ip a show eth1
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:23:c8:11 brd ff:ff:ff:ff:ff:ff
    inet 172.16.1.13/24 scope global eth1
       valid_lft forever preferred_lft forever
[root@centos7 ~]# ip a add 10.0.0.20/32 dev eth1 label eth1:1	#使用label选项创建别名IP。
[root@centos7 ~]# ip a show eth1
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:23:c8:11 brd ff:ff:ff:ff:ff:ff
    inet 172.16.1.13/24 scope global eth1
       valid_lft forever preferred_lft forever
    inet 10.0.0.20/32 scope global eth1:1
       valid_lft forever preferred_lft forever
[root@centos7 ~]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 10.0.0.201  netmask 255.255.255.0  broadcast 10.0.0.255
        inet6 fe80::4122:b9af:997b:a6ff  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:23:c8:7a  txqueuelen 1000  (Ethernet)
        RX packets 2084  bytes 185241 (180.8 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 1471  bytes 164474 (160.6 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

eth1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.16.1.13  netmask 255.255.255.0  broadcast 0.0.0.0
        ether 00:0c:29:23:c8:11  txqueuelen 1000  (Ethernet)
        RX packets 54  bytes 5054 (4.9 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 31  bytes 2948 (2.8 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
#别名IP格式。
eth1:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 10.0.0.20  netmask 255.255.255.255  broadcast 0.0.0.0
        ether 00:0c:29:23:c8:11  txqueuelen 1000  (Ethernet)

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 1000  (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

​ **备注:**使用ifconfig命令创建的别名IP,ip命令能够查询到;相反,通过IP命令创建的辅助IP,ifconfig命令则查询不了,除非使用ip命令的label功能创建别名IP。

​ **范例10-21:**查看路由表。

[root@centos7 ~]# ip route	
default via 10.0.0.254 dev eth0 
10.0.0.0/24 dev eth0 proto kernel scope link src 10.0.0.201 metric 100 
172.16.1.0/24 dev eth1 proto kernel scope link src 172.16.1.13 
[root@centos7 ~]# ip route |column -t	#使用column命令格式化,选项-t,默认根据空格分隔判断输入行的列数来创建一个表。
default        via  10.0.0.254  dev    eth0
10.0.0.0/24    dev  eth0        proto  kernel  scope  link  src  10.0.0.201   metric  100
172.16.1.0/24  dev  eth1        proto  kernel  scope  link  src  172.16.1.13
[root@centos7 ~]# route -n	#与我们前面学习过的route命令对比一下。
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         10.0.0.254      0.0.0.0         UG    0      0        0 eth0
10.0.0.0        0.0.0.0         255.255.255.0   U     100    0        0 eth0
172.16.1.0      0.0.0.0         255.255.255.0   U     0      0        0 eth1

​ **范例10-22:**添加或删除路由表。

[root@centos7 ~]# ip route add 10.1.0.0/24 via 10.0.0.254 dev eth0	#添加静态路由。
[root@centos7 ~]# ip route |column -t
default        via  10.0.0.254  dev    eth0
10.0.0.0/24    dev  eth0        proto  kernel  scope  link  src  10.0.0.201   metric  100
10.1.0.0/24    via  10.0.0.254  dev    eth0
172.16.1.0/24  dev  eth1        proto  kernel  scope  link  src  172.16.1.13
[root@centos7 ~]# ip route del 10.1.0.0/24	#删除静态路由。
[root@centos7 ~]# ip route |column -t
default        via  10.0.0.254  dev    eth0
10.0.0.0/24    dev  eth0        proto  kernel  scope  link  src  10.0.0.201   metric  100
172.16.1.0/24  dev  eth1        proto  kernel  scope  link  src  172.16.1.13

​ **范例10-23:**查看ARP缓存(显示网络邻居的信息)。

[root@centos7 ~]# ip neighbour	#使用neighbour指令查看arp缓存。
10.0.0.254 dev eth0 lladdr 00:50:56:e7:33:4d STALE
10.0.0.1 dev eth0 lladdr 00:50:56:c0:00:08 DELAY

​ **范例10-24:**添加或删除静态ARP项。

[root@centos7 ~]# ip neighbour add 192.168.1.100 lladdr 00:0c:29:c0:5a:ef dev eth0	#添加静态ARP。
[root@centos7 ~]# ip neighbour
192.168.1.100 dev eth0 lladdr 00:0c:29:c0:5a:ef PERMANENT
10.0.0.254 dev eth0 lladdr 00:50:56:e7:33:4d STALE
10.0.0.1 dev eth0 lladdr 00:50:56:c0:00:08 REACHABLE
[root@centos7 ~]# ip neighbour del 192.168.1.100 dev eth0	#删除静态ARP。
[root@centos7 ~]# ip neighbour
10.0.0.254 dev eth0 lladdr 00:50:56:e7:33:4d STALE
10.0.0.1 dev eth0 lladdr 00:50:56:c0:00:08 REACHABLE

​ **提示:**CentOS 7开始推广ip命令,用于替代传统的ifconfig和route命令,因此请读者掌握好ip命令。

猜你喜欢

转载自blog.csdn.net/qq_25599925/article/details/126202863