介绍
linux相关优化
虚拟机优化
用于第一次安装完linux后,执行,关闭防火墙和安装常用工具。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| cp -a /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak wget -O /etc/yum.repos.d/CentOS-Base.repo https://repo.huaweicloud.com/repository/conf/CentOS-7-reg.repo yum clean all && yum makecache echo "export PS1='\[\e[1;32m\][\u@\h \W]\\$ \[\e[0m\]'" >> /etc/bashrc echo "export TIME_STYLE='+%Y-%m-%d %H:%M:%S'" >> /etc/bashrc echo "export HISTTIMEFORMAT='%F %T '" >> /etc/profile sed -i "s/^#UseDNS.*/UseDNS no/g" /etc/ssh/sshd_config sed -i "s/SELINUX=.*/SELINUX=disabled/g" /etc/selinux/config systemctl stop firewalld && systemctl disable firewalld && rpm -e --nodeps firewalld systemctl disable chronyd systemctl stop chronyd yum -y install iptables-services systemctl start iptables && iptables -F && service iptables save yum -y install lrzsz net-tools vim psmisc bash-completion kernel-tools tree wget dos2unix ntpdate unzip tcpdump cat >> ~/.vimrc <<EOF set ts=4 set expandtab set paste EOF
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| rm -rf /etc/yum.repos.d/* curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-8.repo yum clean all && yum makecache echo "export PS1='\[\e[1;32m\][\u@\h \W]\\$ \[\e[0m\]'" >> /etc/bashrc echo "export TIME_STYLE='+%Y-%m-%d %H:%M:%S'" >> /etc/bashrc echo "export HISTTIMEFORMAT='%F %T '" >> /etc/profile sed -i "s/^#UseDNS.*/UseDNS no/g" /etc/ssh/sshd_config sed -i "s/SELINUX=.*/SELINUX=disabled/g" /etc/selinux/config sed -i "s/pool 2.centos.pool.ntp.org iburst/#pool 2.centos.pool.ntp.org iburst/g" /etc/chrony.conf sed -i '4a server ntp.aliyun.com iburst' /etc/chrony.conf systemctl restart chronyd.service systemctl enable chronyd.service chronyc sources -v timedatectl set-timezone Asia/Shanghai systemctl stop firewalld && systemctl disable firewalld && rpm -e --nodeps firewalld yum -y install iptables-services systemctl start iptables && iptables -F && service iptables save yum -y install lrzsz net-tools vim psmisc bash-completion kernel-tools tree wget dos2unix unzip tcpdump cat >> ~/.vimrc <<EOF set ts=4 set expandtab set paste EOF
|
内核优化
Linux服务器内核参数优化,主要是指在Linux系统中针对业务服务应用而进行的系统内核参数调整,优化并无一定的标准.下面是生产环境下Linux常见的内核优化:
vim /etc/sysctl.conf
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
| net.ipv6.conf.all.disable_ipv6 = 1 net.ipv6.conf.default.disable_ipv6 = 1
net.ipv4.icmp_echo_ignore_broadcasts = 1
net.ipv4.icmp_ignore_bogus_error_responses = 1
net.ipv4.ip_forward = 0 net.ipv4.conf.all.send_redirects = 0 net.ipv4.conf.default.send_redirects = 0
net.ipv4.conf.all.rp_filter = 1 net.ipv4.conf.default.rp_filter = 1
net.ipv4.conf.all.accept_source_route = 0 net.ipv4.conf.default.accept_source_route = 0
kernel.sysrq = 0
kernel.core_uses_pid = 1
net.ipv4.tcp_syncookies = 1
kernel.msgmnb = 65536 kernel.msgmax = 65536
kernel.shmmax = 68719476736 kernel.shmall = 4294967296
net.ipv4.tcp_max_tw_buckets = 6000 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
net.core.netdev_max_backlog = 262144
net.ipv4.tcp_max_orphans = 3276800
net.ipv4.tcp_max_syn_backlog = 262144 net.ipv4.tcp_timestamps = 0
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_mem = 94500000 915000000 927000000 net.ipv4.tcp_fin_timeout = 1
net.ipv4.tcp_keepalive_time = 30
net.ipv4.ip_local_port_range = 1024 65000
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
kernel.pid_max = 32768
fs.file-max = 6553560
|
ulimit(进程占用资源优化)
配置文件位置:vim /etc/security/limits.conf
centos6:/etc/security/limits.d/90-nproc.conf
centos7:/etc/security/limits.d/20-nproc.conf
ulimit -a
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| core file size //限制内核文件的大小限制 data seg size //最大数据大小限制 scheduling priority //调度优先级,一般根据nice设置 file size //最大文件大小限制 pending signals //信号可以被挂起的最大数,这个值针对所有用户,表示可以被挂起/阻塞的最大信号数量。 max locked memory //内存锁定值的限制 max memory size //最大可以使用内存限制 open files //进程打开文件数的限制 pipe size //管道文件大小限制 POSIX message queues //可以创建使用POSIX消息队列的最大值,单位为bytes real-time priority //限制程序实时优先级的范围,只针对普通用户 stack size //限制进程使用堆栈段的大小 cpu time //程序占用CPU的时间,单位是秒 max user processes //限制程序可以fork的进程数,只对普通用户有效 virtual memory //限制进程使用虚拟内存的大小 file locks //锁定文件大小限制
|
同时还有个要注意的值 file-max 是设置 系统所有进程一共可以打开的文件数量 ,可以通过如下方法进行修改echo 6553560 > /proc/sys/fs/file-max
或修改 /etc/sysctl.conf, 加入fs.file-max = 6553560 重启生效
另外还有一个,/proc/sys/fs/file-nr,可以看到整个系统目前使用的文件句柄数量