跳转至

第六章:网络配置

网络接口

查看网络

# 查看 IP 地址
ip addr
ifconfig

# 查看路由
ip route
route -n

# 查看端口
netstat -tlnp
ss -tlnp

配置 IP

# 临时配置
ip addr add 192.168.1.100/24 dev eth0

# 永久配置(Ubuntu)
# /etc/netplan/01-netcfg.yaml
network:
  version: 2
  ethernets:
    eth0:
      addresses:
        - 192.168.1.100/24
      gateway4: 192.168.1.1
      nameservers:
        addresses:
          - 8.8.8.8
          - 8.8.4.4

# 应用配置
netplan apply

防火墙

ufw(Ubuntu)

# 启用防火墙
ufw enable

# 开放端口
ufw allow 80
ufw allow 443
ufw allow 22

# 查看状态
ufw status

# 删除规则
ufw delete allow 80

firewalld(CentOS)

# 启动服务
systemctl start firewalld

# 开放端口
firewall-cmd --add-port=80/tcp --permanent
firewall-cmd --reload

# 查看规则
firewall-cmd --list-all

网络诊断

# 测试连通性
ping google.com

# 查看路由
traceroute google.com

# DNS 查询
nslookup google.com
dig google.com

# 抓包
tcpdump -i eth0 port 80

小结

网络配置要点:

  • 网络接口:ip addr、ifconfig
  • 配置 IP:netplan、静态配置
  • 防火墙:ufw、firewalld
  • 网络诊断:ping、traceroute、tcpdump

下一章我们将学习性能监控。