iproute2

Linux 下的 ip 命令

linux下的 ip 命令,来自包 iproute2,用以替代以前的 ifconfig 等工具

查看IP

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# ip address
> 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
> link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
> inet 127.0.0.1/8 scope host lo
> valid_lft forever preferred_lft forever
> inet6 ::1/128 scope host
> valid_lft forever preferred_lft forever
> 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
> link/ether fc:00:00:00:10:01 brd ff:ff:ff:ff:ff:ff
> inet 192.168.1.2/24 brd 192.168.1.255 scope global dynamic eth0
> valid_lft 84155sec preferred_lft 84155sec
> inet6 fe80::a67d:1260:c66b:2ea7/64 scope link
> valid_lft forever preferred_lft forever
> inet6 fe80::3351:917d:79c7:e77e/64 scope link tentative dadfailed
> valid_lft forever preferred_lft forever
> inet6 fe80::52f2:636f:e9c9:58ff/64 scope link tentative dadfailed
> valid_lft forever preferred_lft forever
> 3: wlan0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
> link/ether fc:00:00:00:20:01 brd ff:ff:ff:ff:ff:ff
> inet 192.168.100.2/24 brd 192.168.100.255 scope global dynamic wlan0
> valid_lft 39458sec preferred_lft 39458sec
> inet6 fd2c:9da4:f76a::5ae/128 scope global
> valid_lft forever preferred_lft forever
> inet6 fd2c:9da4:f76a:0:6c63:768d:b3b1:634/64 scope global noprefixroute
> valid_lft forever preferred_lft forever
> inet6 fe80::a027:63e1:ece2:200c/64 scope link
> valid_lft forever preferred_lft forever

设置默认路由

当你有两个网卡,那么你可以添加默认路由来选择使用哪个网卡,立即生效,被替换的连接会被立即断开

1
ip route add default via 192.168.100.1 dev wlan0

其中 dev 表示设备 wlan0 表示具体那个接口(网络接口名称,例如eth0, eth0.1, wlan0等)

查看路由表

1
2
3
4
5
6
7
# ip route 
> default via 192.168.100.1 dev wlan0
> default via 192.168.1.1 dev eth0 proto static metric 100
> default dev wlan0 proto static scope link metric 600
> 169.254.0.0/16 dev eth0 scope link metric 1000
> 192.168.1.0/24 dev eth0 proto kernel scope link src 192.168.1.2 metric 100
> 192.168.100.0/24 dev wlan0 proto kernel scope link src 192.168.100.2 metric 600

参考:

Debian / Ubuntu Linux设置默认网关

鄂ICP备14007840号-1