• ifconfig与ip命令的比较


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

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

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

    ip用于替代ifconfig

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

    1. [root@localhost network-scripts]# yum whatprovides "ip"
    2. Loaded plugins: fastestmirror, langpacks
    3. Loading mirror speeds from cached hostfile
    4. * base: mirror.lzu.edu.cn
    5. * epel: ftp.iij.ad.jp
    6. * extras: mirror.lzu.edu.cn
    7. * updates: download.nus.edu.sg
    8. iproute-4.11.0-30.el7.x86_64 : Advanced IP routing and network device configuration tools
    9. Repo : base
    10. Matched from:
    11. 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命令

    1. [root@localhost network-scripts]# ifconfig
    2. eno1: flags=4163 mtu 1500
    3. inet 192.168.50.74 netmask 255.255.255.0 broadcast 192.168.50.255
    4. inet6 fe80::755d:a1a5:8380:e9f9 prefixlen 64 scopeid 0x20
    5. ether 00:19:0f:3d:c9:3d txqueuelen 1000 (Ethernet)
    6. RX packets 1079386 bytes 104138658 (99.3 MiB)
    7. RX errors 0 dropped 1027598 overruns 0 frame 0
    8. TX packets 7795 bytes 711277 (694.6 KiB)
    9. TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
    10. device interrupt 20 memory 0xf7f00000-f7f20000
    11. ...
    12. enp3s0: flags=4163 mtu 1500
    13. inet 192.168.3.30 netmask 255.255.255.0 broadcast 192.168.3.255
    14. inet6 fe80::53ff:e021:58e4:1629 prefixlen 64 scopeid 0x20
    15. ether c4:00:ad:54:a3:23 txqueuelen 1000 (Ethernet)
    16. RX packets 21748 bytes 1311860 (1.2 MiB)
    17. RX errors 0 dropped 21271 overruns 0 frame 0
    18. TX packets 123 bytes 13015 (12.7 KiB)
    19. TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
    20. device memory 0xf6f00000-f6ffffff
    21. ...

    ip命令

    1. [root@localhost network-scripts]# ip a
    2. 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    3. link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    4. inet 127.0.0.1/8 scope host lo
    5. valid_lft forever preferred_lft forever
    6. inet6 ::1/128 scope host
    7. valid_lft forever preferred_lft forever
    8. 2: enp3s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    9. link/ether c4:00:ad:54:a3:23 brd ff:ff:ff:ff:ff:ff
    10. inet 192.168.3.30/24 brd 192.168.3.255 scope global noprefixroute enp3s0
    11. valid_lft forever preferred_lft forever
    12. inet6 fe80::53ff:e021:58e4:1629/64 scope link noprefixroute
    13. valid_lft forever preferred_lft forever
    14. ...
    15. 8: eno1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    16. link/ether 00:19:0f:3d:c9:3d brd ff:ff:ff:ff:ff:ff
    17. inet 192.168.50.74/24 brd 192.168.50.255 scope global noprefixroute dynamic eno1
    18. valid_lft 81779sec preferred_lft 81779sec
    19. inet6 fe80::755d:a1a5:8380:e9f9/64 scope link noprefixroute
    20. valid_lft forever preferred_lft forever
    21. ...

    2、添加或删除IP地址

    ifconfig添加IP地址

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

    ifconfig删除IP地址

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

    ip添加IP地址

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

    ip删除IP地址

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

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

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

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

    1. [root@localhost network-scripts]# ifconfig enp3s0 hw ether 00:aa:bb:cc:dd:ee
    2. [root@localhost network-scripts]# ifconfig enp3s0
    3. enp3s0: flags=4163 mtu 1500
    4. inet 192.168.3.30 netmask 255.255.255.0 broadcast 192.168.3.255
    5. inet6 fe80::53ff:e021:58e4:1629 prefixlen 64 scopeid 0x20
    6. ether 00:aa:bb:cc:dd:ee txqueuelen 1000 (Ethernet)
    7. RX packets 22661 bytes 1366894 (1.3 MiB)
    8. RX errors 0 dropped 22182 overruns 0 frame 0
    9. TX packets 158 bytes 17635 (17.2 KiB)
    10. TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
    11. device memory 0xf6f00000-f6ffffff

    ip添加MAC地址

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

    1. [root@localhost network-scripts]# ip link set dev enp3s0 address 00:ee:dd:cc:bb:aa
    2. [root@localhost network-scripts]# ip addr show enp3s0
    3. 2: enp3s0: mtu 1500 qdisc mq state UP group default qlen 1000
    4. link/ether 00:ee:dd:cc:bb:aa brd ff:ff:ff:ff:ff:ff
    5. inet 192.168.3.30/24 brd 192.168.3.255 scope global noprefixroute enp3s0
    6. valid_lft forever preferred_lft forever
    7. inet6 fe80::53ff:e021:58e4:1629/64 scope link noprefixroute
    8. 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禁用特定网络接口

    1. [root@localhost network-scripts]# ifconfig enp3s0 down
    2. [root@localhost network-scripts]# ifconfig enp3s0
    3. enp3s0: flags=4866 mtu 2000
    4. ether 00:ee:dd:cc:bb:aa txqueuelen 1200 (Ethernet)
    5. RX packets 23966 bytes 1445944 (1.3 MiB)
    6. RX errors 0 dropped 23483 overruns 0 frame 0
    7. TX packets 211 bytes 25494 (24.8 KiB)
    8. TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
    9. device memory 0xf6f00000-f6ffffff

    ifconfig启用特定网络接口

    1. [root@localhost network-scripts]# ifconfig enp3s0 up
    2. [root@localhost network-scripts]# ifconfig enp3s0
    3. enp3s0: flags=4931 mtu 2000
    4. inet 192.168.3.30 netmask 255.255.255.0 broadcast 192.168.3.255
    5. inet6 fe80::53ff:e021:58e4:1629 prefixlen 64 scopeid 0x20
    6. ether c4:00:ad:54:a3:23 txqueuelen 1200 (Ethernet)
    7. RX packets 23969 bytes 1446147 (1.3 MiB)
    8. RX errors 0 dropped 23486 overruns 0 frame 0
    9. TX packets 228 bytes 28022 (27.3 KiB)
    10. TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
    11. device memory 0xf6f00000-f6ffffff

    ip禁用特定网络接口

    1. [root@localhost network-scripts]# ip link set enp3s0 down
    2. [root@localhost network-scripts]# ip addr show enp3s0
    3. 2: enp3s0: mtu 2000 qdisc mq state DOWN group default qlen 1200
    4. link/ether 00:ee:dd:cc:bb:aa brd ff:ff:ff:ff:ff:ff

    ip启用特定网络接口

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

    6、启用或者禁用ARP协议

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

    ifconfig--启用/禁用ARP协议

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

    1. [root@localhost network-scripts]# ifconfig enp3s0 arp
    2. [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
  • 相关阅读:
    如何设计一份问卷?
    BF518/BF516/BF514/BF512系列DSP的开发教程四十一:数字信号处理-FFT
    VC中怎么进行结构体数组的初始化?
    Python正则表达式
    NET-MongoDB的安装使用
    Scikit-Learn支持向量机分类
    C++ “引用”究竟是什么?(代码实测)
    Primavera Unifier 21.12.4~21.12.7.0疑似漏洞
    基于无线通信模块对焦炉发讯装置的设计
    java计算机毕业设计家教管理系统MyBatis+系统+LW文档+源码+调试部署
  • 原文地址:https://blog.csdn.net/yuyuyuliang00/article/details/126121097