Myluzh Blog

CentOS系统安装后的使用优化

发布时间: 2022-12-1 文章作者: myluzh 分类名称: Linux 朗读文章


0x01操作系统名称
Linux:CentOs7、CentOs8

说明:对于使用Linux系统新手,或者普通用户而言,在对Linux系统不熟悉的情况下,可能更多的是直接部署相关应用,但缺乏后期维护的情况下,通常部署过程,或者运行过程中出现各种莫名其妙的问题,通常多数问题又往往的基础环境的某些配置问题。

优化内容项:

修改网卡名称
网卡配置
设置语言及主机名
创建常用目录
关闭SELinux及防火墙
优化ssh速度
关闭firewall防火墙
配置yum源
升级CentOS8内核
升级CentOS7内核
CentOS8安装常用软件
CentOS7安装常用软件
安装其他软件
配置vim环境
配置时钟同步
配置bash别名环境
CentOS7升级openssl
安装Python 3.9
配置国内pip源
pip自动补全
清理所有残留

0x02详细指令
一、修改网卡名称
通常CentOS7、CentOS8的网卡名为ensXXX命名,而历史过往习惯为ethX,为符合经验和习惯,可通过如下方式修改:

# cp -brpf /etc/udev/rules.d/70-persistent-net.rules /etc/udev/rules.d/70-persistent-net.rules.bak
# rm -rf /etc/udev/rules.d/-persistent-.rules
# cp -brpf /etc/default/grub /etc/default/grub.bak
# sed -i 's/GRUB_CMDLINE_LINUX=\"/GRUB_CMDLINE_LINUX=\"net.ifnames=0 biosdevname=0 /g' /etc/default/grub
# grub2-mkconfig -o /boot/grub2/grub.cfg > /dev/null
# systemctl restart NetworkManager
# systemctl enable NetworkManager

二、修改静态IP

1.CentOS6配置静态IP

对于CentOS6配置静态IP,可直接vi修改网卡配置文件,主要配置参数如下:

vi /etc/sysconfig/network-scripts/ifcfg-eth0
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=none
IPADDR=172.24.8.134
PREFIX=24
GATEWAY=172.24.8.2
DNS1=223.5.5.5
DNS2=223.6.6.6
DEFROUTE=yes
#IPV4_FAILURE_FATAL=no
#IPV6INIT=yes
#IPV6_AUTOCONF=yes
#IPV6_DEFROUTE=yes
#IPV6_FAILURE_FATAL=no
#IPV6_ADDR_GEN_MODE=stable-privacy
NAME=eth1
DEVICE=eth1
ONBOOT=yes
2.CentOS7配置静态IP

对于CentOS7配置静态IP,不推荐直接修改if-eth0网卡配置文件参数,最佳实践为使用nmcli命令进行配置:

#nmcli connection add connection.autoconnect yes autoconnect yes ipv4.method manual con-name eth0 ifname eth0 ipv4.addresses 172.16.10.172/24 ipv4.dns 223.5.5.5 type ethernet mtu 1452 ipv4.gateway 172.16.10.1

# nmcli c reload eth0

# nmcli c up eth0

三、设置语言及主机名

在有时候需要设置主机名及系统语言。

# hostnamectl set-hostname imxhy #设置主机名

# localectl set-locale LANG=zh_CN.utf8 #设置为中文
# localectl set-locale LANG=en_US.UTF-8 #设置为英文

四、创建常用目录

部分目录系统不自带,但为了后续维护和管理方便,建议独立创建进行区分。

mkdir -p /etc/yum.repos.d/backup
# mkdir -p /mnt/cdrom
# mkdir -p /media/cdrom

五、关闭SELinux及防火墙

# cp -brpf /etc/selinux/config /etc/selinux/config.bak
# sed -i 's/=enforcing/=disabled/g' /etc/selinux/config
# systemctl disable firewalld --now

六、优化ssh访问速度

对于某些情况,ssh登录Linux的时候,会尝试解析为DNS name,需要花费时间,可进行关闭。
# sed -i 's/^UseDNS.*/UseDNS no/' /etc/ssh/sshd_config
# sed -i 's/^#UseDNS.*/UseDNS no/' /etc/ssh/sshd_config

七、优化yum源

这一项非常重要,通常默认的CentOS其yum源在国外,可如下修改为国内的yum源,此yum源文件为本人自行收录在个人站点上。

mv -b /etc/yum.repos.d/*.repo /etc/yum.repos.d/backup
# mv -b /etc/yum.repos.d/*.rpmnew /etc/yum.repos.d/backup
# wget -P /etc/yum.repos.d/ http://down.linuxsb.com/myoptions/CentOS7-Base-aliyun-ustc.repo #CentOS7配置


# wget -P /etc/yum.repos.d/ http://down.linuxsb.com/myoptions/epel7-aliyun-ustc.repo
# wget -P /etc/yum.repos.d/ http://down.linuxsb.com/myoptions/CentOS8-Base-aliyun-ustc.repo
# wget -P /etc/yum.repos.d/ http://down.linuxsb.com/myoptions/epel8-aliyun-ustc-modular.repo
# wget -P /etc/yum.repos.d/ http://down.linuxsb.com/myoptions/epel8-aliyun-ustc-playground.repo
# wget -P /etc/yum.repos.d/ http://down.linuxsb.com/myoptions/epel8-aliyun-ustc-testing-modular.repo
# wget -P /etc/yum.repos.d/ http://down.linuxsb.com/myoptions/epel8-aliyun-ustc-testing.repo
# wget -P /etc/yum.repos.d/ http://down.linuxsb.com/myoptions/epel8-aliyun-ustc.repo #CentOS8配置

提示:所有国内外常见的yum站点统计参考:https://www.cnblogs.com/itzgr/p/16218821.html

八、升级内核

对于部分应用,需要升级CentOS内核,比如docker,可通过如下方式升级内核。

1.升级CentOS7内核

#rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org
# yum -y install https://www.elrepo.org/elrepo-release-7.el7.elrepo.noarch.rpm
# mv -b /etc/yum.repos.d/elrepo.repo /etc/yum.repos.d/backup
# wget -P /etc/yum.repos.d/ http://down.linuxsb.com/myoptions/elrepo7.repo
# yum --disablerepo="*" --enablerepo="elrepo-kernel" install -y kernel-ml
# grub2-set-default 0

2.升级CentOS8内核

#rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org
# rpm --import http://mirrors.ustc.edu.cn/centos/RPM-GPG-KEY-CentOS-Official
# yum -y install https://www.elrepo.org/elrepo-release-8.el8.elrepo.noarch.rpm
# mv -b /etc/yum.repos.d/elrepo.repo /etc/yum.repos.d/backup
# wget -P /etc/yum.repos.d/ http://down.linuxsb.com/myoptions/elrepo8.repo
# yum --disablerepo="*" --enablerepo="elrepo-kernel" install -y kernel-ml
# grub2-set-default 0

提示:更多升级内核操作参考:https://www.cnblogs.com/itzgr/p/14668991.html

九、安装常用软件

1.CentOS7常用软件安装

# yum -y install tar bzip2 git gcc gcc-c++ make wget openssl-devel openssh-clients bash-completion net-tools vim ntp screen lrzsz bash-completion-extras epel-release openssl tree jq curl lvm2 yum-utils zlib zlib-devel libffi-devel --skip-broken

2.CentOS8常用软件安装

#rpm --import http://mirrors.ustc.edu.cn/centos/RPM-GPG-KEY-CentOS-Official
# rpm --import https://mirrors.aliyun.com/epel/RPM-GPG-KEY-EPEL-8
# dnf -y install tar bzip2 git gcc gcc-c++ make wget openssl-devel openssh-clients bash-completion net-tools vim chrony screen lrzsz epel-release tree jq curl lvm2 yum-utils zlib zlib-devel libffi-devel perl-Pod-Html xz unzip --skip-broken

十、安装其他软件

建议安装额外运维小命令,如tcping、fping命令。

#mkdir -p /usr/local/tcping/sbin
# cd /tmp
# wget http://down.linuxsb.com/software/tcping-1.3.5-4-x86_64.tar.gz
# tar -zxvf tcping-1.3.5-4-x86_64.tar.gz
# mv usr/bin/tcping /usr/local/tcping/sbin/
# wget http://down.linuxsb.com/software/fping-5.0.tar.gz
# tar -zxvf fping-5.0.tar.gz
# cd fping-5.0/
# ./configure --prefix=/usr/local/fping
# make && make install
# echo 'export PATH=$PATH:/usr/local/tcping/sbin:/usr/local/fping/sbin' >> /root/.bashrc

十一、配置vim环境

对于经常使用vim的用户,配置一个属于自己的独立的vimrc环境,会让你显得非常有逼格,如下为本人分享的其中一个。

#cp -brpf /root/.vimrc /root/.vimrc.bak
# wget http://down.linuxsb.com/myoptions/my_Vimrc_Diy01_v2 -O /root/.vimrc
# wget http://down.linuxsb.com/myoptions/my_Vimrc_Diy01_v02 -O /etc/skel/.vimrc

十二、配置时钟同步

1.CentOS7时钟同步

#cp -brpf /etc/ntp.conf /etc/ntp.conf.bak
# wget http://down.linuxsb.com/myoptions/Aliyunntp.conf -O /etc/ntp.conf
#
# systemctl enable ntpd –now

2.CentOS8时钟同步

#cp -brpf /etc/chrony.conf /etc/chrony.conf.bak
# wget http://down.linuxsb.com/myoptions/Aliyunchrony.conf -O /etc/chrony.conf
# systemctl enable chronyd --now

十三、配置bash别名环境

对于常用的命令,建议配置别名,提高工作效率。

vim ~/.bashrc
# Xianghy defined aliases and functions
export TERM=linux
alias sroot='su - root'
alias useradd='useradd -m'
alias rm='rm -i'
alias cp='cp -ia'
alias mv='mv -i'
alias dff='df -Th'
alias du='du -h'
alias gj='shutdown -h now'
alias cq='shutdown -r now'
alias vi='vim'
alias ls='ls -h --color=auto'
alias ll='ls -lh --color=auto'
alias grep='grep --color=auto'
alias freem='free -m'
alias cip='curl -s icanhazip.com'

十四、CentOS7升级openssl

曾经旧版openssl出现重大bug,建议升级为更新版本的openssl。

#cd /tmp
# wget http://down.linuxsb.com/software/openssl-1.1.1m.tar.gz --no-check-certificate
# tar -zxvf openssl-1.1.1m.tar.gz
# cd openssl-1.1.1m
# ./config --prefix=/usr/local/openssl no-zlib
# make && make install
# mv /usr/bin/openssl /usr/bin/openssl.bak
# ln -s /usr/local/openssl/bin/openssl /usr/bin/openssl
# ln -snf /usr/local/openssl/lib/libssl.so.1.1 /usr/lib64/libssl.so.1.1
# ln -snf /usr/local/openssl/lib/libcrypto.so.1.1 /usr/lib64/libcrypto.so.1.1
# mv /usr/include/openssl/ /usr/include/openssl.bak
# ln -s /usr/local/openssl/include/openssl /usr/include/openssl
# ln -s /usr/local/openssl/lib/libssl.so.1.1 /usr/local/lib64/libssl.so
# echo "/usr/local/openssl/lib" >> /etc/ld.so.conf
# echo "CentOS openssl update!"

十五、CentOS7安装Python3.9

对于CentOS7而言,默认的Python为2.7,普通软件依赖高版本Python,可通过如下方式升级。

#cd /tmp
# pyversion=3.9.9
# wget https://mirrors.huaweicloud.com/python/${pyversion}/Python-${pyversion}.tar.xz --no-check-certificate
# tar -Jxvf Python-${pyversion}.tar.xz
# cd Python-${pyversion}/
# ./configure --prefix=/usr/local/python3 --with-openssl=/usr/local/openssl
# make && make install
# pyversionnew=echo ${pyversion} | awk -F '.' 9;{ print $1"."$2 }'
# cp -brpf /usr/local/python3/bin/{python,pip,pydoc}${pyversionnew} /usr/bin/
# cp -brpf /usr/local/python3/bin/python${pyversionnew}-config /usr/bin/
# pyversionold=python -V 2>&1 | awk '{ print $2 }' | awk -F 9;.' '{ print $1"."$2 }'
# pyversionnew=echo ${pyversion} | awk -F '.' '{ print $1"."$2 }'
# mv /usr/bin/python /usr/bin/python.bak
# alternatives --install /usr/bin/python python /usr/bin/python${pyversionold} 30
# alternatives --install /usr/bin/python python /usr/local/python3/bin/python${pyversionnew} 50
# alternatives --auto python
# ln -s /usr/bin/python${pyversionnew} /usr/bin/python3
# ln -s /usr/bin/pip${pyversionnew} /usr/bin/pip3
# ln -s /usr/bin/python${pyversionnew}-config /usr/bin/python3-config3
# echo 'export PATH=$PATH:/usr/local/python3/bin/' >> /root/.bashrc
# source /root/.bashrc
# sed -i '1s/python/python2.7/g' /usr/bin/yum* #仅可执行一次
# sed -i '1s/python/python2.7/g' /usr/libexec/urlgrabber-ext-down #仅可执行一次

提示:对于Python多版本情况下,可能会出现一些依赖性问题,可参考 https://www.cnblogs.com/itzgr/p/16324135.html,在系统存在多个版本Python的情况下借助第三方软件进行版本管理和切换。

十六、配置国内pip源

对于Python而言,将pip源修改为国内也非常重要。

#mkdir -p ~/.pip
# cat > ~/.pip/pip.conf <<EOF
# [global]
# index-url = https://pypi.mirrors.ustc.edu.cn/simple/
# [install]
# trusted-host = pypi.mirrors.ustc.edu.cn
# EOF
# pip3 install --upgrade pip
# pip install pyreadline#

十八、清理垃圾

对于优化/初始化完成后,在正式部署应用交付之前,建议将所有历史信息和垃圾清理掉。

<font size="2">rm -rf /var/log/anaconda/*
rm -rf /var/log/boot.log*
rm -rf /var/log/btmp*
rm -rf /var/log/chrony/*
rm -rf /var/log/cloud-init*
rm -rf /var/log/cron*
rm -rf /var/log/dmesg\.*
rm -rf /var/log/grubby*
rm -rf /var/log/dnf*
rm -rf /var/log/hawkey.log*
rm -rf /var/log/messages*
rm -rf /var/log/qemu-ga*
rm -rf /var/log/rhsm*
rm -rf /var/log/samba*
rm -rf /var/log/secure*
rm -rf /var/log/sssd*
rm -rf /var/log/tallylog
rm -rf /var/log/tuned*
rm -rf /var/log/dnf.*log
rm -rf /var/log/openwebmail.log
rm -rf /var/log/vmware*
rm -rf /var/log/update*.log
rm -rf /var/log/kdump.log
rm -rf /var/spool/mail/root
echo "" > /var/log/audit/audit.log
echo "" > /var/log/boot.log
echo "" > /var/log/btmp
echo "" > /var/log/dmesg
echo "" > /var/log/dnf.log
echo "" > /var/log/firewalld
echo "" > /var/log/httpd/access_log
echo "" > /var/log/httpd/error_log
echo "" > /var/log/syslog
echo "" > /var/log/lastlog
echo "" > /var/log/wtmp
echo "" > /var/log/maillog
echo "" > /var/log/messages
echo "" > /var/log/maillog
echo "" > /var/log/ntp.log
echo "" > /var/log/README
echo "" > /var/log/secure
echo "" > /var/log/spooler
echo "" > /var/log/yum.log
echo "" > $HOME/.bash_history

rm -rf /etc/yum.repos.d/epel.repo
rm -rf /etc/yum.repos.d/epel-*.repo
rm -rf /var/spool/mail/root

rm -rf /tmp/*

history -c

0x03命令作用
提高centos系统的稳定性和易用性

0x04注意事项
以上内容仅供参考,具体操作根据实际环境而定

标签: centos linux 优化

发表评论