Linux 网络配置命令参考

Linux 网络配置命令参考

Linux 中与 IP 相关的命令主要分为两代:iproute2 套件 (现代主流,ip 命令为核心)和net-tools 套件(传统,已逐渐废弃)。


一、ip 命令(iproute2,推荐)

基本语法:ip [选项] 对象 { 命令 | help }

1. 查看/配置 IP 地址 ------ ip addr

bash 复制代码
ip addr show                    # 查看所有网卡地址(可简写 ip a)
ip addr show eth0               # 查看指定网卡
ip addr show dev eth0
ip addr add 192.168.1.100/24 dev eth0          # 添加 IP
ip addr add 192.168.1.101/24 dev eth0 label eth0:1   # 添加别名 IP
ip addr del 192.168.1.100/24 dev eth0          # 删除 IP
ip addr flush dev eth0          # 清空网卡上所有地址
ip -4 addr show                 # 只看 IPv4
ip -6 addr show                 # 只看 IPv6
ip -br a                        # 简洁模式(一行一网卡,非常常用)
bash 复制代码
ip link show                    # 查看所有网卡状态(ip l)
ip link show eth0
ip link set eth0 up             # 启用网卡
ip link set eth0 down           # 禁用网卡
ip link set eth0 mtu 1500       # 设置 MTU
ip link set eth0 name eth1      # 重命名网卡(需先 down)
ip link set eth0 address 00:11:22:33:44:55     # 修改 MAC 地址
ip link set eth0 promisc on     # 开启混杂模式

3. 路由管理 ------ ip route

bash 复制代码
ip route show                   # 查看路由表(ip r)
ip route show table all         # 查看所有路由表
ip route get 8.8.8.8            # 查询到某地址走哪条路由(排查利器)
ip route add 192.168.2.0/24 via 192.168.1.1              # 添加静态路由
ip route add 192.168.3.0/24 dev eth1                     # 指定出口网卡
ip route add default via 192.168.1.1 dev eth0             # 设置默认网关
ip route del 192.168.2.0/24                             # 删除路由
ip route replace default via 192.168.1.254               # 替换默认网关
ip route add 10.0.0.0/24 via 192.168.1.1 metric 100     # 设置路由优先级(metric 越小越优先)
ip route flush cache                                    # 刷新路由缓存

4. 邻居(ARP)表 ------ ip neigh

bash 复制代码
ip neigh show                   # 查看 ARP 表(ip n)
ip neigh add 192.168.1.50 lladdr 00:11:22:33:44:55 dev eth0   # 静态 ARP
ip neigh del 192.168.1.50 dev eth0
ip neigh flush dev eth0         # 清空 ARP 缓存
ip neigh replace 192.168.1.50 lladdr 00:11:22:33:44:55 dev eth0 nud permanent

5. 其他常用对象

bash 复制代码
# 隧道
ip tunnel add tun0 mode gre remote 1.2.3.4 local 5.6.7.8
ip tunnel show

# 策略路由规则
ip rule show
ip rule add from 192.168.1.0/24 table 100

# 网卡监控(抓取链路事件)
ip monitor link
ip monitor route

# VRF / netns
ip netns add ns1
ip netns exec ns1 ip addr show

二、连通性测试

ping / ping6

bash 复制代码
ping 8.8.8.8
ping -c 4 -i 0.2 192.168.1.1     # 发 4 个包,间隔 0.2 秒
ping -s 1472 -M do 192.168.1.1   # 测试 MTU(不分片)
ping -I eth0 8.8.8.8             # 指定出口网卡
ping6 fe80::1%eth0               # IPv6 链路本地地址需带 zone

traceroute / mtr

bash 复制代码
traceroute 8.8.8.8
traceroute -n -T -p 80 8.8.8.8   # 用 TCP 探测
mtr 8.8.8.8                      # traceroute + ping 持续统计

三、连接与端口排查

ss(替代 netstat,推荐)

bash 复制代码
ss -tunlp          # t:TCP u:UDP n:数字显示 l:监听 p:进程
ss -tan state established
ss -s              # 连接统计摘要
ss -tn dst 192.168.1.1:80
ss -o              # 显示计时器信息

netstat(传统)

bash 复制代码
netstat -tunlp
netstat -rn        # 查看路由表(等价 route -n)

四、DNS 与域名

bash 复制代码
nslookup baidu.com
nslookup baidu.com 8.8.8.8        # 指定 DNS 服务器
dig baidu.com +short
dig @114.114.114.114 baidu.com
dig -x 8.8.8.8                    # 反向解析
host baidu.com
cat /etc/resolv.conf              # DNS 配置文件
resolvectl status                 # systemd-resolved 查看(新系统)

五、ARP / 邻居发现

bash 复制代码
arp -n                            # 查看 ARP 表(传统)
arping -I eth0 192.168.1.1        # 发 ARP 探测(检测 IP 冲突)
ip neigh                          # 现代替代

六、抓包

bash 复制代码
tcpdump -i eth0 -nn host 192.168.1.1
tcpdump -i any -nn port 80 -w capture.pcap
tcpdump -i eth0 -nn 'tcp and port 443 and host 1.2.3.4'

七、传统命令对照表(net-tools → iproute2)

net-tools(旧) iproute2(新)
ifconfig ip addr / ip link
ifconfig eth0 192.168.1.10 netmask 255.255.255.0 up ip addr add 192.168.1.10/24 dev eth0 + ip link set eth0 up
route add default gw 192.168.1.1 ip route add default via 192.168.1.1
route -n ip route show
arp -n ip neigh show
netstat -tunlp ss -tunlp
iptunnel ip tunnel
nameif ip link set dev eth0 name ...

八、配置持久化(按发行版)

发行版 方式
RHEL/CentOS 7+ nmcli / nmtui,配置文件 /etc/sysconfig/network-scripts/(旧)或 NetworkManager keyfile
Ubuntu 18.04+ Netplan,/etc/netplan/*.yaml,执行 netplan apply
Debian 传统 /etc/network/interfaces,systemctl restart networking
通用现代 nmcli con mod / nmcli con up
bash 复制代码
# nmcli 示例
nmcli con show
nmcli con mod "Wired connection 1" ipv4.addresses 192.168.1.100/24
nmcli con mod "Wired connection 1" ipv4.gateway 192.168.1.1
nmcli con mod "Wired connection 1" ipv4.method manual
nmcli con up "Wired connection 1"

九、常用技巧速查

bash 复制代码
ip -br a                          # 快速看所有 IP
ip r get 1.1.1.1                  # 快速查出口
ss -tunlp | grep :80              # 查 80 端口占用
ethtool eth0                      # 网卡速率/双工/链路状态
ethtool -S eth0                   # 网卡统计
cat /proc/net/dev                 # 网卡流量统计
watch -n 1 'ip -br a'             # 每秒刷新

小结 :日常排查网络问题的"黄金组合"是 ip a + ip r + ping + ss + tcpdump 。掌握 ip 命令的 addr / link / route / neigh 四个对象,就能覆盖绝大多数 IP 相关操作。

相关推荐
Eyfcom13 小时前
〖物流 SaaS 实战 02〗SaaS 租户的计费与订阅模型:从“一锤子买卖”到“按需订阅”
linux·服务器·数据库·快递系统·物流saas实战
忆挽篱笙歌15 小时前
systemV进程通信
linux·运维·服务器
无奈笑天下20 小时前
银河麒麟桌面操作系统上磁盘无法自动挂载问题处理
linux·运维·服务器
木头左21 小时前
【Linux运维】服务器磁盘清理与Docker日志治理实战:一次巡检释放70G空间
linux·docker·磁盘清理·日志治理
星恒随风21 小时前
MySQL 从零入门(七):索引原理与 B+ 树详解——从磁盘 I/O 到 InnoDB 聚簇索引
linux·服务器·数据库·笔记·mysql
多弗朗皮卡丘1 天前
网络基础:数据链路层与其他协议或技术
linux·网络·tcp/ip
dingdingfish2 天前
TLPI 第52章 读书笔记:POSIX Message Queues
linux·mq·ipc·posix·message·tlpi