zoukankan      html  css  js  c++  java
  • Centos Linux系统优化一

    一、磁盘分区

    (一)分区概况

     磁盘在使用前需要进行分区,磁盘分区有:

    • 主分区
    • 扩展分区
    • 逻辑分区

      一块磁盘最多有4个主分区(数字编号1-4),不过有时可能根据需要会有逻辑分区,这时可以将一个主分区的位置用一个扩展分区来替换,然后再扩展分区中进行逻辑分区的划分。注意的是一块磁盘最多有一个扩展分区。

      扩展分区是抽象的,它不能使用,必须要将其划分为一个或者多个逻辑分区(数字编号从5开始),然后格式化,创建文件系统。

      在windows系统中,我们可以将磁盘最后划分为C盘、D盘...等来进行访问,而在linux中划分后的分区通过设备名来进行访问,这些设备存放在/dev/下:

    [root@localhost ~]# ll /dev/
    total 0
    ...
    brw-rw----. 1 root disk      8,   0 Oct  8 17:45 sda
    brw-rw----. 1 root disk      8,   1 Oct  8 17:45 sda1 #第一块磁盘的第1个分区
    brw-rw----. 1 root disk      8,   2 Oct  8 17:45 sda2 #第一块磁盘的第2个分区
    brw-rw----. 1 root disk      8,   3 Oct  8 17:45 sda3 #第一块磁盘的第3个分区
    ...

    因为我们使用的是SCSI接口,所以我们使用的设备是以sd开头的,第一块磁盘就是sda,第二块磁盘就是sdb,...以此类推。

    (二)服务器分区选择

    1、常规分区方案

    • /boot(100-200M)
    • swap(内存的1.5倍)
    • /(剩余所有的硬盘空间)

    2、大量存储

    对于有大量数据存储的场景,此时可以多一个分区专门用于存储数据,这样重装系统数据不会丢失,相当于windows系统重装系统C盘丢失,D盘等数据不会丢失。

    • /boot(100-200M)
    • swap(内存的1.5倍)
    • /(50-200G)
    • /data(剩余所有的硬盘空间)

    3、门户网站

    • /boot(100-200M)
    • swap(内存的1.5倍)
    • /(50-200G)

    对于剩余的硬盘空间不在进行划分,后续根据需求再进行划分,这样做的目的是较为灵活。

    一般情况选择第一种就可以了,其中/boot分区是Linux引导分区,存放系统引导文件,如Linux内核等。swap(交换分区)的作用是用作虚拟内存。

    二、系统优化

    (一)yum源更换

    Linux下安装软件的工具用yum,它可以解决软件依赖关系,通过yum安装软件,默认是获取rpm包的配置(官方镜像),位于/etc/yum.repos.d:

    # CentOS-Base.repo
    #
    # The mirror system uses the connecting IP address of the client and the
    # update status of each mirror to pick mirrors that are updated to and
    # geographically close to the client.  You should use this for CentOS updates
    # unless you are manually picking other mirrors.
    #
    # If the mirrorlist= does not work for you, as a fall back you can try the 
    # remarked out baseurl= line instead.
    #
    #
    
    [base]
    name=CentOS-$releasever - Base
    mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=os&infra=$infra
    #baseurl=http://mirror.centos.org/centos/$releasever/os/$basearch/
    gpgcheck=1
    gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
    
    #released updates 
    [updates]
    name=CentOS-$releasever - Updates
    mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=updates&infra=$infra
    #baseurl=http://mirror.centos.org/centos/$releasever/updates/$basearch/
    gpgcheck=1
    gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
    
    #additional packages that may be useful
    [extras]
    name=CentOS-$releasever - Extras
    mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=extras&infra=$infra
    #baseurl=http://mirror.centos.org/centos/$releasever/extras/$basearch/
    gpgcheck=1
    gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
    
    #additional packages that extend functionality of existing packages
    [centosplus]
    name=CentOS-$releasever - Plus
    mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=centosplus&infra=$infra
    #baseurl=http://mirror.centos.org/centos/$releasever/centosplus/$basearch/
    gpgcheck=1
    enabled=0
    gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
    CentOS-Base.repo

    所以yum安装软件的速度会较慢,为了提高下载速度可以将默认下载rmp包的配置从国外官方yum源改为国内的yum源,下面是一些可选的国内yum源。

    具体yum源更换可按照下面的步骤:

    • 备份原始yum源
    • 下载需要更换的yum源文件
    • 修改文件名称
    # 备份
    [root@localhost yum.repos.d]# mv CentOS-Base.repo CentOS-Base.repo.ori
    
    # 下载yum源配置文件
    使用https://mirrors.cnnic.cn/help/centos/中的内容覆盖掉 /etc/yum.repos.d/CentOS-Base.repo 文件
    vim CentOS-Base.repo
    
    # CentOS-Base.repo
    #
    # The mirror system uses the connecting IP address of the client and the
    # update status of each mirror to pick mirrors that are updated to and
    # geographically close to the client.  You should use this for CentOS updates
    # unless you are manually picking other mirrors.
    #
    # If the mirrorlist= does not work for you, as a fall back you can try the
    # remarked out baseurl= line instead.
    #
    #
    
    
    [base]
    name=CentOS-$releasever - Base
    baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos/$releasever/os/$basearch/
    #mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=os
    enabled=1
    gpgcheck=1
    gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-7
    
    #released updates
    [updates]
    name=CentOS-$releasever - Updates
    baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos/$releasever/updates/$basearch/
    #mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=updates
    enabled=1
    gpgcheck=1
    gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-7
    
    
    
    #additional packages that may be useful
    [extras]
    name=CentOS-$releasever - Extras
    baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos/$releasever/extras/$basearch/
    #mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=extras
    enabled=1
    gpgcheck=1
    gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-7
    
    
    
    #additional packages that extend functionality of existing packages
    [centosplus]
    name=CentOS-$releasever - Plus
    baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos/$releasever/centosplus/$basearch/
    #mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=centosplus
    gpgcheck=1
    enabled=0
    gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-7
    
    # 更新更新软件包缓存
    [root@localhost yum.repos.d]# yum makecache

    在上面更新yum源后可以安装一些必要的软件包:

    # sysstat 它的主要用途是观察服务负载,比如CPU和内存的占用率、网络的使用率以及磁盘写入和读取速度等。
    [root@localhost yum.repos.d]# yum install tree telnet  sysstat lrzsz -y

    (二)用户管理

    一般情况下应该尽量避免用root用户来进行操作,所以需要新建用户:

    [root@localhost ~]# useradd test  #新建用户test
    [root@localhost ~]# passwd test #给新建用户设置密码,后面如果不给用户名,改变的就是当前登录的用户密码
    Changing password for user test.
    New password: 
    BAD PASSWORD: The password fails the dictionary check - it is too simplistic/systematic
    Retype new password: 
    passwd: all authentication tokens updated successfully.
    
    [root@localhost ~]# su - test #切换用户,带-切换,保证用户变量全部带过来
    Last login: Sun Oct 11 22:30:13 CST 2020 on pts/1
    [test@localhost ~]$
    
    [test@localhost ~]$ who am i #查看当前登录用户
    root     pts/1        2020-10-11 21:45 (192.168.159.1)
    [test@localhost ~]$ su #su不加参数直接切换到root用户
    Password: 

    注意:

    • $是普通用户提示符,#是root用户提示符,普通用户只能在自己的家目录下进行操作
    • 切换用户使用su进行切换,其中su和su -的区别是su - 会将用户的变量一起带过来,所以确保自己使用su -进行切换
    • 超级用户root切换到普通用户不需要密码,普通用户切换到root或者普通用户需要密码

    (三)安全管理 

    Linux中的Selinux是保证Linux的安全,但是这个东西会阻碍其它很多东西,对于Linux的安全有防火墙,所以一般是关闭Selinux。

    Selinux有以下几种状态,可以通过以下的方式查看:

    [root@localhost ~]# cat /etc/selinux/config 
    
    # This file controls the state of SELinux on the system.
    # SELINUX= can take one of these three values:
    #     enforcing - SELinux security policy is enforced.
    #     permissive - SELinux prints warnings instead of enforcing.
    #     disabled - No SELinux policy is loaded.
    SELINUX=enforcing
    # SELINUXTYPE= can take one of three two values:
    #     targeted - Targeted processes are protected,
    #     minimum - Modification of targeted policy. Only selected processes are protected. 
    #     mls - Multi Level Security protection.
    SELINUXTYPE=targeted 

    可以看到它有enforcing、permissive、disabled三种状态。enforcing处于启动状态,permissive是警告状态但非启动状态,disabled是关闭状态。SELINUX=enforcing表明处于启动状态,当然你也可以通过:

    [root@localhost ~]# getenforce
    Enforcing

    查看目前状态。那么如何关闭呢?

    [root@localhost ~]# sed -i s#SELINUX=enforcing#SELINUX=disabled#g /etc/selinux/config
    [root@localhost ~]# grep "disabled" /etc/selinux/config 
    #     disabled - No SELinux policy is loaded.
    SELINUX=disabled

    上面虽然已经关闭了Selinux但是需要重启服务器,但是在生产场景下,一般是不会重启的,此时可以进行临时生效方案:

    [root@localhost ~]# getenforce 
    Enforcing
    # 临时生效方案,不需要重启服务器
    [root@localhost ~]# setenforce 0
    [root@localhost ~]# getenforce 
    Permissive

    所以,对于Selinux需要进行永久关闭和临时生效方案。

  • 相关阅读:
    记处理PostgreSQL连接太多的问题
    记服务器迁移上云出现故障
    PostgreSQL-迁移同步序列
    为什么你的pgbouncer连接这么慢
    TS基础知识储备
    前端实在好用的工具类库 【后续持续添加若有请留言添加积累供大家使用】
    Vue 3 + Typescript + Vite2.0 搭建移动端 代码规范以及注意的点
    flutter2.0 环境搭建(window环境下Mac自行查看官网即可)
    vite2.0+vue3+ts前端最新热门技术项目搭建
    React官方状态管理库—— Recoil
  • 原文地址:https://www.cnblogs.com/shenjianping/p/13797105.html
Copyright © 2011-2022 走看看