关闭selinux
setenforce 0
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
关闭防火墙
systemctl stop firewalld
systemctl disable firewalld
systemctl stop iptables
systemctl disable iptables
关闭network管理系统
systemctl stop NetworkManager
systemctl disable NetworkManager
配置DNS
sed -i '1i\nameserver 114.114.114.114' /etc/resolv.conf
sed -i '2i\nameserver 8.8.8.8' /etc/resolv.conf
更新yum源为阿里云
cd /etc/yum.repos.d && mkdir back
mv *.repo back
curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
yum clean all
yum makecache
安装一些常用的依赖插件,开发库
yum -y install wget vim ntp unzip zip net-snmp* telnet sysstat gcc gcc-c++ make openssl* perl ncurses* nethogs lsof lrzsz libselinux-python bash-completion net-tools setuptool libffi-devel system-config-network-tui ntsysv expat-devel psmisc nmap fping traceroute python2-pip readline-devel cpp cmake bison libaio-devel sqlite-devel ncurses-devel perl-DBD-MySQL perl-Time-HiRes openssh-clients libaio bzip2-devel tk-devel zlib-devel openssl-devel libssl.so.6 numactl jemalloc compat-readline5-devel kde-l10n-Chinese installpolicycoreutils-python-2.5-9.el7.x86_64
时间同步
配置同步时间为自启动
systemctl start ntpd
systemctl enable ntpd
设置为false,硬件始终不与utc时间一致
vim /etc/sysconfig/clock
ZONE="Asia/Shanghai"
UTC=false
ARC=false
修改时区为上海
timedatectl set-timezone Asia/Shanghai
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
进行时间同步 (外网:ntp1.aliyun.com 内网:ntp7.cloud.aliyuncs.com )
ntpdate ntp1.aliyun.com
添加时间同步定时任务
crontab -e
00 01 * * * /usr/sbin/ntpdate ntp1.aliyun.com >/dev/null 2>&1 ; /sbin/hwclock -w
修改字符编码,解决部分中文乱码问题
echo 'LANG="en_US.UTF-8"
SUPPORTED="zh_CN.GB18030:zh_CN:zh:en_US.UTF-8:en_US:en"
SYSFONT="latarcyrheb-sun16"' > /etc/locale.conf
配置密码策略,设置密码强度
设置密码长度不低于8位
authconfig --passminlen=8 --update
设置密码中连续字符最大数目3个
authconfig --passmaxclassrepeat=3 --update
密码需包含小写,大写,数字,特殊字符
authconfig --enablereqlower --update
authconfig --enablerequpper --update
authconfig --enablereqdigit --update
authconfig --enablereqother --update
检查是否配置成功
cat /etc/security/pwquality.conf
设置ssh登录超时
设置20分钟登录无操作自动退出,服务器每120秒心跳包测试客户端,三次不成功断开
echo 'export TMOUT=1200' >> /etc/profile
source /etc/profile
echo 'ClientAliveInterval 120
ClientAliveCountMax 3' >> /etc/ssh/sshd_config
systemctl restart sshd
设置用户登录记录
echo '#!/bin/bash
loginFile="/var/log/sshd/sshlogin.log"
user=$USER
ip=${SSH_CLIENT%% *}
#if [ "$user" != "root" ] || [ "$ip" != "192.168.2.88" ]
#then
echo "LoginUser:"$user"--IP:"$ip"--LoginTime:"`date "+%Y-%m-%d %H:%M:%S"` >> "$loginFile";
#fi' >> /etc/ssh/sshrc
mkdir /var/log/sshd
touch /var/log/sshd/sshlogin.log
chmod -R 777 /var/log/sshd
chmod +x /etc/ssh/sshrc
查看历史操作记录,并加时间戳
echo 'export HISTTIMEFORMAT="%F %T `whoami` "' >> /etc/profile
source /etc/profile
系统启动配置文件赋权
chmod +x /etc/rc.d/rc.local
SSH安全策略,为了防止暴力破解,最好是修改22默认端口号,并指定监听服务的具体IP地址,以提高在网络中的隐蔽性(更改ssh端口的操作出现失误,可能会造成无法远程服务器,最好是做一个快照或者做好telnet服务)
开机自启:
systemctl enable sshd.service
方式1:配置文件修改,SSH端口
vi /etc/ssh/sshd_config
找到#Port 22,在下面添加两行,修改端口时候最好挑10000~65535之间的端口号,10000以下容易被系统或一些特殊软件占用,并且在准备新端口时应该检查一下该端口有没有被其他程序占用
Port 22 #添加22默认端口,防止设置失败导致不能连接
Port 54213 #54213为你的新端口
保存退出,并且重启ssh服务。
systemctl restart sshd.service
方式2:iptables方式修改SSH端口,如果你的防火墙和Selinux是关闭的就无需进行以下配置
防火墙添加新端口
firewall-cmd --zone=public --add-port=54213/tcp --permanent
重新加载防火墙配置
firewall-cmd --reload
查看新端口状态
firewall-cmd --zone=public --query-port=54213/tcp
方式3、添加SELinux策略来修改SSH端口,如果你的防火墙和Selinux是关闭的就无需进行以下配置
防火墙添加新端口
yum -y install policycoreutils-python provides semanage
向 SELinux 中添加 ssh 端口:
semanage port -a -t ssh_port_t -p tcp 54213
验证 ssh 端口是否添加成功:
semanage port -l | grep ssh
以上方法,最后都要重启ssh服务,如果使用新端口能ssh上就把原来的22端口注释掉,再重启。
systemctl restart sshd.service
优化tcp连接数
可打开的文件描述符的最大数,用户可用的最大进程数量
cat >> /etc/security/limits.conf
* soft nproc 65536
* hard nproc 65536
* soft nofile 65536
* hard nofile 65536