在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 隧道配置
一、ip addr配置ip
1、临时添加ip地址
[root@localhost ~]#ip addr add 1.1.1.1/24 dev ens37
[root@localhost ~]#
[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 ~]#
2、删除ip地址
比如我添加了两个ip 地址 如果想删除其中一个(使用ip addr del),删除全部( ip addr flush)。
[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 ~]#
3、配置子接口地址
label 添加子地址,现在仅仅是加个标签,其实可以配合交换机做单臂路由。用服务器的一个接口当多个vlan的网关,以后介绍。
删除的话,把add 换成 del 即可。
[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 ~]#
4、查看ip地址
查看单个网卡的ip地址(ip addr show 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 ~]#
查看全部网卡的(简写:ip a)全称是 ip addr show 或者ip addr
[root@localhost ~]#ip a
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: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> 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: <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 ~]#
5、有条件筛选查看
ip -4 a 查看所有接口的ipv4地址
ip -6 addr show ens33 查看ens33 接口的ipv6地址
[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 ~]#
二、ip link 禁用网卡,配置mac,修改mtu
1、禁用启用网卡,使用(ip link set up/down)
注意 state 那个状态,如果使用ip link 关闭的设备是不被nmcli 纳管的,在nmcli 里显示的是不可用
[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
2、修改mac地址(如非必要,不要修改)(使用 ip link set 修改)
[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 ~]#
3、修改mtu值(如非必要,不要修改)
[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 ~]#
三、ip route 配置路由
1、添加路由
添加网段的路由记得加上掩码,主机路由不需要加掩码,后面不用跟设备也可。
添加默认路由 需要加设备。删除的时候不需要(ip route del default )
[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
2、删除路由
删除路由只需要加上前面的主机路由或者网段路由即可,不用加网关或设备
使用ip route flush dev ens37 清空ens37 的路由
[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
3、查看路由
使用ip route list 查看全部路由表或者ip route
使用ip route list get 目标ip 可以查看这个目标ip 会丢进哪个网卡
[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 ~]#
四、ip neigh 配置arp
1、查看arp
[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 ~]#
2、添加静态arp
[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 ~]#