【Linux入门篇】服务器优化

简介: 【Linux入门篇】服务器优化

       作为系统管理员,新服务器到手,都需要调整哪些参数?有些参数不需要调整就能正常使用,那我为什么调优?如何调整?如何调优,怎么选择参数,这就是本篇文章的目的!

selinux优化

       selinux全称 Security-Enhanced Linux 即安全增强型 Linux,它是一个 Linux 内核模块,也是 Linux 的一个安全子系统。SELinux 主要作用就是最大限度地减小系统中服务进程可访问的资源。

selinux三种工作模式如下:

1. enforcing:强制模式,违反 SELinux 规则的行为将被阻止并记录到日志中。

2. permissive:宽容模式,违反 SELinux 规则的行为只会记录到日志中。一般为调试用。SELinux Permissive 模式主要用于审核当前的 SELinux 策略规则;它还能用于测试新应用程序,将 SELinux 策略规则应用到程序时会有什么效果;以及用于解决某一特定服务或应用程序在 SELinux 下不再正常工作的故障

3. disabled:关闭 SELinux。

1. getenforce                      #查看selinux状态
2. setenforce 0                    #临时关闭(1严格的;0宽容的)
3. vim /etc/sysconfig/selinux      #永久关闭
4. SELINUX=disabled                #修改selinux状态为disabled

系统主机名优化

1. hostname                                #查看主机名,临时修改主机名
2. hostnamectl                             #查看主机配置信息
3. hostnamectl set-hostname 新主机名        #永久修改主机名
4. vim  /etc/hostname                      #永久修改主机名

命令提示符优化

1. PS1='[\u@\h \t \W]\$'                   #临时修改日期提示
2. 
3. vim /etc/profile                        #永久修改日期提示
4. 添加末尾:
5. PS1='[\u@\h \t \W]\$'

系统时间同步优化

1. date                                        #查看当前日期时间
2. date -s '2023-02-22'                        #默认修改日期,时间自动修改为00:00:00
3. date -s '2023-03-01 13:00:00'               #修改当前时间日期
4. timedatectl                                 #系统时间配置查看
5. timedatectl list-timezones                  #查看时区列表
6. timedatectl set-timezone Asia/Shanghai      #设置上海时区
7. timedatectl set-ntp 1                       #开启网络时间同步功能
8. ntpdate ntp1.aliyun.com                     #手动同步时间服务器(ntp1..7)
9. timedatectl set-time '2023-03-01 12:00:00'  #手动调整时间

系统字符编码优化

1. echo $LANG                                  #查看当前字符编码
2. localectl list-locales                      #查看系统支持的字符编码
3. 
4. LANG=zh_CN.utf8                             #临时设置字符编码
5. vim /etc/locale.conf                        #永久修改字符编码
6. 或
7. localectl set-locale LANG=zh_CN.utf8

系统远程连接优化

1. [root@localhost ~]# vim /etc/ssh/sshd_config 
2. GSSAPIAuthentication no                     #修改79行 
3. UseDNS no                                   #修改115行
4. [root@localhost ~]# systemctl restart sshd

或者使用sed命令修改

1. [root@localhost ~]# sed -i '79s#yes#no#g'  /etc/ssh/sshd_config
2. [root@localhost ~]# sed -i 's@#UseDNS yes@UseDNS no@g' /etc/ssh/sshd_config
3. [root@localhost ~]# systemctl restart sshd

系统资源限制优化

查看限制

ulimit -a               #查看系统限制值

核心文件大小(块,-c)0

数据段大小(kbytes,-d)不受限制 计划优先级0

文件大小(块,-f)不受限制

挂起信号(-i)7788

最大锁定内存(KB,-l)64

最大内存大小(kbytes,-m)不受限制

打开文件(-n)1024

管道大小(512字节,-p)8

POSIX消息队列(字节,-q)819200 实时优先级0

堆栈大小(kbytes,-s)8192

cpu时间(秒,-t)无限制

最大用户进程(-u)7788

虚拟内存(KB,-v)不受限制

文件锁定(-x)不受限制

1. ulimit -n                       #查看用户同时打开的文件数
2. ulimit -n 65535                 #临时设置打开文件数
3. 
4. cat /proc/sys/fs/file-max       #查看系统级最大打开文件数限制

设置资源限制

1. [root@localhost ~]# vim /etc/security/limits.conf
2. 添加末行:
3. * soft nofile 65535
4. * hard nofile 65535
5. * soft nproc 65535
6. * hard nproc 65535

解释: noproc 是代表最大进程数;nofile 是代最大文件打开数

"*" 是表示修改所有用户的限制

生产环境修改建议如下(如果公司有一定需求,请根据公司需求调整)

1. [root@localhost ~]# vim /etc/security/limits.conf
2. *           soft  core   unlimit
3. *           hard  core   unlimit
4. *           soft  fsize  unlimited
5. *           hard  fsize  unlimited
6. *           soft  data   unlimited
7. *           hard  data   unlimited
8. *           soft  nproc  65535
9. *           hard  nproc  63535
10. *           soft  stack  unlimited
11. *           hard  stack  unlimited
12. *           soft  nofile  409600
13. *           hard  nofile  409600

系统内核优化

1. [root@localhost ~]# vim /etc/sysctl.d/99-sysctl.conf
2. 添加:
3. 
4. #关闭ipv6
5. net.ipv6.conf.all.disable_ipv6 = 1
6. net.ipv6.conf.default.disable_ipv6 = 1
7. 
8. #避免放大攻击
9. net.ipv4.icmp_echo_ignore_broadcasts = 1
10. 
11. #开启恶意icmp错误消息保护
12. net.ipv4.icmp_ignore_bogus_error_responses = 1
13. 
14. #关闭路由转发
15. net.ipv4.ip_forward = 0
16. net.ipv4.conf.all.send_redirects = 0
17. net.ipv4.conf.default.send_redirects = 0
18. 
19. #开启反向路径过滤
20. net.ipv4.conf.all.rp_filter = 1
21. net.ipv4.conf.default.rp_filter = 1
22. 
23. #关闭sysrq功能
24. kernel.sysrq = 0
25. 
26. #core文件名中添加pid作为扩展名
27. kernel.core_uses_pid = 1
28. net.ipv4.tcp_syncookies = 1
29. 
30. #修改消息队列长度
31. kernel.msgmnb = 65536
32. kernel.msgmax = 65536
33. 
34. #设置最大内存共享段大小bytes
35. kernel.shmmax = 68719476736
36. kernel.shmall = 4294967296
37. 
38. #timewait的数量,默认180000
39. net.ipv4.tcp_max_tw_buckets = 6000
40. net.ipv4.tcp_sack = 1
41. net.ipv4.tcp_window_scaling = 1
42. net.ipv4.tcp_rmem = 4096        87380   4194304
43. net.ipv4.tcp_wmem = 4096        16384   4194304
44. net.core.wmem_default = 8388608
45. net.core.rmem_default = 8388608
46. net.core.rmem_max = 16777216
47. net.core.wmem_max = 16777216
48. net.core.netdev_max_backlog = 262144
49. 
50. 
51. #限制仅仅是为了防止简单的DoS 攻击
52. net.ipv4.tcp_max_orphans = 3276800
53. 
54. #未收到客户端确认信息的连接请求的最大值
55. net.ipv4.tcp_max_syn_backlog = 262144
56. net.ipv4.tcp_timestamps = 0
57. 
58. #内核放弃建立连接之前发送SYNACK 包的数量
59. net.ipv4.tcp_synack_retries = 1
60. 
61. #内核放弃建立连接之前发送SYN 包的数量
62. net.ipv4.tcp_syn_retries = 1
63. 
64. #启用timewait 快速回收
65. net.ipv4.tcp_tw_recycle = 1
66. 
67. #开启重用。允许将TIME-WAIT sockets 重新用于新的TCP连接
68. net.ipv4.tcp_tw_reuse = 1
69. net.ipv4.tcp_mem = 94500000 915000000 927000000
70. net.ipv4.tcp_fin_timeout = 1
71. 
72. #当keepalive 起用的时候,TCP 发送keepalive 消息的频度。缺省是2 小时
73. net.ipv4.tcp_keepalive_time = 30
74. 
75. #允许系统打开的端口范围
76. net.ipv4.ip_local_port_range = 1024    65000
77. 
78. #修改防火墙表大小,默认65536
79. net.netfilter.nf_conntrack_max=655350
80. net.netfilter.nf_conntrack_tcp_timeout_established=1200
81. 
82. #确保无人能修改路由表
83. net.ipv4.conf.all.accept_redirects = 0
84. net.ipv4.conf.default.accept_redirects = 0
85. net.ipv4.conf.all.secure_redirects = 0
86. net.ipv4.conf.default.secure_redirects = 0
87. 
88. [root@localhost ~]# sysctl -p           #刷新生效

系统内核版本升级

       版本升级不一定使用,根据公司要求是否需要。

1. 载入公钥

rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org

2. 安装epel

rpm -Uvh http://www.elrepo.org/elrepo-release-7.0-3.el7.elrepo.noarch.rpm

3. 载入elrepo-kernel元数据

yum --disablerepo=\* --enablerepo=elrepo-kernel repolist

4. 查看可用的rpm包

yum --disablerepo=\* --enablerepo=elrepo-kernel list kernel*

5. 安装最新版本的kernel

yum --disablerepo=\* --enablerepo=elrepo-kernel install -y kernel-ml.x86_64

重启,选择新版本内核进入系统。

6. 删除旧版本工具包

yum remove kernel-tools-libs.x86_64 kernel-tools.x86_64 -y

7. 安装新版本工具包

yum --disablerepo=\* --enablerepo=elrepo-kernel install -y kernel-ml-tools.x86_64

8. 查看系统中的全部内核,也可以删除多余的内核

1. rpm -qa | grep kernel
2. yum remove -y kernel-3.10.0-1127.el7.x86_64


相关文章
|
1天前
|
关系型数据库 MySQL Linux
服务器Linux系统配置mysql数据库主从自动备份
这是一个基本的配置主从复制和设置自动备份的指南。具体的配置细节和命令可能因您的环境和需求而有所不同,因此建议在操作前详细阅读MySQL文档和相关资源,并谨慎操作以避免数据丢失或不一致。
9 3
|
2天前
|
Oracle Java 关系型数据库
【服务器】python通过JDBC连接到位于Linux远程服务器上的Oracle数据库
【服务器】python通过JDBC连接到位于Linux远程服务器上的Oracle数据库
14 6
|
2天前
|
存储 弹性计算 监控
探索阿里云弹性计算:如何优化云服务器ECS的性能与成本
在云时代,【阿里云ECS】的性能优化与成本控制至关重要。利用实例规格选择、自动伸缩、网络和存储配置,可增强性能、减少成本。结合监控工具和优化建议,用户能解决性能问题,提升应用稳定性,实现高效且经济的云计算运营。
9 1
|
3天前
|
缓存 安全 Linux
Linux入门基本指令(2)
Linux入门基本指令(2)
10 0
|
3天前
|
Linux Windows
Linux入门基本指令(1)-2
Linux入门基本指令(1)
9 1
|
3天前
|
Linux 数据安全/隐私保护 Windows
Linux入门基本指令(1)-1
Linux入门基本指令(1)
13 1
|
4天前
|
运维 监控 安全
2023年最详细的:本地Linux服务器安装宝塔面板,并内网穿透实现公网远程登录
2023年最详细的:本地Linux服务器安装宝塔面板,并内网穿透实现公网远程登录
|
4天前
|
Linux Shell
Linux 终端入门
Linux 终端入门
|
6天前
|
缓存 安全 网络安全
Apache服务器配置与优化指南
【5月更文挑战第7天】Apache服务器配置与优化指南包括基础配置和性能优化。安装Apache后,编辑`httpd.conf`配置文件,设置`ServerRoot`、`Listen`、`ServerName`和`DocumentRoot`。启用虚拟主机以托管多个网站。性能优化涉及启用MPM模块(如worker或event),启用压缩功能,优化KeepAlive参数,配置缓存和限制并发连接数。安全配置包括禁用不必要的模块,设置目录权限,启用SSL/TLS及限制IP访问。通过这些措施,提升服务器性能和安全性。
|
5天前
|
缓存 负载均衡 安全
深入探索Nginx高性能Web服务器配置与优化
【5月更文挑战第7天】本文深入探讨了Nginx的配置与优化,重点介绍了基础配置参数如`worker_processes`、`worker_connections`和`keepalive_timeout`,以及优化策略,包括使用epoll事件驱动模型、开启gzip压缩、启用缓存、负载均衡和安全配置。此外,还提到了性能调优工具,如ab、nginx-stats和nmon,以助于提升Nginx的性能和稳定性。