欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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

程序员文章站 2024-02-12 13:27:22
...
[[email protected] ~]# ip addr add 192.168.140.3/24 dev ens37   //设置IP地址
[[email protected] ~]# ip -4 a s ens37                        //显示IP地址
3: ens37: <BROADCAST,MULTICAST,UP,LOWER_UP> 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
[[email protected] ~]# ip addr del 192.168.140.3/24 dev ens37
[[email protected] ~]# ip -4 a s ens37
3: ens37: <BROADCAST,MULTICAST,UP,LOWER_UP> 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
[[email protected] ~]# 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 
[[email protected] ~]# ip route add 192.0.2.1 via 192.168.145.2 dev ens37 
//添加到主机的路由:ip route add +主机地址+via +网关地址+dev 网卡
[[email protected] ~]# 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 
[[email protected] ~]# ip route del 192.0.2.1 via 192.168.145.2 dev ens37
//删除到主机的路由:ip route del +主机地址+via +网关地址+dev 网卡
[[email protected] ~]# 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 
[[email protected] ~]# ip route add 192.0.2.0/24 via 192.168.145.2 dev ens37
//添加到网络的路由:ip route add +网络地址+via +网关地址+dev 网卡
[[email protected] ~]# 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 
[[email protected] ~]# ip route del 192.0.2.0/24 via 192.168.145.2 dev ens37
//删除到网络的路由:ip route del +网络地址+via +网关地址+dev 网卡
[[email protected] ~]# 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 
[[email protected] ~]# ip route add default via 192.168.145.2 dev ens37
//添加默认路由:ip route add + via +主机地址+dev 网卡
[[email protected] ~]# 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 
[[email protected] ~]# ip route del default via 192.168.145.2 dev ens37
//删除默认路由:ip route del + via +主机地址+dev 网卡
[[email protected] ~]# 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