跳轉到內容

Linux 網絡配置與管理 | 網絡命令完全指南與故障排除

Linux Network Management

網絡配置是 Linux 系統管理的核心技能之一。無論是服務器部署、容器編排還是日常開發,都離不開對網絡的深入理解。本文將全面介紹 Linux 網絡管理的各個方面。

Linux 常用網絡操作

主機名操作

  • hostname:顯示主機名
  • hostname XXX:修改主機名,不推薦,臨時生效

永久生效修改主機名需要修改 /etc/sysconfig/network 文件

現代系統的主機名管理

bash
# 查看主機名
hostname
hostnamectl

# 臨時修改主機名
hostname new-hostname

# 永久修改主機名(推薦)
sudo hostnamectl set-hostname new-hostname

# 修改靜態主機名
sudo hostnamectl set-hostname "server.example.com" --static

# 修改相關文件
sudo vim /etc/hostname
sudo vim /etc/hosts

# 驗證修改
hostname
hostname -f  # 完整限定域名

查詢系統完整信息

bash
uname -a #顯示完整的系統信息

uname 命令詳解

bash
# 所有信息
uname -a

# 單獨查詢各項信息
uname -s    # 內核名稱 (Linux)
uname -n    # 節點名稱 (主機名)
uname -r    # 內核版本
uname -v    # 內核版本詳細信息
uname -m    # 機器硬件名稱 (x86_64)
uname -p    # 處理器類型
uname -i    # 硬件平臺
uname -o    # 操作系統

# 示例輸出
$ uname -a
Linux server 5.15.0-91-generic #101-Ubuntu SMP x86_64 GNU/Linux

IP 地址操作

bash
ip addr # 查看 IP 地址
vim /etc/sysconfig/network-scripts/ # 修改 IP 地址
service network restart # 重啟網絡服務

現代 IP 地址管理(ip 命令)

bash
# 查看所有網絡接口
ip addr show
ip a  # 簡寫

# 查看特定接口
ip addr show eth0
ip a show ens33

# 查看路由表
ip route show
ip r

# 查看 ARP 表
ip neigh show

# 臨時添加 IP 地址
sudo ip addr add 192.168.1.100/24 dev eth0

# 刪除 IP 地址
sudo ip addr del 192.168.1.100/24 dev eth0

# 啟用/禁用接口
sudo ip link set eth0 up
sudo ip link set eth0 down

# 查看統計信息
ip -s link show eth0

配置靜態 IP(Ubuntu/Debian - Netplan)

yaml
# /etc/netplan/01-netcfg.yaml
network:
  version: 2
  renderer: networkd
  ethernets:
    eth0:
      addresses:
        - 192.168.1.100/24
      gateway4: 192.168.1.1
      nameservers:
        addresses:
          - 8.8.8.8
          - 8.8.4.4
bash
# 應用配置
sudo netplan apply

# 測試配置(120秒後自動回滾)
sudo netplan try

配置靜態 IP(CentOS/RHEL)

bash
# 編輯網卡配置
sudo vim /etc/sysconfig/network-scripts/ifcfg-eth0

# 配置內容
TYPE=Ethernet
BOOTPROTO=static
DEVICE=eth0
ONBOOT=yes
IPADDR=192.168.1.100
NETMASK=255.255.255.0
GATEWAY=192.168.1.1
DNS1=8.8.8.8
DNS2=8.8.4.4

# 重啟網絡服務
sudo systemctl restart network
# 或
sudo nmcli connection reload
sudo nmcli connection up eth0

域名映射

bash
vim /etc/hosts

/etc/hosts 文件詳解

bash
# 查看當前配置
cat /etc/hosts

# 示例內容
127.0.0.1   localhost
127.0.1.1   myhostname
::1         localhost ip6-localhost ip6-loopback

# 添加自定義映射
192.168.1.100   server.example.com server
192.168.1.101   db.example.com db

# 編輯文件
sudo vim /etc/hosts

# 驗證解析
ping server
getent hosts server

網絡服務管理

bash
systemctl stauts network # 查看網絡服務狀態
systemctl start network # 啟動網絡服務
systemctl stop network # 停止網絡服務
systemctl restart network # 重啟網絡服務
systemctl enable network # 設置開啟啟動

systemd 網絡管理(現代系統)

bash
# Ubuntu/Debian (NetworkManager)
sudo systemctl status NetworkManager
sudo systemctl restart NetworkManager
sudo systemctl enable NetworkManager

# CentOS/RHEL (network)
sudo systemctl status network
sudo systemctl restart network
sudo systemctl enable network

# 使用 NetworkManager CLI
nmcli device status
nmcli connection show
nmcli connection up "Wired connection 1"
nmcli connection down "Wired connection 1"

# 查看網絡連接
nmcli device wifi list
nmcli device wifi connect SSID password PASSWORD

傳統網絡服務管理

bash
# SysVinit 系統
sudo service networking restart
sudo service network-manager restart

# 查看網絡接口
ifconfig  # 舊命令
ip addr   # 新命令(推薦)

# 重啟特定接口
sudo ifdown eth0 && sudo ifup eth0

防火牆設置

bash
systemctl status firewalld #查看防火牆狀態
systemctl start firewalld #啟動防火牆
systemctl stop firewalld #關閉防火牆
systemctl is-enable firewalld #查看防火牆服務是否開機啟動
systemctl enable firewalld #開機時啟用防火牆服務
systemctl disable firewalld #開機時禁用防火牆服務
systemctl list-unit-files|grep enabled #查詢已經啟動的服務列表
systemctl --failed #查詢啟動失敗的服務列表

firewalld 防火牆管理(CentOS/RHEL 7+)

bash
# 查看狀態
sudo systemctl status firewalld
sudo firewall-cmd --state

# 啟動/停止/重啟
sudo systemctl start firewalld
sudo systemctl stop firewalld
sudo systemctl restart firewalld

# 開機自啟
sudo systemctl enable firewalld
sudo systemctl disable firewalld

# 查看默認區域
sudo firewall-cmd --get-default-zone

# 查看所有區域
sudo firewall-cmd --get-zones

# 查看當前區域的規則
sudo firewall-cmd --list-all

# 開放端口
sudo firewall-cmd --permanent --add-port=80/tcp
sudo firewall-cmd --permanent --add-port=443/tcp
sudo firewall-cmd --permanent --add-port=3306/tcp

# 開放服務
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --permanent --add-service=ssh

# 移除端口/服務
sudo firewall-cmd --permanent --remove-port=3306/tcp

# 重載配置
sudo firewall-cmd --reload

# 查看開放的端口
sudo firewall-cmd --list-ports

# 查看開放的服務
sudo firewall-cmd --list-services

UFW 防火牆管理(Ubuntu/Debian)

bash
# 安裝 UFW
sudo apt install ufw

# 查看狀態
sudo ufw status
sudo ufw status verbose

# 啟用/禁用
sudo ufw enable
sudo ufw disable

# 默認策略
sudo ufw default deny incoming
sudo ufw default allow outgoing

# 允許端口
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw allow 22/tcp

# 允許服務
sudo ufw allow ssh
sudo ufw allow http
sudo ufw allow https

# 允許特定 IP
sudo ufw allow from 192.168.1.100
sudo ufw allow from 192.168.1.0/24

# 拒絕訪問
sudo ufw deny 3306/tcp

# 刪除規則
sudo ufw delete allow 80/tcp
sudo ufw delete allow ssh

# 重置防火牆
sudo ufw reset

iptables 防火牆(傳統方式)

bash
# 查看規則
sudo iptables -L -n -v
sudo iptables -L INPUT -n -v

# 允許 SSH
sudo iptables -A INPUT -p tcp --dport 22 -j ACCEPT

# 允許 HTTP/HTTPS
sudo iptables -A INPUT -p tcp --dport 80 -j ACCEPT
sudo iptables -A INPUT -p tcp --dport 443 -j ACCEPT

# 允許已建立的連接
sudo iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

# 拒絕其他所有輸入
sudo iptables -P INPUT DROP

# 保存規則(Ubuntu)
sudo iptables-save > /etc/iptables/rules.v4

# 保存規則(CentOS)
sudo service iptables save

網絡診斷工具

ping - 測試連通性

bash
# 基本用法
ping google.com

# 指定次數
ping -c 4 google.com

# 指定間隔
ping -i 2 google.com

# 快速 ping
ping -c 4 -i 0.2 google.com

# IPv6
ping6 google.com
ping -6 google.com

traceroute - 追蹤路由

bash
# 安裝
sudo apt install traceroute     # Debian/Ubuntu
sudo yum install traceroute     # CentOS/RHEL

# 使用
traceroute google.com
traceroute -n google.com  # 不解析域名

# 替代方案
tracepath google.com
mtr google.com  # 更強大的工具

nslookup/dig - DNS 查詢

bash
# nslookup
nslookup google.com
nslookup -type=MX google.com

# dig(更強大)
dig google.com
dig google.com A
dig google.com MX
dig google.com NS
dig +short google.com

# 指定 DNS 服務器
dig @8.8.8.8 google.com

netstat/ss - 網絡連接查看

bash
# netstat(舊命令)
netstat -tuln          # 查看監聽端口
netstat -an            # 查看所有連接
netstat -s             # 查看統計信息

# ss(新命令,推薦)
ss -tuln               # 查看監聽端口
ss -tunap              # 查看所有連接及進程
ss -s                  # 查看摘要統計

# 查看特定端口
ss -tlnp | grep :80
lsof -i :80

curl/wget - HTTP 測試

bash
# curl
curl -I https://example.com       # 查看響應頭
curl -v https://example.com       # 詳細輸出
curl -o file.html https://example.com  # 下載文件

# wget
wget https://example.com/file.zip
wget -O output.zip https://example.com/file.zip
wget -c https://example.com/largefile.zip  # 斷點續傳

網絡性能測試

iperf3 - 網絡帶寬測試

bash
# 安裝
sudo apt install iperf3

# 服務器端
iperf3 -s

# 客戶端
iperf3 -c server_ip
iperf3 -c server_ip -t 30  # 測試 30 秒
iperf3 -c server_ip -R     # 反向測試(下載)

speedtest-cli - 網速測試

bash
# 安裝
pip install speedtest-cli

# 使用
speedtest-cli
speedtest-cli --simple

網絡安全最佳實踐

1. 禁用不必要的服務

bash
# 查看監聽端口
sudo ss -tlnp

# 禁用不需要的服務
sudo systemctl disable telnet
sudo systemctl disable ftp

2. 更改 SSH 默認端口

bash
# 編輯 SSH 配置
sudo vim /etc/ssh/sshd_config

# 修改端口
Port 2222

# 重啟 SSH
sudo systemctl restart sshd

# 更新防火牆
sudo ufw allow 2222/tcp
sudo ufw delete allow 22/tcp

3. 配置 Fail2Ban

bash
# 安裝
sudo apt install fail2ban

# 配置
sudo vim /etc/fail2ban/jail.local

[sshd]
enabled = true
port = ssh
filter = sshd
logpath = /var/log/auth.log
maxretry = 3
bantime = 3600

# 啟動
sudo systemctl enable fail2ban
sudo systemctl start fail2ban

4. 網絡監控

bash
# 實時監控
iftop           # 類似 top 的網絡流量監控
nethogs         # 按進程查看帶寬
bmon            # 帶寬監控
vnstat          # 長期流量統計

# 安裝
sudo apt install iftop nethogs bmon vnstat

常見問題排查

問題 1:無法連接網絡

bash
# 診斷步驟
ping 127.0.0.1              # 測試本地網絡棧
ping 192.168.1.1            # 測試網關
ping 8.8.8.8                # 測試外網
ping google.com             # 測試 DNS

# 檢查網絡接口
ip addr show
ip route show

# 檢查 DNS
cat /etc/resolv.conf
nslookup google.com

# 重啟網絡
sudo systemctl restart NetworkManager

問題 2:DNS 解析失敗

bash
# 檢查 DNS 配置
cat /etc/resolv.conf

# 修改 DNS
sudo vim /etc/resolv.conf
nameserver 8.8.8.8
nameserver 8.8.4.4

# 或使用 systemd-resolved
sudo systemd-resolve --set-dns=8.8.8.8 --interface=eth0

問題 3:端口被佔用

bash
# 查找佔用端口的進程
sudo lsof -i :80
sudo ss -tlnp | grep :80
sudo netstat -tlnp | grep :80

# 終止進程
sudo kill -9 PID

問題 4:防火牆阻止連接

bash
# 檢查防火牆狀態
sudo ufw status
sudo firewall-cmd --list-all

# 臨時關閉防火牆測試
sudo ufw disable
sudo systemctl stop firewalld

# 添加規則
sudo ufw allow 8080/tcp
sudo firewall-cmd --add-port=8080/tcp --permanent
sudo firewall-cmd --reload

總結

Linux 網絡管理涉及多個方面:

  1. IP 配置:靜態/動態 IP、路由、DNS
  2. 防火牆:firewalld、UFW、iptables
  3. 服務管理:systemd、NetworkManager
  4. 網絡診斷:ping、traceroute、dig、ss
  5. 安全加固:端口管理、Fail2Ban、監控

關鍵命令速查:

bash
ip addr show              # 查看 IP
ip route show             # 查看路由
sudo ufw allow 80/tcp     # 開放端口
sudo systemctl restart NetworkManager  # 重啟網絡
ping -c 4 google.com      # 測試連通性
ss -tlnp                  # 查看監聽端口

下一步學習:

掌握網絡管理,讓你的 Linux 系統暢通無阻!🌐

最後更新於: