mobile wallpaper 1mobile wallpaper 2mobile wallpaper 3mobile wallpaper 4mobile wallpaper 5
858 字
3 分钟
Linux操作大全(五):网络配置与管理详解
2026-05-25

Linux操作大全(五):网络配置与管理详解#

本文是Linux操作大全系列的第五篇,详细讲解Linux网络配置和管理。


一、网络基础概念#

1.1 IP地址#

IP地址是网络中设备的唯一标识,类似于门牌号。

IPv4格式192.168.1.100

IP地址分类

类型范围说明
A类1.0.0.0 - 126.255.255.255大型网络
B类128.0.0.0 - 191.255.255.255中型网络
C类192.0.0.0 - 223.255.255.255小型网络
私有地址10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16局域网使用

1.2 子网掩码#

子网掩码用于划分网络地址和主机地址。

常见子网掩码

掩码CIDR可用主机数
255.255.255.0/24254
255.255.0.0/1665534
255.0.0.0/816777214

1.3 网关#

网关是连接不同网络的”关口”,通常是路由器的IP地址。

1.4 DNS#

DNS(Domain Name System)将域名解析为IP地址。

常见DNS服务器

DNSIP地址
阿里DNS223.5.5.5, 223.6.6.6
腾讯DNS119.29.29.29
Google DNS8.8.8.8, 8.8.4.4
Cloudflare DNS1.1.1.1

二、查看网络信息#

2.1 ip命令(推荐)#

# 查看所有网络接口
ip addr show
ip a
# 查看指定接口
ip addr show eth0
# 查看IP地址(简略)
ip -4 addr show
ip -6 addr show
# 查看路由表
ip route show
ip r
# 查看邻居表(ARP)
ip neigh show

2.2 ifconfig命令(旧版)#

# 安装net-tools(如果没有)
<a id="apt"></a>
sudo apt install net-tools
# 查看所有接口
ifconfig
# 查看指定接口
ifconfig eth0
# 启用接口
sudo ifconfig eth0 up
# 禁用接口
sudo ifconfig eth0 down
# 设置IP地址
sudo ifconfig eth0 192.168.1.100 netmask 255.255.255.0

2.3 hostname命令#

# 查看主机名
hostname
# 查看详细信息
hostnamectl
# 设置主机名
sudo hostnamectl set-hostname new-hostname

三、网络配置方法#

3.1 Netplan(Ubuntu 18.04+)#

Netplan是Ubuntu 18.04引入的网络配置工具。

配置文件位置/etc/netplan/

# 查看配置文件
ls /etc/netplan/
# 编辑配置
sudo vim /etc/netplan/01-netcfg.yaml

静态IP配置示例

network:
version: 2
renderer: networkd
ethernets:
eth0:
dhcp4: no
addresses:
- 192.168.1.100/24
routes:
- to: default
via: 192.168.1.1
nameservers:
addresses:
- 223.5.5.5
- 223.6.6.6

DHCP配置示例

network:
version: 2
renderer: networkd
ethernets:
eth0:
dhcp4: yes

应用配置

# 测试配置
sudo netplan try
# 应用配置
sudo netplan apply

3.2 /etc/network/interfaces(Debian传统)#

# 编辑配置
sudo vim /etc/network/interfaces

静态IP配置

auto eth0
iface eth0 inet static
address 192.168.1.100
netmask 255.255.255.0
gateway 192.168.1.1
dns-nameservers 223.5.5.5 223.6.6.6

DHCP配置

auto eth0
iface eth0 inet dhcp

重启网络

sudo systemctl restart networking

3.3 NetworkManager(桌面版)#

# 查看连接
nmcli connection show
# 查看设备状态
nmcli device status
# 创建静态IP连接
sudo nmcli connection add type ethernet con-name static-eth0 ifname eth0 \
ipv4.addresses 192.168.1.100/24 \
ipv4.gateway 192.168.1.1 \
ipv4.dns "223.5.5.5 223.6.6.6" \
ipv4.method manual
# 启用连接
sudo nmcli connection up static-eth0
# 修改连接
sudo nmcli connection modify static-eth0 ipv4.addresses 192.168.1.101/24
# 删除连接
sudo nmcli connection delete static-eth0

3.4 /etc/sysconfig/network-scripts(CentOS 7)#

# 编辑配置
sudo vim /etc/sysconfig/network-scripts/ifcfg-eth0

静态IP配置

TYPE=Ethernet
BOOTPROTO=static
NAME=eth0
DEVICE=eth0
ONBOOT=yes
IPADDR=192.168.1.100
NETMASK=255.255.255.0
GATEWAY=192.168.1.1
DNS1=223.5.5.5
DNS2=223.6.6.6

重启网络

sudo systemctl restart network

四、DNS配置#

4.1 /etc/resolv.conf#

# 查看DNS配置
cat /etc/resolv.conf
# 临时修改DNS
sudo vim /etc/resolv.conf

添加内容:

nameserver 223.5.5.5
nameserver 223.6.6.6

注意:此文件可能会被DHCP覆盖,永久修改需要在Netplan或NetworkManager中配置。

4.2 systemd-resolved(Ubuntu 18.04+)#

# 查看DNS状态
resolvectl status
# 查看DNS统计
resolvectl statistics
# 刷新DNS缓存
sudo resolvectl flush-caches

4.3 测试DNS解析#

# 使用nslookup
nslookup example.com
# 使用dig
dig example.com
dig example.com A
dig example.com MX
dig @8.8.8.8 example.com

五、网络诊断工具#

5.1 ping命令#

功能:测试网络连通性。

# 基本ping
ping example.com
# 指定次数
ping -c 4 example.com
# 指定间隔(秒)
ping -i 0.5 example.com
# 指定包大小
ping -s 1024 example.com
# 指定TTL
ping -t 64 example.com
# 指定接口
ping -I eth0 example.com

5.2 traceroute命令#

功能:追踪数据包的路由路径。

# 基本traceroute
traceroute example.com
# 使用TCP(更准确)
sudo traceroute -T example.com
# 使用UDP
traceroute -U example.com
# 指定端口
traceroute -p 80 example.com

5.3 mtr命令#

功能:实时追踪路由(结合ping和traceroute)。

# 基本mtr
mtr example.com
# 报告模式
mtr -r -c 100 example.com
# 使用TCP
sudo mtr -T example.com

5.4 netstat / ss命令#

功能:查看网络连接、监听端口等。

# 查看所有监听端口
sudo netstat -tuln
sudo ss -tuln
# 查看所有连接
sudo netstat -an
sudo ss -an
# 查看指定端口
sudo netstat -tuln | grep :80
sudo ss -tuln | grep :80
# 查看进程使用的端口
sudo netstat -tulnp
sudo ss -tulnp
# 查看路由表
netstat -r
ip route

5.5 curl命令#

功能:发送HTTP请求。

# 基本GET请求
curl https://example.com
# 下载文件
curl -O https://example.com/file.zip
# 指定输出文件
curl -o output.html https://example.com
# POST请求
curl -X POST -d "key=value" https://example.com/api
# 携带Header
curl -H "Content-Type: application/json" https://example.com
# 显示详细信息
curl -v https://example.com
# 跟随重定向
curl -L https://example.com
# 忽略SSL证书验证
curl -k https://example.com

5.6 wget命令#

功能:下载文件。

# 下载文件
wget https://example.com/file.zip
# 指定输出文件名
wget -O output.zip https://example.com/file.zip
# 后台下载
wget -b https://example.com/large-file.zip
# 断点续传
wget -c https://example.com/large-file.zip
# 递归下载(整站)
wget -r -np -k https://example.com
# 限速下载
wget --limit-rate=1m https://example.com/file.zip

5.7 nmap命令#

功能:网络扫描和端口探测。

# 安装nmap
sudo apt install nmap
# 扫描指定IP的端口
nmap 192.168.1.100
# 扫描指定端口
nmap -p 80,443 192.168.1.100
# 扫描端口范围
nmap -p 1-1000 192.168.1.100
# 扫描整个网段
nmap 192.168.1.0/24
# 服务版本探测
nmap -sV 192.168.1.100
# 操作系统探测
sudo nmap -O 192.168.1.100

六、防火墙配置#

6.1 ufw(Ubuntu推荐)#

ufw = Uncomplicated Firewall,简单易用的防火墙。

# 启用防火墙
sudo ufw enable
# 禁用防火墙
sudo ufw disable
# 查看状态
sudo ufw status
sudo ufw status verbose
sudo ufw status numbered
# 允许端口
sudo ufw allow 22 # SSH
sudo ufw allow 80 # HTTP
sudo ufw allow 443 # HTTPS
sudo ufw allow 8080 # 自定义端口
# 允许端口范围
sudo ufw allow 3000:4000/tcp
# 允许特定IP
sudo ufw allow from 192.168.1.100
# 允许特定IP访问特定端口
sudo ufw allow from 192.168.1.100 to any port 22
# 拒绝端口
sudo ufw deny 23
# 删除规则
sudo ufw delete allow 80
sudo ufw delete 1 # 按编号删除
# 重置防火墙
sudo ufw reset

6.2 firewalld(CentOS/RHEL)#

# 启动防火墙
sudo systemctl start firewalld
# 查看状态
sudo firewall-cmd --state
# 查看开放的端口
sudo firewall-cmd --list-all
# 开放端口
sudo firewall-cmd --permanent --add-port=80/tcp
sudo firewall-cmd --permanent --add-port=443/tcp
# 重载配置
sudo firewall-cmd --reload
# 关闭端口
sudo firewall-cmd --permanent --remove-port=80/tcp
# 查看服务
sudo firewall-cmd --list-services
# 开放服务
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https

6.3 iptables(底层工具)#

# 查看规则
sudo iptables -L -n
sudo iptables -L -n -v
# 允许SSH
sudo iptables -A INPUT -p tcp --dport 22 -j ACCEPT
# 允许HTTP
sudo iptables -A INPUT -p tcp --dport 80 -j ACCEPT
# 允许HTTPS
sudo iptables -A INPUT -p tcp --dport 443 -j ACCEPT
# 允许已建立的连接
sudo iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
# 拒绝其他输入
sudo iptables -A INPUT -j DROP
# 删除规则
sudo iptables -D INPUT -p tcp --dport 80 -j ACCEPT
# 清空规则
sudo iptables -F
# 保存规则
sudo iptables-save > /etc/iptables.rules
# 恢复规则
sudo iptables-restore < /etc/iptables.rules

七、SSH远程连接#

7.1 SSH基础#

# 基本连接
ssh username@hostname
# 指定端口
ssh -p 2222 username@hostname
# 使用密钥连接
ssh -i ~/.ssh/id_rsa username@hostname
# 执行远程命令
ssh username@hostname ls -la
# 保持连接
ssh -o ServerAliveInterval=60 username@hostname

7.2 SSH密钥认证#

# 生成密钥对
ssh-keygen -t ed25519 -C "your_email@example.com"
# 或使用RSA
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
# 复制公钥到服务器
ssh-copy-id username@hostname
# 或手动复制
cat ~/.ssh/id_ed25519.pub | ssh username@hostname "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys"

7.3 SSH配置文件#

编辑 ~/.ssh/config

Host myserver
HostName 192.168.1.100
User ubuntu
Port 22
IdentityFile ~/.ssh/id_ed25519
Host production
HostName prod.example.com
User deploy
Port 2222
IdentityFile ~/.ssh/prod_key

然后可以直接:

ssh myserver
ssh production

7.4 SCP文件传输#

# 上传文件
scp file.txt username@hostname:/home/user/
# 下载文件
scp username@hostname:/home/user/file.txt ./
# 上传目录
scp -r directory/ username@hostname:/home/user/
# 指定端口
scp -P 2222 file.txt username@hostname:/home/user/

7.5 SFTP交互式传输#

# 连接SFTP
sftp username@hostname
# 常用命令
sftp> ls # 远程列表
sftp> lls # 本地列表
sftp> pwd # 远程当前目录
sftp> lpwd # 本地当前目录
sftp> get file # 下载文件
sftp> put file # 上传文件
sftp> mkdir dir # 创建远程目录
sftp> exit # 退出

7.6 SSH安全加固#

编辑 /etc/ssh/sshd_config

# 禁止root登录
PermitRootLogin no
# 禁止密码登录(只允许密钥)
PasswordAuthentication no
# 修改默认端口
Port 2222
# 限制登录用户
AllowUsers ubuntu deploy
# 限制最大尝试次数
MaxAuthTries 3
# 设置超时时间
ClientAliveInterval 300
ClientAliveCountMax 2

重启SSH服务:

sudo systemctl restart sshd

八、网络服务管理#

8.1 systemd管理网络服务#

# 查看网络服务状态
sudo systemctl status networking
sudo systemctl status NetworkManager
# 重启网络服务
sudo systemctl restart networking
# 启用/禁用NetworkManager
sudo systemctl enable NetworkManager
sudo systemctl disable NetworkManager

8.2 网络接口管理#

# 启用接口
sudo ip link set eth0 up
# 禁用接口
sudo ip link set eth0 down
# 设置MTU
sudo ip link set eth0 mtu 9000

九、实战:配置Web服务器网络#

9.1 配置静态IP#

/etc/netplan/01-webserver.yaml
network:
version: 2
renderer: networkd
ethernets:
eth0:
dhcp4: no
addresses:
- 192.168.1.100/24
routes:
- to: default
via: 192.168.1.1
nameservers:
addresses:
- 223.5.5.5
- 223.6.6.6

9.2 配置防火墙#

# 启用防火墙
sudo ufw enable
# 允许SSH
sudo ufw allow 22/tcp
# 允许HTTP和HTTPS
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
# 查看状态
sudo ufw status

9.3 配置SSH安全#

# 编辑SSH配置
sudo vim /etc/ssh/sshd_config
# 修改端口
Port 2222
# 禁止root登录
PermitRootLogin no
# 重启SSH
sudo systemctl restart sshd
# 更新防火墙规则
sudo ufw allow 2222/tcp
sudo ufw delete allow 22/tcp

十、总结#

本章学习了:

  1. 网络基础:IP地址、子网掩码、网关、DNS
  2. 查看网络:ip、ifconfig、hostname
  3. 网络配置:Netplan、interfaces、NetworkManager
  4. DNS配置:resolv.conf、systemd-resolved
  5. 网络诊断:ping、traceroute、mtr、curl、wget、nmap
  6. 防火墙:ufw、firewalld、iptables
  7. SSH:密钥认证、安全加固、SCP/SFTP

下一章预告:《Linux操作大全(六):进程管理与服务管理详解》


如有疑问或发现错误,欢迎在评论区指出!

分享

如果这篇文章对你有帮助,欢迎分享给更多人!

Linux操作大全(五):网络配置与管理详解
https://emilia520.icu/posts/linux-manual-05-network/
作者
火花花
发布于
2026-05-25
许可协议
CC BY-NC-SA 4.0

部分信息可能已经过时

目录