ifconfig与ip使用对比

/ bin / ip 命令已经存在了一段时间。但是人们继续使用旧命令/ sbin / ifconfig。让我们清楚一点:ifconfig不会很快消失,但是它的较新版本ip更加强大,最终将取代它。

ip 的手册页乍看起来可能令人生畏,但是一旦您熟悉了命令语法,就很容易阅读。该页面不会介绍ip的新功能。而是比较ifconfig和ip命令以快速了解命令语法。

显示网络设备和配置

ifconfig
[root@gbase_rh7_002 gbase]# ifconfig
enp0s3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 10.0.2.102  netmask 255.255.255.0  broadcast 10.0.2.255
        inet6 fe80::4a75:17a4:8122:823f  prefixlen 64  scopeid 0x20<link>
        ether 08:00:27:12:27:16  txqueuelen 1000  (Ethernet)
        RX packets 623  bytes 61090 (59.6 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 574  bytes 77959 (76.1 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1  (Local Loopback)
        RX packets 10355  bytes 1053386 (1.0 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 10355  bytes 1053386 (1.0 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

virbr0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 192.168.122.1  netmask 255.255.255.0  broadcast 192.168.122.255
        ether 52:54:00:4a:d6:8a  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
ip addr show 
ip link show
[root@gbase_rh7_002 gbase]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
    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: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 08:00:27:12:27:16 brd ff:ff:ff:ff:ff:ff
    inet 10.0.2.102/24 brd 10.0.2.255 scope global enp0s3
       valid_lft forever preferred_lft forever
    inet6 fe80::4a75:17a4:8122:823f/64 scope link
       valid_lft forever preferred_lft forever
3: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN qlen 1000
    link/ether 52:54:00:4a:d6:8a brd ff:ff:ff:ff:ff:ff
    inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0
       valid_lft forever preferred_lft forever
4: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast master virbr0 state DOWN qlen 1000
    link/ether 52:54:00:4a:d6:8a brd ff:ff:ff:ff:ff:ff
[root@gbase_rh7_002 gbase]# ip link
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT qlen 1000
    link/ether 08:00:27:12:27:16 brd ff:ff:ff:ff:ff:ff
3: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN mode DEFAULT qlen 1000
    link/ether 52:54:00:4a:d6:8a brd ff:ff:ff:ff:ff:ff
4: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast master virbr0 state DOWN mode DEFAULT qlen 1000
    link/ether 52:54:00:4a:d6:8a brd ff:ff:ff:ff:ff:ff
[root@gbase_rh7_002 gbase]#

启用网络接口

ifconfig eth0 up
ip link set eth0 up

网络接口以类似的方式被禁用:

ifconfig eth0 down
ip link set eth0 down

设置IP地址

ifconfig eth0 192.168.0.77
ip address add 192.168.0.77 dev eth0

这是命令的简单版本。通常,还需要指定网络掩码或广播地址。以下示例显示了ifconfig和ip 变体。

不用说,网络掩码也可以CIDR表示法给出,例如192.168.0.77/24。

ifconfig eth0 192.168.0.77 netmask 255.255.255.0 broadcast 192.168.0.255
ip addr add 192.168.0.77/24 broadcast 192.168.0.255 dev eth0
[root@rh6-1 ~]# ip a
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 08:00:27:30:04:2f brd ff:ff:ff:ff:ff:ff
    inet 10.0.2.201/24 brd 10.0.2.255 scope global eth0
    inet6 fe80::a00:27ff:fe30:42f/64 scope link
       valid_lft forever preferred_lft forever
3: virbr0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UNKNOWN
    link/ether 52:54:00:f9:e5:9e brd ff:ff:ff:ff:ff:ff
    inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0
4: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN qlen 500
    link/ether 52:54:00:f9:e5:9e brd ff:ff:ff:ff:ff:ff
[root@rh6-1 ~]# ip address add 10.0.2.251 dev eth0 label eth0:1
[root@rh6-1 ~]# ifconfig
eth0      Link encap:Ethernet  HWaddr 08:00:27:30:04:2F
          inet addr:10.0.2.201  Bcast:10.0.2.255  Mask:255.255.255.0
          inet6 addr: fe80::a00:27ff:fe30:42f/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:1173 errors:0 dropped:0 overruns:0 frame:0
          TX packets:1317 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:102138 (99.7 KiB)  TX bytes:117246 (114.4 KiB)

eth0:1    Link encap:Ethernet  HWaddr 08:00:27:30:04:2F
          inet addr:10.0.2.251  Bcast:0.0.0.0  Mask:255.255.255.255
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

lo        Link encap:Local Loopback
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:3921 errors:0 dropped:0 overruns:0 frame:0
          TX packets:3921 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:403061 (393.6 KiB)  TX bytes:403061 (393.6 KiB)

virbr0    Link encap:Ethernet  HWaddr 52:54:00:F9:E5:9E
          inet addr:192.168.122.1  Bcast:192.168.122.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:0 (0.0 b)  TX bytes:0 (0.0 b)

[root@rh6-1 ~]#

删除IP地址

使用ip也可以删除地址:

ip addr del 192.168.0.77/24 dev eth0

添加别名界面

ifconfig eth0:1 10.0.0.1/8
ip addr add 10.0.0.1/8 dev eth0 label eth0:1

ARP协议

在您的ARP表中添加一个条目。

arp -i eth0 -s 192.168.0.1 00:11:22:33:44:55
ip neigh add 192.168.0.1 lladdr 00:11:22:33:44:55 nud permanent dev eth0

交换机ARP解析关闭一台设备上

ifconfig -arp eth0
ip link set dev eth0 arp off

显示路由表

route
ip route show

ip路由的 一个不错的功能是可以查询到给定IP地址的数据包将路由到哪个接口(和网关)。

ip route get 192.168.88.77

更改路由表

在接口上添加路由的命令非常相似:

route add -net 192.168.3.0/24 dev eth3
ip route add 192.168.3.0/24 dev eth3

这同样适用于从路由表中删除条目:

route del -net 192.168.3.0/24 dev eth3
ip route del 192.168.3.0/24 dev eth3

为了完整起见,添加网关的命令:

route add -net 192.168.4.0/24 gw 192.168.4.1
ip route add 192.168.4.0/24 via 192.168.4.1

这也可以与dev eth3接口结合使用。

添加VLAN接口

添加VLAN的命令比使用旧版vconfig工具的命令稍长,但是不需要安装其他工具。

vconfig add eth3 42
ip link add link eth3 name eth3.42 type vlan id 42