ifconfig与ip命令的比较

基于Linux发行版提供了用简单和强大方式配置网络的命令行工具。这些命令集合从net-tools软件包获取,这个软件包在绝大多数发行版中存在了很长时间,并且包含像ifconfig, route, nameif, iwconfig, iptunnel, netstat, arp的命令。

这些命令对于任何新手或者linux专家在配置网络中基本已经够用了,但因为在过去几年Linux内核的进步以及未维护这个命令包集合,它们被弃用了并且一个能够替代所有这些命令的更强大替代品出现了。

这个替代品已经出现很长一段时间了并且要比任何这些命令更强大。余下部分将重点强调这个替代品并且把它与来自net-tools包一个命令(ifconfig)做比较。

ip用于替代ifconfig

ifconfig已经存在很长时间了,并且仍然被很多人用于配置,显示以及控制网络接口,但现在在Linux发行版上存在一个新的替代品,它比ifconfig要强大的多。这个替代品是来自iproute软件包的ip命令。

[root@localhost network-scripts]# yum whatprovides "ip"
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: mirror.lzu.edu.cn
 * epel: ftp.iij.ad.jp
 * extras: mirror.lzu.edu.cn
 * updates: download.nus.edu.sg
iproute-4.11.0-30.el7.x86_64 : Advanced IP routing and network device configuration tools
Repo        : base
Matched from:
Filename    : /usr/sbin/ip

虽然这个命令起初看起来有点复杂,但功能上比ifconfig更加全面。在两层网络栈功能上组织它,即是第二层(数据链路层),第三层(IP层),并且具有来自net-tools软件包以上提及命令的所有功能。

ifconfig基本上显示或修改系统的接口,ip命令能够做以下任务:

  • 显示或修改接口属性
  • 添加、移除ARP缓存条目以及未主机创建新的静态ARP条目
  • 显示与所有接口相关的MAC地址
  • 显示和修改内核路由表

ip命令其中一个主要不同于ifconfig的重点是使用ioctl用于网络配置,ifconfig使用ioctl用于网络配置,这是一种与内核交互不受欢迎的方式,ip为相同目的利用netlink套接字机制,这是使用rtnetlink用于内核和用户空间之间内部通信的ioctl的更加灵活的继任者。

我们现在开始突出ifconfig的特性并且如何有效地用ip命令替代它们。

ip vs ifconfig命令

以下部分强调ifconfig命令以及使用ip命令替换:

1、在Linux中显示所有网络接口

ifconfig命令

[root@localhost network-scripts]# ifconfig
eno1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.50.74  netmask 255.255.255.0  broadcast 192.168.50.255
        inet6 fe80::755d:a1a5:8380:e9f9  prefixlen 64  scopeid 0x20<link>
        ether 00:19:0f:3d:c9:3d  txqueuelen 1000  (Ethernet)
        RX packets 1079386  bytes 104138658 (99.3 MiB)
        RX errors 0  dropped 1027598  overruns 0  frame 0
        TX packets 7795  bytes 711277 (694.6 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
        device interrupt 20  memory 0xf7f00000-f7f20000

...

enp3s0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.3.30  netmask 255.255.255.0  broadcast 192.168.3.255
        inet6 fe80::53ff:e021:58e4:1629  prefixlen 64  scopeid 0x20<link>
        ether c4:00:ad:54:a3:23  txqueuelen 1000  (Ethernet)
        RX packets 21748  bytes 1311860 (1.2 MiB)
        RX errors 0  dropped 21271  overruns 0  frame 0
        TX packets 123  bytes 13015 (12.7 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
        device memory 0xf6f00000-f6ffffff
...

ip命令

[root@localhost network-scripts]# ip a
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: enp3s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    link/ether c4:00:ad:54:a3:23 brd ff:ff:ff:ff:ff:ff
    inet 192.168.3.30/24 brd 192.168.3.255 scope global noprefixroute enp3s0
       valid_lft forever preferred_lft forever
    inet6 fe80::53ff:e021:58e4:1629/64 scope link noprefixroute
       valid_lft forever preferred_lft forever
...
8: eno1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 00:19:0f:3d:c9:3d brd ff:ff:ff:ff:ff:ff
    inet 192.168.50.74/24 brd 192.168.50.255 scope global noprefixroute dynamic eno1
       valid_lft 81779sec preferred_lft 81779sec
    inet6 fe80::755d:a1a5:8380:e9f9/64 scope link noprefixroute
       valid_lft forever preferred_lft forever
...

2、添加或删除IP地址

ifconfig添加IP地址

[root@localhost network-scripts]# ifconfig enp3s0 add 192.168.3.31
[root@localhost network-scripts]# ip addr show enp3s0
2: enp3s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    link/ether c4:00:ad:54:a3:23 brd ff:ff:ff:ff:ff:ff
    inet 192.168.3.30/24 brd 192.168.3.255 scope global noprefixroute enp3s0
       valid_lft forever preferred_lft forever
    inet 192.168.3.31/24 brd 192.168.3.255 scope global secondary enp3s0:0
       valid_lft forever preferred_lft forever
    inet6 fe80::53ff:e021:58e4:1629/64 scope link noprefixroute
       valid_lft forever preferred_lft forever

ifconfig删除IP地址

[root@localhost network-scripts]# ifconfig enp3s0 del 192.168.3.31
[root@localhost network-scripts]# ip addr show enp3s0
2: enp3s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    link/ether c4:00:ad:54:a3:23 brd ff:ff:ff:ff:ff:ff
    inet 192.168.3.30/24 brd 192.168.3.255 scope global noprefixroute enp3s0
       valid_lft forever preferred_lft forever
    inet6 fe80::53ff:e021:58e4:1629/64 scope link noprefixroute
       valid_lft forever preferred_lft forever

ip添加IP地址

[root@localhost network-scripts]# ip addr add 192.168.3.31 dev enp3s0
[root@localhost network-scripts]# ip addr show enp3s0
2: enp3s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    link/ether c4:00:ad:54:a3:23 brd ff:ff:ff:ff:ff:ff
    inet 192.168.3.30/24 brd 192.168.3.255 scope global noprefixroute enp3s0
       valid_lft forever preferred_lft forever
    inet 192.168.3.31/32 scope global enp3s0
       valid_lft forever preferred_lft forever
    inet6 fe80::53ff:e021:58e4:1629/64 scope link noprefixroute
       valid_lft forever preferred_lft forever

ip删除IP地址

[root@localhost network-scripts]# ip addr del 192.168.3.31 dev enp3s0
Warning: Executing wildcard deletion to stay compatible with old scripts.
         Explicitly specify the prefix length (192.168.3.31/32) to avoid this warning.
         This special behaviour is likely to disappear in further releases,
         fix your scripts!
[root@localhost network-scripts]# ip addr show enp3s0
2: enp3s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    link/ether c4:00:ad:54:a3:23 brd ff:ff:ff:ff:ff:ff
    inet 192.168.3.30/24 brd 192.168.3.255 scope global noprefixroute enp3s0
       valid_lft forever preferred_lft forever
    inet6 fe80::53ff:e021:58e4:1629/64 scope link noprefixroute
       valid_lft forever preferred_lft forever

3、添加MAC硬件地址到网络接口

以下命令为接口enp3s0设置硬件地址为命令中指定的值。检查ifconfig命令输出中HWaddr可以确认。

使用ifconfig命令添加MAC地址的语法:

[root@localhost network-scripts]# ifconfig enp3s0 hw ether 00:aa:bb:cc:dd:ee
[root@localhost network-scripts]# ifconfig enp3s0
enp3s0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.3.30  netmask 255.255.255.0  broadcast 192.168.3.255
        inet6 fe80::53ff:e021:58e4:1629  prefixlen 64  scopeid 0x20<link>
        ether 00:aa:bb:cc:dd:ee  txqueuelen 1000  (Ethernet)
        RX packets 22661  bytes 1366894 (1.3 MiB)
        RX errors 0  dropped 22182  overruns 0  frame 0
        TX packets 158  bytes 17635 (17.2 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
        device memory 0xf6f00000-f6ffffff

ip添加MAC地址

使用ip命令添加MAC地址的语法:

[root@localhost network-scripts]# ip link set dev enp3s0 address 00:ee:dd:cc:bb:aa
[root@localhost network-scripts]# ip addr show enp3s0
2: enp3s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    link/ether 00:ee:dd:cc:bb:aa brd ff:ff:ff:ff:ff:ff
    inet 192.168.3.30/24 brd 192.168.3.255 scope global noprefixroute enp3s0
       valid_lft forever preferred_lft forever
    inet6 fe80::53ff:e021:58e4:1629/64 scope link noprefixroute
       valid_lft forever preferred_lft forever

4、设置网络接口的其它配置

除了IP地址或硬件地址,能够应用于接口的其它配置包括:

  • MTU
  • 多播标记
  • 传输队列长度
  • 混杂模式
  • 启用或禁用所有多播模式

a) 设置MTU值为2000

ifconfig --其它网络配置

[root@localhost network-scripts]# ifconfig enp3s0 mtu 2000

ip -- 其它网络配置

[root@localhost network-scripts]# ip link set dev enp3s0 mtu 2000

b) 启用或者禁用多播标记

ifconfig

[root@localhost network-scripts]# ifconfig enp3s0 multicast

ip

[root@localhost network-scripts]# ip link set dev enp3s0 multicast on

c) 设置传输队列长度

ifconfig

[root@localhost network-scripts]# ifconfig enp3s0 txqueuelen 1200

ip

[root@localhost network-scripts]# ip link set dev enp3s0 txqueuelen 1200

4) 启用或禁用混杂模式

ifconfig

[root@localhost network-scripts]# ifconfig enp3s0 promisc

ip

[root@localhost network-scripts]# ip link set dev enp3s0 promisc on

5、启用或禁用网络接口

ifconfig禁用特定网络接口

[root@localhost network-scripts]# ifconfig enp3s0 down
[root@localhost network-scripts]# ifconfig enp3s0
enp3s0: flags=4866<BROADCAST,PROMISC,ALLMULTI,MULTICAST>  mtu 2000
        ether 00:ee:dd:cc:bb:aa  txqueuelen 1200  (Ethernet)
        RX packets 23966  bytes 1445944 (1.3 MiB)
        RX errors 0  dropped 23483  overruns 0  frame 0
        TX packets 211  bytes 25494 (24.8 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
        device memory 0xf6f00000-f6ffffff

ifconfig启用特定网络接口

[root@localhost network-scripts]# ifconfig enp3s0 up
[root@localhost network-scripts]# ifconfig enp3s0
enp3s0: flags=4931<UP,BROADCAST,RUNNING,PROMISC,ALLMULTI,MULTICAST>  mtu 2000
        inet 192.168.3.30  netmask 255.255.255.0  broadcast 192.168.3.255
        inet6 fe80::53ff:e021:58e4:1629  prefixlen 64  scopeid 0x20<link>
        ether c4:00:ad:54:a3:23  txqueuelen 1200  (Ethernet)
        RX packets 23969  bytes 1446147 (1.3 MiB)
        RX errors 0  dropped 23486  overruns 0  frame 0
        TX packets 228  bytes 28022 (27.3 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
        device memory 0xf6f00000-f6ffffff

ip禁用特定网络接口

[root@localhost network-scripts]# ip link set enp3s0 down
[root@localhost network-scripts]# ip addr show enp3s0
2: enp3s0: <BROADCAST,MULTICAST,ALLMULTI,PROMISC> mtu 2000 qdisc mq state DOWN group default qlen 1200
    link/ether 00:ee:dd:cc:bb:aa brd ff:ff:ff:ff:ff:ff

ip启用特定网络接口

[root@localhost network-scripts]# ip link set enp3s0 up
[root@localhost network-scripts]# ip addr show enp3s0
2: enp3s0: <BROADCAST,MULTICAST,ALLMULTI,PROMISC,UP,LOWER_UP> mtu 2000 qdisc mq state UP group default qlen 1200
    link/ether c4:00:ad:54:a3:23 brd ff:ff:ff:ff:ff:ff
    inet 192.168.3.30/24 brd 192.168.3.255 scope global noprefixroute enp3s0
       valid_lft forever preferred_lft forever
    inet6 fe80::53ff:e021:58e4:1629/64 scope link tentative noprefixroute
       valid_lft forever preferred_lft forever

6、启用或者禁用ARP协议

以下命令对指定网卡接口启用或禁用ARP协议

ifconfig--启用/禁用ARP协议

以下命令使ARP协议能与接口enp3s0一起使用。要禁用这个选项,只要用-arp替代arp。

[root@localhost network-scripts]# ifconfig enp3s0 arp
[root@localhost network-scripts]# ifconfig enp3s0 -arp

ip--启用或禁用ARP协议

以下命令是ip使得ARP协议能用于接口enp3s0。要禁用它,只要用off替代on。

[root@localhost network-scripts]# ip link set dev enp3s0 arp on
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值