Ubuntu Server 22.04 系统性能优化

ubuntu 系统是非常流行和常用的系统,但是在安装系统完毕后系统默认参数可能不太适合物理机性能和并发情况,本篇文章重点介绍通用的性能优化和简单的安全方案。

1、更换系统镜像源

将系统自带的镜像源更新为阿里的镜像源,可以提高软件下载速度。

1)先备份原有系统镜像源文件:

mv /etc/apt/sources.list /etc/apt/sources.listbak

2)创建新的镜像源文件并写入阿里镜像源

cd /etc/apt/
vi /etc/apt/sources.list

写入下方代码

deb http://mirrors.aliyun.com/ubuntu/ jammy main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ jammy main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ jammy-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ jammy-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ jammy-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ jammy-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ jammy-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ jammy-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ jammy-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ jammy-backports main restricted universe multiverse

2、更新升级系统软件包

定期更新升级系统软件包,可以修复系统漏洞、提高系统稳定性和性能,同时能够获取系统新发布的功能和特性。

# 获取最新的所有可更新的软件清单命令
sudo apt update
# 列出本次可更新的软件包及版本信息
# !!!注意:本次可能有内核更新或者大版本的软件更新,会有需要重启服务器或者对应的软件不需要升级的情况
apt list --upgradeable
# 如果不需要升级某软件,例如docker,可以参考如下命令
sudo apt-mark hold docker*
# 升级所有软件
sudo apt upgrade

3、设置 ulimit 的 open files:

先备份limit.conf文件

cp /etc/security/limits.conf /etc/security/limits.conf.bak

执行如下命令,写入文件更新配置,注意:验证时需要断开原ssh连接,重新打开新的连接验证;root用户需要显式配置

echo '* soft noproc 65535'>>/etc/security/limits.conf
echo '* hard noproc 65535'>>/etc/security/limits.conf
echo '* soft nofile 409600'>>/etc/security/limits.conf
echo '* hard nofile 409600'>>/etc/security/limits.conf
# 注意:以下是root用户的配置,ubuntu 系统root用户必须显式指定配置
echo 'root soft noproc 65535'>>/etc/security/limits.conf
echo 'root hard noproc 65535'>>/etc/security/limits.conf
echo 'root soft nofile 409600'>>/etc/security/limits.conf
echo 'root hard nofile 409600'>>/etc/security/limits.conf

然后,在下面的两文件中加入:DefaultLimitNOFILE=204800

echo "DefaultLimitNOFILE=204800" >> /etc/systemd/user.conf
echo "DefaultLimitNOFILE=204800" >> /etc/systemd/system.conf

4、core文件设置

正确设置core文件的必要性:corefile 是Linux系统程序崩溃时生成的文件,可以用来分析程序崩溃的原因,因为它包含了程序崩溃时的内存堆栈信息。

1)设置生成core文件

echo "* soft core 4194304" >> /etc/security/limits.conf
echo "* hard core 4194304" >> /etc/security/limits.conf

2)设置core文件命名方式

/proc/sys/kernel/core_uses_pid 文件是用来控制生成的core文件的文件名中是否添加应用程序pid作为名称的扩展。

  • 值为1:表示添加pid作为文件名一部分,生成的core文件格式为core.pid;
  • 值为0:表示生成的core文件都是同一名称,新的core文件会覆盖旧的core文件。

可通过以下命令修改此文件:

echo "1" > /proc/sys/kernel/core_uses_pid

3)设置core文件保存位置和文件命名格式

proc/sys/kernel/core_pattern 文件可以控制core文件保存位置和文件名格式。
可通过以下命令修改此文件,可以将core文件统一生成到/data/corefile目录下,产生的文件名为core-命令名-pid-时间戳

echo "/data/corefile/core-%e-%p-%t" > /proc/sys/kernel/core_pattern

5、关闭防火墙ufw

如果没有特殊要求,防火墙可以不使用,毕竟防火墙就是起一个端口过滤作用,并且服务器机房单独配置的防火墙已经能搞定绝大部分,如果搞不定,估计ufw也够呛。
这里摘抄ubuntu官网的一段话,大家自行理解:

Ubuntu 配有 Uncomplicated Firewall(ufw),但默认情况下不会启动此防火墙。由于 Ubuntu 没有任何开放式网络服务(基本网络架构除外),在默认安装状态下,您并不需要通过防火墙来阻止有恶意企图的连接。(https://people.ubuntu.com/~happyaron/ubuntu-docs/precise-html/net-firewall-on-off.html)

sudo ufw disable # 停止并禁用自动启动
sudo ufw status # 查看防火墙状态,会输出结果:Status: inactive

6、 修改Linux主机hostname

修改主机hostname和host可以加快内网网络速度,没必要每次本地主机名解析都走网络。

  1. 使用hostnamectl修改主机名,同时更新/etc/hostname文件中的主机名
sudo hostnamectl set-hostname your_name
  1. 如果是云实例上运行Ubuntu,并且已安装cloud-init软件包,则还需要编辑/etc/cloud/cloud.cfg文件。
sudo vim /etc/cloud/cloud.cfg

在文件中搜索preserve_hostname,并将值从false更改为true:

# This will cause the set+update hostname module to not operate (if true)
preserve_hostname: true
  1. 按如下修改host
vi /etc/hosts
192.168.110.22 test01
192.168.110.33 test02
192.168.110.44 test03

7、sshd安全性优化

编辑ssh配置文件

vi /etc/ssh/sshd_config

如果文件中没有这两个配置则添加,如果有则需要修改为下面的配置值,配置完成后重启sshd服务

Ciphers aes256-ctr,aes192-ctr,aes128-ctr
MACs hmac-sha1

重启sshd服务

systemctl restart sshd
  • 13
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

fire_in_java

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值