利用sysctl工具調整linux核心參數
命令格式:
sysctl [-n] [-e] -w variable=value
sysctl [-n] [-e] -p (default /etc/sysctl.conf)
sysctl [-n] [-e] -a
常用參數的意義:
-w 臨時改變某個指定參數的值,如
sysctl -w net.ipv4.ip_forward=1
-a 顯示所有的系統參數
-p 從指定的文件加載系統參數,如不指定即從/etc/sysctl.conf中加載
如果僅僅是想臨時改變某個系統參數的值,可以用兩種方法來實現,例如想啓用IP路由轉發功能:
1) #echo 1 > /proc/sys/net/ipv4/ip_forward
2) #sysctl -w net.ipv4.ip_forward=1
以上兩種方法都可能立即開啓路由功能,但如果系統重啓,或執行了
# service network restart
命令,所設置的值即會丟失,如果想永久保留配置,可以修改/etc/sysctl.conf文件
將 net.ipv4.ip_forward=0改爲net.ipv4.ip_forward=1
and replace the contents of the file with the following:
# Kernel sysctl configuration file for Red Hat Linux
#
# For binary values, 0 is disabled, 1 is enabled. See sysctl(8) and
# sysctl.conf(5) for more details.
# Disables packet forwarding
net.ipv4.ip_forward=0
# Disables IP source routing
net.ipv4.conf.all.accept_source_route = 0
net.ipv4.conf.lo.accept_source_route = 0
net.ipv4.conf.eth0.accept_source_route = 0
net.ipv4.conf.default.accept_source_route = 0
# Enable IP spoofing protection, turn on source route verification
net.ipv4.conf.all.rp_filter = 1
net.ipv4.conf.lo.rp_filter = 1
net.ipv4.conf.eth0.rp_filter = 1
net.ipv4.conf.default.rp_filter = 1
# Disable ICMP Redirect Acceptance
net.ipv4.conf.all.accept_redirects = 0
net.ipv4.conf.lo.accept_redirects = 0
net.ipv4.conf.eth0.accept_redirects = 0
net.ipv4.conf.default.accept_redirects = 0
# Enable Log Spoofed Packets, Source Routed Packets, Redirect Packets
net.ipv4.conf.all.log_martians = 0
net.ipv4.conf.lo.log_martians = 0
net.ipv4.conf.eth0.log_martians = 0
# Disables the magic-sysrq key
kernel.sysrq = 0
# Decrease the time default value for tcp_fin_timeout connection
net.ipv4.tcp_fin_timeout = 15
# Decrease the time default value for tcp_keepalive_time connection
net.ipv4.tcp_keepalive_time = 1800
# Turn off the tcp_window_scaling
net.ipv4.tcp_window_scaling = 0
# Turn off the tcp_sack
net.ipv4.tcp_sack = 0
# Turn off the tcp_timestamps
net.ipv4.tcp_timestamps = 0
# Enable TCP SYN Cookie Protection
net.ipv4.tcp_syncookies = 1
# Enable ignoring broadcasts request
net.ipv4.icmp_echo_ignore_broadcasts = 1
# Enable bad error message Protection
net.ipv4.icmp_ignore_bogus_error_responses = 1
# Log Spoofed Packets, Source Routed Packets, Redirect Packets
net.ipv4.conf.all.log_martians = 1
# Increases the size of the socket queue (effectively, q0).
net.ipv4.tcp_max_syn_backlog = 1024
# Increase the tcp-time-wait buckets pool size
net.ipv4.tcp_max_tw_buckets = 1440000
# Allowed local port range
net.ipv4.ip_local_port_range = 16384 65536
CTRL + X to exit and save the file
To make your changes take effect immediately, type this command:
/sbin/sysctl -p
引用自:FreeBSD下構建安全的Web服務器
作者:heiyeluren http://www.unixsky.net
編輯 /etc/sysctl.conf 文件,在裏面加入如下內容:(有註釋)
#最大的待發送TCP數據緩衝區空間
net.inet.tcp.sendspace=65536
#最大的接受TCP緩衝區空間
net.inet.tcp.recvspace=65536
#最大的接受UDP緩衝區大小
net.inet.udp.sendspace=65535
#最大的發送UDP數據緩衝區大小
net.inet.udp.maxdgram=65535
#本地套接字連接的數據發送空間
net.local.stream.sendspace=65535
#加快網絡性能的協議
net.inet.tcp.rfc1323=1
net.inet.tcp.rfc1644=1
net.inet.tcp.rfc3042=1
net.inet.tcp.rfc3390=1
#最大的套接字緩衝區
kern.ipc.maxsockbuf=2097152
#系統中允許的最多文件數量
kern.maxfiles=65536
#每個進程能夠同時打開的最大文件數量
kern.maxfilesperproc=32768
#當一臺計算機發起TCP連接請求時,系統會迴應ACK應答數據包。該選項設置是否延遲ACK應答數據包,把它和包含數據的數據包一起發送,在高速網絡和低負載的情況下會略微提高性能,但在網絡連接較差的時候,對方計算機得不到應答會持續發起連接請求,反而會降低性能。
net.inet.tcp.delayed_ack=0
#屏蔽ICMP重定向功能
net.inet.icmp.drop_redirect=1
net.inet.icmp.log_redirect=1
net.inet.ip.redirect=0
net.inet6.ip6.redirect=0
#防止ICMP廣播風暴
net.inet.icmp.bmcastecho=0
net.inet.icmp.maskrepl=0
#限制系統發送ICMP速率
net.inet.icmp.icmplim=100
#安全參數,編譯內核的時候加了options TCP_DROP_SYNFIN纔可以用
net.inet.icmp.icmplim_output=0
net.inet.tcp.drop_synfin=1
#設置爲1會幫助系統清除沒有正常斷開的TCP連接,這增加了一些網絡帶寬的使用,但是一些死掉的連接最終能被識別並清除。死的TCP連接是被撥號用戶存取的系統的一個特別的問題,因爲用戶經常斷開modem而不正確的關閉活動的連接
net.inet.tcp.always_keepalive=1
#若看到net.inet.ip.intr_queue_drops這個在增加,就要調大net.inet.ip.intr_queue_maxlen,爲0最好
net.inet.ip.intr_queue_maxlen=1000
#防止DOS攻擊,默認爲30000
net.inet.tcp.msl=7500
#接收到一個已經關閉的端口發來的所有包,直接drop,如果設置爲1則是隻針對TCP包
net.inet.tcp.blackhole=2
#接收到一個已經關閉的端口發來的所有UDP包直接drop
net.inet.udp.blackhole=1
#爲網絡數據連接時提供緩衝
net.inet.tcp.inflight.enable=1
#如果打開的話每個目標地址一次轉發成功以後它的數據都將被記錄進路由表和arp數據表,節約路由的計算時間,但會需要大量的內核內存空間來保存路由表
net.inet.ip.fastforwarding=0
#kernel編譯打開options POLLING功能,高負載情況下使用低負載不推薦SMP不能和polling一起用
#kern.polling.enable=1
#併發連接數,默認爲128,推薦在1024-4096之間,數字越大佔用內存也越大
kern.ipc.somaxconn=32768
#禁止用戶查看其他用戶的進程
security.bsd.see_other_uids=0
#設置kernel安全級別
kern.securelevel=0
#記錄下任何TCP連接
net.inet.tcp.log_in_vain=1
#記錄下任何UDP連接
net.inet.udp.log_in_vain=1
#防止不正確的udp包的攻擊
net.inet.udp.checksum=1
#防止DOS攻擊
net.inet.tcp.syncookies=1
#僅爲線程提供物理內存支持,需要256兆以上內存
kern.ipc.shm_use_phys=1
# 線程可使用的最大共享內存
kern.ipc.shmmax=67108864
# 最大線程數量
kern.ipc.shmall=32768
# 程序崩潰時不記錄
kern.coredump=0
# lo本地數據流接收和發送空間
net.local.stream.recvspace=65536
net.local.dgram.maxdgram=16384
net.local.dgram.recvspace=65536
# 數據包數據段大小,ADSL爲1452。
net.inet.tcp.mssdflt=1460
# 爲網絡數據連接時提供緩衝
net.inet.tcp.inflight_enable=1
# 數據包數據段最小值,ADSL爲1452
net.inet.tcp.minmss=1460
# 本地數據最大數量
net.inet.raw.maxdgram=65536
# 本地數據流接收空間
net.inet.raw.recvspace=65536
#ipfw防火牆動態規則數量,默認爲4096,增大該值可以防止某些病毒發送大量TCP連接,導致不能建立正常連接
net.inet.ip.fw.dyn_max=65535
#設置ipf防火牆TCP連接空閒保留時間,默認8640000(120小時)
net.inet.ipf.fr_tcpidletimeout=864000