【运维杂谈】如何进行Linux系统优化

简介: 【运维杂谈】如何进行Linux系统优化

今天给大家讲讲如何进行Linux系统优化,包含修改操作系统语言或字符集,关闭SElinux,优化防火墙,修改yum源,修改系统时间和时区几个方面!


一、查看版本信息

系统优化前,先要明白自己用的啥版本的系统,啥版本的内核

方法1:
[root@VMware-koten ~]#cat /etc/redhat-release
CentOS Linux release 7.9.2009 (Core)
[root@VMware-koten ~]#
方法2:
[root@VMware-koten ~]#hostnamectl
   Static hostname: oldboyedu                             #主机名称
         Icon name: computer-vm                            
           Chassis: vm
        Machine ID: 1d9694143775401a8528930ac5875a12
           Boot ID: 872b60a688e44f5d80c529309344477a
    Virtualization: vmware                                #虚拟平台
  Operating System: CentOS Linux 7 (Core)                 #7系列centos系统
       CPE OS Name: cpe:/o:centos:centos:7
            Kernel: Linux 3.10.0-1160.el7.x86_64          #内核版本号
      Architecture: x86-64
[root@VMware-koten ~]#
centos6.x操作系统内核版本2.6.32
方法3:uname -a 显示所有的 -r只显示内核版本
[root@VMware-koten ~]#uname -a
Linux VMware-koten 3.10.0-1160.el7.x86_64 #1 SMP Mon Oct 19 16:18:59 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux
[root@VMware-koten ~]#uname -r
3.10.0-1160.el7.x86_64
[root@VMware-koten ~]#

二、修改操作系统语言或字符集

1.修改操作系统的语言
编码常用两种:UTF-8 GBK
1.UTF-8 万国码
2.GBK 国标
中文能在Linux系统中显示,Xshell编码必须跟系统编码一致
方法1:通过LANG变量查看系统语言
[root@VMware-koten ~]#echo $LANG
en_US.UTF-8  #语言.字符集
[root@VMware-koten ~]#
方法2:在文件中查看
[root@VMware-koten ~]#cat /etc/locale.conf 
LANG="en_US.UTF-8"
[root@VMware-koten ~]#
临时修改,重新连接后失效
[root@VMware-koten ~]#LANG=zh_CN.UTF-8

三、关闭SElinux

美国国家安全局研发,企业中不用

查看selinux状态
开着的话创建文件,权限后面会显示.
[root@VMware-koten ~]#ll
total 20
-rw-r--r--. 1 root root  50 Mar  1 19:52 1
-rw-r--r--  1 root root   0 Mar  2 10:34 1.log
[root@VMware-koten ~]#
有点是selinux开启时候创建的,没有点是selinux关闭的时候创建的
[root@VMware-koten ~]#getenforce
Enforcing    #默认开启
[root@VMware-koten ~]#
修改配置文件,重启后生效 重启禁止启动selinux
[root@VMware-koten ~]#vim /etc/selinux/config 
# This file controls the state of SELinux on the sys
tem.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforce
d.
#     permissive - SELinux prints warnings instead o
f enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of three values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Onl
y selected processes are protected. 
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted
<elinux/config" 14L, 542C written 
[root@VMware-koten ~]#reboot
如果服务跑起来了,不能重启,需要在系统中关闭selinux 
这个是临时生效 但是也需要改配置文件,防止重启后失效
这是配置文件,不是变量,所以不能source 
[root@VMware-koten ~]#setenforce 
usage:  setenforce [ Enforcing | Permissive | 1 | 0 ]
setenforce Enforcing    #开启selinux 
setenforce 1    #开启selinux 
setenforce Permissive    #关闭selinux 
setenforce 0    #关闭selinux 

四、关闭防火墙(优化防火墙)

作用:保护我们的操作系统,进行流量控制

CentOS6:iptables #默认防火墙 比较复杂 但企业常用
CentOS7:firewalld #默认防火墙 比较简单 
通常禁止启动firwealld 再安装iptables
查看状态
[root@VMware-koten ~]#systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
                           主配置文件                    开机自动运行,disable是开机禁止运行
   Active: active (running) since Thu 2023-03-02 10:24:02 CST; 18min ago
           状态活跃,运行中
     Docs: man:firewalld(1)
 Main PID: 812 (firewalld)
   CGroup: /system.slice/firewalld.service
           └─812 /usr/bin/python2 -Es /usr/sbin/f...
Mar 02 10:24:00 VMware-koten systemd[1]: Starting...
Mar 02 10:24:02 VMware-koten systemd[1]: Started ...
Mar 02 10:24:02 VMware-koten firewalld[812]: WARN...
Hint: Some lines were ellipsized, use -l to show in full.
[root@VMware-koten ~]#
systemctl 动作 服务名称
systemctl restart firewalld
systemctl status firewalld
动作有stop停止
start开启
reatart重启
status状态
enable开机自启动
disable开机禁止启动
2.关闭防火墙 stop disable
[root@VMware-koten ~]#systemctl stop firewalld
[root@VMware-koten ~]#systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
   Active: inactive (dead) since Thu 2023-03-02 10:48:38 CST; 9s ago
     Docs: man:firewalld(1)
  Process: 812 ExecStart=/usr/sbin/firewalld --nofork --nopid $FIREWALLD_ARGS (code=exited, status=0/SUCCESS)
 Main PID: 812 (code=exited, status=0/SUCCESS)
Mar 02 10:24:00 VMware-koten systemd[1]: Starting...
Mar 02 10:24:02 VMware-koten systemd[1]: Started ...
Mar 02 10:24:02 VMware-koten firewalld[812]: WARN...
Mar 02 10:48:37 VMware-koten systemd[1]: Stopping...
Mar 02 10:48:38 VMware-koten systemd[1]: Stopped ...
Hint: Some lines were ellipsized, use -l to show in full.
[root@VMware-koten ~]#systemctl disable firewalld
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@VMware-koten ~]#
什么情况下开启防火墙
1.在公网IP地址的情况下,防止被攻击
2.有对外提供服务的情况下,局域网的服务也可以通过NAT使外网能访问,防止通过服务漏洞被攻击。
3.只要用户可以访问到服务器都需要开启防火墙
什么情况下关闭防火墙
1.局域网中的服务器
2.没有对外提供服务的服务器
3.有大流量访问服务器的情况下,压力大(为了防止被攻击,需要安装硬件防火墙)

五、修改默认YUM仓库,添加额外的软件仓库

yum仓库是安装软件的软件商店 yum中央仓库在美国 跨过去很慢 
所以国内大厂,大学就自己做了个,提供给国内用户,下载就快了
我们就通过国内的仓库下载
国内仓库需要每隔一段时间就同步到自己的仓库
公司也可以搭建自己的yum仓库,同步国内的源就行
正常yum是随机匹配,随机匹配的可能不好,所以我们修改默认仓库为阿里云
1、查看默认的yum仓库
[root@VMware-koten ~]#yum repolist
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.huaweicloud.com #默认yum仓库地址
 * extras: mirrors.huaweicloud.com
 * updates: mirrors.huaweicloud.com
repo id               repo name               status
!base/7/x86_64        CentOS-7 - Base         10,072
!extras/7/x86_64      CentOS-7 - Extras          515
!updates/7/x86_64     CentOS-7 - Updates       4,706
repolist: 15,293 #可用的软件数量
[root@VMware-koten ~]#
2.修改默认的仓库为阿里云的仓库,阿里云提供了教程,不再赘述
备份:
[root@VMware-koten ~]#mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
[root@VMware-koten ~]#
下载:
[root@VMware-koten ~]#wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
--2023-03-02 11:32:29--  https://mirrors.aliyun.com/repo/Centos-7.repo
Resolving mirrors.aliyun.com (mirrors.aliyun.com)... 42.59.1.234, 42.59.1.236, 42.59.1.229, ...
Connecting to mirrors.aliyun.com (mirrors.aliyun.com)|42.59.1.234|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 2523 (2.5K) [application/octet-stream]
Saving to: ‘/etc/yum.repos.d/CentOS-Base.repo’
100%[==========>] 2,523       --.-K/s   in 0.007s  
2023-03-02 11:32:29 (361 KB/s) - ‘/etc/yum.repos.d/CentOS-Base.repo’ saved [2523/2523]
[root@VMware-koten ~]#
3、检查是否修改成功,注意不需要生成缓存,防止其他yum仓库使用时候出现问题。
[root@VMware-koten ~]#yum repolist
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
repo id           repo name                   status
!base/7/x86_64    CentOS-7 - Base - mirrors.a 10,072
!extras/7/x86_64  CentOS-7 - Extras - mirrors    515
!updates/7/x86_64 CentOS-7 - Updates - mirror  4,706
repolist: 15,293
[root@VMware-koten ~]#
下载软件默认仓库里没有,我们通过epel仓库下载
EPEL是由Fedora Special Interest Group维护的Enterprise Linux(RHEL、CentOS)中经常用到的包。
[root@VMware-koten ~]#wget -O /etc/yum.repos.d/epel.repo https://mirrors.aliyun.com/repo/epel-7.repo
[root@VMware-koten ~]#yum repolist
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
repo id          repo name                    status
base/7/x86_64    CentOS-7 - Base - mirrors.al 10,072
epel/x86_64      Extra Packages for Enterpris 13,747
extras/7/x86_64  CentOS-7 - Extras - mirrors.    515
updates/7/x86_64 CentOS-7 - Updates - mirrors  4,724
repolist: 29,058 #安装好总数变为29058
[root@VMware-koten ~]#
yum clean all 清空yum源
[root@VMware-koten ~]#yum -y install ntpdate
Loaded plugins: fastestmirror
Existing lock /var/run/yum.pid: another copy is running as pid 70917.
Another app is currently holding the yum lock; waiting for it to exit...
  The other application is: yum
    Memory :  27 M RSS (923 MB VSZ)
    Started: Fri Oct 10 00:01:04 2008 - 00:36 ago
    State  : Traced/Stopped, pid: 70917
[root@VMware-koten ~]#rm -f /var/run/yum.pid #如果yum被占用锁住,用这个解锁

六、修改系统时间

1.查看系统时间 date 
[root@VMware-koten ~]#date
Thu Mar  2 12:12:11 CST 2023
[root@VMware-koten ~]#
2.修改系统时间
[root@VMware-koten ~]#date -s 20081010
Fri Oct 10 00:00:00 CST 2008
[root@VMware-koten ~]#date
Fri Oct 10 00:00:02 CST 2008
[root@VMware-koten ~]#
3.时间同步(使用命令同步时间服务器的时间)
需要提前安装utpdate
yum -y install ntpdate
[root@VMware-koten ~]#yum -y install ntpdate
[root@VMware-koten ~]#ntpdate ntp1.aliyun.com
 2 Mar 12:18:33 ntpdate[71145]: step time server 120.25.115.20 offset 454162418.482034 sec
[root@VMware-koten ~]#date
Thu Mar  2 12:18:38 CST 2023
[root@VMware-koten ~]#
linux时间
系统时间 date
[root@VMware-koten ~]#date
Thu Mar  2 12:19:16 CST 2023
硬件时间 clock
[root@VMware-koten ~]#clock
Thu 02 Mar 2023 12:18:59 PM CST  -0.618183 seconds
将系统时间同步到硬件时间 -w 保证硬件和系统时间一致
[root@VMware-koten ~]#clock -w
同步流程:
1、安装ntpdate命令
2、使用ntpdate同步时间服务器ntpl.aliyun.com
3、clock -w将系统时间同步给硬件时间

七、修改时区

1.查询当前时区
[root@VMware-koten ~]#timedatectl
      Local time: Thu 2023-03-02 12:26:33 CST
  Universal time: Thu 2023-03-02 04:26:33 UTC
        RTC time: Thu 2023-03-02 04:26:36
       Time zone: Asia/Shanghai (CST, +0800)
     NTP enabled: yes
NTP synchronized: yes
 RTC in local TZ: no
      DST active: n/a
2.设置时区
[root@VMware-koten ~]#timedatectl set-timezone “Asia/Shanghai” 
/Shanghai”l set-timezone “Asia 
Failed to set time zone: Invalid time zone '“Asia/Shanghai”'
[root@VMware-koten ~]#
#因为我这个引号没用对,所以失败了
[root@VMware-koten ~]#timedatectl set-timezone "Asia/Shanghai" 
[root@VMware-koten ~]#
#ok了
3.显示系统的所有时区
[root@VMware-koten ~]#timedatectl list-timezones

我是koten,10年运维经验,持续分享运维干货,感谢大家的阅读和关注

目录
相关文章
|
25天前
|
运维 Linux Shell
linux运维常用命令
linux运维常用命令
|
1月前
|
运维 监控 Linux
linux脚本自动化运维任务
Linux自动化运维通过脚本提升效率,涵盖服务管理(启停服务、异常恢复)、系统监控(资源警报)、日志管理(清理分析)、备份恢复、补丁更新、自动化部署(如Ansible)、网络管理、定时任务(cron)和故障排查。结合shell、Python及工具,形成高效运维体系。
24 3
|
1月前
|
监控 网络协议 Linux
Linux 命令大全 & CentOS常用运维命令
Linux 命令大全 & CentOS常用运维命令
161 0
|
1月前
|
运维 监控 Linux
Linux本地部署1Panel现代化运维管理面板并实现公网访问
Linux本地部署1Panel现代化运维管理面板并实现公网访问
83 1
|
2月前
|
运维 Linux 数据安全/隐私保护
【Linux专题_01】宝塔面板安装及运维
【Linux专题_01】宝塔面板安装及运维
|
2月前
|
Kubernetes Linux 开发工具
容器开发运维人员的 Linux 操作机配置优化建议
容器开发运维人员的 Linux 操作机配置优化建议
|
2月前
|
运维 网络协议 Linux
Linux 这50个 systemd 命令值得每位Linux运维工程师收藏!
Linux 这50个 systemd 命令值得每位Linux运维工程师收藏!
443 0
Linux 这50个 systemd 命令值得每位Linux运维工程师收藏!
|
3月前
|
存储 运维 安全
1Panel开源Linux服务器运维管理面板
1Panel开源Linux服务器运维管理面板
122 0
|
3月前
|
域名解析 监控 Linux
Linux - Linux系统优化思路
Linux - Linux系统优化思路
45 0
Linux - Linux系统优化思路
|
4月前
|
运维 Linux
Linux 运维小技巧
Linux 运维小技巧
49 0

热门文章

最新文章