Linux之ip命令

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linux之ip命令相关的知识,希望对你有一定的参考价值。

ip命令是route2的一个网络配置工具,用于取代ifconfig,route等命令。此命令只是临时添加,重启网卡IP丢失。


常用参数

    link:常用于查看网看属性

     addr:用于配置/删除多IP

    route:用于配置路由信息

     rule:用于查看路由表


命令详解:


ip link:主要用于查看网卡属性,或者开启,关闭网卡


    show

     set


例:show

[[email protected] ~]# ip link show   #####查看所有网卡的硬件属性,不包括IP地址

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN 

    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00

2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000

    link/ether 00:0c:29:01:bf:a0 brd ff:ff:ff:ff:ff:ff

3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000

    link/ether 00:0c:29:01:bf:aa brd ff:ff:ff:ff:ff:ff


[[email protected] ~]# ip link show eth0  ########查看某一块网卡信息

2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000

    link/ether 00:0c:29:01:bf:a0 brd ff:ff:ff:ff:ff:ff


[[email protected] ~]# ip -s link show eth1    ######  -s  参数查看统计信息

3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000

    link/ether 00:0c:29:01:bf:aa brd ff:ff:ff:ff:ff:ff

    RX: bytes  packets  errors  dropped overrun mcast   

    2085187    28951    0       0       0       0      

    TX: bytes  packets  errors  dropped carrier collsns 

    5161       35       0       0       0       0      

例:set

# ip link set eth1 up|down          ####关闭或开始某个网卡


ip addr:用于管理多IP


     add  增加一个IP

    del  删除IP

     show  显示IP

     flush  清空IP

例:

[[email protected] ~]# ip addr add 10.1.8.16/8 dev eth1  ###eth1增加IP,并没有使用别名

[[email protected] ~]# ip addr

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 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

    link/ether 00:0c:29:01:bf:a0 brd ff:ff:ff:ff:ff:ff

    inet 172.16.16.173/24 brd 172.16.16.255 scope global eth0

    inet6 fe80::20c:29ff:fe01:bfa0/64 scope link 

       valid_lft forever preferred_lft forever

3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000

    link/ether 00:0c:29:01:bf:aa brd ff:ff:ff:ff:ff:ff

    inet 192.168.7.173/24 brd 192.168.7.255 scope global eth1

    inet 10.1.8.16/8 scope global eth1   注:ifconfig看不到

    inet6 fe80::20c:29ff:fe01:bfaa/64 scope link 

       valid_lft forever preferred_lft forever

[[email protected] ~]# ip addr add 192.168.29.10/24 dev eth1 label eth1:1 

                                   用label关键字增加子卡

[[email protected] ~]# ifconfig 

eth0      Link encap:Ethernet  HWaddr 00:0C:29:01:BF:A0  

          inet addr:172.16.16.173  Bcast:172.16.16.255  Mask:255.255.255.0

          inet6 addr: fe80::20c:29ff:fe01:bfa0/64 Scope:Link

          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

          RX packets:27735 errors:0 dropped:0 overruns:0 frame:0

          TX packets:549 errors:0 dropped:0 overruns:0 carrier:0

          collisions:0 txqueuelen:1000 

          RX bytes:1978580 (1.8 MiB)  TX bytes:67070 (65.4 KiB)


eth1      Link encap:Ethernet  HWaddr 00:0C:29:01:BF:AA  

          inet addr:192.168.7.173  Bcast:192.168.7.255  Mask:255.255.255.0

          inet6 addr: fe80::20c:29ff:fe01:bfaa/64 Scope:Link

          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

          RX packets:31549 errors:0 dropped:0 overruns:0 frame:0

          TX packets:35 errors:0 dropped:0 overruns:0 carrier:0

          collisions:0 txqueuelen:1000 

          RX bytes:2277774 (2.1 MiB)  TX bytes:5161 (5.0 KiB)


eth1:1    Link encap:Ethernet  HWaddr 00:0C:29:01:BF:AA  

          inet addr:192.168.29.10  Bcast:0.0.0.0  Mask:255.255.255.0

          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1


lo        Link encap:Local Loopback  

          inet addr:127.0.0.1  Mask:255.0.0.0

          inet6 addr: ::1/128 Scope:Host

          UP LOOPBACK RUNNING  MTU:16436  Metric:1

          RX packets:16 errors:0 dropped:0 overruns:0 frame:0

          TX packets:16 errors:0 dropped:0 overruns:0 carrier:0

          collisions:0 txqueuelen:0 

          RX bytes:960 (960.0 b)  TX bytes:960 (960.0 b)


[[email protected] ~]# ip addr del 192.168.29.10/24 dev eth1:1   ###删除eth1:1


[[email protected] ~]# ifconfig 

eth0      Link encap:Ethernet  HWaddr 00:0C:29:01:BF:A0  

          inet addr:172.16.16.173  Bcast:172.16.16.255  Mask:255.255.255.0

          inet6 addr: fe80::20c:29ff:fe01:bfa0/64 Scope:Link

          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

          RX packets:29138 errors:0 dropped:0 overruns:0 frame:0

          TX packets:627 errors:0 dropped:0 overruns:0 carrier:0

          collisions:0 txqueuelen:1000 

          RX bytes:2083498 (1.9 MiB)  TX bytes:76506 (74.7 KiB)


eth1      Link encap:Ethernet  HWaddr 00:0C:29:01:BF:AA  

          inet addr:192.168.7.173  Bcast:192.168.7.255  Mask:255.255.255.0

          inet6 addr: fe80::20c:29ff:fe01:bfaa/64 Scope:Link

          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

          RX packets:32839 errors:0 dropped:0 overruns:0 frame:0

          TX packets:35 errors:0 dropped:0 overruns:0 carrier:0

          collisions:0 txqueuelen:1000 

          RX bytes:2372722 (2.2 MiB)  TX bytes:5161 (5.0 KiB)


lo        Link encap:Local Loopback  

          inet addr:127.0.0.1  Mask:255.0.0.0

          inet6 addr: ::1/128 Scope:Host

          UP LOOPBACK RUNNING  MTU:16436  Metric:1

          RX packets:16 errors:0 dropped:0 overruns:0 frame:0

          TX packets:16 errors:0 dropped:0 overruns:0 carrier:0

          collisions:0 txqueuelen:0 

          RX bytes:960 (960.0 b)  TX bytes:960 (960.0 b)


[[email protected] ~]# ip addr del 10.1.8.16/8 dev eth1  ####删除IP


[[email protected] ~]# ip addr show

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 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

    link/ether 00:0c:29:01:bf:a0 brd ff:ff:ff:ff:ff:ff

    inet 172.16.16.173/24 brd 172.16.16.255 scope global eth0

    inet6 fe80::20c:29ff:fe01:bfa0/64 scope link 

       valid_lft forever preferred_lft forever

3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000

    link/ether 00:0c:29:01:bf:aa brd ff:ff:ff:ff:ff:ff

    inet 192.168.7.173/24 brd 192.168.7.255 scope global eth1

    inet6 fe80::20c:29ff:fe01:bfaa/64 scope link 

       valid_lft forever preferred_lft forever


ip route

    show:显示路由

    add:增加路由

     del:删除路由


[[email protected] ~]# ip route show  查看路由

192.168.7.0/24 dev eth1  proto kernel  scope link  src 192.168.7.173 

172.16.16.0/24 dev eth0  proto kernel  scope link  src 172.16.16.173 

169.254.0.0/16 dev eth0  scope link  metric 1002 

10.0.0.0/8 dev eth1  proto kernel  scope link  src 10.1.5.10 

[[email protected] ~]# route -n    查看路由

Kernel IP routing table

Destination     Gateway         Genmask         Flags Metric Ref    Use Iface

192.168.7.0     0.0.0.0         255.255.255.0   U     0      0        0 eth1

172.16.16.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0

169.254.0.0     0.0.0.0         255.255.0.0     U     1002   0        0 eth0

10.0.0.0        0.0.0.0         255.0.0.0       U     0      0        0 eth1


[[email protected] ~]# ip route add 10.0.8.0/24 via 192.168.7.1 

      等价于: ip route add 10.0.8.0/24 dev eth1

[[email protected] ~]# ip route show

192.168.7.0/24 dev eth1  proto kernel  scope link  src 192.168.7.173 

172.16.16.0/24 dev eth0  proto kernel  scope link  src 172.16.16.173 

10.0.8.0/24 via 192.168.7.1 dev eth1 

169.254.0.0/16 dev eth0  scope link  metric 1002 


[[email protected] ~]# ip route del 10.0.8.0/24

[[email protected] ~]# ip route show   ########删除路由

192.168.7.0/24 dev eth1  proto kernel  scope link  src 192.168.7.173 

172.16.16.0/24 dev eth0  proto kernel  scope link  src 172.16.16.173 

169.254.0.0/16 dev eth0  scope link  metric 1002 


ip rule:查看默认路由,策略路由,本地路由

     show|list

[[email protected] ~]# ip rule show

0:from all lookup local 

32766:from all lookup main 

32767:from all lookup default 


以上是关于Linux之ip命令的主要内容,如果未能解决你的问题,请参考以下文章

iproute家族命令之ip

Linux之网络管理ip及路由相关命令

Linux学习之命令行获取公网IP方法详解

Linux命令之ip

N天学习一个linux命令之ip

Linux之last命令