linux包之iproute之ip命令
[root@localhost ~]# rpm -qf /sbin/ip
iproute-2.6.32-31.el6.x86_64
ip 是个命令, ip 命令的功能很多!基本上它整合了 ifconfig 与 route 这两个命令
ip - show / manipulate routing, devices, policy routing and tunnels
ip [ OPTIONS ] OBJECT { COMMAND | help }
OBJECT := { link | addr | addrlabel | route | rule | neigh | tunnel |
maddr | mroute | monitor }
OPTIONS := { -V[ersion] | -s[tatistics] | -r[esolve] | -f[amily] { inet
| inet6 | ipx | dnet | link } | -o[neline] }
[root@localhost ~]# ip link
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:75:34:a5 brd ff:ff:ff:ff:ff:ff
ip link
ip link 可以设定与设备 (device) 有关的相关设定,包括 MTU 以及该网络设备的 MAC 等等,当然也可以启动 (up) 或关闭 (down) 某个网络设备。
ip link set [device] [动作与参数] device 指的是 eth0, eth1 等等设备代号
up|down :启动 (up) 或关闭 (down) 某个设备,其他参数使用预设的以太网参数;
address :如果这个设备可以更改 MAC ,用这个参数修改;
name :给予这个设备一个特殊的名字;
mtu :设置最大传输单元。
[root@linux ~]# ip link set eth0 up
# 启动eth0这个设备;
[root@linux ~]# ip link set eth0 down
# 关闭eth0这个设备;
[root@linux ~]# ip link set eth0 mtu 1000
# 更改 MTU为1000 bytes,单位就是 bytes 。
ip link set eth0 address aa:aa:aa:aa:aa:aa
[root@localhost ~]# ip -s link
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
RX: bytes packets errors dropped overrun mcast
108016 473 0 0 0 0
TX: bytes packets errors dropped carrier collsns
108016 473 0 0 0 0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:75:34:a5 brd ff:ff:ff:ff:ff:ff
RX: bytes packets errors dropped overrun mcast
23016263 27785 0 0 0 0
TX: bytes packets errors dropped carrier collsns
2402975 17084 0 0 0 0
[root@localhost ~]# 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:75:34:a5 brd ff:ff:ff:ff:ff:ff
inet 192.168.2.128/24 brd 192.168.2.255 scope global eth0
inet6 fe80::20c:29ff:fe75:34a5/64 scope link
valid_lft forever preferred_lft forever
ip addr
如果说 ip link 是与 OSI 七层模型的第二层数据链路层有关的话,那么 ip address (ip addr) 就是与第三层网络层有关的了。主要是在设定与 IP 有关的各项参数,包括 netmask, broadcast 等等。
ip address [add|del] [IP参数] [dev 设备名] [相关参数]
IP 参数 :主要就是网域的设定,例如 192.168.100.100/24 之类的设定
相关参数如下所示:
broadcast:设定广播位址,如果设定值是 + 表示让系统自动计算;
label :该设备的别名,例如eth0:0;
scope :这个设备的领域,通常是以下几个大类:
global :允许来自所有来源的连接;
site :仅支持IPv6 ,仅允许本主机的连接;
link :仅允许本设备自我连接;
host :仅允许本主机内部的连接;
所以当然是使用 global 了。预设也是 global !
ip addr add 192.168.50.50/24 dev eth1
ip addr del 192.168.50.50/24 dev eth1
[root@localhost ~]# ip addrlabel
prefix ::1/128 label 0
prefix ::/96 label 3
prefix ::ffff:0.0.0.0/96 label 4
prefix 2001::/32 label 6
prefix 2001:10::/28 label 7
prefix 2002::/16 label 2
prefix fc00::/7 label 5
prefix ::/0 label 1
[root@localhost ~]# ip rule
0: from all lookup local
32766: from all lookup main
32767: from all lookup default
[root@localhost ~]# ip neigh
192.168.2.190 dev eth0 lladdr 6c:fd:b9:3b:57:ff REACHABLE
192.168.2.2 dev eth0 lladdr 50:46:5d:b9:cd:68 REACHABLE
[root@localhost ~]# ip -s neigh
192.168.2.190 dev eth0 lladdr 6c:fd:b9:3b:57:ff ref 3 used 58/0/2 REACHABLE
192.168.2.2 dev eth0 lladdr 50:46:5d:b9:cd:68 ref 120 used 102/102/82 STALE
[root@localhost ~]# ip route
192.168.50.0/24 dev eth1 proto kernel scope link src 192.168.50.50
192.168.2.0/24 dev eth1 proto kernel scope link src 192.168.2.131 metric 1
default via 192.168.2.2 dev eth1 proto static
ip route
proto:此路由的路由协定,主要有 redirect, kernel, boot, static, ra 等, 其中 kernel 指的是直接由核心判断自动设定。
scope:路由的范围,主要是 link ,是与本设备有关的直接连接。
ip route add 192.168.5.0/24 dev eth0 直连网段
ip route add 192.168.10.0/24 via 192.168.5.100 dev eth0 非直连网段