首页 > 代码库 > linux网络配置命令(二)——ip

linux网络配置命令(二)——ip

ip命令    

    查看/设置路由、设备、路由策略和渠道信息

格式

         ip [ OPTIONS ] OBJECT { COMMAND | help }  

         OBJECT := { link | addr | addrlabel | route | rule | neigh | tunnel | maddr | mroute |

               monitor }    #  对象命令,类似于ip命令的子命令

 

      OPTIONS := { -V[ersion] | -s[tatistics] | -r[esolve] | -f[amily] { inet | inet6 | ipx |

               dnet | link } | -o[neline] }

常见选项

                   -V             查看版本

                   -s              查看更详细的内容

                   -f               强制使用指定的协议族

                   -4              指定使用IPv4协议

                   -6              指定使用IPv6协议

                   -0              不换行输出信息

                   -r              使用主机的域名显示主机

 

常用对象命令

                   ip link       设置、查看数据链路层信息

                   ip addr    查看设置ip信息

                   ip route   查看设置路由信息

 

查看对象命令使用方法

                   ip OBJECT help

                  

 

示例

  1. 查看所有网卡的信息

[root@vathe-pc ~]#ip a   #  查看所有网卡信息
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    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 qlen 1000
...
4: eth2: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN qlen 1000
    link/ether 00:0c:29:d3:79:32 brd ff:ff:ff:ff:ff:ff
[root@vathe-pc ~]#ip a show eth2  # 查看网卡eth2的信息
4: eth2: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN qlen 1000
    link/ether 00:0c:29:d3:79:32 brd ff:ff:ff:ff:ff:ff

  2. 查看addr对象命令使用方法

[root@vathe-pc ~]#ip route help   #  查看route对象命令的使用方法
Usage: ip route { list | flush } SELECTOR
       ip route get ADDRESS [ from ADDRESS iif STRING ]
                            [ oif STRING ]  [ tos TOS ]
       ip route { add | del | change | append | replace | monitor } ROUTE
SELECTOR := [ root PREFIX ] [ match PREFIX ] [ exact PREFIX ]
            [ table TABLE_ID ] [ proto RTPROTO ]
            [ type TYPE ] [ scope SCOPE ]
ROUTE := NODE_SPEC [ INFO_SPEC ]
NODE_SPEC := [ TYPE ] PREFIX [ tos TOS ]
...

  3. 显示路由信息

[root@vathe-pc ~]#ip route
169.254.0.0/16 dev eth0  scope link  metric 1002 
169.254.0.0/16 dev eth1  scope link  metric 1003 
172.16.0.0/16 dev eth0  proto kernel  scope link  src 172.16.253.55 
55.0.0.0/8 dev eth1  proto kernel  scope link  src 55.0.0.55 
default via 55.0.0.1 dev eth1 

  4. 显示下一跳或上一条的路由器的路由信息

[root@vathe-pc ~]#ip neigh list
172.16.253.209 dev eth0 lladdr f0:76:1c:a9:8b:1b DELAY
172.16.0.1 dev eth0 lladdr f8:32:e4:73:bf:a4 STALE
172.16.252.15 dev eth0 lladdr 1c:cd:e5:3e:12:31 STALE
55.0.0.1 dev eth1  FAILED

  5. 分配ip地址信息

[root@vathe-pc ~]#ip addr add 172.100.100.100/16 dev eth2      #  分配ip
[root@vathe-pc ~]#ip a show eth2                   #  查看eth2网卡信息
4: eth2: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN qlen 1000
    link/ether 00:0c:29:d3:79:32 brd ff:ff:ff:ff:ff:ff
    inet 172.100.100.100/16 scope global eth2
[root@vathe-pc ~]#ip addr del 172.100.100.100/16 dev eth2 # 删除ip配置
[root@vathe-pc ~]#ip addr flush eth2                  # 清楚eth2网卡上所有的ip配置

  6. 修改路由信息

[root@vathe-pc ~]#ip route add 15.0.0.0/8 via 172.16.0.1          # 添加路由记录
[root@vathe-pc ~]#ip route                          # 查看路由表  
169.254.0.0/16 dev eth0  scope link  metric 1002 
169.254.0.0/16 dev eth1  scope link  metric 1003 
172.16.0.0/16 dev eth0  proto kernel  scope link  src 172.16.253.55   
55.0.0.0/8 dev eth1  proto kernel  scope link  src 55.0.0.55        
15.0.0.0/8 via 172.16.0.1 dev eth0                      # 刚添加进去的
default via 55.0.0.1 dev eth1 
[root@vathe-pc ~]#ip route del 15.0.0.0/8                  # 删除路由记录
[root@vathe-pc ~]#route -n                          # 使用route命令查看路由记录
Kernel IP routing table                       
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
169.254.0.0     0.0.0.0         255.255.0.0     U     1002   0        0 eth0
169.254.0.0     0.0.0.0         255.255.0.0     U     1003   0        0 eth1
172.16.0.0      0.0.0.0         255.255.0.0     U     0      0        0 eth0
55.0.0.0        0.0.0.0         255.0.0.0       U     0      0        0 eth1
0.0.0.0         55.0.0.1        0.0.0.0         UG    0      0        0 eth1

 

linux网络配置命令(二)——ip