在现代Linux系统中,ifconfig 已经逐渐被 ip 命令取代。ip 是一个功能更强大、更灵活的工具,用于管理和配置网络接口、路由表以及链路状态等。本文将详细介绍 ip 命令的基本用法,并与 ifconfig 进行对比,帮助用户更好地理解和使用这个现代网络工具。
ifconfig 和 ip 的区别ifconfig:
ip:
iproute2 软件包的一部分。ip 命令的基本用法ip 命令可以分为多个子命令,包括 link、addr、route 等,分别用于不同的网络管理任务。
ip link show
该命令显示所有网络接口的状态,类似于 ifconfig 中的输出。例如:
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: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
link/ether 08:00:27:4e:69:6c brd ff:ff:ff:ff:ff:ff
ip addr show
该命令显示每个网络接口的IP地址信息,类似于 ifconfig 的主要功能。例如:
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
inet 192.168.1.100/24 brd 192.168.1.255 scope global dynamic eth0
valid_lft 86399sec preferred_lft 86399sec
可以通过以下命令为网络接口分配或删除IP地址:
ip addr add 192.168.1.200/24 dev eth0
ip addr del 192.168.1.200/24 dev eth0
ip link set eth0 up
ip link set eth0 down
ip route show
该命令显示当前系统的路由表信息,类似于 route -n。例如:
default via 192.168.1.1 dev eth0 proto dhcp metric 100
192.168.1.0/24 dev eth0 proto kernel scope link src 192.168.1.100 metric 100
ip route add 10.0.0.0/24 via 192.168.1.1 dev eth0
ip route del 10.0.0.0/24 dev eth0
ifconfig 切换到 ip假设你正在使用一个基于 ifconfig 的脚本,需要将其转换为使用 ip 命令。以下是具体的转换示例:
ifconfig:
ifconfig -a
ip:
ip addr show
ifconfig:
ifconfig eth0 up
ip:
ip link set eth0 up
ifconfig:
ifconfig eth0 192.168.1.200 netmask 255.255.255.0
ip:
ip addr add 192.168.1.200/24 dev eth0
ip link set eth0 up
route:
route add default gw 192.168.1.1
ip:
ip route add default via 192.168.1.1
ip 命令是现代Linux系统中不可或缺的网络管理工具,提供了比 ifconfig 更强大的功能和更高的灵活性。通过本文的学习,你应该能够熟练掌握 ip 的基本用法,并能够将其应用到实际工作中。