使用ip命令配置IP地址和静态路由

[root@FQDN ~]# ip addr add 192.168.140.3/24 dev ens37   //设置IP地址
[root@FQDN ~]# ip -4 a s ens37                        //显示IP地址
3: ens37:  mtu 1500 qdisc pfifo_fast state UP qlen 1000
    inet 192.168.145.128/24 brd 192.168.145.255 scope global dynamic ens37
       valid_lft 1640sec preferred_lft 1640sec
    inet 192.168.140.3/24 scope global ens37
       valid_lft forever preferred_lft forever
[root@FQDN ~]# ip addr del 192.168.140.3/24 dev ens37
[root@FQDN ~]# ip -4 a s ens37
3: ens37:  mtu 1500 qdisc pfifo_fast state UP qlen 1000
    inet 192.168.145.128/24 brd 192.168.145.255 scope global dynamic ens37
       valid_lft 1632sec preferred_lft 1632sec
[root@FQDN ~]# ip route
default via 192.168.145.2 dev ens37 proto static metric 100 
192.168.122.0/24 dev virbr0 proto kernel scope link src 192.168.122.1 
192.168.145.0/24 dev ens37 proto kernel scope link src 192.168.145.128 metric 100 
[root@FQDN ~]# ip route add 192.0.2.1 via 192.168.145.2 dev ens37 
//添加到主机的路由:ip route add +主机地址+via +网关地址+dev 网卡
[root@FQDN ~]# ip route
default via 192.168.145.2 dev ens37 proto static metric 100 
192.0.2.1 via 192.168.145.2 dev ens37 
192.168.122.0/24 dev virbr0 proto kernel scope link src 192.168.122.1 
192.168.145.0/24 dev ens37 proto kernel scope link src 192.168.145.128 metric 100 
[root@FQDN ~]# ip route del 192.0.2.1 via 192.168.145.2 dev ens37
//删除到主机的路由:ip route del +主机地址+via +网关地址+dev 网卡
[root@FQDN ~]# ip route
default via 192.168.145.2 dev ens37 proto static metric 100 
192.168.122.0/24 dev virbr0 proto kernel scope link src 192.168.122.1 
192.168.145.0/24 dev ens37 proto kernel scope link src 192.168.145.128 metric 100 
[root@FQDN ~]# ip route add 192.0.2.0/24 via 192.168.145.2 dev ens37
//添加到网络的路由:ip route add +网络地址+via +网关地址+dev 网卡
[root@FQDN ~]# ip route
default via 192.168.145.2 dev ens37 proto static metric 100 
192.0.2.0/24 via 192.168.145.2 dev ens37 
192.168.122.0/24 dev virbr0 proto kernel scope link src 192.168.122.1 
192.168.145.0/24 dev ens37 proto kernel scope link src 192.168.145.128 metric 100 
[root@FQDN ~]# ip route del 192.0.2.0/24 via 192.168.145.2 dev ens37
//删除到网络的路由:ip route del +网络地址+via +网关地址+dev 网卡
[root@FQDN ~]# ip route
default via 192.168.145.2 dev ens37 proto static metric 100 
192.168.122.0/24 dev virbr0 proto kernel scope link src 192.168.122.1 
192.168.145.0/24 dev ens37 proto kernel scope link src 192.168.145.128 metric 100 
[root@FQDN ~]# ip route add default via 192.168.145.2 dev ens37
//添加默认路由:ip route add + via +主机地址+dev 网卡
[root@FQDN ~]# ip route
default via 192.168.145.2 dev ens37 
default via 192.168.145.2 dev ens37 proto static metric 100 
192.168.122.0/24 dev virbr0 proto kernel scope link src 192.168.122.1 
192.168.145.0/24 dev ens37 proto kernel scope link src 192.168.145.128 metric 100 
[root@FQDN ~]# ip route del default via 192.168.145.2 dev ens37
//删除默认路由:ip route del + via +主机地址+dev 网卡
[root@FQDN ~]# ip route
default via 192.168.145.2 dev ens37 proto static metric 100 
192.168.122.0/24 dev virbr0 proto kernel scope link src 192.168.122.1 
192.168.145.0/24 dev ens37 proto kernel scope link src 192.168.145.128 metric 100

你可能感兴趣的:(Linux学习)