參考:
什么是 sysctl
sysctl
命令被用于在內(nèi)核運行時動態(tài)地修改內(nèi)核的運行參數(shù)闻妓,可用的內(nèi)核參數(shù)在目錄 /proc/sys
中由缆。它包含一些 Tcp/Ip堆棧和虛擬內(nèi)存系統(tǒng)的高級選項,可以通過修改某些值來提高系統(tǒng)性能浸卦。
sysctl 可以讀取和設(shè)置超過五百個系統(tǒng)變量限嫌。
sysctl 變量的設(shè)置通常是 字符串怒医、數(shù)字或布爾型(布爾型用 1 表示 yes稚叹,0 表示 no)扒袖。
語法:
sysctl [選項] [參數(shù)]
sysctl -w net.ipv4.tcp_syncookies=1
可以通過 sysctl 修改系統(tǒng)變量季率,也可以通過編輯 sysctl.conf 配置文件來修改系統(tǒng)變量飒泻。
vim /etc/sysctl.conf
舉個栗子泞遗,請根據(jù)實際情況調(diào)整:
# Controls source route verification
# Default should work for all interfaces net.ipv4.conf.default.rp_filter = 1
# net.ipv4.conf.all.rp_filter = 1
# net.ipv4.conf.lo.rp_filter = 1
# net.ipv4.conf.eth0.rp_filter = 1
# Disables IP source routing
# Default should work for all interfaces net.ipv4.conf.default.accept_source_route = 0
# net.ipv4.conf.all.accept_source_route = 0
# net.ipv4.conf.lo.accept_source_route = 0
# net.ipv4.conf.eth0.accept_source_route = 0
# Controls the System Request debugging functionality of the kernel
kernel.sysrq = 0
# Controls whether core dumps will append the PID to the core filename.
# Useful for debugging multi-threaded applications.
kernel.core_uses_pid = 1
# Increase maximum amount of memory allocated to shm
# Only uncomment if needed
# kernel.shmmax = 67108864
# Disable ICMP Redirect Acceptance
# Default should work for all interfaces
net.ipv4.conf.default.accept_redirects = 0
# net.ipv4.conf.all.accept_redirects = 0
# net.ipv4.conf.lo.accept_redirects = 0
# net.ipv4.conf.eth0.accept_redirects = 0
# enable Log Spoofed Packets, Source Routed Packets, Redirect Packets
# Default should work for all interfaces
net.ipv4.conf.default.log_martians = 1
#net.ipv4.conf.all.log_martians = 1
# net.ipv4.conf.lo.log_martians = 1
# net.ipv4.conf.eth0.log_martians = 1
# Decrease the time default value for tcp_fin_timeout connection
net.ipv4.tcp_fin_timeout = 25
# Decrease the time default value for tcp_keepalive_time connection
net.ipv4.tcp_keepalive_time = 1200
# Turn on the tcp_window_scaling
net.ipv4.tcp_window_scaling = 1
# Turn on the tcp_sack
net.ipv4.tcp_sack = 1
# tcp_fack should be on because of sack
net.ipv4.tcp_fack = 1
# Turn on the tcp_timestamps
net.ipv4.tcp_timestamps = 1
# Enable TCP SYN Cookie Protection
net.ipv4.tcp_syncookies = 1
# Enable ignoring broadcasts request
net.ipv4.icmp_echo_ignore_broadcasts = 1
# Disable ping requests
net.ipv4.icmp_echo_ignore_all = 1
# Enable bad error message Protection
net.ipv4.icmp_ignore_bogus_error_responses = 1
# make more local ports available
# net.ipv4.ip_local_port_range = 1024 65000
# set TCP Re-Ordering value in kernel to ‘5′
net.ipv4.tcp_reordering = 5
# Lower syn retry rates
net.ipv4.tcp_synack_retries = 2
net.ipv4.tcp_syn_retries = 3
# Set Max SYN Backlog to ‘2048′
net.ipv4.tcp_max_syn_backlog = 2048
# Various Settings
net.core.netdev_max_backlog = 1024
# Increase the maximum number of skb-heads to be cached
net.core.hot_list_length = 256
# Increase the tcp-time-wait buckets pool size
net.ipv4.tcp_max_tw_buckets = 360000
# This will increase the amount of memory available for socket input/output queues
net.core.rmem_default = 65535
net.core.rmem_max = 8388608
net.ipv4.tcp_rmem = 4096 87380 8388608 net.core.wmem_default = 65535
net.core.wmem_max = 8388608
net.ipv4.tcp_wmem = 4096 65535 8388608
net.ipv4.tcp_mem = 8388608 8388608 8388608
net.core.optmem_max = 40960
重新加載內(nèi)核參數(shù):
sysctl -p
什么是 ulimit
大多Unix-Like系統(tǒng)髓霞,都提供了限制每個進程和每個基本用戶使用線程方库,文件和網(wǎng)絡連接等系統(tǒng)資源的一些方法纵潦。
ulimit
是指每個user使用各種資源的限制值邀层。ulimit
命令用來限制系統(tǒng)用戶對shell資源的訪問寥院,它是一種簡單并且有效的實現(xiàn)資源限制的方式。
-
ulimit
的設(shè)置值是 per-process的估蹄,也就是說臭蚁,每個進程都有自己的limits值垮兑; - 使用
ulimit
進行修改,是立即生效的嗤无; -
ulimit
只影響shell進程及其子進程垢村,用戶登出后失效嘉栓; - 修改
ulimit
設(shè)置之后,要重啟程序修改值才會有效馋辈÷趺可通過/proc
文件系統(tǒng)查看運行進程當前的限制值; - 使用
ulimit
對系統(tǒng)限制的改變在系統(tǒng)重啟后都會恢復到默認值; - 可以在 profile 中加入 ulimit 的設(shè)置,便想做到永久生效洗搂。
假設(shè)有這樣一種情況,當一臺Linux主機上同時登陸了10人驼鞭,在資源無限制的情況下,這10個用戶同時打開了500個文件洛心。假設(shè)每個文件的大小有10M,這是系統(tǒng)的內(nèi)存資源就會收到巨大挑戰(zhàn)法严。
但是任何一臺主機的資源都不可能是無限的拗馒。所以诱桂,資源的合理配置和分配,不僅僅是保證系統(tǒng)可用性的必要條件肝劲,也與系統(tǒng)上軟件運行的性能有著密不可分的聯(lián)系。
ulimit 用于限制 shell 啟動進程所占用的資源切威,支持以下各種類型的限制:
- 所創(chuàng)建的內(nèi)核文件的大戌衷;
- 進程數(shù)據(jù)塊的大忻灾!;
- Shell進程創(chuàng)建文件的大邪悦谩鹃骂;
- 內(nèi)存鎖住的大小;
- 常駐內(nèi)存集的大斜亍史隆;
- 打開文件描述符的數(shù)量鬓照;
- 分配堆棧的最大大胁蝰伞;
- CPU時間;
- 單個用戶的最大線程數(shù);
- Shell進程所能使用的最大虛擬內(nèi)存;
- 它支持硬資源(hard)和軟資源(soft)的限制。
sort和hard限制:
- hard:是指用戶在任何時候都可以活動的進程的最大數(shù)量紊馏,這是上限飞蚓。沒有任何non-root進程能夠增加hard ulimit俭驮;
- soft:是對會話或進程實際執(zhí)行的限制,但任何進程都可以將其增加到hard ulimit的最大值。
#顯示目前資源限定
ulimit -a
設(shè)置ulimit
可以在一下位置進行ulimit的設(shè)置:
-
/etc/profile
问芬, 所有用戶有效悦析,永久生效; -
~/.bash_profile
當前用戶有效此衅,永久生效她按; - 直接在控制臺輸入,當前用戶有效炕柔,臨時生效;
關(guān)于 limit.conf
limits.conf
是 pam_limits.so
的配置文件媒佣,Linux PAM(Pluggable Authentication Modules匕累,插入式認證模塊)。突破系統(tǒng)默認限制默伍,對系統(tǒng)資源有一定保護作用欢嘿。
pam_limits模塊對用戶的會話進行資源限制,然后 /etc/pam.d/
下的應用程序調(diào)用 pam_***.so
模塊也糊。
limits.conf 是針對用戶炼蹦,而 sysctl.conf 是針對整個系統(tǒng)參數(shù)配置。
一個shell的初始 limits 就是由 pam_limits 設(shè)定的狸剃,用戶登錄后掐隐,pam_limits會給用戶的shell設(shè)定在limits.conf定義的值.
pam_limits的設(shè)定值也是 per-process的;pam_limits的設(shè)置是 永久生效的.
配置limits.conf:
vim /etc/security/limits.conf
舉個栗子:
#<domain> <type> <item> <value>
#* soft core 0
#* hard rss 10000
#@student hard nproc 20
#@faculty soft nproc 20
#@faculty hard nproc 50
#ftp hard nproc 0
#@student - maxlogins 4
domain:
username | @groupname;
type:
soft虑省、hard匿刮、-;
item:
core探颈,限制內(nèi)核文件的大惺焱琛;
date伪节,最大數(shù)據(jù)大泄庑摺;
fsize怀大,最大文件大猩炊摇;
memlock叉寂,最大鎖定內(nèi)存地址空間萍启;
nofile,打開文件的最大數(shù)目屏鳍;
rss勘纯,最大持久設(shè)置大小钓瞭;
stack驳遵,最大棧大小山涡;
cpu堤结,以分鐘為單位的最多CPU時間;
nproc鸭丛,進程的最大數(shù)目竞穷;
as,地址空間限制鳞溉;
maxlogins瘾带,此用戶允許登錄的最大數(shù)目;
value:
item值的大惺旆啤看政;
/proc文件系統(tǒng)
什么是/proc文件系統(tǒng)
Linux內(nèi)核提供了一種通過 /proc
文件系統(tǒng),在運行時訪問內(nèi)核內(nèi)部數(shù)據(jù)結(jié)構(gòu)抄罕,改變內(nèi)核設(shè)置的機制允蚣。
proc 文件系統(tǒng)是一個偽文件系統(tǒng),它只存在內(nèi)存當中呆贿,不占用外部空間嚷兔。它以文件系統(tǒng)的方式為訪問系統(tǒng)內(nèi)核數(shù)據(jù)的操作提供接口。
對 /proc
中內(nèi)核文件的修改,針對的是 整個系統(tǒng) 的 內(nèi)核參數(shù)谴垫,修改后 立即生效章母,但修改是 臨時的,重啟后失效翩剪。
/proc文件系統(tǒng)與sysctl.conf的對應關(guān)系
修改 /proc
文件系統(tǒng)中的參數(shù)是臨時的乳怎,但修改 sysctl.conf
的參數(shù)確實永久有效的。
配置文件 sysctl.conf
變量在 /proc/sys
下前弯,其對應關(guān)系如下:
將文件名的 . 變?yōu)? /
/proc/sys/net/ipv4/icmp_echo_ignore_all ---- net.ipv4.icmp_echo_ignore_all
/proc文件系統(tǒng)幾個常用的內(nèi)核文件
- /proc/meminfo 內(nèi)存信息蚪缀;
- /proc/cpuinfo CPU信息;
- /proc/sys/fs/file-max 文件打開數(shù)恕出;
- /proc/sys/fs/file-nr 整個系統(tǒng)目前使用的文件句柄數(shù)量询枚;
/proc文件系統(tǒng)中文件的權(quán)限
proc中的每個文件都有一組分配給它的非常特殊的文件許可權(quán),并且每個文件屬于特定的用戶標識浙巫。
- 只讀:任何用戶都不能更改該文件金蜀,它用于表示系統(tǒng)信息;
- root寫的畴;
- root讀 渊抄;
對/proc進行讀寫
cat /proc/sys/net/ipv4/icmp_echo_ignore_all
0
echo "1" > /proc/sys/net/ipv4/icmp_echo_ignore_all
#當然,也可是用sysctl來配置
/proc內(nèi)核文件詳解
/proc/buddyinfo 每個內(nèi)存區(qū)中的每個order有多少塊可用,和內(nèi)存碎片問題有關(guān)
/proc/cmdline 啟動時傳遞給kernel的參數(shù)信息
/proc/cpuinfo cpu的信息
/proc/crypto 內(nèi)核使用的所有已安裝的加密密碼及細節(jié)
/proc/devices 已經(jīng)加載的設(shè)備并分類
/proc/dma 已注冊使用的ISA DMA頻道列表
/proc/execdomains Linux內(nèi)核當前支持的execution domains
/proc/fb 幀緩沖設(shè)備列表丧裁,包括數(shù)量和控制它的驅(qū)動
/proc/filesystems 內(nèi)核當前支持的文件系統(tǒng)類型
/proc/interrupts x86架構(gòu)中的每個IRQ中斷數(shù)
/proc/iomem 每個物理設(shè)備當前在系統(tǒng)內(nèi)存中的映射
/proc/ioports 一個設(shè)備的輸入輸出所使用的注冊端口范圍
/proc/kcore 代表系統(tǒng)的物理內(nèi)存护桦,存儲為核心文件格式,里邊顯示的是字節(jié)數(shù)煎娇,等于RAM大小加上4kb
/proc/kmsg 記錄內(nèi)核生成的信息二庵,可以通過/sbin/klogd或/bin/dmesg來處理
/proc/loadavg 根據(jù)過去一段時間內(nèi)CPU和IO的狀態(tài)得出的負載狀態(tài),與uptime命令有關(guān)
/proc/locks 內(nèi)核鎖住的文件列表
/proc/mdstat 多硬盤缓呛,RAID配置信息(md=multiple disks)
/proc/meminfo RAM使用的相關(guān)信息
/proc/misc 其他的主要設(shè)備(設(shè)備號為10)上注冊的驅(qū)動
/proc/modules 所有加載到內(nèi)核的模塊列表
/proc/mounts 系統(tǒng)中使用的所有掛載
/proc/mtrr 系統(tǒng)使用的Memory Type Range Registers (MTRRs)
/proc/partitions 分區(qū)中的塊分配信息
/proc/pci 系統(tǒng)中的PCI設(shè)備列表
/proc/slabinfo 系統(tǒng)中所有活動的 slab 緩存信息
/proc/stat 所有的CPU活動信息
/proc/sysrq-trigger 使用echo命令來寫這個文件的時候催享,遠程root用戶可以執(zhí)行大多數(shù)的系統(tǒng)請求關(guān)鍵命令,就好- 像在本地終端執(zhí)行一樣哟绊。要寫入這個文件睡陪,需要把/proc/sys/kernel/sysrq不能設(shè)置為0。這個文件對root也是不可- 讀的
/proc/uptime 系統(tǒng)已經(jīng)運行了多久
/proc/swaps 交換空間的使用情況
/proc/version Linux內(nèi)核版本和gcc版本
/proc/bus 系統(tǒng)總線(Bus)信息匿情,例如pci/usb等
/proc/driver 驅(qū)動信息
/proc/fs 文件系統(tǒng)信息
/proc/ide ide設(shè)備信息
/proc/irq 中斷請求設(shè)備信息
/proc/net 網(wǎng)卡設(shè)備信息
/proc/scsi scsi設(shè)備信息
/proc/tty tty設(shè)備信息
/proc/net/dev 顯示網(wǎng)絡適配器及統(tǒng)計信息
/proc/vmstat 虛擬內(nèi)存統(tǒng)計信息
/proc/vmcore 內(nèi)核panic時的內(nèi)存映像
/proc/diskstats 取得磁盤信息
/proc/schedstat kernel調(diào)度器的統(tǒng)計信息
/proc/zoneinfo 顯示內(nèi)存空間的統(tǒng)計信息,對分析虛擬內(nèi)存行為很有用
以下是/proc目錄中進程N的信息:
/proc/N pid為N的進程信息
/proc/N/cmdline 進程啟動命令
/proc/N/cwd 鏈接到進程當前工作目錄
/proc/N/environ 進程環(huán)境變量列表
/proc/N/exe 鏈接到進程的執(zhí)行命令文件
/proc/N/fd 包含進程相關(guān)的所有的文件描述符
/proc/N/maps 與進程相關(guān)的內(nèi)存映射信息
/proc/N/mem 指代進程持有的內(nèi)存信殊,不可讀
/proc/N/root 鏈接到進程的根目錄
/proc/N/stat 進程的狀態(tài)
/proc/N/statm 進程使用的內(nèi)存的狀態(tài)
/proc/N/status 進程狀態(tài)信息炬称,比stat/statm更具可讀性
/proc/self 鏈接到當前正在運行的進程