Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學
您现在的位置: Windows教程網 >> Windows教程 >  >> 電腦軟體教學 >> 伺服器技術 >> 關於伺服器

Linux VPS 安全加固六大核心實戰:SSH 埠修改、禁用 Root、DDoS Deflate、iftop、Nginx 升級、netstat 監控腳本

編輯:關於伺服器

VPS 直面公網,基礎加固決定生存週期。六大模組構建最小可行性安全基線。

一、修改 SSH 埠 (非 22)

vi /etc/ssh/sshd_config
# Port 22 → Port 3333 (取消註釋並修改)
systemctl restart sshd
# 後續連接:ssh -p 3333 user@ip

二、禁用 Root 直接登入

# 建立普通用戶
useradd opsuser
passwd opsuser
# 修改 sshd_config
PermitRootLogin no
systemctl restart sshd
# 登入流程:ssh -p 3333 opsuser@ip → su - root

三、DDoS Deflate 自動封鎖異常連接

安裝

wget https://www.inetbase.com/scripts/ddos/install.sh
chmod +x install.sh
./install.sh

核心配置 /usr/local/ddos/ddos.conf

APF_BAN=0          # 使用 iptables 而非 APF
KILL=1             # 實際封鎖
EMAIL_TO="admin@example.com"  # 告警郵箱
NO_OF_CONNECTIONS=150  # 單 IP 連接閾值
FREQ=1             # 檢查間隔 (分鐘)
BAN_PERIOD=600     # 封鎖時長 (秒)
IGNORE_IP_LIST="/usr/local/ddos/ignore.ip.list"  # 白名單

四、iftop 即時流量分析

yum -y install flex byacc libpcap ncurses ncurses-devel libpcap-devel
wget https://www.ex-parrot.com/pdw/iftop/download/iftop-0.17.tar.gz
tar xzf iftop-0.17.tar.gz && cd iftop-0.17
./configure && make && make install
# 執行:iftop -i eth0
# 快捷鍵:h(幫助) n(IP/主機名) s(本機) d(遠端) N(埠/服務) b(流量條)

五、升級 Nginx 至最新穩定版 (以 1.24.0 為例)

wget https://nginx.org/download/nginx-1.24.0.tar.gz
tar xzf nginx-1.24.0.tar.gz && cd nginx-1.24.0
./configure --user=www --group=www --prefix=/usr/local/nginx \
  --with-http_stub_status_module --with-http_ssl_module --with-http_sub_module \
  --with-http_v2_module --with-http_gzip_static_module
make
mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.old
cp objs/nginx /usr/local/nginx/sbin/
/usr/local/nginx/sbin/nginx -t
kill -USR2 $(cat /usr/local/nginx/logs/nginx.pid)
kill -QUIT $(cat /usr/local/nginx/logs/nginx.pid.oldbin)
/usr/local/nginx/sbin/nginx -v

六、netstat/ss 實戰監控腳本庫

# 1. 80 埠連接總數
netstat -nat | grep -c ':80 '

# 2. IP 連接數排序 (Top 20)
netstat -ntu | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -nr | head -20

# 3. TCP 狀態統計
netstat -nat | awk '{print $6}' | sort | uniq -c | sort -nr

# 4. 80 埠 Top 20 IP (含連接數)
netstat -anlp | grep ':80 ' | grep tcp | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -nr | head -20

# 5. tcpdump 抓包分析 80 埠 Top 20
tcpdump -i eth0 -tnn dst port 80 -c 1000 | awk -F. '{print $1"."$2"."$3"."$4}' | sort | uniq -c | sort -nr | head -20

# 6. TIME_WAIT 過多排查
netstat -n | grep TIME_WAIT | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -nr | head -20

# 7. SYN Flood 排查
netstat -an | grep SYN_RECV | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -nr | head -20

持久化建議

  • 將上述腳本封裝為 /usr/local/bin/vps_monitor.sh,crontab -e 定時執行並日誌
  • 配合 logrotate 管理日誌體積
  • 關鍵指標接入 Prometheus+Grafana 或 Zabbix 告警
Copyright © Windows教程網 All Rights Reserved