Linux系统参数配置简介

 

 

Linux服务器在对应用程序进行优化配置的时候,经常使用到sysctl和PAM两个模块对服务器进行优化。

 

一、sysctl内核参数配置

使用“sysctl -a”命令可以查看所有正在使用的内核参数。内核参数比较多(一般多达500项),按照前缀主要分为以下几大类:net.ipv4、net.ipv6、net.core、vm、fs、dev.parport、dev.cdrom 、dev.raid、kernel等等。
注:安装的组件和使用的方式不一样,正在使用的内核参数是不一样的。所有的内核参数的说明文档是放到linux-source-code\Documentation\sysctl中的,如果想知道对内核参数的说明,可以到该目录下查看相应的说明文档。

sysctl内核参数有两种修改方法
1) 命令设置的方式,对应内核运行配置参数
2) 文件设置的方式,对应内核静态配置参数

sysctl内核动态系统参数配置
其参数保存的主要位置为/proc/sys

使用“sysctl -w 参数名=值”的方式
例如:把net.ipv4.ip_forward的值修改为1,使用命令“sysctl -w net.ipv4.ip_forward=1”。

修改内核参数对应的proc文件
内核参数位于/proc/sys/之下,参数名称是以文件所在的路径,并将“/”以“.”来取代。举例来说,/proc/sys/net/ip_forward的参数名称为net.ipv4.ip_forward。
例如:把net.ipv4.ip_forward的值修改为1,使用命令“echo “1” > /proc/sys/net/ipv4/ip_forward”。

注意,这里proc文件跟普通的文件不一样。一般一个文件用echo写入内容之后,会变成一个文本文件,但echo修改proc文件之后还是个空文件。

 

sysctl内核静态系统参数配置
其参数保存的主要位置为/etc/sysctl.conf,针对整个系统参数配置。文件方式的好处是内核参数设置的值可以用文件保留下来。

内核参数默认保存在/etc/sysctl.conf文件中。修改的时候可以直接用vi编辑sysctl.conf文件,增加要修改的内核参数内容,修改的格式为:参数名=值。
例如,把net.ipv4.ip_forward的值修改为1,在sysctl.conf中增加下面这行内容:

net.ipv4.ip_forward=1

文件修改好后,进行保存。

使用“sysctl -p 配置文件名”来使配置生效
如果配置文件是默认的,可以不用输配置文件名,即使用“sysctl -p”。

系统重启,通过配置文件启动内核生效
之前认为修改后的内核参数放在文件中,系统启动的时候会读这个文件,重启后设置应该不会失效。
但经过验证,一般会失效。需要把将默认的boot.sysctl服务打开,系统启动时就会执行这个文件的设置。
或者把修改参数的命令“
/sbin/sysctl -e -p /etc/sysctl.conf”写入启动执行脚本文件里/etc/rc.local,这样系统重启后配置就不会失效。

 

1./etc/sysctl.conf 内核参数优化详解
以下为参考
kernel.shmall = 2043878         #共享内存总页数,计算公式,共享内存/4(KB)=N(页)。
kernel.shmmax = 17179869184     #单个共享内存段的最大值,Oracle中要足够大,应该>= sga_max_size,一般对于数据库系统影响大。
kernel.shmmni = 4096            #设置系统范围内共享内存段的最大数量,DB作用更明显。
kernel.sem = 250 32000 100 128  #信号量,分别为,每个信号集中的最大信号量数目、表示系统范围内的最大信号量总数目、表示每个信号发生时的最大系统操作数目、表示系统范围内的最大信号集总数目。
fs.file-max = 999999              #系统级能打开的文件句柄数量,是系统级限制。
net.ipv4.ip_local_port_range = 1000 65534 #可分配的端口范围,一般不是性能瓶颈。

#接收缓存
net.core.rmem_default = 262144 
net.core.rmem_max = 16777216
net.ipv4.tcp_rmem = 262144 262144 16777216 #接收区最小字节数/正常负载默认值/最大字节数

#发送区缓存
net.core.wmem_default = 262144
net.core.wmem_max = 16777216
net.ipv4.tcp_wmem = 262144 262144 16777216 #发送区最小字节数/正常负载默认值/最大字节数

net.ipv4.tcp_keepalive_time = 30 #keepalive探测消息的间隔(s),用于确认TCP连接是否有效
net.ipv4.tcp_synack_retries = 2 #控制内核向某个输入的SYN/ACK段重新发送相应的次数,默认5
net.ipv4.tcp_syn_retries = 2 #设置放弃连接请求前,需要进行多少次重试,默认5
net.ipv4.tcp_max_tw_buckets = 30000 #同时处理的TIME_WAIT数目,一般防止DoS
net.ipv4.tcp_tw_recycle = 0 #禁止TW快速回收,NAT下禁用
net.ipv4.tcp_timestamps = 1 #默认1,防止伪造seq从而防止DoS
net.ipv4.tcp_tw_reuse = 1 #默认0,重新应用TW用于新的TCP连接
net.ipv4.tcp_max_syn_backlog = 262144 #服务端参数,未经客户端确认的连接请求的队列大小,即SYN_RECV状态队列的大小,启用syncookies此参数无效。最终值=min(syn_backlog,somaxconn)。
net.core.netdev_max_backlog = 262144 #网卡接收速度超过内核处理速度,接收队列的数据包最大数目,Nginx代码中设置为511。
net.core.somaxconn = 65535 #握手完成尚未被Accept的Socket队列,最大0xFFFF。
net.ipv4.tcp_max_orphans = 262144 #不隶属于任何文件句柄的最大Socket数量,主要防止DoS。


2.某云厂商ECS实行的参数优化配置
[root@xxx ~]# cat /etc/sysctl.conf  
vm.swappiness = 20
kernel.sysrq = 1
net.core.rmem_default = 262144
net.core.wmem_default = 262144
net.core.rmem_max = 8388608
net.core.wmem_max = 8388608
net.ipv4.tcp_rmem = 8192 87380 8388608
net.ipv4.tcp_wmem = 8192 65536 8388608
net.ipv4.udp_rmem_min = 16384
net.ipv4.udp_wmem_min = 16384
net.ipv4.tcp_mem = 8388608 12582912 16777216
net.ipv4.udp_mem = 8388608 12582912 16777216
net.core.netdev_max_backlog = 4096
net.ipv4.tcp_max_syn_backlog = 4096
net.ipv4.ip_local_port_range = 10240 65000
kernel.sem = 250 256000 100 1024
net.ipv4.tcp_syncookies = 1
net.ipv4.icmp_echo_ignore_broadcasts = 1
net.ipv4.conf.all.accept_redirects = 0

##pvdriver<begin> do not change this comment
net.ipv4.conf.all.arp_notify = 1
net.ipv4.conf.default.arp_notify = 1
net.ipv4.conf.eth0.arp_notify = 1
net.ipv4.conf.lo.arp_notify = 1
###pvdriver<end>
net.ipv4.tcp_max_tw_buckets =10000
net.ipv4.tcp_tw_recycle = 0
net.ipv4.tcp_tw_reuse = 0
/etc/sysctl.conf 内核参数优化详解

 

 

 

 

二、PAM插入式认证模块参数配置


其参数保存的主要位置为/etc/security/limits.conf,针对用户参数配置。

Linux PAM(插入式认证模块,Pluggable Authentication Modules)中pam_limits.so 的配置文件,突破系统的默认限制,对系统访问资源有一定保护作用。其工作原理为当用户 访问服务器,服务程序将请求发送到PAM模块,PAM模块根据服务名称在/etc/pam.d目录下选择一个对应的服务文件,然后根据服务文件的内容选择具体的PAM模块进行处理.

注意:要使limits.conf文件配置生效,必须要确保pam_limits.so文件被加入到启动文件中。查看程序配置文件中有"session required /lib/security/pam_limits.so"

例如:限制admin用户登录到sshd的服务不能超过2个
在/etc/pam.d/sshd中添加:session required pam_limits.so
在/etc/security/limits.conf中添加:admin - maxlogins 2

注意:查看应用程序能否被PAM支持,用ldd查看链接关系。

 

$ ldd /usr/sbin/sshd
linux-vdso.so.1 => (0x00007fffcb5fe000)
libwrap.so.0 => /lib/x86_64-linux-gnu/libwrap.so.0 (0x00007fa5412ce000)
libaudit.so.1 => /lib/x86_64-linux-gnu/libaudit.so.1 (0x00007fa5410aa000)
libpam.so.0 => /lib/x86_64-linux-gnu/libpam.so.0 (0x00007fa540e9b000)
libselinux.so.1 => /lib/x86_64-linux-gnu/libselinux.so.1 (0x00007fa540c78000)
libck-connector.so.0 => /usr/lib/x86_64-linux-gnu/libck-connector.so.0 (0x00007fa540a74000)
libdbus-1.so.3 => /lib/x86_64-linux-gnu/libdbus-1.so.3 (0x00007fa54082e000)
libcrypto.so.1.0.0 => /lib/x86_64-linux-gnu/libcrypto.so.1.0.0 (0x00007fa540452000)
libutil.so.1 => /lib/x86_64-linux-gnu/libutil.so.1 (0x00007fa54024f000)
libz.so.1 => /lib/x86_64-linux-gnu/libz.so.1 (0x00007fa540035000)
libcrypt.so.1 => /lib/x86_64-linux-gnu/libcrypt.so.1 (0x00007fa53fdfc000)
libgssapi_krb5.so.2 => /usr/lib/x86_64-linux-gnu/libgssapi_krb5.so.2 (0x00007fa53fbb5000)
libkrb5.so.3 => /usr/lib/x86_64-linux-gnu/libkrb5.so.3 (0x00007fa53f8e9000)
libcom_err.so.2 => /lib/x86_64-linux-gnu/libcom_err.so.2 (0x00007fa53f6e5000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fa53f320000)
libnsl.so.1 => /lib/x86_64-linux-gnu/libnsl.so.1 (0x00007fa53f105000)
libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007fa53ef01000)
libpcre.so.3 => /lib/x86_64-linux-gnu/libpcre.so.3 (0x00007fa53ecc3000)
/lib64/ld-linux-x86-64.so.2 (0x00007fa5417b0000)
libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007fa53eaa4000)
librt.so.1 => /lib/x86_64-linux-gnu/librt.so.1 (0x00007fa53e89c000)
libk5crypto.so.3 => /usr/lib/x86_64-linux-gnu/libk5crypto.so.3 (0x00007fa53e66d000)
libkrb5support.so.0 => /usr/lib/x86_64-linux-gnu/libkrb5support.so.0 (0x00007fa53e461000)
libkeyutils.so.1 => /lib/x86_64-linux-gnu/libkeyutils.so.1 (0x00007fa53e25d000)
libresolv.so.2 => /lib/x86_64-linux-gnu/libresolv.so.2 (0x00007fa53e042000)

 

 

PAM插入式认证模块配置格式


username|@groupname type resource limit

username|@groupname
设置需要被限制的用户名,组名前面加@和用户名区别。也可用通配符*来做所有用户的限制

type
类型有soft,hard 和 -,其中soft 指的是当前系统生效的设置值。hard表明系统中所能设定的最大值。soft的限制不能比har限制高。用 - 就表明同时设置了soft和hard的值。可以超出软规则的限制(警告),但不能超过硬规则的限制。

resource
表示要限制的资源

limit
表示资源数量

 

PAM插入式认证模块配置资源


1)core - 限制内核文件的大小,何谓core文件,当一个程序崩溃时,在进程当前工作目录的core文件中复制了该进程的存储图像。core文件仅仅是一个内存映象(同时加上调试信息),主要是用来调试的。core文件是个二进制文件,需要用相应的工具来分析程序崩溃时的内存映像,系统默认core文件的大小为0,所以没有被创建。可以用ulimit命令查看和修改core文件的大小。

2)date - 最大数据大小

3)fsize - 最大文件大小

4)memlock - 最大锁定内存地址空间

5)nofile - 打开文件的最大数目,对于需要做许多套接字连接并使它们处于打开状态的应用程序而言,最好通过使用ulimit -n,或者通过设置nofile参数,为用户把文件描述符的数量设置得比默认值高一些

6)rss - 最大持久设置大小

7)stack - 最大栈大小

8)cpu - 以分钟为单位的最多 CPU 时间

9)noproc - 进程的最大数目

10)as - 地址空间限制

11)maxlogins - 此用户允许登录的最大数目

PAM插入式认证模块配置方式
1)暂时生效
适用于通过ulimit命令登录shell会话期间,Ulimit命令参数
-a 显示所有限制
-c core文件大小的上限
-d 进程数据段大小的上限
-f shell所能创建的文件大小的上限
-m 驻留内存大小的上限
-s 堆栈大小的上限
-t 每秒可占用的CPU时间上限
-p 管道大小
-n 打开文件数的上限
-u 进程数的上限
-v 虚拟内存的上限

2)永久生效
通过将一个相应的ulimit语句添加到由登录shell读取的文件之一(例如 ~/.profile),即特定于shell的用户资源文件;或者通过编辑/etc/security/limits.conf


PAM插入式认证模块常用配置


1)建议设置成无限制(unlimited)


数据段长度:ulimit –d unlimited

最大内存大小:ulimit –m unlimited

堆栈大小:ulimit –s unlimited

CPU 时间:ulimit –t unlimited

虚拟内存:ulimit –v unlimited

2)套接字应用程序
对于需要做许多套接字连接并使它们处于打开状态的应用程序而言,最好通过使用 ulimit –n,或者通过设置/etc/security/limits.conf中的nofile 参数,为用户把文件描述符的数量设置得比默认值高一些。

3)应用程序开发调试
# ulimit -c 1000
-c 指定修改core文件的大小,1000指定了core文件大小。也可以对core文件的大小不做限制,如: ulimit -c unlimited
注意:如果想让修改永久生效,则需要修改配置文件,如 .bash_profile、/etc/profile或/etc/security/limits.conf

4)提高 Oracle 用户的 shell 限制

首先,修改/etc/security/limits.conf
oracle soft nproc 2047
oracle hard nproc 16384
oracle soft nofile 1024
oracle hard nofile 65536

其次,修改程序配置要求/etc/pam.d/login
session required /lib/security/pam_limits.so

最后,运行环境进行动态设置
/etc/profile
if [ $USER = "oracle" ]; then
if [ $SHELL = "/bin/ksh" ]; then
ulimit -p 16384
ulimit -n 65536
else
ulimit -u 16384 -n 65536
fi
fi


版权声明:本文为CSDN博主「lida2003」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/lida2003/java/article/details/51508396

 

 

/etc/security/limits.conf 和/etc/systemd/system.conf 及/etc/systemd/user.conf的区别

 

系统文件数限制
相关的配置
1、/proc/sys/fs/file-nr是系统当前打开的文件数,是一个只读的参数
2、/proc/sys/fs/file-max指定了系统范围内所有进程可以打开的文件句柄的数量限制,修改方法如下:
临时调整file-max:

 echo  6553560 > /proc/sys/fs/file-max
1.
永久调整file-max:

 vim /etc/sysctl.conf, 
 fs.file-max = 6553560
1.
2.
重启生效

3、ulimit是bash内置命令,提供了对shell及该shell启动的进程的可用资源控制,资源限制的配置可以在 /etc/security/limits.conf 设置,示例如下:

 *    soft  nofile    65535
 *    hard  nofile    65535

4、Systemd下的最大打开文件数设置

在Systemd替代了之前的SysV后,/etc/security/limits.conf 文件的配置作用域缩小了一些。limits.conf这里的配置,只适用于通过PAM认证登录用户的资源限制,它对systemd的service的资源限制不生效。对于systemd service的资源限制的配置如下:
全局的配置,放在文件 /etc/systemd/system.conf和/etc/systemd/user.conf

 DefaultLimitCORE=infinity
 DefaultLimitNOFILE=100000
 DefaultLimitNPROC=100000

查看一个进程的limit设置:

 cat /proc/[PID]/limits
1.
针对单个Service,也可以设置,以nginx为例。编辑 /usr/lib/systemd/system/nginx.service 文件,或者 /usr/lib/systemd/system/nginx.service.d/my-limit.conf 文件,做如下配置:

 [Service]
 LimitCORE=infinity
 LimitNOFILE=65535
 LimitNPROC=65535

然后运行如下命令,才能生效。

登录后复制 
 sudo systemctl daemon-reload
 sudo systemctl restart nginx.service
-----------------------------------
系统文件数限制
https://blog.51cto.com/as007012/1956222
#! /bin/bash

clock_server=$1
#创建应用用户
createUser() {
    id 1024 &>/dev/null || groupadd -g 1024 mxxluser && useradd -u 1024 -g 1024 mxxluser&& echo "mxxluser" |passwd --stdin mxxluser && \
    echo -e "\033[32m==================> createUser Done.\033[0m"
}

# 调整ulimit
setUlimit() {
grep '165535' /etc/security/limits.conf &>/dev/null || \
cat >>/etc/security/limits.conf <<LLS
* soft nofile 165535
* hard nofile 165535
* soft nproc 65535
* hard nproc 65535
LLS

[ -f /etc/security/limits.d/20-nproc.conf  ]  && sed -i 's/4096/65535/' /etc/security/limits.d/20-nproc.conf

grep 'DefaultLimitNOFILE=100000' /etc/systemd/system.conf &>/dev/null || \
cat >>/etc/systemd/system.conf<<LLS
DefaultLimitNOFILE=100000
DefaultLimitNPROC=65535
LLS

grep 'DefaultLimitNOFILE=100000' /etc/systemd/user.conf &>/dev/null || \
cat >>/etc/systemd/user.conf<<LLS
DefaultLimitNOFILE=100000
DefaultLimitNPROC=65535
LLS
}

# 优化内核参数
setKernel() {
cp -p /etc/sysctl.conf /etc/sysctl.conf.`date +%F`

grep 'net.core.netdev_max_backlog =  262144' /etc/sysctl.conf &>/dev/null  || \
cat >>/etc/sysctl.conf<<LLS
net.core.netdev_max_backlog =  262144
net.core.wmem_default = 8388608
net.core.rmem_default = 8388608
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.core.somaxconn = 65535
net.ipv4.tcp_max_tw_buckets = 60000
net.ipv4.tcp_rmem = 4096 87380 4194304
net.ipv4.tcp_wmem = 4096 16384 4194304
net.ipv4.tcp_max_orphans = 3276800
net.ipv4.tcp_max_syn_backlog = 262144
net.ipv4.tcp_timestamps = 0
net.ipv4.tcp_synack_retries = 1
net.ipv4.tcp_syn_retries = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_mem = 94500000 915000000 927000000
net.ipv4.ip_local_port_range = 1024 65535
net.nf_conntrack_max = 2621400
kernel.pid_max = 100000
vm.max_map_count= 262144
vm.swappiness = 5
net.ipv6.conf.all.disable_ipv6 = 1
net.ipv6.conf.default.disable_ipv6 = 1
net.ipv4.tcp_keepalive_time = 600
net.ipv4.tcp_keepalive_intvl = 30
net.ipv4.tcp_keepalive_probes = 10
net.ipv4.ip_forward = 1
net.bridge.bridge-nf-call-iptables = 1
net.bridge.bridge-nf-call-ip6tables = 1
fs.inotify.max_user_watches=8192000
LLS

sysctl -p > /dev/null 2>&1
}

# 关闭不重要的服务
disableService () {
  StopServiceList="
  abrt-ccpp
  abrtd
  acpid
  agentwatch
  atd
  auditd
  blk-availability
  cpuspeed
  haldaemon
  ip6tables
  lvm2-monitor
  mdmonitor
  messagebus
  netconsole
  netfs
  nscd
  postfix
  psacct
  quota_nld
  rdisc
  restorecond
  rngd
  saslauthd
  smartd
  snmptrapd
  svnserve
  psacct
  firewalld
  "

  for s in ${StopServiceList}
  do
    systemctl disable ${s} > /dev/null 2>&1 && systemctl stop ${s}
  done
  echo -e "\033[32m==================> disableService Done.\033[0m"
}

# 关闭 ipv6
closeIpv6 () {
  grep 'alias net-pf-10 off' /etc/modprobe.d/disable_ipv6.conf &>/dev/null || echo "alias net-pf-10 off" >> /etc/modprobe.d/disable_ipv6.conf
  grep 'options ipv6 disable=1' /etc/modprobe.d/disable_ipv6.conf &>/dev/null || echo "options ipv6 disable=1" >> /etc/modprobe.d/disable_ipv6.conf
  grep 'NETWORKING_IPV6=no' /etc/sysconfig/network &>/dev/null || echo "NETWORKING_IPV6=no" >> /etc/sysconfig/network
  grep 'NOZEROCONF=yes' /etc/sysconfig/network &>/dev/null || echo "NOZEROCONF=yes" >> /etc/sysconfig/network
  grep 'IPV6INIT=no' /etc/sysconfig/network &>/dev/null || echo "IPV6INIT=no" >> /etc/sysconfig/network
  echo -e "\033[32m==================> closeIpv6 Done.\033[0m"
}

#设置chronyd时间同步
setChrony() {
  cp -p /etc/chrony.conf /etc/chrony.conf.`date +%F`

cat >/etc/chrony.conf<<EOF
server ${clock_server} iburst
driftfile /var/lib/chrony/drift
makestep 1.0 3
rtcsync
logdir /var/log/chrony
EOF
  systemctl enable chronyd.service
  systemctl restart chronyd.service
}

# 关闭 SELinux
closeSelinux() {
  setenforce 0
  sed -i '/SELINUX/s/enforcing/disabled/' /etc/selinux/config
  echo -e "\033[31m selinux is disabled,if you need,you must reboot.\033[0m"
  echo -e "\033[32m==================> closeSelinux Done.\033[0m"
}

# 命令历史记录
setupHistory() {
  cp /etc/bashrc /etc/bashrc.bak
  grep 'MY_IP' /etc/bashrc &>/dev/null || echo -e 'MY_IP=`who -u am i 2>/dev/null|awk '"'{print \$NF}'"'|sed -e '"'s/[\(\)]//g'"'`' >> /etc/bashrc

  echo -e '''
  export HISTSIZE=5000
  export HISTTIMEFORMAT="%F %T ${MY_IP} `whoami`"
  export PROMPT_COMMAND="history -a"
  unset HISTCONTROL''' >> /etc/bashrc
  echo -e "\033[32m==================> setupHistory Done.\033[0m"
}

#删除 banner 信息
setBanner() {
  cp /etc/issue /etc/issue.bak
  cp /etc/issue.net /etc/issue.net.bak
  cp /etc/redhat-release /etc/redhat-release.bak
  cp /etc/motd /etc/motd.bak
  grep '#' /etc/issue &>/dev/null || sed -i 's/^/#/' /etc/issue
  grep '#' /etc/issue.net &>/dev/null || sed -i 's/^/#/' /etc/issue.net
  grep '#' /etc/redhat-release &>/dev/null || sed -i 's/^/#/' /etc/redhat-release
  grep '#' /etc/motd &>/dev/null || sed -i 's/^/#/' /etc/motd
  echo -e "\033[32m==================> setBanner Done.\033[0m"
}

#设置客户端超时时间
setClientTime() {
  cp /etc/profile /etc/profile.bak
  sed -i '/TMOUT=600/d' /etc/profile
  sed -i '/unset -f pathmunge/a TMOUT=600' /etc/profile
  echo -e "\033[32m==================> setClientTime Done.\033[0m"
}


Kernel() {
  cp /etc/default/grub /etc/default/grub.`date +%F`  && \
  grub2-set-default 0 && \
  echo -e "\033[32m==================> upgradeKernel Done.\033[0m"
  #grubby --args="user_namespace.enable=1" --update-kernel="$(grubby --default-kernel)"
}

upgradeKernel() {
  local_kernel_version=`uname -r|awk -F"-" '{print $1}'`
  upgrade_kernel_version=5.4.188
  [  "$local_kernel_version" == $upgrade_kernel_version ] || Kernel
}

 #开启防火墙,放开端口
setFirewalld() {
    systemctl start firewalld.service && systemctl enable firewalld.service
    firewall-cmd --permanent --zone=trusted --add-source=10.8.12.70
        firewall-cmd --permanent --zone=trusted --add-source=10.8.12.71
        firewall-cmd --permanent --zone=trusted --add-source=10.8.12.72
        firewall-cmd --permanent --zone=trusted --add-source=10.8.12.73
        firewall-cmd --permanent --zone=trusted --add-source=10.8.12.74
        firewall-cmd --permanent --zone=trusted --add-source=10.8.12.75
        firewall-cmd --permanent --zone=trusted --add-source=10.8.12.76
        firewall-cmd --permanent --zone=trusted --add-source=10.8.12.77
        firewall-cmd --permanent --zone=trusted --add-source=10.8.12.78
        firewall-cmd --permanent --zone=trusted --add-source=10.8.28.44
        firewall-cmd --permanent --zone=trusted --add-source=10.8.28.45
    firewall-cmd --permanent --zone=trusted --add-source=172.16.0.0/16
    firewall-cmd --permanent --zone=trusted --add-source=172.17.0.0/16
    firewall-cmd --permanent --add-port=22/tcp
    firewall-cmd --permanent --add-port=6443/tcp
    firewall-cmd --permanent --add-port=2379-2380/tcp
    firewall-cmd --permanent --add-port=10250/tcp
    firewall-cmd --permanent --add-port=10251/tcp
    firewall-cmd --permanent --add-port=10252/tcp
    firewall-cmd --permanent --add-port=443/tcp
    firewall-cmd --permanent --add-port=10255/tcp
    firewall-cmd --permanent --add-port=8472/udp
    firewall-cmd --permanent --add-port=443/udp
    firewall-cmd --permanent --add-port=80/tcp
    firewall-cmd --permanent --add-port=53/udp
    firewall-cmd --permanent --add-port=53/tcp
    firewall-cmd --permanent --add-port=179/tcp
    firewall-cmd --permanent --add-port=23306/tcp
    firewall-cmd --permanent --add-port=30000-32767/tcp
    firewall-cmd --permanent --add-port=9153/tcp
        firewall-cmd --permanent --add-port=9991/tcp
    firewall-cmd --add-masquerade –-permanent
    firewall-cmd --reload
        echo -e "\033[32m==================> setFirewalld Done.\033[0m"
}

createUser
setUlimit
setKernel
disableService
closeIpv6
setChrony
closeSelinux
setupHistory
setBanner
#setClientTime
upgradeKernel
#setFirewalld
系统优化小脚本
#! /bin/bash
#
#

clock_server="192.168.75.17"

#创建应用用户
createUser() {
    id 1024 &>/dev/null || groupadd -g 1024 mxxluser && useradd -u 1024 -g 1024 mxxluser && echo "mxxluser" |passwd --stdin mxxluser && \
    echo -e "\033[32m==================> createUser Done.\033[0m"
}

# 调整ulimit

setUlimit() {
grep '165535' /etc/security/limits.conf &>/dev/null || \
cat >>/etc/security/limits.conf <<MXXL
* soft nofile 165535
* hard nofile 165535
* soft nproc 65535
* hard nproc 65535
MXXL

[ -f /etc/security/limits.d/20-nproc.conf  ]  && sed -i 's/4096/65535/' /etc/security/limits.d/20-nproc.conf

grep 'DefaultLimitNOFILE=100000' /etc/systemd/system.conf &>/dev/null || \
cat >>/etc/systemd/system.conf<<MXXL
DefaultLimitNOFILE=100000
DefaultLimitNPROC=65535
MXXL

grep 'DefaultLimitNOFILE=100000' /etc/systemd/user.conf &>/dev/null || \
cat >>/etc/systemd/user.conf<<MXXL
DefaultLimitNOFILE=100000
DefaultLimitNPROC=65535
MXXL
}

# 优化内核参数
setKernel() {
cp -p /etc/sysctl.conf /etc/sysctl.conf.`date +%F`

grep 'net.core.netdev_max_backlog =  262144' /etc/sysctl.conf &>/dev/null  || \
cat >>/etc/sysctl.conf<<MXXL
net.core.netdev_max_backlog =  262144
net.core.wmem_default = 8388608
net.core.rmem_default = 8388608
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.core.somaxconn = 65535
net.ipv4.tcp_max_tw_buckets = 60000
net.ipv4.tcp_rmem = 4096 87380 4194304
net.ipv4.tcp_wmem = 4096 16384 4194304
net.ipv4.tcp_max_orphans = 3276800
net.ipv4.tcp_max_syn_backlog = 262144
net.ipv4.tcp_timestamps = 0
net.ipv4.tcp_synack_retries = 1
net.ipv4.tcp_syn_retries = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_mem = 94500000 915000000 927000000
net.ipv4.ip_local_port_range = 1024 65535
net.nf_conntrack_max = 2621400
kernel.pid_max = 100000
vm.max_map_count= 262144
vm.swappiness = 5
net.ipv6.conf.all.disable_ipv6 = 1
net.ipv6.conf.default.disable_ipv6 = 1
net.ipv4.tcp_keepalive_time = 600
net.ipv4.tcp_keepalive_intvl = 30
net.ipv4.tcp_keepalive_probes = 10
net.ipv4.ip_forward = 1
net.bridge.bridge-nf-call-iptables = 1
net.bridge.bridge-nf-call-ip6tables = 1
MXXL

sysctl -p > /dev/null 2>&1
}

# 关闭不重要的服务
disableService () {
  StopServiceList="
  abrt-ccpp
  abrtd
  acpid
  agentwatch
  atd
  auditd
  blk-availability
  cpuspeed
  haldaemon
  ip6tables
  lvm2-monitor
  mdmonitor
  messagebus
  netconsole
  netfs
  nscd
  postfix
  psacct
  quota_nld
  rdisc
  restorecond
  rngd
  saslauthd
  smartd
  snmptrapd
  svnserve
  psacct
  NetworkManager
  firewalld
  "

  for s in ${StopServiceList}
  do
    systemctl disable ${s} > /dev/null 2>&1
  done
  echo -e "\033[32m==================> disableService Done.\033[0m"
}

# 关闭 ipv6
closeIpv6 () {
  grep 'alias net-pf-10 off' /etc/modprobe.d/disable_ipv6.conf &>/dev/null || echo "alias net-pf-10 off" >> /etc/modprobe.d/disable_ipv6.conf
  grep 'options ipv6 disable=1' /etc/modprobe.d/disable_ipv6.conf &>/dev/null || echo "options ipv6 disable=1" >> /etc/modprobe.d/disable_ipv6.conf
  grep 'NETWORKING_IPV6=no' /etc/sysconfig/network &>/dev/null || echo "NETWORKING_IPV6=no" >> /etc/sysconfig/network
  grep 'NOZEROCONF=yes' /etc/sysconfig/network &>/dev/null || echo "NOZEROCONF=yes" >> /etc/sysconfig/network
  grep 'IPV6INIT=no' /etc/sysconfig/network &>/dev/null || echo "IPV6INIT=no" >> /etc/sysconfig/network
  echo -e "\033[32m==================> closeIpv6 Done.\033[0m"
}

#设置chronyd时间同步
setChrony() {
  yum -y install chronyd &>/dev/null
  cp -p /etc/chrony.conf /etc/chrony.conf.`date +%F`
  
cat >/etc/chrony.conf<<EOF
server ${clock_server}
driftfile /var/lib/chrony/drift
makestep 1.0 3
rtcsync
logdir /var/log/chrony
allow 198.168.75.0/24
EOF
  systemctl enable chronyd.service
  systemctl restart chronyd.service
}

# 关闭 SELinux
closeSelinux() {
  setenforce 0
  sed -i '/SELINUX/s/enforcing/disabled/' /etc/selinux/config
  echo -e "\033[31m selinux is disabled,if you need,you must reboot.\033[0m"
  echo -e "\033[32m==================> closeSelinux Done.\033[0m"
}

# 命令历史记录
setupHistory() {
  cp /etc/bashrc /etc/bashrc.bak
  grep 'MY_IP' /etc/bashrc &>/dev/null || echo -e 'MY_IP=`who -u am i 2>/dev/null|awk '"'{print \$NF}'"'|sed -e '"'s/[\(\)]//g'"'`' >> /etc/bashrc

  echo -e '''
  export HISTSIZE=5000
  export HISTTIMEFORMAT="%F %T ${MY_IP} `whoami`"
  export PROMPT_COMMAND="history -a"
  unset HISTCONTROL''' >> /etc/bashrc
  echo -e "\033[32m==================> setupHistory Done.\033[0m"
}

#删除 banner 信息
setBanner() {
  cp /etc/issue /etc/issue.bak
  cp /etc/issue.net /etc/issue.net.bak
  cp /etc/redhat-release /etc/redhat-release.bak
  cp /etc/motd /etc/motd.bak
  grep '#' /etc/issue &>/dev/null || sed -i 's/^/#/' /etc/issue
  grep '#' /etc/issue.net &>/dev/null || sed -i 's/^/#/' /etc/issue.net
  grep '#' /etc/redhat-release &>/dev/null || sed -i 's/^/#/' /etc/redhat-release
  grep '#' /etc/motd &>/dev/null || sed -i 's/^/#/' /etc/motd
  echo -e "\033[32m==================> setBanner Done.\033[0m"
}

#设置客户端超时时间
setClientTime() {
  cp /etc/profile /etc/profile.bak
  sed -i '/TMOUT=600/d' /etc/profile
  sed -i '/unset -f pathmunge/a TMOUT=600' /etc/profile
  echo -e "\033[32m==================> setClientTime Done.\033[0m"
}

#更新部分默认安装包
updatePKG() {
  yum -y install wget
  mkdir /etc/yum.repos.d/bak && mv /etc/yum.repos.d/* /etc/yum.repos.d/bak
  wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
  wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
  #wget -O /etc/yum.repos.d/MXXL.repo https://mirrors.MXXLchain.com/MXXL.repo
  yum -y install vim net-tools gcc gcc-c++ make autoconf libtool-ltdl-devel gd-devel freetype-devel libxml2-devel libjpeg-devel libpng-devel pcre-devel pcre openssl-devel curl-devel bison patch unzip libmcrypt-devel libmhash-devel ncurses-devel sudo bzip2 mlocate flex lrzsz sysstat lsof setuptool system-config-network-tui system-config-firewall-tui ntpdate libaio-devel wget screen iptables-services ipvsadm socat chrony telnet tcpdump nfs-utils iftop mtr policycoreutils-python zlib zlib-devel openssl utpdate rsync vim-enhanced tree ansible epel-release libnl libnl-devel libnfnetlink-devel &>/dev/null
}

Kernel() {
  rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org && \
  yum install -y https://www.elrepo.org/elrepo-release-7.el7.elrepo.noarch.rpm && \
  yum list available --disablerepo=* --enablerepo=elrepo-kernel
  yum -y --enablerepo=elrepo-kernel install kernel-lt.x86_64 && \
  cp /etc/default/grub /etc/default/grub.`date +%F`  && \
  grub2-set-default 0 && \
  echo -e "\033[32m==================> upgradeKernel Done.\033[0m"
  #grubby --args="user_namespace.enable=1" --update-kernel="$(grubby --default-kernel)"
}

upgradeKernel() {
  [ -f /etc/yum.repos.d/elrepo.repo ] || Kernel 
}

#set ssh
if [ ! -f /etc/ssh/sshd_config.bak ];then
cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak
sed -i 's/^GSSAPIAuthentication yes$/GSSAPIAuthentication no/' /etc/ssh/sshd_config
sed -i 's/#UseDNS yes/UseDNS no/' /etc/ssh/sshd_config
systemctl  restart sshd.service
fi

createUser
setUlimit
setKernel
disableService
closeIpv6
setChrony
closeSelinux
setupHistory
setBanner
setClientTime
#updatePKG
#upgradeKernel
View Code

 

posted @ 2020-07-14 10:40  冥想心灵  阅读(2490)  评论(0编辑  收藏  举报