centos7系统优化脚本-init.sh-20190404 Linux教程


Linux系统sshd服务安全优化方案

生产服务器环境最小化安装Centos优化配置


init.sh

#!/bin/sh
#---------------------------------------#
# description:      centos7系统优化脚本  #
# author:                 cndo.org      #
# email:         522588122@qq.com       #
#---------------------------------------#

# 禁用并关闭selinux,防火墙,;    # getenforce: 查看状态
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
setenforce 0
systemctl stop iptables.service
systemctl stop firewalld.service 
systemctl disable firewalld.service
systemctl stop firewalld     # 停止firewalld服务   
systemctl mask firewalld     # 禁用firewalld服务

# 创建admin用户组及admin用户
groupadd admin   
useradd admin -g admin 
passwd admin # @a Wdqdmm@54321
chown -R admin:admin  /home/admin  # 更改文件夹及内部文件的所属用户及组
#echo 'admin ALL=(ALL) ALL' >> /etc/sudoers  #为用户添加sudo权限
echo 'admin ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers  # 为新用户添加sudo权限
sed -i 's/.*PermitRootLogin yes/PermitRootLogin no/' /etc/ssh/sshd_config # 禁用root远程登陆

# 安装基础应用
yum -y install --skip-broken epel-release axel yum-axelget vim net-tools bzip2 lrzsz unzip zip wget ftp gcc make kernel-headers ntpdate lsof screen curl python psmisc telnet-server telnet.* vim-enhanced xz iftop sysstat dstat htop iotop lrzsz lftp bash-completion python-simplejson libselinux-python smartmontools automake autoconf libtool

curl https://setup.ius.io | sh;yum remove -y git | yum -y install git2u;

#Kernel性能调优
cat >> /etc/sysctl.conf<<EOF
vm.max_map_count=655360
net.ipv4.ip_forward=1
net.ipv4.neigh.default.gc_thresh1=4096
net.ipv4.neigh.default.gc_thresh2=6144
net.ipv4.neigh.default.gc_thresh3=8192
EOF
sysctl -p

#提高系统打开文件数、打开进程数限制,减小默认栈空间大小限制
grep -q "* soft nofile 60000" /etc/security/limits.conf || cat >> /etc/security/limits.conf << EOF
########################################
* soft nofile 60000
* hard nofile 65536
* soft nproc  2048
* hard nproc  16384
* soft stack  10240
* hard stack  32768
EOF

#提高Shell打开文件数、打开进程数限制,减小默认栈空间大小限制
grep -q "ulimit -Sn 60000" /etc/profile || cat >> /etc/profile << EOF
########################################
ulimit -Sn 60000
ulimit -Hn 65536
ulimit -Su 2048
ulimit -Hu 16384
ulimit -Ss 10240
ulimit -Hs 32768
alias grep='grep --color=auto'
export HISTTIMEFORMAT="%Y-%m-%d %H:%M:%S "
EOF

#删除不必要的用户
userdel adm
userdel lp
userdel sync
userdel shutdown
userdel halt
userdel news
userdel uucp
userdel operator
userdel games
userdel gopher
userdel ftp
#删除不必要的群组
groupdel adm
groupdel lp
groupdel news
groupdel uucp
groupdel games
groupdel dip
groupdel pppusers

# Linux清除日志. 
cat /dev/null > /var/log/wtmp
cat /dev/null > /var/log/messages
cat /dev/null > /var/log/secure
cat /dev/null > /var/log/lastlog
rm -f /var/log/wtmp*
rm -f /root/.bash_history
touch .hushlogin
history -c
cat /dev/null > /var/log/syslog
cat /dev/null > /var/adm/sylog
cat /dev/null > /var/log/wtmp
cat /dev/null > /var/log/maillog
cat /dev/null > /var/log/messages
cat /dev/null > /var/log/openwebmail.log
cat /dev/null > /var/log/maillog
cat /dev/null > /var/log/secure

# 修改系统语言环境,修改时区
echo 'LANG="en_US.UTF-8"' >> /etc/profile;source /etc/profile 
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
ntpdate time.windows.com && hwclock -w && hwclock --systohc

# 优化SSH
sed -i 's/.*#Port 22/Port 55555/' /etc/ssh/sshd_config
sed -i 's/.*#PermitEmptyPasswords no/GSSAPIAuthentication no/' /etc/ssh/sshd_config
sed -i 's/.*UseDNS yes/UseDNS no/' /etc/ssh/sshd_config
sed -i 's/.*GSSAPIAuthentication yes/GSSAPIAuthentication no/' /etc/ssh/sshd_config
grep -q '7.' /etc/redhat-release

# 优化DNS
cat > /etc/resolv.conf << EOF
nameserver 8.8.8.8
nameserver 8.8.4.4
EOF


# 升级最新内核
rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org
rpm -Uvh http://www.elrepo.org/elrepo-release-7.0-3.el7.elrepo.noarch.rpm
yum -y --enablerepo=elrepo-kernel install kernel-ml
grub2-set-default 0
echo "net.core.default_qdisc=fq" >> /etc/sysctl.conf && echo "net.ipv4.tcp_congestion_control=bbr" >> /etc/sysctl.conf
yum -y remove kernel-tools-3.10.0-693.17.1.el7.x86_64
yum -y remove kernel-tools-libs-3.10.0-693.17.1.el7.x86_64
sysctl -p

# 国内服务器,需换repo源
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup # *备份*/
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo # *下载*/
yum makecache /*生成缓存*/

# 更新软件
yum check-update
yum update -y
yum clean all

systemctl restart sshd
reboot


签名:这个人很懒,什么也没有留下!
最新回复 (0)
返回