| 层次 | 名称 | 功能 | 协议示例 |
|---|
| 7 | 应用层 | 提供应用程序网络接口 | HTTP, FTP, SSH |
| 6 | 表示层 | 数据加密、解密、压缩 | SSL/TLS |
| 5 | 会话层 | 建立、管理和终止会话 | RPC, NetBIOS |
| 4 | 传输层 | 端到端通信,流量控制 | TCP, UDP |
| 3 | 网络层 | 路由选择,分组转发 | IP, ICMP, ARP |
| 2 | 数据链路层 | 介质访问控制,帧传输 | Ethernet, PPP |
| 1 | 物理层 | 物理介质,比特传输 | 网线、光纤 |
| 层次 | 对应OSI层次 | 协议 |
|---|
| 应用层 | 5-7层 | HTTP, FTP, SSH, DNS |
| 传输层 | 4层 | TCP, UDP |
| 网络层 | 3层 | IP, ICMP, ARP |
| 网络接口层 | 1-2层 | Ethernet, PPP |
# 查看网络接口信息
ip addr
ifconfig # 旧命令,需要net-tools
# 查看路由表
ip route
route -n
# 查看ARP缓存
arp -a
ip neigh
# 临时配置IP地址
ip addr add 192.168.1.100/24 dev eth0
# 启用/禁用网络接口
ip link set eth0 up
ip link set eth0 down
# 配置默认网关
ip route add default via 192.168.1.1
# /etc/netplan/00-installer-config.yaml
network:
ethernets:
eth0:
addresses:
- 192.168.1.100/24
gateway4: 192.168.1.1
nameservers:
addresses: [8.8.8.8, 8.8.4.4]
version: 2
# 配置静态IP
nmcli con mod eth0 ipv4.addresses 192.168.1.100/24
nmcli con mod eth0 ipv4.gateway 192.168.1.1
nmcli con mod eth0 ipv4.dns 8.8.8.8
nmcli con mod eth0 ipv4.method manual
# 启用连接
nmcli con up eth0
# 临时配置DNS
echo "nameserver 8.8.8.8" > /etc/resolv.conf
# 永久配置DNS(Debian/Ubuntu)
echo "nameserver 8.8.8.8" >> /etc/resolvconf/resolv.conf.d/base
resolvconf -u
# 永久配置DNS(RHEL/CentOS)
echo "DNS1=8.8.8.8" >> /etc/sysconfig/network-scripts/ifcfg-eth0
# 安装SSH服务器
apt install openssh-server # Debian/Ubuntu
yum install openssh-server # RHEL/CentOS
# 启动SSH服务
systemctl start sshd
systemctl enable sshd
# 配置SSH
vim /etc/ssh/sshd_config
# 禁用root登录
PermitRootLogin no
# 禁用密码登录(使用密钥登录)
PasswordAuthentication no
# 限制登录用户
AllowUsers user1 user2
# 修改默认端口
Port 2222
# 生成密钥对
ssh-keygen -t ed25519
# 复制公钥到服务器
ssh-copy-id user@server
# 安装Apache
apt install apache2 # Debian/Ubuntu
yum install httpd # RHEL/CentOS
# 启动服务
systemctl start apache2
systemctl enable apache2
# 配置文件位置
/etc/apache2/apache2.conf # Debian/Ubuntu
/etc/httpd/conf/httpd.conf # RHEL/CentOS
# 安装Nginx
apt install nginx # Debian/Ubuntu
yum install nginx # RHEL/CentOS
# 启动服务
systemctl start nginx
systemctl enable nginx
# 配置文件位置
/etc/nginx/nginx.conf
# 安装vsftpd
apt install vsftpd # Debian/Ubuntu
yum install vsftpd # RHEL/CentOS
# 启动服务
systemctl start vsftpd
systemctl enable vsftpd
# 配置文件位置
/etc/vsftpd.conf
# 安装BIND
apt install bind9 # Debian/Ubuntu
yum install bind # RHEL/CentOS
# 启动服务
systemctl start bind9
systemctl enable bind9
# 配置文件位置
/etc/bind/named.conf
# 查看规则
iptables -L -n -v
# 允许SSH连接
iptables -A INPUT -p tcp --dport 22 -j ACCEPT
# 允许HTTP连接
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
# 允许HTTPS连接
iptables -A INPUT -p tcp --dport 443 -j ACCEPT
# 允许回环接口
iptables -A INPUT -i lo -j ACCEPT
# 允许已建立的连接
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
# 拒绝其他所有连接
iptables -P INPUT DROP
# 查看状态
firewall-cmd --state
# 允许服务
firewall-cmd --add-service=http --permanent
firewall-cmd --add-service=https --permanent
firewall-cmd --add-service=ssh --permanent
# 重新加载配置
firewall-cmd --reload
# 查看规则
firewall-cmd --list-all
# 启用ufw
ufw enable
# 允许SSH
ufw allow ssh
# 允许HTTP
ufw allow http
# 允许HTTPS
ufw allow https
# 查看状态
ufw status verbose
# ping测试
ping -c 4 google.com
# traceroute测试
traceroute google.com
tracepath google.com # 不需要root权限
# mtr综合测试
mtr google.com
# 查看监听端口
ss -tuln
netstat -tuln # 旧命令
# 测试端口连通性
nc -zv google.com 80
# 端口扫描(需要nmap)
nmap -p 1-1000 192.168.1.0/24
# tcpdump抓包
tcpdump -i eth0 port 80 -w capture.pcap
# wireshark分析(图形界面)
wireshark capture.pcap
# tshark分析(命令行)
tshark -r capture.pcap
# 安装Nginx
apt install nginx
# 安装MySQL
apt install mysql-server
# 安装PHP
apt install php-fpm php-mysql
# 配置Nginx支持PHP
vim /etc/nginx/sites-available/default
# 安装WireGuard
apt install wireguard
# 生成密钥对
wg genkey | tee privatekey | wg pubkey > publickey
# 配置WireGuard
vim /etc/wireguard/wg0.conf
# 启动服务
wg-quick up wg0
systemctl enable wg-quick@wg0
# 检查物理连接
ethtool eth0
# 检查IP配置
ip addr
# 检查路由
ip route
# 检查DNS
nslookup google.com
# 检查服务是否启动
systemctl status nginx
# 检查端口是否监听
ss -tuln | grep 80
# 检查防火墙规则
iptables -L -n | grep 80
# 测试网络速度
curl -s https://raw.githubusercontent.com/sivel/speedtest-cli/master/speedtest.py | python3 -
# 查看网络流量
tcpdump -i eth0
iftop
- 最小权限原则:只开放必要的端口和服务
- 定期更新:及时安装安全补丁
- 日志审计:定期检查网络日志
- 加密通信:使用SSH、HTTPS等加密协议
- TCP参数优化:调整TCP连接参数
- 负载均衡:使用Nginx或HAProxy实现负载均衡
- CDN加速:使用CDN加速静态资源访问
- 缓存策略:合理配置缓存减少重复请求