码迷,mamicode.com
首页 > 其他好文 > 详细

centos6\centos7基本优化

时间:2020-04-13 15:32:36      阅读:84      评论:0      收藏:0      [点我收藏+]

标签:not run   sysctl   时间   ntp   ase   time   lib   emd   允许   

优化项
1、  关闭selinux、关闭firewalld(centos7)/iptables(centos6)
2、  修改字符集
3、  精简开机启动项
4、  修改最大文件描述符
5、  修改yum源及安装常用工具
6、  优化系统内核
7、  优化ssh登录速度
8、  禁用ctrl+alt+del重启
9、  设置时间同步
10、  history优化

1、 关闭selinux、关闭防火墙


a/关闭selinux

[root@gitlab ~]# sed  -i "s#SELINUX=enforcing#SELINUX=disabled#g" /etc/sysconfig/selinux
[root@gitlab ~]# setenforce 0

查看状态

[root@gitlab ~]# grep SELINUX=disabled /etc/sysconfig/selinux
SELINUX=disabled
[root@gitlab ~]# getenforce
Permissive

b/关闭防火墙

centos 6

[root@slb ~]# /etc/init.d/iptables stop
iptables:将链设置为政策 ACCEPT:filter                      [确定]
iptables:清除防火墙规则:                                            [确定]
iptables:正在卸载模块:                                                [确定]
[root@slb ~]# chkconfig iptables off
查看状态
[root@slb ~]# /etc/init.d/iptables status
iptables:未运行防火墙。
[root@slb ~]# chkconfig --list | grep iptables | awk ‘{print $1,$5}‘
iptables 3:关闭

centos 7

[root@gitlab ~]# systemctl stop firewalld.service
[root@gitlab ~]# systemctl disable firewalld.service
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
查看状态
[root@gitlab ~]# firewall-cmd  --state
not running
[root@gitlab ~]# systemctl list-unit-files | grep firewalld
firewalld.service                             disabled

2、 修改字符集


centos 6

cat > /etc/sysconfig/i18n <<EOF
LANG="zh_CN.UTF-8"
#LANG="en_US.UTF-8"
SYSFONT="latarcyrheb-sun16"
EOF
[root@slb ~]# source /etc/sysconfig/i18n
查看状态
[root@slb ~]# echo $LANG
zh_CN.UTF-8

centos 7

cat > /etc/locale.conf <<EOF
LANG="zh_CN.UTF-8"
#LANG="en_US.UTF-8"
SYSFONT="latarcyrheb-sun16"
EOF
[root@gitlab ~]# source /etc/locale.conf
查看状态
[root@gitlab ~]# echo $LANG
zh_CN.UTF-8

3、 精简开机启动项


centos 6

查看除crond、sshd、network、rsyslog的开机启动项
[root@slb ~]# /sbin/chkconfig --list | grep 3:启用 | grep -Ev "crond|sshd|network|rsyslog" |awk ‘{print $1}‘
服务1
服务2
....
关闭上述查出的启动项,如:
[root@slb ~]# chkconfig sysstat off
用以上命令依次关闭,查看状态没有返回内容表示关闭成功
[root@slb ~]# /sbin/chkconfig --list | grep 3:启用 | grep -Ev "crond|sshd|network|rsyslog" |awk ‘{print $1}‘

centos 7

可以关闭一下三个开机启动服务
[root@gitlab ~]# systemctl disable auditd.service
[root@gitlab ~]# systemctl disable postfix.service
[root@gitlab ~]# systemctl disable dbus-org.freedesktop.NetworkManager.service
查看状态
[root@gitlab ~]# systemctl list-unit-files | grep -E "auditd|postfix|dbus-org\.freedesktop\.NetworkManager"

4、 修改最大文件描述符


[root@gitlab ~]# echo ‘* - nofile 65535‘>/etc/security/limits.conf
[root@gitlab ~]# ulimit -SHn 65535
查看状态
[root@gitlab ~]# cat /etc/security/limits.conf
* - nofile 65535
[root@gitlab ~]# ulimit -Sn ; ulimit -Hn
65535
65535

5、 修改yum源及安装常用工具


centos 6

[root@slb ~] yum install wget -y
[root@slb ~] cd /etc/yum.repos.d/
[root@slb ~] \cp CentOS-Base.repo CentOS-Base.repo.$(date +%F)
[root@slb ~] wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo
[root@slb ~] yum clean all
[root@slb ~] yum makecache
安装常用工具
[root@slb ~] yum -y install lsof lrzsz vim lrzsz tree nmap nc sysstat

centos 7

[root@gitlab yum.repos.d]# yum install wget -y
[root@gitlab yum.repos.d]# cd /etc/yum.repos.d/
[root@gitlab yum.repos.d]# \cp CentOS-Base.repo CentOS-Base.repo.$(date +%F)
[root@gitlab yum.repos.d]# wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
[root@gitlab yum.repos.d]# yum clean all
[root@gitlab yum.repos.d]# yum makecache
安装常用工具
[root@gitlab yum.repos.d]# yum -y install lsof lrzsz vim lrzsz tree nmap nc sysstat

6、 优化系统内核


cat >>/etc/sysctl.conf<<EOF
net.ipv4.tcp_fin_timeout = 2
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 0
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_keepalive_time = 600
net.ipv4.ip_local_port_range = 4000 65000
net.ipv4.tcp_max_syn_backlog = 16384
net.ipv4.tcp_max_tw_buckets = 36000
net.ipv4.route.gc_timeout = 100
net.ipv4.tcp_syn_retries = 1
net.ipv4.tcp_synack_retries = 1
net.core.somaxconn = 16384
net.core.netdev_max_backlog = 16384
net.ipv4.tcp_max_orphans = 16384
net.netfilter.nf_conntrack_max = 25000000
net.netfilter.nf_conntrack_tcp_timeout_established = 180
net.netfilter.nf_conntrack_tcp_timeout_time_wait = 120
net.netfilter.nf_conntrack_tcp_timeout_close_wait = 60
net.netfilter.nf_conntrack_tcp_timeout_fin_wait = 120
EOF
[root@gitlab sysctl.d]# sysctl -p

配置说明:

 #表示套接字由本端要求关闭,这个参数决定了它保持在FIN-wAIT-2状态的时间,默认值是60秒,建议调整为2,该参数对应系统路径为:/proc/sys/net/ipv4/tcp_fin_timeout 60
net.ipv4.tcp_fin_timeout = 2
#表示开启重用,允许TIME-wAIT sockets重新用于新的TCP链接,默认值为0,表示关闭,该参数对应系统路径为:/proc/sys/net/ipv4/tcp_tw_reuse 0
net.ipv4.tcp_tw_reuse = 1
#表示开启TCP链接中TIME_WAIT sockets的快速回收,该参数对应系统路径为:/proc/sys/net/ipv4/tcp_tw_recycle,默认为0 表示关闭,不建议开启,因为nat网络问题
net.ipv4.tcp_tw_recycle = 0
#reuse和recycle这俩个参数是为防止生产环境下web,squid等业务服务器time_wait网络状态数量过多设置的
#表示开启SYN Cookies功能,当出现SYN等待队列溢出时,启用Cookies来处理,可防范少量SYN***,该参数对应系统路径为:/proc/sys/net/ipv4/tcp_syscookies,默认为1,表示开启
net.ipv4.tcp_syncookies = 1
#表示当keepalive启用时,TCP发送keepalive消息的频度,默认是2小时,建议更改为10分钟,该参数对应系统路径为:/proc/sys/net/ipv4/tcp_keepalive_time,默认为7200秒
net.ipv4.tcp_keepalive_time =600
#该选项用来设定允许系统打开的端口范围,即用于向外链接的端口范围,该参数对应系统路径为:/proc/sys/net/ipv4/ip_local_port_range 默认,32768 60999
net.ipv4.ip_local_port_range = 4000 65000
#表示SYN队列的长度,默认为1024,建议加大队列的长度,为8182或更多,这样可以容纳更多等待链接的网络连接数,该参数为服务器端用于记录那些尚未收到客户端确认信息的链接请求的最大值,该参数对应系统路径为:/proc/sys/net/ipv4/tcp_max_syn_backlog
net.ipv4.tcp_max_syn_backlog = 16384
#表示系统同时保持TIME_WAIT套接字的最大数量,如果超过这个数值,TIME_WAIT套接字将立刻被清除并打印警告信息,默认为5000,对于Aapache,Nginx等服务器来说可以将其调低一点,如改为5000-40000,不用业务的服务器也可以给大一点,比如LVS,Squid,该参数对应系统路径为:/proc/sys/net/ipv4/tcp_max_tw_buckets
net.ipv4.tcp_max_tw_buckets = 36000
#路由缓存刷新频率,当一个路由失败后多长时间跳到另一个路由,默认是300
net.ipv4.route.gc_timeout = 100
#表示内核放弃建立链接之前发送SYN包的数量,该参数对应系统路径为:/proc/sys/net/ipv4/tcp_syn_retries,默认是6
net.ipv4.tcp_syn_retries = 1
#参数的值决定了内核放弃链接之前发送SYN+ACK包的数量,该参数对应系统路径为:/proc/sys/net/ipv4/tcp_synack_retries,默认是2
net.ipv4.tcp_synack_retries = 1
#该选项默认值是128,这个参数用于调节系统同时发起的TCP连接数,在高并发的请求中,默认的值可能会导致链接超时或重传,因此,需要结合并发请求数来调节此值,该参数对应系统路径为:/proc/sys/net/ipv4/somaxconn 128   # 默认没有这个配置,需要自己生成
net.core.somaxconn = 16384
#表示当每个网络接口接收数据包的速率比内核处理这些包的速率快时,允许发送到队列的数据包最大数,该参数对应系统路径为:/proc/sys/net/ipv4/netdev_max_backlog,默认值为1000
net.core.netdev_max_backlog = 16384
#用于设定系统中最多有多少个TCP套接字不被关联到任何一个用户文件句柄上,如果超过这个数值,孤立链接将立即被复位并打印出警号信息,这个限制只是为了防止简单的DoS***,不能过分依靠这个限制甚至人为减小这个值,更多的情况是增加这个值,默认是4096,建议该值修改为2000,该参数对应系统路径为:/proc/sys/net/ipv4/tcp_max_orphans
net.ipv4.tcp_max_orphans = 16384
#以下参数是对iptables防火墙的优化,防火墙不开会有提示,可以忽略不理。
net.ipv4.ip_conntrack_max = 25000000
net.ipv4.netfilter.ip_conntrack_max = 25000000
net.ipv4.netfilter.ip_conntrack_tcp_timeout_established = 180
net.ipv4.netfilter.ip_conntrack_tcp_timeout_time_wait = 120
net.ipv4.netfilter.ip_conntrack_tcp_timeout_close_wait = 60
net.ipv4.netfilter.ip_conntrack_tcp_timeout_fin_wait = 120

7、 优化ssh登录速度


[root@gitlab sysctl.d]# sed -i ‘s#^GSSAPIAuthentication yes$#GSSAPIAuthentication no#g‘ /etc/ssh/sshd_config
[root@gitlab sysctl.d]# sed -i ‘s/#UseDNS yes/UseDNS no/g‘ /etc/ssh/sshd_config
centos 6重启ssh
[root@gitlab sysctl.d]# /etc/init.d/sshd restart
centos 7重启ssh
[root@gitlab sysctl.d]# systemctl restart sshd.service

8、 禁用ctrl+alt+del重启


centos 6
[root@slb ~]# sed -i ‘s#exec /sbin/shutdown -r now#\#exec /sbin/shutdown -r now#‘ /etc/init/control-alt-delete.conf
centos 7
[root@gitlab ~]# rm -rf /usr/lib/systemd/system/ctrl-alt-del.target

9、 设置时间同步


[root@gitlab ~]# yum -y install ntpdate
[root@gitlab ~]# ntpdate ntp.aliyun.com
13 Apr 03:02:10 ntpdate[1673]: adjust time server 203.107.6.88 offset 0.007882 sec

10、 history优化


优化内容:

a、让history命令记录用户、用户IP、时间
b、将history记录到/var/log/message中

cat >> /etc/profile <<‘EOF‘
#设置history格式
export HISTTIMEFORMAT="[%Y-%m-%d %H:%M:%S] [`whoami`] [`who am i|awk ‘{print $NF}‘|sed -r ‘s#[()]##g‘`]: "
#记录shell执行的每一条命令
export PROMPT_COMMAND=‘if [ -z "$OLD_PWD" ];then
    export OLD_PWD=$PWD;
fi;
if [ ! -z "$LAST_CMD" ] && [ "$(history 1)" != "$LAST_CMD" ]; then
    logger -t `whoami`_shell_dir "[$OLD_PWD]$(history 1)";
fi;
export LAST_CMD="$(history 1)";
export OLD_PWD=$PWD;‘
EOF
[root@slb home]# source /etc/profile

centos6\centos7基本优化

标签:not run   sysctl   时间   ntp   ase   time   lib   emd   允许   

原文地址:https://blog.51cto.com/14623268/2486740

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!