ip

网络配置工具

补充说明

ip命令 用来显示或操纵Linux主机的路由、网络设备、策略路由和隧道,是Linux下较新的功能强大的网络配置工具。

语法

  1. ip(选项)(参数)
  2. Usage: ip [ OPTIONS ] OBJECT { COMMAND | help }
  3. ip [ -force ] -batch filename

选项

  1. OBJECT := { link | address | addrlabel | route | rule | neigh | ntable |
  2. tunnel | tuntap | maddress | mroute | mrule | monitor | xfrm |
  3. netns | l2tp | macsec | tcp_metrics | token }
  4. -V:显示指令版本信息;
  5. -s:输出更详细的信息;
  6. -f:强制使用指定的协议族;
  7. -4:指定使用的网络层协议是IPv4协议;
  8. -6:指定使用的网络层协议是IPv6协议;
  9. -0:输出信息每条记录输出一行,即使内容较多也不换行显示;
  10. -r:显示主机时,不使用IP地址,而使用主机的域名。

参数

  1. OPTIONS := { -V[ersion] | -s[tatistics] | -d[etails] | -r[esolve] |
  2. -h[uman-readable] | -iec |
  3. -f[amily] { inet | inet6 | ipx | dnet | bridge | link } |
  4. -4 | -6 | -I | -D | -B | -0 |
  5. -l[oops] { maximum-addr-flush-attempts } |
  6. -o[neline] | -t[imestamp] | -ts[hort] | -b[atch] [filename] |
  7. -rc[vbuf] [size] | -n[etns] name | -a[ll] }
  8. 网络对象:指定要管理的网络对象;
  9. 具体操作:对指定的网络对象完成具体操作;
  10. help:显示网络对象支持的操作命令的帮助信息。

实例

  1. ip link show # 显示网络接口信息
  2. ip link set eth0 up # 开启网卡
  3. ip link set eth0 down # 关闭网卡
  4. ip link set eth0 promisc on # 开启网卡的混合模式
  5. ip link set eth0 promisc offi # 关闭网卡的混个模式
  6. ip link set eth0 txqueuelen 1200 # 设置网卡队列长度
  7. ip link set eth0 mtu 1400 # 设置网卡最大传输单元
  8. ip addr show # 显示网卡IP信息
  9. ip addr add 192.168.0.1/24 dev eth0 # 设置eth0网卡IP地址192.168.0.1
  10. ip addr del 192.168.0.1/24 dev eth0 # 删除eth0网卡IP地址
  11. ip route show # 显示系统路由
  12. ip route add default via 192.168.1.254 # 设置系统默认路由
  13. ip route list # 查看路由信息
  14. ip route add 192.168.4.0/24 via 192.168.0.254 dev eth0 # 设置192.168.4.0网段的网关为192.168.0.254,数据走eth0接口
  15. ip route add default via 192.168.0.254 dev eth0 # 设置默认网关为192.168.0.254
  16. ip route del 192.168.4.0/24 # 删除192.168.4.0网段的网关
  17. ip route del default # 删除默认路由
  18. ip route delete 192.168.1.0/24 dev eth0 # 删除路由

用ip命令显示网络设备的运行状态

  1. [root@localhost ~]# ip link list
  2. 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue
  3. link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
  4. 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000
  5. link/ether 00:16:3e:00:1e:51 brd ff:ff:ff:ff:ff:ff
  6. 3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000
  7. link/ether 00:16:3e:00:1e:52 brd ff:ff:ff:ff:ff:ff

显示更加详细的设备信息

  1. [root@localhost ~]# ip -s link list
  2. 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue
  3. link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
  4. RX: bytes packets errors dropped overrun mcast
  5. 5082831 56145 0 0 0 0
  6. TX: bytes packets errors dropped carrier collsns
  7. 5082831 56145 0 0 0 0
  8. 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000
  9. link/ether 00:16:3e:00:1e:51 brd ff:ff:ff:ff:ff:ff
  10. RX: bytes packets errors dropped overrun mcast
  11. 3641655380 62027099 0 0 0 0
  12. TX: bytes packets errors dropped carrier collsns
  13. 6155236 89160 0 0 0 0
  14. 3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000
  15. link/ether 00:16:3e:00:1e:52 brd ff:ff:ff:ff:ff:ff
  16. RX: bytes packets errors dropped overrun mcast
  17. 2562136822 488237847 0 0 0 0
  18. TX: bytes packets errors dropped carrier collsns
  19. 3486617396 9691081 0 0 0 0

显示核心路由表

  1. [root@localhost ~]# ip route list
  2. 112.124.12.0/22 dev eth1 proto kernel scope link src 112.124.15.130
  3. 10.160.0.0/20 dev eth0 proto kernel scope link src 10.160.7.81
  4. 192.168.0.0/16 via 10.160.15.247 dev eth0
  5. 172.16.0.0/12 via 10.160.15.247 dev eth0
  6. 10.0.0.0/8 via 10.160.15.247 dev eth0
  7. default via 112.124.15.247 dev eth1

显示邻居表

  1. [root@localhost ~]# ip neigh list
  2. 112.124.15.247 dev eth1 lladdr 00:00:0c:9f:f3:88 REACHABLE
  3. 10.160.15.247 dev eth0 lladdr 00:00:0c:9f:f2:c0 STALE

获取主机所有网络接口

  1. ip link | grep -E '^[0-9]' | awk -F: '{print $2}'