此文档还未完善,谨慎使用
!/bin/bash
Kernel_optimization(){ net.ipv6.conf.all.disable_ipv6 = 1 net.ipv4.icmp_echo_ignore_broadcasts = 1 net.ipv4.icmp_ignore_bogus_error_responses = 1 net.ipv4.conf.all.rp_filter = 1 net.ipv4.conf.all.accept_source_route = 0 kernel.sysrq = 0 kernel.core_uses_pid = 1 net.ipv4.tcp_syncookies = 1 kernel.msgmnb = 65536 kernel.shmmax = 68719476736 net.ipv4.tcp_max_tw_buckets = 6000 net.core.netdev_max_backlog = 262144 net.ipv4.tcp_max_orphans = 3276800 net.ipv4.tcp_max_syn_backlog = 262144 net.ipv4.tcp_synack_retries = 1 net.ipv4.tcp_syn_retries = 1 net.ipv4.tcp_tw_recycle = 1 net.ipv4.tcp_tw_reuse = 1 net.ipv4.tcp_keepalive_time = 30 net.ipv4.ip_local_port_range = 1024 65000 EOF sed -i "s#keepcache=0#keepcache=1#g" /etc/yum.conf yum install lrzsz ntpdate sysstat net-tools wget vim bash-completion dos2unix unzip tcpdump telnet lsof -y &>/dev/null SELINUX_status=2}') \cp /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup && wget -O /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo &>/dev/null cp /etc/ssh/sshd_config{,. sed -i 's/^GSSAPIAuthentication yes2}' /etc/ssh/sshd_config sed -i 's/#UseDNS yes/UseDNS no/' /etc/ssh/sshd_config systemctl restart sshd tmout=300 histfilesize=100 histsize=100 sed -i "/export HISTTIMEFORMAT=/d" /etc/profile && echo 'export HISTTIMEFORMAT="%Y-%m-%d %H:%M:%S"' >>/etc/profile Kernel_optimization kernel_optimization_ver={kernel_optimization_ver} -eq 0 ];then sed -i '//usr/sbin/ntpdate/d' /var/spool/cron/root && echo "*/5 * * * * /usr/sbin/ntpdate cn.pool.ntp.org > /dev/null 2>&1" >/var/spool/cron/root [ -d /tmp/garbage_dir ] || mkdir /tmp/garbage_dir -p cat >/root/.bashrc< alias rm='rm -i' export HISTFILESIZE=100000 if [ -f /etc/bashrc ]; then
cat >/etc/sysctl.conf<kernel_flag
关闭ipv6
net.ipv6.conf.default.disable_ipv6 = 1避免放大攻击
开启恶意icmp错误消息保护
关闭路由转发
net.ipv4.ip_forward = 0
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.default.send_redirects = 0
开启反向路径过滤
net.ipv4.conf.default.rp_filter = 1处理无源路由的包
net.ipv4.conf.default.accept_source_route = 0关闭sysrq功能
core文件名中添加pid作为扩展名
开启SYN洪水攻击保护
修改消息队列长度
kernel.msgmax = 65536设置最大内存共享段大小bytes
kernel.shmall = 4294967296timewait的数量,默认180000
net.ipv4.tcp_sack = 1
net.ipv4.tcp_window_scaling = 1
net.ipv4.tcp_rmem = 4096 87380 4194304
net.ipv4.tcp_wmem = 4096 16384 4194304
net.core.wmem_default = 8388608
net.core.rmem_default = 8388608
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216每个网络接口接收数据包的速率比内核处理这些包的速率快时,允许送到队列的数据包的最大数目
限制仅仅是为了防止简单的DoS 攻击
未收到客户端确认信息的连接请求的最大值
net.ipv4.tcp_timestamps = 0内核放弃建立连接之前发送SYNACK 包的数量
内核放弃建立连接之前发送SYN 包的数量
启用timewait 快速回收
开启重用。允许将TIME-WAIT sockets 重新用于新的TCP 连接
net.ipv4.tcp_mem = 94500000 915000000 927000000
net.ipv4.tcp_fin_timeout = 1当keepalive 起用的时候,TCP 发送keepalive 消息的频度。缺省是2 小时
允许系统打开的端口范围
修改防火墙表大小,默认65536
net.netfilter.nf_conntrack_max=655350
net.netfilter.nf_conntrack_tcp_timeout_established=1200
确保无人能修改路由表
net.ipv4.conf.all.accept_redirects = 0
net.ipv4.conf.default.accept_redirects = 0
net.ipv4.conf.all.secure_redirects = 0
net.ipv4.conf.default.secure_redirects = 0
}保留yum安装的软件包
安装常用软件
if [ $? -ne 0 ];then
echo "常用软件安装失败!!!"
fi关闭selinux
sed -i 's/=enforcing/=disabled/' /etc/selinux/config
echo "SELINUX状态为 ${SELINUX_status}"修改yum源
备份/etc/ssh/sshd_conf
date +%F
.bak}
[ -e "sshd_config{,.date +%F
.bak}" ] || echo "sshd_config{,.date +%F
.bak} 备份成功"不允许基于GSSAPI的用户认证
不允许sshd对远程主机名进行反向解析
awk '/UseDNS/{print "sshd对远程主机名进行反向解析",$2}' /etc/ssh/sshd_config重启sshd服务
if [ $? -ne 0 ];then
echo "sshd 未正常启动"
else
echo "sshd 已重启"
fi历史记录及登录超时环境变量设置
设置闲置超时时间为300s
sed -i "/export TMOUT=/d" /etc/profile && echo "export TMOUT=${tmout}" >>/etc/profile设置历史记录文件的命令数量为100
sed -i "/export HISTFILESIZE=/d" /etc/profile && echo "export HISTFILESIZE=${histfilesize}" >>/etc/profile设置命令行的历史记录数量
sed -i "/export HISTSIZE=/d" /etc/profile && echo "export HISTSIZE=${histsize}" >>/etc/profile格式化输出历史记录(以年月日分时秒的格式输出)
source /etc/profile调整文件描述符
高并发的业务Linux服务器,调整系统文件描述符为65535(按照情况配置)
echo '* - nofile 65535' >>/etc/security/limits.conf
内核优化
echo "内核优化配置失败"
fi开启计划任务时间同步
创建回收站
禁用危险命令 在非交互式模式下alias扩展功能默认是关闭的,需要source一下脚本
.bashrc
User specific aliases and functions
alias cp='cp -i'
alias mv='mv -i'
alias rm=' ''echo -e "\033[40;32;1m【运维温馨提示】\033[0m \033[40;31;1;5m请不要轻易运行危险命令!如需删除无用文件或目录请将文件或目录移动至/tmp/rm\033[0m \033[40;33;1m rm \033[0m "'''
alias shutdown='''echo -e "\033[40;32;1m【运维温馨提示】\033[0m \033[40;31;1;5m请不要轻易运行危险命令!\033[0m \033[40;33;1m shutdown \033[0m"'''
alias init='''echo -e "\033[40;32;1m【运维温馨提示】\033[0m \033[40;31;1;5m请不要轻易运行危险命令!\033[0m \033[40;33;1m init \033[0m"'''
alias reboot='''echo -e "\033[40;32;1m【运维温馨提示】\033[0m \033[40;31;1;5m请不要轻易运行危险命令!\033[0m \033[40;33;1m reboot \033[0m"'''
alias halt='''echo -e "\033[40;32;1m【运维温馨提示】\033[0m \033[40;31;1;5m请不要轻易运行危险命令!\033[0m \033[40;33;1m halt \033[0m"'''
alias poweroff='''echo -e "\033[40;32;1m【运维温馨提示】\033[0m \033[40;31;1;5m请不要轻易运行危险命令!\033[0m \033[40;33;1m poweroff \033[0m"'''
alias pkill='''echo -e "\033[40;32;1m【运维温馨提示】\033[0m \033[40;31;1;5m请不要轻易运行危险命令!\033[0m \033[40;33;1m pkill \033[0m"'''
alias killall='''echo -e "\033[40;32;1m【运维温馨提示】\033[0m \033[40;31;1;5m请不要轻易运行危险命令!\033[0m \033[40;33;1m killall \033[0m"'''
alias kill='''echo -e "\033[40;32;1m【运维温馨提示】\033[0m \033[40;31;1;5m请不要轻易运行危险命令!\033[0m \033[40;33;1m kill \033[0m"'''change configuration
export HISTTIMEFORMAT="%Y-%m-%d %H:%M:%S " ##(history 显示时间)
export PS1='[\e[32;1m][[\e[36;1m]\u@\h [\e[33;1m]\w [\e[32;1m]\t [\e[34;1m]##[\e[32;1m]]\$ [\e[0;0m' ###(显示主机名 目录 时间)
ulimit -c unlimited
ulimit -HSn 1024000
echo "%e.core" > /proc/sys/kernel/core_patternSource global definitions
. /etc/bashrc
fi
EOF