nf_conntrack: table full, dropping packet 鏈接跟蹤表已滿,開始丟包 的解決辦法bash
中午業務說機器不能登陸,我經過USM管理界面登陸單板的時候發現機器沒有僵死,而後一看日誌,g一會兒就明白了服務器
tail -2000 /var/log/messagescookie
Apr 10 12:48:35 bj-push-pushserver83 kernel: [95129.138804] __ratelimit: 16523 callbacks suppressed (「鏈接跟蹤表已滿,開始丟包」!相信很多用iptables的同窗都會見過這個錯誤信息吧)ssh
Apr 10 12:48:35 bj-xx kernel: [95129.138806] nf_conntrack: table full, dropping packet.tcp
Apr 10 12:48:35 bj-xx kernel: [95129.138974] nf_conntrack: table full, dropping packet.ide
Apr 10 12:48:35 bj-xx kernel: [95129.139142] nf_conntrack: table full, dropping packet.性能
Apr 10 12:48:35 bj-xx kernel: [95129.139566] nf_conntrack: table full, dropping packet.優化
Apr 10 12:48:35 bj-xx kernel: [95129.139747] nf_conntrack: table full, dropping packet.spa
Apr 10 12:48:35 bj-xx kernel: [95129.139823] nf_conntrack: table full, dropping packet.操作系統
Apr 10 12:48:35 bj-xx kernel: [95129.140188] nf_conntrack: table full, dropping packet.
Apr 10 12:48:35 bj-xx kernel: [95129.140435] nf_conntrack: table full, dropping packet.
Apr 10 12:48:35 bj-xx kernel: [95129.140508] nf_conntrack: table full, dropping packet.
Apr 10 12:48:35 bj-xx kernel: [95129.141133] nf_conntrack: table full, dropping packet.
Apr 10 12:48:38 bj-xx kernel: [95131.483097] possible SYN flooding on port 443. Sending cookies.
Apr 10 12:49:01 bj-xx /usr/sbin/cron[9492]: (root) CMD (/usr/bin/tsar --cron > /dev/null 2>&1)
Apr 10 12:49:38 bj-xx kernel: [95191.382486] possible SYN flooding on port 443. Sending cookies.
Apr 10 12:50:01 bj-xx /usr/sbin/cron[9761]: (root) CMD (/opt/huawei/logs/LoadRst/suseRst.sh 2>/dev/null)
Apr 10 12:50:01 bj-xx /usr/sbin/cron[9762]: (root) CMD (/usr/bin/tsar --cron > /dev/null 2>&1)
Apr 10 12:50:38 bj-xx kernel: [95251.283552] possible SYN flooding on port 443. Sending cookies.
Apr 10 12:51:01 bj-xx /usr/sbin/cron[9990]: (root) CMD (/usr/bin/tsar --cron > /dev/null 2>&1)
Apr 10 12:51:38 bj-xx kernel: [95311.185024] possible SYN flooding on port 443. Sending cookies.
Apr 10 12:52:01 bj-xx /usr/sbin/cron[10232]: (root) CMD (/usr/bin/tsar --cron > /dev/null 2>&1)
Apr 10 12:52:38 bj-xx kernel: [95371.082714] possible SYN flooding on port 443. Sending cookies.
Apr 10 12:52:59 bj-xx sshd[9994]: pam_unix2(sshd:auth): conversation failed
Apr 10 12:52:59 bj-xx sshd[9994]: error: ssh_msg_send: write
Apr 10 12:53:01 bj-xx /usr/sbin/cron[10891]: (root) CMD (/usr/bin/tsar --cron > /dev/null 2>&1)
Apr 10 12:53:38 bj-xx kernel: [95430.983871] possible SYN flooding on port 443. Sending cookies.
Apr 10 12:54:01 bj-xx /usr/sbin/cron[11097]: (root) CMD (/usr/bin/tsar --cron > /dev/null 2>&1)
Apr 10 12:54:04 bj-xx sshd[11094]: pam_tally(sshd:account): unknown option: reset
Apr 10 12:54:04 bj-xx sshd[11094]: Accepted publickey for root from 183.62.156.75 port 16959 ssh2
Apr 10 12:54:38 bj-xx kernel: [95490.883402] possible SYN flooding on port 443. Send
都是腳本和任務計劃惹的禍
腳本內容
cat /opt/xx/logs/LoadRst/suseRst.sh
#!/bin/bash
cd `dirname $0`
loadnum=`uptime|awk -F':' '{print $4}'|awk -F',' '{print $1*1000}' `
fileDate=`date +"%Y%m%d_%H:%M:%S"`
#echo $fileDate
#echo $loadnum
#loadnum_ora=`uptime|awk -F':' '{print $4}'|awk -F',' '{print $2}' `
softirq=`top -bn 1|awk '/ksoftirqd/ {print $9}'|head -1`
echo -e $fileDate >>log
echo $softirq >>log
if [ $loadnum -ge "900" ]
then
#echo "asdfasdf"
echo -e $fileDate >>log
/sbin/rcSuSEfirewall2 restart >> log 2>&1
#else
#echo -e "${fileDate}:success" >>log
fi
任務計劃
crontab -l
# DO NOT EDIT THIS FILE - edit the master and reinstall.
# (/tmp/crontab.XXXXWNPsHE installed on Wed Apr 9 20:10:57 2014)
# (Cron version V5.0 -- $Id: crontab.c,v 1.12 2004/01/23 18:56:42 vixie Exp $)
*/5 * * * * /opt/xx/logs/LoadRst/suseRst.sh 2>/dev/null
0 0 * * * /opt/xx/logs/Firewall_log/tar-firewall.sh >/dev/null 2>&1
解決辦法
1、關閉防火牆。 簡單粗暴,直接有效
#/etc/init.d/SuSEfirewall2_init stop
#/etc/init.d/SuSEfirewall2_setup stop
切記:在防火牆關閉狀態下,不要經過iptables指令(好比 iptables -nL)來查看當前狀態!由於這樣會致使防火牆被啓動,並且規則爲空。雖然不會有任何攔截效果,但全部鏈接狀態都會被記錄,浪費資源且影響性能並可能致使防火牆主動丟包!
2、加大防火牆跟蹤表的大小,優化對應的系統參數
一、狀態跟蹤表的最大行數的設定,理論最大值
CONNTRACK_MAX = RAMSIZE (in bytes) / 16384 / (ARCH / 32)
以64G的64位操做系統爲例
CONNTRACK_MAX = 64*1024*1024*1024/16384/2 = 2097152
即時生效請執行:
sysctl –w net.netfilter.nf_conntrack_max = 2100000
或者
vi /etc/sysctl.conf
net.netfilter.nf_conntrack_max = 2100000
sysctl -p
二、其哈希表大小一般爲總表的1/8,最大爲1/2。
CONNTRACK_BUCKETS = CONNTRACK_MAX / 8
一樣64G的64位操做系統,哈希最佳範圍是 262144 ~ 1048576 。
運行狀態中查看
sysctl net.netfilter.nf_conntrack_buckets
經過文件 /sys/module/nf_conntrack/parameters/hashsize 進行設置。
或者新建 /etc/modprobe.d/iptables.conf,從新加載模塊才生效:
options nf_conntrack hashsize=262144
三、還有些相關的系統參數`sysctl -a | grep nf_conntrack`能夠調優(/etc/sysctl.conf ):
net.netfilter.nf_conntrack_max = 1048576
net.netfilter.ip_conntrack_tcp_timeout_established = 3600
net.netfilter.nf_conntrack_tcp_timeout_close_wait = 60
net.netfilter.nf_conntrack_tcp_timeout_fin_wait = 120
net.netfilter.nf_conntrack_tcp_timeout_time_wait = 120
3、使用祼表,添加「不跟蹤」標識。以下示例更適合桌面系統或隨意性強的服務器。由於它開啓了鏈接的狀態機制,方便和外部通訊。修改 /etc/sysconfig/iptables 文件:
*raw
# 對TCP鏈接不啓用追蹤,解決ip_contrack滿致使沒法鏈接的問題
-A PREROUTING -p tcp -m tcp --dport 80 -j NOTRACK
-A PREROUTING -p tcp -m tcp --dport 22 -j NOTRACK
-A PREROUTING -p tcp -m tcp --dport 21 -j NOTRACK
-A PREROUTING -p tcp -m tcp --dport 11211 -j NOTRACK
-A PREROUTING -p tcp -m tcp --dport 60000:60100 -j NOTRACK
-A PREROUTING -p tcp -s 192.168.10.1 -j NOTRACK
-A OUTPUT -p tcp -m tcp --sport 80 -j NOTRACK
-A OUTPUT -p tcp -m tcp --sport 22 -j NOTRACK
-A OUTPUT -p tcp -m tcp --sport 21 -j NOTRACK
-A OUTPUT -p tcp -m tcp --sport 11211 -j NOTRACK
-A OUTPUT -p tcp -m tcp --sport 60000:60100 -j NOTRACK
-A OUTPUT -p tcp -s 192.168.10.1 -j NOTRACK
COMMIT
*filter
# 容許ping
-A INPUT -p icmp -j ACCEPT
# 對本地迴路、第5張網卡放行
-A INPUT -i lo -j ACCEPT
-A INPUT -i eth4 -j ACCEPT
# 鏈接狀態跟蹤,已創建的鏈接容許傳輸數據
-A INPUT -m state --state ESTABLISHED,RELATED,INVALID,UNTRACKED -j ACCEPT
# filter表裏存在但在raw裏不存在的,默認會進行鏈接狀態跟蹤
-A INPUT -s 192.168.10.31 -p tcp --dport 2669 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT
或者乾脆對全部鏈接都關閉跟蹤,不跟蹤任何鏈接狀態。不過規則就限制比較嚴謹,進出都須要顯式申明。示例以下:
*raw
# 對TCP/UDP鏈接不啓用追蹤,解決nf_contrack滿致使沒法鏈接的問題
-A PREROUTING -p tcp -j NOTRACK
-A PREROUTING -p udp -j NOTRACK
-A OUTPUT -p tcp -j NOTRACK
-A OUTPUT -p udp -j NOTRACK
COMMIT
*filter
# 容許ping
-A INPUT -p icmp -j ACCEPT
# 對本地迴路和eth1放行
-A INPUT -i lo -j ACCEPT
-A INPUT -i eth1 -j ACCEPT
# 只容許符合條件的鏈接進行傳輸數據
-A INPUT -p tcp --dport 22 -j ACCEPT
-A INPUT -p tcp --sport 80 -j ACCEPT
-A INPUT -p udp --sport 53 -j ACCEPT
-A INPUT -p udp --sport 123 -j ACCEPT
# 出去的包都不限制
-A OUTPUT -p tcp -j ACCEPT
-A OUTPUT -p udp -j ACCEPT
# 輸入和轉發的包不符合規則的全攔截
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT
效果以下圖:
4、刪除鏈接跟蹤模塊`lsmod | grep nf_conntrack`,不使用鏈接狀態的跟蹤功能。
一、刪除nf_conntrack和相關的依賴模塊,示例:
rmmod nf_conntrack_ipv4
rmmod nf_conntrack_ipv6
rmmod xt_state
rmmod xt_CT
rmmod xt_conntrack
rmmod iptable_nat
rmmod ipt_REDIRECT
rmmod nf_nat
rmmod nf_conntrack
二、禁用跟蹤模塊,把它加到黑名單(/etc/modprobe.d/blacklist.conf ):
# 禁用 nf_conntrack 模塊
blacklist nf_conntrack
blacklist nf_conntrack_ipv6
blacklist xt_conntrack
blacklist nf_conntrack_ftp
blacklist xt_state
blacklist iptable_nat
blacklist ipt_REDIRECT
blacklist nf_nat
blacklist nf_conntrack_ipv4
三、去掉防火牆裏全部和狀態相關的配置(好比state狀態,NAT功能),示例:
*filter
# 容許ping
-A INPUT -p icmp -j ACCEPT
# 對本地迴路和第2張網卡放行
-A INPUT -i lo -j ACCEPT
-A INPUT -i eth1 -j ACCEPT
# 對端口放行
-A INPUT -p tcp --dport 1331 -j ACCEPT
# 對IP放行
-A INPUT -s 192.168.10.31 -j ACCEPT
#容許本機進行DNS查詢
-A INPUT -p udp --sport 53 -j ACCEPT
-A OUTPUT -p udp -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT
另外,防火牆的配置文件最好也改下,不要加載任何額外模塊(/etc/sysconfig/iptables-config):
IPTABLES_MODULES="" # 不須要任何附加模塊
IPTABLES_MODULES_UNLOAD="no" # 避免iptables重啓後sysctl中對應的參數被重置爲系統默認值
IPTABLES_SAVE_ON_STOP="no"
IPTABLES_SAVE_ON_RESTART="no"
IPTABLES_SAVE_COUNTER="no"
IPTABLES_STATUS_NUMERIC="yes"
IPTABLES_STATUS_VERBOSE="no"
IPTABLES_STATUS_LINENUMBERS="no"
每每咱們對鏈接的跟蹤都是基於操做系統的(netstat / ss ),防火牆的鏈接狀態徹底是它自身實現產生的。