在centos 后续版本中 net-tools 的包被替换成了iproute2这个包,这个命令添加的东西都是临时生效的,关机就会消失,如果想要永久保存需要编写配置文件,或者使用nmcli生成,在nmcli有介绍IP地址的配置。
今天介绍写ip 下的几个命令:
ip addr 网络三层的IP地址配置,如修改ip,添加ip等(临时生效)
ip link 网络二层的配置 如启用禁用某个网卡设备,改变mtu,修改mac地址等(临时生效)
ip route 网络三层的路由表配置,如添加路由,删除路由等(临时生效)
ip neigh 查看arp ,添加删除静态arp表项
ip tunnel 隧道配置
[root@localhost ~]#ip addr add 1.1.1.1/24 dev ens37
[root@localhost ~]#
[root@localhost ~]#ip addr show ens37
5: ens37: mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:b4:5b:af brd ff:ff:ff:ff:ff:ff
inet 1.1.1.1/24 scope global ens37
valid_lft forever preferred_lft forever
[root@localhost ~]#
[root@localhost ~]#ip addr add 2.2.2.2/24 dev ens37
[root@localhost ~]#ip addr show ens37
5: ens37: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:b4:5b:af brd ff:ff:ff:ff:ff:ff
inet 1.1.1.1/24 scope global ens37
valid_lft forever preferred_lft forever
inet 2.2.2.2/24 scope global ens37
valid_lft forever preferred_lft forever
[root@localhost ~]#ip addr del 2.2.2.2/24 dev ens37
[root@localhost ~]#ip addr show ens37
5: ens37: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:b4:5b:af brd ff:ff:ff:ff:ff:ff
inet 1.1.1.1/24 scope global ens37
valid_lft forever preferred_lft forever
[root@localhost ~]#ip addr flush dev ens37
[root@localhost ~]#ip addr show ens37
5: ens37: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:b4:5b:af brd ff:ff:ff:ff:ff:ff
[root@localhost ~]#
[root@localhost ~]#ip addr add 2.2.2.2/24 dev ens37 label ens37:1
[root@localhost ~]#ip addr add 1.1.1.1/24 dev ens37
[root@localhost ~]#ip add show ens37
5: ens37: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:b4:5b:af brd ff:ff:ff:ff:ff:ff
inet 2.2.2.2/24 scope global ens37:1
valid_lft forever preferred_lft forever
inet 1.1.1.1/24 scope global ens37
valid_lft forever preferred_lft forever
[root@localhost ~]#
[root@localhost ~]#ip add show ens37
5: ens37: mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:b4:5b:af brd ff:ff:ff:ff:ff:ff
inet 2.2.2.2/24 scope global ens37:1
valid_lft forever preferred_lft forever
inet 1.1.1.1/24 scope global ens37
valid_lft forever preferred_lft forever
[root@localhost ~]#
查看全部网卡的(简写:ip a)全称是 ip addr show 或者ip addr
[root@localhost ~]#ip a
1: lo: 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: ens33: mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:b4:5b:a5 brd ff:ff:ff:ff:ff:ff
inet 192.168.198.138/24 brd 192.168.198.255 scope global noprefixroute dynamic ens33
valid_lft 1654sec preferred_lft 1654sec
inet6 fe80::61ae:79b3:1455:75e9/64 scope link noprefixroute
valid_lft forever preferred_lft forever
4: ens37: mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:b4:5b:af brd ff:ff:ff:ff:ff:ff
inet 2.2.2.2/24 scope global ens37:1
valid_lft forever preferred_lft forever
inet 1.1.1.1/24 scope global ens37
valid_lft forever preferred_lft forever
[root@localhost ~]#
[root@localhost ~]#ip -4 a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
inet 192.168.198.138/24 brd 192.168.198.255 scope global noprefixroute dynamic ens33
valid_lft 1577sec preferred_lft 1577sec
3: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default qlen 1000
inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0
valid_lft forever preferred_lft forever
5: ens37: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
inet 2.2.2.2/24 scope global ens37:1
valid_lft forever preferred_lft forever
inet 1.1.1.1/24 scope global ens37
valid_lft forever preferred_lft forever
[root@localhost ~]#ip -6 addr show ens33
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 state UP qlen 1000
inet6 fe80::61ae:79b3:1455:75e9/64 scope link noprefixroute
valid_lft forever preferred_lft forever
[root@localhost ~]#
[root@localhost ~]#ip a show ens37
5: ens37: <BROADCAST,MULTICAST,DYNAMIC,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:b4:5b:af brd ff:ff:ff:ff:ff:ff
inet 5.5.5.5/24 scope global ens37
valid_lft forever preferred_lft forever
[root@localhost ~]#ip link set down ens37
[root@localhost ~]#ip a show ens37
5: ens37: <BROADCAST,MULTICAST,DYNAMIC> mtu 1500 qdisc pfifo_fast state DOWN group default qlen 1000
link/ether 00:0c:29:b4:5b:af brd ff:ff:ff:ff:ff:ff
inet 5.5.5.5/24 scope global ens37
valid_lft forever preferred_lft forever
[root@localhost ~]#
[root@localhost ~]#ip link set up ens37
[root@localhost ~]#ip a show ens37
5: ens37: <BROADCAST,MULTICAST,DYNAMIC,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:b4:0a:aa brd ff:ff:ff:ff:ff:ff
inet 5.5.5.5/24 scope global ens37
valid_lft forever preferred_lft forever
[root@localhost ~]#
[root@localhost ~]#ip link set ens37 address 00:0c:29:aa:aa:aa
[root@localhost ~]#ip a show ens37
5: ens37: <BROADCAST,MULTICAST,DYNAMIC,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:aa:aa:aa brd ff:ff:ff:ff:ff:ff
inet 5.5.5.5/24 scope global ens37
valid_lft forever preferred_lft forever
[root@localhost ~]#
[root@localhost ~]#ip a show ens37
5: ens37: <BROADCAST,MULTICAST,DYNAMIC,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:aa:aa:aa brd ff:ff:ff:ff:ff:ff
inet 5.5.5.5/24 scope global ens37
valid_lft forever preferred_lft forever
[root@localhost ~]#ip link set ens37 mtu 1492
[root@localhost ~]#ip a show ens37
5: ens37: <BROADCAST,MULTICAST,DYNAMIC,UP,LOWER_UP> mtu 1492 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:aa:aa:aa brd ff:ff:ff:ff:ff:ff
inet 5.5.5.5/24 scope global ens37
valid_lft forever preferred_lft forever
[root@localhost ~]#
[root@localhost ~]#ip route add default via 192.168.198.145 dev ens33
[root@localhost ~]#ip route add 192.168.0.0/24 via 5.5.5.1
[root@localhost ~]#ip route add 2.2.2.2 via 5.5.5.1
[root@localhost ~]#ip route del 2.2.2.2
[root@localhost ~]#ip route
default via 192.168.198.2 dev ens33 proto dhcp metric 100
5.5.5.0/24 dev ens37 proto kernel scope link src 5.5.5.5
192.168.0.0/24 via 5.5.5.1 dev ens37
192.168.122.0/24 dev virbr0 proto kernel scope link src 192.168.122.1
192.168.198.0/24 dev ens33 proto kernel scope link src 192.168.198.138 metric 100
[root@localhost ~]#ip route del 5.5.5.0/24
[root@localhost ~]#ip route
default via 192.168.198.2 dev ens33 proto dhcp metric 100
192.168.0.0/24 via 5.5.5.1 dev ens37
192.168.122.0/24 dev virbr0 proto kernel scope link src 192.168.122.1
192.168.198.0/24 dev ens33 proto kernel scope link src 192.168.198.138 metric 100
[root@localhost ~]#ip route flush dev ens37
[root@localhost ~]#ip route
default via 192.168.198.2 dev ens33 proto dhcp metric 100
2.2.2.2 via 5.5.5.1 dev ens37
5.5.5.0/24 dev ens37 proto kernel scope link src 5.5.5.5
192.168.0.0/24 via 5.5.5.1 dev ens37
192.168.122.0/24 dev virbr0 proto kernel scope link src 192.168.122.1
192.168.198.0/24 dev ens33 proto kernel scope link src 192.168.198.138 metric 100
[root@localhost ~]#ip route get 1.1.1.1
1.1.1.1 via 192.168.198.2 dev ens33 src 192.168.198.138
cache
[root@localhost ~]#ip route get 5.5.5.9
5.5.5.9 dev ens37 src 5.5.5.5
cache
[root@localhost ~]#
[root@localhost ~]#ip neigh
192.168.198.1 dev ens33 lladdr 00:50:56:c0:00:08 DELAY
192.168.198.145 dev ens33 lladdr 00:0c:29:84:13:80 REACHABLE
192.168.198.2 dev ens33 lladdr 00:50:56:f6:6a:a7 REACHABLE
[root@localhost ~]#
[root@localhost ~]#ip neigh add 5.5.5.2 lladdr 00:50:56:f6:aa:aa dev ens37
[root@localhost ~]#ip neigh
192.168.198.1 dev ens33 lladdr 00:50:56:c0:00:08 REACHABLE
5.5.5.2 dev ens37 lladdr 00:50:56:f6:aa:aa PERMANENT
192.168.198.145 dev ens33 lladdr 00:0c:29:84:13:80 STALE
192.168.198.254 dev ens33 lladdr 00:50:56:f7:97:89 STALE
192.168.198.2 dev ens33 lladdr 00:50:56:f6:6a:a7 STALE
[root@localhost ~]#
[root@localhost ~]#ip neigh add 5.5.5.2 lladdr 00:50:56:f6:aa:aa dev ens37