0%

网络配置的大拿 ip

网络配置的大拿 ip

Linux ip 命令与 ifconfig 命令类似,但比 ifconfig 命令更加强大,主要用于显示或设置网络设备。

已经在Linux 2.2 加入到了内核。所以ip是加强版的网络配置工具,用来替代ifconfig并强化其他功能。

官方定义为:

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

对于这个命令,命令集是相当的多。先说一些基础的,其他就要自己摸索了。

使用方法为:

1
2
3
4
5
6
7
8
9
$ ip [ OPTIONS ] OBJECT { COMMAND | help }

$ ip [ -force ] -batch filename

# OBJECT的取值
# OBJECT := { link | address | addrlabel | route | rule | neigh | ntable | tunnel | tuntap | maddress | mroute | mrule | monitor | xfrm | netns | l2tp | tcp_metrics | token | macsec }

# OPTIONS的取值
# OPTIONS := { -V[ersion] | -h[uman-readable] | -s[tatistics] | -d[etails] | -r[esolve] | -iec | -f[amily] { inet | inet6 | ipx | dnet | link } | -4 | -6 | -I | -D | -B | -0 | -l[oops] { maximum-addr-flush-attempts } | -o[neline] | -rc[vbuf] [size] | -t[imestamp] | -ts[hort] | -n[etns] name | -a[ll] | -c[olor] }

COMMAND的值主要取决于OBJECT,可能有所不同,一般可以使用adddeleteshow(或者list),均可以输入help来进行查询。

OBJECT中常用的为:

  • link 网络设备
  • address 设备上的协议地址
  • -s, -stats, -statistics 统计化输出

显示网络设备

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
# 显示网络设备
$ ip link show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: eno1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP mode DEFAULT group default qlen 1000
link/ether xx:xx:xx:xx:xx:xx brd ff:ff:ff:ff:ff:ff
3: eno2: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 9000 qdisc mq state DOWN mode DEFAULT group default qlen 1000
link/ether xx:xx:xx:xx:xx:xx brd ff:ff:ff:ff:ff:ff

# 显示IP等更多信息
$ ip address show
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: eno1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
link/ether xx:xx:xx:xx:xx:xx brd ff:ff:ff:ff:ff:ff
inet 192.168.1.123/24 brd 192.168.254.255 scope global noprefixroute eno1
valid_lft forever preferred_lft forever
inet6 xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx/64 scope global noprefixroute
valid_lft forever preferred_lft forever
3: eno2: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 9000 qdisc mq state DOWN group default qlen 1000
link/ether xx:xx:xx:xx:xx:xx brd ff:ff:ff:ff:ff:ff

命令中的show为默认,也可以直接使用ip link或者ip address,结果一致。

设置IP地址

可以通过ip addr add/del xxx.xxx.xxx.xxx dev interface 来设置或者删除IP地址。

如下设置or删除eth0的IP地址。

1
2
3
4
5
# 设置IP地址
$ ip addr add 192.168.0.1/24 dev eth0

# 删除IP地址
$ ip addr del 192.168.0.1/24 dev eth0

启动关闭网卡

与ifconfig类似,也使用up与down来进行启动和关闭,具体如下:

1
2
3
4
5
# 开启网卡
$ ip link set eth0 up

# 关闭网卡
$ ip link set eth0 down

统计方便阅读

选项-s可以统计一些信息方便我们阅读,如下看看网络的情况:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
$ ip -s link
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
RX: bytes packets errors dropped overrun mcast
871883256468 251700492 0 0 0 0
TX: bytes packets errors dropped carrier collsns
871883256468 251700492 0 0 0 0
2: eno1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP mode DEFAULT group default qlen 1000
link/ether xx:xx:xx:xx:xx:xx brd ff:ff:ff:ff:ff:ff
RX: bytes packets errors dropped overrun mcast
64930085920632 50955323447 0 613156 0 472190933
TX: bytes packets errors dropped carrier collsns
17534345850354 17448077191 0 0 0 0
3: eno2: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 9000 qdisc mq state DOWN mode DEFAULT group default qlen 1000
link/ether xx:xx:xx:xx:xx:xx brd ff:ff:ff:ff:ff:ff
RX: bytes packets errors dropped overrun mcast
0 0 0 0 0 0
TX: bytes packets errors dropped carrier collsns
0 0 0 0 0 0

可以看到对输出进行了一些格式化,看起来更直观。

处无为之事,行不言之教;作而弗始,生而弗有,为而弗恃,功成不居!

欢迎关注我的其它发布渠道