iproute2 对决 net-tools


如今很多系统管理员依然通过组合使用诸如ifconfig、route、arp和netstat等命令行工具(统称为net-tools)来配置网络功能,解决网络故障。net-tools起源于BSD的TCP/IP工具箱,后来成为老版本Linux内核中配置网络功能的工具。但自2001年起,Linux社区已经对其停止维护。同时,一些Linux发行版比如Arch Linux和CentOS/RHEL 7则已经完全抛弃了net-tools,只支持iproute2。

作为网络配置工具的一份子,iproute2的出现旨在从功能上取代net-tools。net-tools通过procfs(/proc)和ioctl系统调用去访问和改变内核网络配置,而iproute2则通过netlink套接字接口与内核通讯。抛开性能而言,iproute2的用户接口比net-tools显得更加直观。比如,各种网络资源(如link、IP地址、路由和隧道等)均使用合适的对象抽象去定义,使得用户可使用一致的语法去管理不同的对象。更重要的是,到目前为止,iproute2仍处在持续开发中。

如果你仍在使用net-tools,而且尤其需要跟上新版Linux内核中的最新最重要的网络特性的话,那么是时候转到iproute2的阵营了。原因就在于使用iproute2可以做很多net-tools无法做到的事情。

对于那些想要转到使用iproute2的用户,有必要了解下面有关net-tools和iproute2的众多对比。

使用iproute2配置force-onlink路由 

使用netstat检测及监测网络连接

netstat 的10个基本用法

Linux netstat命令详解

试试Linux下的ip命令,ifconfig已经过时了

显示所有已连接的网络接口

下面的命令显示出所有可用网络接口的列表(无论接口是否激活)。

使用net-tools:

  1. $ ifconfig -a

使用iproute2:

  1. $ ip link show

为什么在 RedHat Linux 5 下不能使用 ifconfig 命令 

激活或停用网络接口

使用这些命令来激活或停用某个指定的网络接口。

使用net-tools:

  1. $ sudo ifconfig eth1 up
  2. $ sudo ifconfig eth1 down

使用iproute2:

  1. $ sudo ip link set down eth1
  2. $ sudo ip link set up eth1

为网络接口分配IPv4地址

使用这些命令配置网络接口的IPv4地址。

使用net-tools:

  1. $ sudo ifconfig eth1 10.0.0.1/24

使用iproute2:

  1. $ sudo ip addr add 10.0.0.1/24 dev eth1

值得注意的是,可以使用iproute2给同一个接口分配多个IP地址,ifconfig则无法这么做。使用ifconfig的变通方案是使用IP别名。

  1. $ sudo ip addr add 10.0.0.1/24 broadcast 10.0.0.255 dev eth1
  2. $ sudo ip addr add 10.0.0.2/24 broadcast 10.0.0.255 dev eth1
  3. $ sudo ip addr add 10.0.0.3/24 broadcast 10.0.0.255 dev eth1

移除网络接口的IPv4地址

就IP地址的移除而言,除了给接口分配全0地址外,net-tools没有提供任何合适的方法来移除网络接口的IPv4地址。相反,iproute2则能很好地完全。

使用net-tools:

  1. $ sudo ifconfig eth1 0

使用iproute2:

  1. $ sudo ip addr del10.0.0.1/24 dev eth1

显示网络接口的IPv4地址

按照如下操作可查看某个指定网络接口的IPv4地址。

使用net-tools:

  1. $ ifconfig eth1

使用iproute2:

  1. $ ip addr show dev eth1

同样,如果接口分配了多个IP地址,iproute2会显示出所有地址,而net-tools只能显示一个IP地址。

为网络接口分配IPv6地址

使用这些命令为网络接口添加IPv6地址。net-tools和iproute2都允许用户为一个接口添加多个IPv6地址。

使用net-tools:

  1. $ sudo ifconfig eth1 inet6 add 2002:0db5:0:f102::1/64
  2. $ sudo ifconfig eth1 inet6 add 2003:0db5:0:f102::1/64

使用iproute2:

  1. $ sudo ip -6 addr add 2002:0db5:0:f102::1/64 dev eth1
  2. $ sudo ip -6 addr add 2003:0db5:0:f102::1/64 dev eth1

显示网络接口的IPv6地址

按照如下操作可显示某个指定网络接口的IPv6地址。net-tools和iproute2都可以显示出所有已分配的IPv6地址。

使用net-tools:

  1. $ ifconfig eth1

使用iproute2:

  1. $ ip -6 addr show dev eth1

更多详情见请继续阅读下一页的精彩内容:

  • 1
  • 2
  • 下一页

相关内容