Linux命令之ip详解程序员

ip [ OPTIONS ] OBJECT { COMMAND | help }

这一条命令几乎包含了所有使用方法,其中

对象OBJECT={ link | addr | addrlabel | route | rule | neigh | ntable | tunnel | maddr | mroute | mrule | monitor | xfrm | token }

选项OPTIONS={ -V[ersion] | -s[tatistics] | -d[etails] | -r[esolve] | -h[uman-readable] | -iec | -f[amily] { inet | inet6 | ipx | dnet | link } | -o[neline] | -t[imestamp] | -b[atch] [filename] | -rc[vbuf] [size] }

不过COMMAND比较长,请使用’ip OBJECT help’查看。

剩下的如下显示:

ip [-force] -batch filename

ip是iproute2软件包里面的一个强大的网络配置工具,用来显示或操作路由、网络设备、策略路由和隧道,它能够替代一些传统的网络管理工具,例如ifconfig、route等。用ip配置的设备信息,大部分会在设备重启后还原,如果想永久保留配置,请尽量进入配置文件修改。

(1).对象

link 网络设备 
address 设备上的协议(IP或IPv6)地址 
addrlabel 协议地址选择的标签配置 
neighbour ARP或NDISC缓存条目 
route 路由表条目 
rule 路由策略数据库中的规则 
maddress 组播地址 
mroute 组播路由缓存条目 
tunnel IP隧道 
xfrm IPSec协议框架

所有对象的名称可以用完整或缩写形式书写,例如address可以缩写成addr或只是a。

(2).选项

-V,-Version 显示指令版本信息 
-s,-stats,statistics 输出详细信息 
-h,-human,-human-readable 输出人类可读的统计信息和后缀 
-iec 以IEC标准单位打印人类可读速率(例如1K=1024) 
-f,-family <FAMILY> 指定要使用的协议族。协议族标识可以是inet、inet6、ipx、dnet或link之一。如果此选项不存在,则从其他参数中推测协议族。如果命令行的其余部分没有提供足够的信息来推测该族,则ip会退回到默认值,通常是inet或any。link是一个特殊的系列标识符,表示不涉及网络协议。 
-4 –family inet的快捷方式 
-6 –family inet6的快捷方式 
-0 –family link的快捷方式 
-o,-oneline 将每条记录输出到一行,用’/’字符替换换行符。 
-r,-resolve 使用系统名称解析程序来打印DNS名称而不是主机地址。

(3).实例

注意:以下介绍的ip命令都是临时配置,一但重启就会还原,如”service network restart”

查看所有IP地址

ip a/addr/address

ip a/addr/address sh/show

查看指定IP地址

ip a/addr/address sh/show dev eth1

ip a/addr/address sh/show eth1

[[email protected] 桌面]# 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: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 
    link/ether 00:0c:29:42:9f:ce brd ff:ff:ff:ff:ff:ff 
    inet 192.168.78.129/24 brd 192.168.78.255 scope global eth1 
    inet6 fe80::20c:29ff:fe42:9fce/64 scope link  
       valid_lft forever preferred_lft forever 
[[email protected] 桌面]# ip a sh eth1 
2: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 
    link/ether 00:0c:29:42:9f:ce brd ff:ff:ff:ff:ff:ff 
    inet 192.168.78.129/24 brd 192.168.78.255 scope global eth1 
    inet6 fe80::20c:29ff:fe42:9fce/64 scope link  
       valid_lft forever preferred_lft forever

增加或删除IP地址

ip a/addr/address add 192.168.78.130/24 dev eth1

ip a/addr/address del/delete 192.168.78.130/24 dev eth1

[[email protected] 桌面]# ip a add 192.168.78.130/24 dev eth1 
[[email protected] 桌面]# ip a sh eth1 
2: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 
    link/ether 00:0c:29:42:9f:ce brd ff:ff:ff:ff:ff:ff 
    inet 192.168.78.129/24 brd 192.168.78.255 scope global eth1 
    inet 192.168.78.130/24 scope global secondary eth1 
    inet6 fe80::20c:29ff:fe42:9fce/64 scope link  
       valid_lft forever preferred_lft forever 
[[email protected] 桌面]# ip a del 192.168.78.130/24 dev eth1 
[[email protected] 桌面]# ip a sh eth1 
2: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 
    link/ether 00:0c:29:42:9f:ce brd ff:ff:ff:ff:ff:ff 
    inet 192.168.78.129/24 brd 192.168.78.255 scope global eth1 
    inet6 fe80::20c:29ff:fe42:9fce/64 scope link  
       valid_lft forever preferred_lft forever

删除eth1所有IP地址

ip a flush dev eth1

[[email protected] 桌面]# 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: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 
    link/ether 00:0c:29:42:9f:ce brd ff:ff:ff:ff:ff:ff 
    inet 192.168.78.129/24 brd 192.168.78.255 scope global eth1 
    inet6 fe80::20c:29ff:fe42:9fce/64 scope link  
       valid_lft forever preferred_lft forever 
[[email protected] 桌面]# ip a add 192.168.78.130/24 dev eth1 
[[email protected] 桌面]# ip a add 192.168.78.131/24 dev eth1 
[[email protected] 桌面]# 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: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 
    link/ether 00:0c:29:42:9f:ce brd ff:ff:ff:ff:ff:ff 
    inet 192.168.78.129/24 brd 192.168.78.255 scope global eth1 
    inet 192.168.78.130/24 scope global secondary eth1 
    inet 192.168.78.131/24 scope global secondary eth1 
    inet6 fe80::20c:29ff:fe42:9fce/64 scope link  
       valid_lft forever preferred_lft forever 
[[email protected] 桌面]# ip a flush dev eth1 
[[email protected] 桌面]# 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: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 
    link/ether 00:0c:29:42:9f:ce brd ff:ff:ff:ff:ff:ff

删除eth1的所有IPv4的IP地址

ip -4 a flush dev eth1

[[email protected] 桌面]# 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: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 
    link/ether 00:0c:29:42:9f:ce brd ff:ff:ff:ff:ff:ff 
    inet 192.168.78.129/24 brd 192.168.78.255 scope global eth1 
    inet6 fe80::20c:29ff:fe42:9fce/64 scope link  
       valid_lft forever preferred_lft forever 
[[email protected] 桌面]# ip a add 192.168.78.130/24 dev eth1 
[[email protected] 桌面]# 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: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 
    link/ether 00:0c:29:42:9f:ce brd ff:ff:ff:ff:ff:ff 
    inet 192.168.78.129/24 brd 192.168.78.255 scope global eth1 
    inet 192.168.78.130/24 scope global secondary eth1 
    inet6 fe80::20c:29ff:fe42:9fce/64 scope link  
       valid_lft forever preferred_lft forever 
[[email protected] 桌面]# ip -4 a flush dev eth1 
[[email protected] 桌面]# 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: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 
    link/ether 00:0c:29:42:9f:ce brd ff:ff:ff:ff:ff:ff 
    inet6 fe80::20c:29ff:fe42:9fce/64 scope link  
       valid_lft forever preferred_lft forever

查看网络设备信息

ip link sh/show/l/list/ls

ip link sh/show/l/ls/lsit eth1

ip link sh/show/l/ls/list dev eth1

[[email protected] 桌面]# ip link l 
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 
2: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 
    link/ether 00:0c:29:42:9f:ce brd ff:ff:ff:ff:ff:ff 
[[email protected] 桌面]# ip link l eth1 
2: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 
    link/ether 00:0c:29:42:9f:ce brd ff:ff:ff:ff:ff:ff 
[[email protected] 桌面]# ip link l dev eth1 
2: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 
    link/ether 00:0c:29:42:9f:ce brd ff:ff:ff:ff:ff:ff

停止与激活网络设备

ip link set dev eth1 down

ip link set dev eth1 up

[[email protected] 桌面]# 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: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 
    link/ether 00:0c:29:42:9f:ce brd ff:ff:ff:ff:ff:ff 
    inet 192.168.78.133/24 brd 192.168.78.255 scope global eth1 
    inet6 fe80::20c:29ff:fe42:9fce/64 scope link  
       valid_lft forever preferred_lft forever 
[[email protected] 桌面]# ip link set dev eth1 down 
[[email protected] 桌面]# 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: eth1: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast state DOWN qlen 1000 
    link/ether 00:0c:29:42:9f:ce brd ff:ff:ff:ff:ff:ff 
    inet 192.168.78.133/24 brd 192.168.78.255 scope global eth1 
[[email protected] 桌面]# 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: eth1: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast state DOWN qlen 1000 
    link/ether 00:0c:29:42:9f:ce brd ff:ff:ff:ff:ff:ff 
[[email protected] 桌面]# ip link set dev eth1 up 
[[email protected] 桌面]# 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: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 
    link/ether 00:0c:29:42:9f:ce brd ff:ff:ff:ff:ff:ff 
    inet6 fe80::20c:29ff:fe42:9fce/64 scope link  
       valid_lft forever preferred_lft forever 
[[email protected] 桌面]# 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: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 
    link/ether 00:0c:29:42:9f:ce brd ff:ff:ff:ff:ff:ff 
    inet 192.168.78.133/24 brd 192.168.78.255 scope global eth1 
    inet6 fe80::20c:29ff:fe42:9fce/64 scope link  
       valid_lft forever preferred_lft forever

查看路由表

ip r/ro/route

ip r/ro/route sh/show

ip r/ro/route sh/show dev eth1

[[email protected] 桌面]# ip r 
192.168.78.0/24 dev eth2  proto kernel  scope link  src 192.168.78.132  metric 1  
192.168.78.0/24 dev eth1  proto kernel  scope link  src 192.168.78.133  metric 1  
default via 192.168.78.2 dev eth1  proto static  
[[email protected] 桌面]# ip r sh dev eth1 
192.168.78.0/24  proto kernel  scope link  src 192.168.78.133  metric 1  
default via 192.168.78.2  proto static 

添加或删除路由

ip r/ro/route add 192.168.79.0/24 dev eth1

ip r/ro/route d/del/delete 192.168.79.0/24

ip r/ro/route d/del/delete 192.168.79.0/24 dev eth1

[[email protected] 桌面]# ip r 
192.168.78.0/24 dev eth2  proto kernel  scope link  src 192.168.78.132  metric 1  
192.168.78.0/24 dev eth1  proto kernel  scope link  src 192.168.78.133  metric 1  
default via 192.168.78.2 dev eth1  proto static  
[[email protected] 桌面]# ip r add 192.168.79.0/24 dev eth1 
[[email protected] 桌面]# ip r 
192.168.79.0/24 dev eth1  scope link  
192.168.78.0/24 dev eth2  proto kernel  scope link  src 192.168.78.132  metric 1  
192.168.78.0/24 dev eth1  proto kernel  scope link  src 192.168.78.133  metric 1  
default via 192.168.78.2 dev eth1  proto static  
[[email protected] 桌面]# ip r d 192.168.79.0/24 dev eth1 
[[email protected] 桌面]# ip r 
192.168.78.0/24 dev eth2  proto kernel  scope link  src 192.168.78.132  metric 1  
192.168.78.0/24 dev eth1  proto kernel  scope link  src 192.168.78.133  metric 1  
default via 192.168.78.2 dev eth1  proto static 

默认路由的删除、添加与修改

ip r/ro/route d/del/delete default

ip r/ro/route add default via 192.168.78.1

ip r/ro/route chg/change default via 192.168.78.2

[[email protected] 桌面]# ip r 
192.168.78.0/24 dev eth1  proto kernel  scope link  src 192.168.78.133  metric 1  
192.168.78.0/24 dev eth2  proto kernel  scope link  src 192.168.78.132  metric 1  
default via 192.168.78.2 dev eth1  proto static  
[[email protected] 桌面]# ip r d default 
[[email protected] 桌面]# ip r 
192.168.78.0/24 dev eth1  proto kernel  scope link  src 192.168.78.133  metric 1  
192.168.78.0/24 dev eth2  proto kernel  scope link  src 192.168.78.132  metric 1  
[[email protected] 桌面]# ip r add default via 192.168.78.1 
[[email protected] 桌面]# ip r 
192.168.78.0/24 dev eth1  proto kernel  scope link  src 192.168.78.133  metric 1  
192.168.78.0/24 dev eth2  proto kernel  scope link  src 192.168.78.132  metric 1  
default via 192.168.78.1 dev eth1  
[[email protected] 桌面]# ip r chg default via 192.168.78.2 
[[email protected] 桌面]# ip r 
192.168.78.0/24 dev eth1  proto kernel  scope link  src 192.168.78.133  metric 1  
192.168.78.0/24 dev eth2  proto kernel  scope link  src 192.168.78.132  metric 1  
default via 192.168.78.2 dev eth1 

查看ARP表

ip n/neigh/neighbuor sh/show

[[email protected] 桌面]# ip n 
192.168.78.2 dev eth1 lladdr 00:50:56:f0:d4:05 REACHABLE 
192.168.78.2 dev eth2 lladdr 00:50:56:f0:d4:05 STALE 
192.168.78.254 dev eth2 lladdr 00:50:56:f5:45:c8 STALE

参考:

https://blog.csdn.net/freeking101/article/details/68939059

原创文章,作者:奋斗,如若转载,请注明出处:https://blog.ytso.com/2878.html

(0)
上一篇 2021年7月16日
下一篇 2021年7月16日

相关推荐

发表回复

登录后才能评论