網路概述

Linux 網路設定與管理

Network Interface Configuration

查看網路介面

# 使用 ip 指令 (推薦)
ip addr show
ip a

# 使用 ifconfig (傳統)
ifconfig
ifconfig eth0

啟用/停用介面

# 使用 ip
ip link set eth0 up
ip link set eth0 down

# 使用 ifconfig
ifconfig eth0 up
ifconfig eth0 down

設定 IP 位址

# 臨時設定 IP
ip addr add 192.168.1.100/24 dev eth0

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

# 使用 ifconfig
ifconfig eth0 192.168.1.100 netmask 255.255.255.0

Routing Table (路由表)

查看路由

# 使用 ip
ip route show
ip r

# 使用 route
route -n

# 查看特定路由
ip route get 8.8.8.8

設定路由

# 新增預設閘道
ip route add default via 192.168.1.1

# 新增特定路由
ip route add 10.0.0.0/8 via 192.168.1.1 dev eth0

# 刪除路由
ip route del 10.0.0.0/8

Set Linux Host as Router

將 Linux 主機設定為路由器,轉發封包。

啟用 IP Forwarding

# 臨時啟用
echo 1 > /proc/sys/net/ipv4/ip_forward
sysctl -w net.ipv4.ip_forward=1

# 永久啟用
# 編輯 /etc/sysctl.conf
net.ipv4.ip_forward=1

# 套用設定
sysctl -p

NAT (Network Address Translation)

使用 iptables 設定 NAT:

# 啟用 NAT (MASQUERADE)
iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE

# 允許轉發
iptables -A FORWARD -i eth1 -o eth0 -j ACCEPT
iptables -A FORWARD -i eth0 -o eth1 -m state --state RELATED,ESTABLISHED -j ACCEPT

# 儲存規則
iptables-save > /etc/iptables/rules.v4

路由器拓樸範例

Internet (公網)
    |
    | eth0: 203.0.113.10/24
[Linux Router]
    | eth1: 192.168.1.1/24
    |
內部網路 (私網)
192.168.1.0/24

DNS Configuration

/etc/resolv.conf

DNS 解析設定檔:

# 編輯 DNS 伺服器
nameserver 8.8.8.8
nameserver 8.8.4.4
nameserver 1.1.1.1

# 搜尋網域
search example.com

/etc/hosts

本機主機名稱對應:

127.0.0.1       localhost
127.0.1.1       hostname
192.168.1.100   server1.example.com server1
192.168.1.101   server2.example.com server2

DNS 查詢工具

# nslookup
nslookup google.com
nslookup google.com 8.8.8.8

# dig (更詳細)
dig google.com
dig @8.8.8.8 google.com
dig +short google.com

# host
host google.com

Firewall (防火牆)

iptables

Linux 核心防火牆工具。

基本概念

Tables (表)
├── filter (封包過濾) - 預設表
│   ├── INPUT (進入)
│   ├── FORWARD (轉發)
│   └── OUTPUT (輸出)
├── nat (網路位址轉換)
│   ├── PREROUTING
│   ├── POSTROUTING
│   └── OUTPUT
└── mangle (封包修改)

常用規則

# 查看規則
iptables -L -n -v
iptables -t nat -L -n -v

# 允許 SSH (port 22)
iptables -A INPUT -p tcp --dport 22 -j ACCEPT

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

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

# 允許 loopback
iptables -A INPUT -i lo -j ACCEPT

# 預設拒絕
iptables -P INPUT DROP
iptables -P FORWARD DROP
iptables -P OUTPUT ACCEPT

# 刪除規則
iptables -D INPUT 1

# 清空規則
iptables -F
iptables -t nat -F

firewalld

較新的防火牆管理工具 (RHEL/CentOS):

# 查看狀態
firewall-cmd --state

# 查看區域
firewall-cmd --get-active-zones

# 允許服務
firewall-cmd --add-service=http --permanent
firewall-cmd --add-service=https --permanent

# 允許埠
firewall-cmd --add-port=8080/tcp --permanent

# 重新載入
firewall-cmd --reload

ufw (Uncomplicated Firewall)

Ubuntu 簡易防火牆:

# 啟用防火牆
ufw enable

# 允許服務
ufw allow ssh
ufw allow 80/tcp
ufw allow 443/tcp

# 拒絕
ufw deny 23/tcp

# 查看狀態
ufw status verbose

# 刪除規則
ufw delete allow 80/tcp

Network Namespaces

網路命名空間,用於網路隔離。

建立與管理

# 建立 namespace
ip netns add ns1

# 列出 namespace
ip netns list

# 在 namespace 中執行指令
ip netns exec ns1 ip addr

# 刪除 namespace
ip netns del ns1

veth pair (虛擬網路介面對)

# 建立 veth pair
ip link add veth0 type veth peer name veth1

# 將一端移到 namespace
ip link set veth1 netns ns1

# 設定 IP
ip addr add 10.0.0.1/24 dev veth0
ip netns exec ns1 ip addr add 10.0.0.2/24 dev veth1

# 啟用介面
ip link set veth0 up
ip netns exec ns1 ip link set veth1 up

# 測試連線
ping 10.0.0.2

Network Monitoring

連線監控

# 查看所有連線
netstat -tunlp
ss -tunlp

# 查看監聽埠
netstat -tlnp
ss -tlnp

# 查看已建立連線
netstat -tan
ss -tan

# 即時監控連線
watch -n 1 'ss -tan'

封包擷取

# tcpdump
tcpdump -i eth0                    # 擷取 eth0 封包
tcpdump -i eth0 port 80           # 只擷取 port 80
tcpdump -i eth0 -w capture.pcap   # 儲存到檔案
tcpdump -r capture.pcap           # 讀取檔案

# 過濾條件
tcpdump -i eth0 'tcp port 80 or tcp port 443'
tcpdump -i eth0 'src host 192.168.1.100'
tcpdump -i eth0 'dst net 10.0.0.0/8'

Network Testing

連線測試

# ping - ICMP 測試
ping -c 4 8.8.8.8
ping6 -c 4 2001:4860:4860::8888

# traceroute - 路由追蹤
traceroute google.com
traceroute -n google.com          # 不解析主機名稱

# mtr - 持續追蹤
mtr google.com

埠掃描

# nc (netcat)
nc -zv 192.168.1.100 22           # 測試單一埠
nc -zv 192.168.1.100 20-25        # 掃描埠範圍

# telnet
telnet 192.168.1.100 80

# nmap (需安裝)
nmap 192.168.1.100
nmap -p 1-1000 192.168.1.100

頻寬測試

# iperf3 (需在兩端安裝)
# 伺服器端
iperf3 -s

# 客戶端
iperf3 -c server_ip
iperf3 -c server_ip -t 30         # 測試 30 秒

設定檔案

Network Configuration Files

Debian/Ubuntu (舊版)

# /etc/network/interfaces
auto eth0
iface eth0 inet static
    address 192.168.1.100
    netmask 255.255.255.0
    gateway 192.168.1.1
    dns-nameservers 8.8.8.8 8.8.4.4

Debian/Ubuntu (netplan)

# /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]

RHEL/CentOS

# /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
BOOTPROTO=static
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
ONBOOT=yes