introduzione
Sysctlconsente all'utente di mettere a punto il kernel senza dover ricostruire il kernel. Inoltre, applicherà immediatamente le modifiche, quindi non sarà necessario riavviare il server per rendere effettive le modifiche. Questo tutorial fornisce una breve introduzione sysctle dimostra come usarlo per modificare parti specifiche del kernel Linux.
comandi
Per iniziare a utilizzare sysctl, rivedere i parametri e gli esempi elencati di seguito.
parametri
-a : questo mostrerà tutti i valori attualmente disponibili nella configurazione sysctl.
-A : Questo mostrerà tutti i valori attualmente disponibili nella configurazione sysctl in forma di tabella.
-e : questa opzione ignorerà gli errori relativi alle chiavi sconosciute.
-p : viene utilizzato per caricare una specifica configurazione sysctl, per impostazione predefinita utilizzerà/etc/sysctl.conf
-n : questa opzione disabilita la visualizzazione dei nomi delle chiavi durante la stampa dei valori.
-w : questa opzione serve per modificare (o aggiungere) valori al sistema su richiesta.
Esempi
$ sysctl -a
$ sysctl -n fs.file-max
$ sysctl -w fs.file-max=2097152
$ sysctl -p
Quindi prima stiamo verificando i valori predefiniti. Se il tuo /etc/sysctl.confè vuoto, mostrerà tutte le chiavi e i valori predefiniti. In secondo luogo, stiamo controllando il valore di fs.file-maxe quindi impostando il nuovo valore su 2097152. Infine, stiamo caricando il nuovo /etc/sysctl.conffile di configurazione.
Se stai cercando ulteriore aiuto, puoi usarlo man sysctl.
Protezione e protezione del kernel
Per rendere permanenti le modifiche, dovremo aggiungere questi valori a un file di configurazione. Utilizzare il file di configurazione di CentOS fornisce di default, /etc/sysctl.conf.
Apri il file con il tuo editor preferito.
Per impostazione predefinita, dovresti vedere qualcosa di simile a questo.
# sysctl settings are defined through files in
# /usr/lib/sysctl.d/, /run/sysctl.d/, and /etc/sysctl.d/.
#
# Vendors settings live in /usr/lib/sysctl.d/.
# To override a whole file, create a new file with the same in
# /etc/sysctl.d/ and put new settings there. To override
# only specific settings, add a file with a lexically later
# name in /etc/sysctl.d/ and put new settings there.
#
# For more information, see sysctl.conf(5) and sysctl.d(5).
Miglioriamo prima la gestione della memoria di sistema.
Ridurremo al minimo la quantità di scambio che dobbiamo fare, aumentare le dimensioni degli handle di file e della cache degli inode e limitare i dump core.
# Minimizing the amount of swapping
vm.swappiness = 20
vm.dirty_ratio = 80
vm.dirty_background_ratio = 5
# Increases the size of file handles and inode cache & restricts core dumps
fs.file-max = 2097152
fs.suid_dumpable = 0
Successivamente, consente di ottimizzare le prestazioni ottimizzate della rete.
Modificheremo la quantità di connessioni in entrata e backlog delle connessioni in entrata, aumenteremo la quantità massima di buffer di memoria e aumenteremo i buffer di invio / ricezione predefiniti e massimi.
# Change the amount of incoming connections and incoming connections backlog
net.core.somaxconn = 65535
net.core.netdev_max_backlog = 262144
# Increase the maximum amount of memory buffers
net.core.optmem_max = 25165824
# Increase the default and maximum send/receive buffers
net.core.rmem_default = 31457280
net.core.rmem_max = 67108864
net.core.wmem_default = 31457280
net.core.wmem_max = 67108864
Infine, miglioreremo la sicurezza generale della rete.
Abiliteremo la protezione dei cookie TCP SYN, la protezione da spoofing IP, ignorando le richieste ICMP, ignorando le richieste di trasmissione e registrando i pacchetti contraffatti, i pacchetti instradati di origine e i pacchetti di reindirizzamento. Insieme a questo, disabiliteremo il routing della sorgente IP e l'accettazione del reindirizzamento ICMP.
# Enable TCP SYN cookie protection
net.ipv4.tcp_syncookies = 1
# Enable IP spoofing protection
net.ipv4.conf.all.rp_filter = 1
# Enable ignoring to ICMP requests and broadcasts request
net.ipv4.icmp_echo_ignore_all = 1
net.ipv4.icmp_echo_ignore_broadcasts = 1
# Enable logging of spoofed packets, source routed packets and redirect packets
net.ipv4.conf.all.log_martians = 1
# Disable IP source routing
net.ipv4.conf.all.accept_source_route = 0
# Disable ICMP redirect acceptance
net.ipv4.conf.all.accept_redirects = 0
Salvare e chiudere il file, quindi caricare il file utilizzando il sysctl -pcomando
Conclusione
Alla fine, il tuo file dovrebbe essere simile a questo.
# sysctl settings are defined through files in
# /usr/lib/sysctl.d/, /run/sysctl.d/, and /etc/sysctl.d/.
#
# Vendors settings live in /usr/lib/sysctl.d/.
# To override a whole file, create a new file with the same in
# /etc/sysctl.d/ and put new settings there. To override
# only specific settings, add a file with a lexically later
# name in /etc/sysctl.d/ and put new settings there.
#
# For more information, see sysctl.conf(5) and sysctl.d(5).
# Minimizing the amount of swapping
vm.swappiness = 20
vm.dirty_ratio = 80
vm.dirty_background_ratio = 5
# Increases the size of file handles and inode cache & restricts core dumps
fs.file-max = 2097152
fs.suid_dumpable = 0
# Change the amount of incoming connections and incoming connections backlog
net.core.somaxconn = 65535
net.core.netdev_max_backlog = 262144
# Increase the maximum amount of memory buffers
net.core.optmem_max = 25165824
# Increase the default and maximum send/receive buffers
net.core.rmem_default = 31457280
net.core.rmem_max = 67108864
net.core.wmem_default = 31457280
net.core.wmem_max = 67108864
# Enable TCP SYN cookie protection
net.ipv4.tcp_syncookies = 1
# Enable IP spoofing protection
net.ipv4.conf.all.rp_filter = 1
# Enable ignoring to ICMP requests and broadcasts request
net.ipv4.icmp_echo_ignore_all = 1
net.ipv4.icmp_echo_ignore_broadcasts = 1
# Enable logging of spoofed packets, source routed packets and redirect packets
net.ipv4.conf.all.log_martians = 1
# Disable IP source routing
net.ipv4.conf.all.accept_source_route = 0
# Disable ICMP redirect acceptance
net.ipv4.conf.all.accept_redirects = 0