【Linux常见命令】ip命令

淺唱寂寞╮ 提交于 2019-12-05 06:52:11

ip命令是用来配置网卡ip信息的命令,且是未来的趋势,重启网卡后IP失效。

ip - show / manipulate routing, devices, policy routing and tunnels

 

语法:

  • 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).对象OBJECT

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

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

(2).选项OPTIONS

(1).对象

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

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

(2).选项

1
2
3
4
5
6
7
8
9
10
-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地址

1. ip a/addr/address

[root@oldboy ~]# 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
    link/ether 08:00:27:10:a5:56 brd ff:ff:ff:ff:ff:ff
    inet 10.0.2.15/24 brd 10.0.2.255 scope global eth0
    inet 10.0.2.10/24 brd 10.0.2.255 scope global secondary eth0:0
    inet6 fe80::a00:27ff:fe10:a556/64 scope link 
       valid_lft forever preferred_lft forever
3: eth3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 08:00:27:4c:6f:ad brd ff:ff:ff:ff:ff:ff
    inet 192.168.0.109/24 brd 192.168.0.255 scope global eth3
    inet6 fe80::a00:27ff:fe4c:6fad/64 scope link tentative dadfailed 
       valid_lft forever preferred_lft forever
[root@oldboy ~]# ip addr
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
    link/ether 08:00:27:10:a5:56 brd ff:ff:ff:ff:ff:ff
    inet 10.0.2.15/24 brd 10.0.2.255 scope global eth0
    inet 10.0.2.10/24 brd 10.0.2.255 scope global secondary eth0:0
    inet6 fe80::a00:27ff:fe10:a556/64 scope link 
       valid_lft forever preferred_lft forever
3: eth3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 08:00:27:4c:6f:ad brd ff:ff:ff:ff:ff:ff
    inet 192.168.0.109/24 brd 192.168.0.255 scope global eth3
    inet6 fe80::a00:27ff:fe4c:6fad/64 scope link tentative dadfailed 
       valid_lft forever preferred_lft forever
[root@oldboy ~]# ip address 
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
    link/ether 08:00:27:10:a5:56 brd ff:ff:ff:ff:ff:ff
    inet 10.0.2.15/24 brd 10.0.2.255 scope global eth0
    inet 10.0.2.10/24 brd 10.0.2.255 scope global secondary eth0:0
    inet6 fe80::a00:27ff:fe10:a556/64 scope link 
       valid_lft forever preferred_lft forever
3: eth3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 08:00:27:4c:6f:ad brd ff:ff:ff:ff:ff:ff
    inet 192.168.0.109/24 brd 192.168.0.255 scope global eth3
    inet6 fe80::a00:27ff:fe4c:6fad/64 scope link tentative dadfailed 
       valid_lft forever preferred_lft forever
ip a/addr/address

 

2. ip a/addr/address sh/show

[root@oldboy ~]# ip a sh  
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
    link/ether 08:00:27:10:a5:56 brd ff:ff:ff:ff:ff:ff
    inet 10.0.2.15/24 brd 10.0.2.255 scope global eth0
    inet 10.0.2.10/24 brd 10.0.2.255 scope global secondary eth0:0
    inet6 fe80::a00:27ff:fe10:a556/64 scope link 
       valid_lft forever preferred_lft forever
3: eth3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 08:00:27:4c:6f:ad brd ff:ff:ff:ff:ff:ff
    inet 192.168.0.109/24 brd 192.168.0.255 scope global eth3
    inet6 fe80::a00:27ff:fe4c:6fad/64 scope link tentative dadfailed 
       valid_lft forever preferred_lft forever
[root@oldboy ~]# ip a show
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
    link/ether 08:00:27:10:a5:56 brd ff:ff:ff:ff:ff:ff
    inet 10.0.2.15/24 brd 10.0.2.255 scope global eth0
    inet 10.0.2.10/24 brd 10.0.2.255 scope global secondary eth0:0
    inet6 fe80::a00:27ff:fe10:a556/64 scope link 
       valid_lft forever preferred_lft forever
3: eth3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 08:00:27:4c:6f:ad brd ff:ff:ff:ff:ff:ff
    inet 192.168.0.109/24 brd 192.168.0.255 scope global eth3
    inet6 fe80::a00:27ff:fe4c:6fad/64 scope link tentative dadfailed 
       valid_lft forever preferred_lft forever
ip a sh/show

 

查看指定IP地址

1. ip a/addr/address sh/show dev eth1

2. ip a/addr/address sh/show eth1

 

增加或删除IP地址

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

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

 

删除eth1所有IP地址

1. ip a flush dev eth1

 

删除eth1的所有IPv4的IP地址

1. ip -4 a flush dev eth1

 

查看网络设备信息

1. ip link sh/show/l/list/ls

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

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

 

停止与激活网络设备

1. ip link set dev eth1 down

2. ip link set dev eth1 up

 

查看路由表

1. ip r/ro/route

2. ip r/ro/route sh/show

3. ip r/ro/route sh/show dev eth1

 

添加或删除路由

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

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

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

 

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

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

2. ip r/ro/route add default via 192.168.78.1

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

 

查看ARP表

1. ip n/neigh/neighbuor sh/show

 

 

 

 

 

 
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!