总结
配置网卡名
01、vi /etc/sysconfig/grub
02、GRUB_CMDLINE_LINUX="crashkernel=128M rhgb net.ifnames=0 biosdevname=0 quiet"
规范功能目录创建存储软件目录、存储脚本目录
01、mkdir -p /server/tools
02、mkdir -p /server/scripts
配置hosts解析信息设置本地服务主机名称与IP地址映射关系。
01、cat /etc/hosts
02、127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
03、::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
04、cat >>/etc/hosts<<EOF
05、172.16.1.5 lb01
06、172.16.1.6 lb02
07、172.16.1.7 web01
08、172.16.1.8 web02
09、172.16.1.9 sweb01
10、172.16.1.10 sweb02
11、172.16.1.31 nfs01
12、172.16.1.41 backup
13、172.16.1.51 db01 db01.etiantian.org
14、172.16.1.61 m01
15、EOF
修改主机名称
01、hostnamectl set-hostname backup
更新yum源
01、curl -s -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
更新EPEL源
01、curl -s -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
优化安全设置
01、关闭SELinux:sed -i 's#SELINUX=.*#SELINUX=disabled#g' /etc/selinux/config
02、查看修改SELinux的状态:grep SELINUX=disabled /etc/selinux/config
03、临时关闭SELinux:setenforce 0
04、查看SELinux状态:getenforce
05、关闭firewalld防火墙服务: systemctl stop firewalld
06、禁止firewalld防火墙开机自启动:systemctl disable firewalld
07、查看firewalld关闭的状态:systemctl status firewalld
精简开机启动程序
01、关闭多余服务:systemctl list-unit-files |grep enable|egrep -v "sshd.service|crond.service|sysstat|rsyslog|^NetworkManager.service|irqbalance.service"|awk '{print "systemctl disable",$1}'|bash
02、查看默认开启的服务:systemctl list-unit-files |grep enabled
03、重启:shutdown -r now
04、查看系统开启的端口:netstat -lnput
普通用户提权操作(可选优化)
01、创建普通用户:useradd oldboy;echo 123456|passwd --stdin odboy
02、\cp /etc/sudoers /etc/sudoers.ori
03、echo "oldboy ALL=(ALL) NOPASSWD: ALL " >>/etc/sudoers
04、tail -1 /etc/sudoers
05、visudo -c
设置系统字符集
01、cat /etc/locale.conf
02、LANG="en_US.UTF-8"
03、cp /etc/locale.conf{,.ori}
04、localectl set-locale LANG="zh_CN.UTF-8"
05、cat /etc/locale.conf
时间同步设置
01、yum install ntpdate –y
02、/usr/sbin/ntpdate ntp3.aliyun.com
03、echo '#crond-id-001:time sync by oldboy' >>/var/spool/cron/root
04、echo "*/5 * * * * /usr/sbin/ntpdate ntp3.aliyun.com >/dev/null 2>&1">>/var/spool/cron/root
05、crontab -l
提升命令行操作安全性(可选优化,测试环境下建议不做)
01、echo 'export TMOUT=300' >>/etc/profile
02、echo 'export HISTSIZE=5' >>/etc/profile
03、echo 'export HISTFILESIZE=5' >>/etc/profile
04、tail -3 /etc/profile
05、. /etc/profile
加大文件描述符
01、echo '* - nofile 65535' >>/etc/security/limits.conf
02、tail -1 /etc/security/limits.conf
03、ulimit -SHn 65535
04、ulimit -n
优化系统内核
01、cat >>/etc/sysctl.conf<<EOF
02、net.ipv4.tcp_fin_timeout = 2
03、net.ipv4.tcp_tw_reuse = 1
04、net.ipv4.tcp_tw_recycle = 1
05、net.ipv4.tcp_syncookies = 1
06、net.ipv4.tcp_keepalive_time = 600
07、net.ipv4.ip_local_port_range = 4000 65000
08、net.ipv4.tcp_max_syn_backlog = 16384
09、net.ipv4.tcp_max_tw_buckets = 36000
10、net.ipv4.route.gc_timeout = 100
11、net.ipv4.tcp_syn_retries = 1
12、net.ipv4.tcp_synack_retries = 1
13、net.core.somaxconn = 16384
14、net.core.netdev_max_backlog = 16384
15、net.ipv4.tcp_max_orphans = 16384
16、对iptables防火墙的优化,防火墙不开会提示,可以忽略。
17、net.nf_conntrack_max = 25000000
18、net.netfilter.nf_conntrack_max = 25000000
19、net.netfilter.nf_conntrack_tcp_timeout_established = 180
20、net.netfilter.nf_conntrack_tcp_timeout_time_wait = 120
21、net.netfilter.nf_conntrack_tcp_timeout_close_wait = 60
22、net.netfilter.nf_conntrack_tcp_timeout_fin_wait = 120
23、net.core.wmem_default = 8388608
24、net.core.rmem_default = 8388608
25、net.core.wmem_max = 16777216
26、net.core.rmem_max = 16777216
27、EOF
28、sysctl -p
安装系统常用软件
01、yum install tree nmap dos2unix lrzsz nc lsof wget tcpdump htop iftop iotop sysstat nethogs -y
02、yum install psmisc net-tools bash-completion vim-enhanced -y
优化SSH远程连接效率
01、\cp /etc/ssh/sshd_config{,.ori}
02、sed -i -e "17s/.*/Port 52113/g" /etc/ssh/sshd_config
03、sed -i -e "19s/.*/ListenAddress 172.16.1.41/g" /etc/ssh/sshd_config(设置好VPN再设置)
04、sed -i -e "38s/.*/PermitRootLogin no/g" /etc/ssh/sshd_config
05、sed -i -e "64s/.*/PermitEmptyPasswords no/g" /etc/ssh/sshd_config
06、grep -Evn "^$|#" /etc/ssh/sshd_config
锁定关键系统文件
01、chattr +i /etc/inittab /etc/fstab /etc/sudoers
02、lsattr /etc/passwd /etc/shadow /etc/group /etc/inittab /etc/fstab /etc/sudoers
03、mv /usr/bin/chattr /opt/oldboy
04、cp /usr/bin/xargs /usr/bin/chattr
清除登录信息中的,版本号、内核信息
01、> /etc/issue
02、> /etc/issue.net
GRUB增加密码
01、grub2-setpassword
02、cat /boot/grub2/user.cfg
禁止用户ping服务器IP地址
01、echo "net.ipv4.icmp_echo_ignore_all=1" >> /etc/sysctl.conf
02、sysctl -p
优化系统提示符(可根据个人喜好选择)
01、echo "PS1='\[\e[32;1m\][\u@\h \W]\\$ \[\e[0m\]'" >>/etc/profile
02、source /etc/profile
定义系统别名
01、echo "alias grep='grep --color=auto'" >>/etc/profile
02、echo "alias ll='ls -l --color=auto --time-style=long-iso'" >>/etc/profile
03、source /etc/profile
修改yum.conf文件配置信息
01、 vim /etc/yum.conf
[main] cachedir=/var/cache/yum/$basearch/$releasever keepcache=1 debuglevel=2 exactarch=1 obsoletes=1 gpgcheck=1 plugins=1 installonly_limit=5 bugtracker_url=http://bugs.centos.org/set_project.php?project_id=19&ref=http://bugs.centos.org/bug_report_page.php?category=yum distroverpkg=centos-release
自动快速配置主机名和IP地址
01、cat /server/scripts/modify.sh
#!/bin/sh
if [ $# -ne 2 ];then
echo "/bin/sh $0 hostname PartIP"
exit 1
fi
hostnamectl set-hostname $1
sed -i "/IPADDR/s#200#$2#g" /etc/sysconfig/network-scripts/ifcfg-eth0
sed -i "/IPADDR/s#200#$2#g" /etc/sysconfig/network-scripts/ifcfg-eth1
systemctl restart network
exit
02、sh modify.sh oldboy 7
集群节点模板机统一优化总结
01、不用root登录管理系统,而以普通用户登录通过sudo授权管理
02、更改默认的远程连接SSH服务端口,禁止root用户远程连接,甚至更改SSH服务只监听内网IP(本章未优化端口、用户以及服务监听的IP
03、定时自动更新服务器的时间,使其和互联网时间同步
04、配置yum更新源,从国内更新源下载安装软件包
05、关闭SELinux及Iptables/firewalld。在工作场景中,如果有外部IP一般要打开防火墙,高并发高流量服务器可能无法开启
06、调整文件描述符的大小,进程及文件的打开都会消耗文件描述符
07、精简并保留必要的开机自启动服务(如crond、sshd、Network、rsyslog、sysstat、irqbalance)
08、Linux内核参数优化/etc/sysctl.conf,执行sysctl-p生效
09、更改系统字符集为“zh_CN.UTF-8”,使其支持中文,防止出现乱码
10、锁定关键系统文件如/etc/passwd、/etc/shadow、/etc/group、/etc/gshadow、/etc/inittab,处理以上内容后把chattr、lsattr改名为oldboy并转移,这样就安全多了
11、清空/etc/issue、/etc/issue.net,去除系统及内核版本登录前的屏幕显示
12、清除多余的系统虚拟用户账号(不是在严苛环境下不做)
13、为GRUB引导菜单加密码
14、禁止主机被ping
15、打补丁升级系统及有已知漏洞的软件
16、全网域名和自身内网IP实现hosts对应解析
17、安装系统默认没有的常用工具
18、提升命令行操作安全性(超时和历史记录)
方法
01、
02、
03、
04、
05、
06、
07、
08、
09、
10、
浙公网安备 33010602011771号