系统优化脚本
- centos7
- rocky10
- ubuntu
#!/usr/bin/env bash
#
set -e
# 修改系统yum源为aliyun并添加epel源
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak
[ ! -e /etc/yum.repos.d/CentOS-Base.repo ] && curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo && sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
[ ! -e /etc/yum.repos.d/epel.repo ] && curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
yum makecache
yum -y install ntpdate tar wget net-tools git vim tree lrzsz htop iftop iotop psmisc python36 python3-devel zlib zlib-devel gcc gcc-c++ conntrack-tools jq socat bash-completion telnet nload strace tcpdump lsof sysstat
# 关闭swap
sed -i 's/.*swap.*/#&/' /etc/fstab
# 关闭防火墙、selinux、NetworkManager
systemctl disable firewalld NetworkManager
sed -i '7s/enforcing/disabled/' /etc/selinux/config
# 同步时间计划任务
echo "*/10 * * * * /usr/sbin/ntpdate ntp2.aliyun.com &>/dev/null" >> /var/spool/cron/root
# 历史命令显示时间
sed -i '/HISTFILESIZE=2000/d' /etc/bashrc
sed -i '/HISTSIZE=2000/d' /etc/bashrc
sed -i '/HISTTIMEFORMAT="%Y-%m-%d %H:%M:%S "/d' /etc/bashrc
sed -i '/export HISTTIMEFORMAT/d' /etc/bashrc
cat >> /etc/bashrc << EOF
HISTFILESIZE=2000
HISTSIZE=2000
HISTTIMEFORMAT="%Y-%m-%d %H:%M:%S "
export HISTTIMEFORMAT
EOF
source /etc/bashrc
# 修改系统最大文件描述符
sed -i '/root soft nofile 65535/d' /etc/security/limits.conf
sed -i '/root hard nofile 65535/d' /etc/security/limits.conf
sed -i '/* soft nofile 65535/d' /etc/security/limits.conf
sed -i '/* hard nofile 65535/d' /etc/security/limits.conf
cat >>/etc/security/limits.conf<<'EOF'
root soft nofile 65535
root hard nofile 65535
* soft nofile 65535
* hard nofile 65535
EOF
# 设置pip国内源
[ -d ~/.pip ] || mkdir ~/.pip
cat > ~/.pip/pip.conf << EOF
[global]
index-url = https://mirrors.aliyun.com/pypi/simple/
[install]
trusted-host=mirrors.aliyun.com
EOF
# 安装 glances mycli
pip3 install --upgrade pip
pip3 install glances mycli==1.24.0
# 设置别名
cat >> ~/.bashrc << EOF
alias n='netstat -ntpl'
alias u='netstat -nupl'
alias a='netstat -natupl'
EOF
# 重启系统使部分配置生效
reboot
#!/usr/bin/env bash
#
set -e
# 修改系统yum源为阿里云
sed -e 's|^mirrorlist=|#mirrorlist=|g' \
-e 's|^#baseurl=http://dl.rockylinux.org/$contentdir|baseurl=https://mirrors.aliyun.com/rockylinux|g' \
-i.bak \
/etc/yum.repos.d/[Rr]ocky*.repo
# 添加epel源
dnf install -y epel-release
# 修改epel源为阿里云
sed -e 's|^metalink=|#metalink=|g' \
-e 's|^#baseurl=https://download.example/pub/epel|baseurl=https://mirrors.aliyun.com/epel|g' \
-i.bak \
/etc/yum.repos.d/epel.repo
# 更新缓存
dnf makecache
# 设置24小时制
localectl set-locale LC_TIME=en_GB.UTF-8
# 安装一些常用工具
dnf -y install \
yum-utils \
tar \
wget \
net-tools \
git \
vim \
tree \
lrzsz \
htop \
iftop \
iotop \
psmisc \
python3-devel \
zlib \
zlib-devel \
gcc \
gcc-c++ \
conntrack-tools \
jq \
socat \
bash-completion \
telnet \
nload \
strace \
tcpdump \
lsof \
sysstat
# 关闭swap
sed -i.bak '/swap/s/^/#/' /etc/fstab
# 关闭防火墙、selinux
systemctl disable firewalld
sed -i.bak 's/^SELINUX=.*/SELINUX=disabled/' /etc/selinux/config
# 配置时间同步
dnf install -y chrony
systemctl enable --now chronyd
sed -i.bak -e '/^pool 2\.rocky\.pool\.ntp\.org iburst$/d' \
-e '/^# Use NTP servers from DHCP\./i\
server ntp.aliyun.com iburst\n\
server ntp.tencent.com iburst\n\
server cn.pool.ntp.org iburst
' /etc/chrony.conf
systemctl restart chronyd
# 配置手动同步时间
echo "*/10 * * * * /usr/bin/chronyc -a makestep >/dev/null 2>&1" >> /var/spool/cron/root
# 历史命令显示时间
sed -i '/HISTFILESIZE=2000/d' /etc/bashrc
sed -i '/HISTSIZE=2000/d' /etc/bashrc
sed -i '/HISTTIMEFORMAT="%Y-%m-%d %H:%M:%S "/d' /etc/bashrc
sed -i '/export HISTTIMEFORMAT/d' /etc/bashrc
cat >> /etc/bashrc << EOF
HISTFILESIZE=2000
HISTSIZE=2000
HISTTIMEFORMAT="%Y-%m-%d %H:%M:%S "
export HISTTIMEFORMAT
EOF
source /etc/bashrc
# 配置历史命令显示时间
sed -i.bak '/HISTFILESIZE=2000/d; /HISTSIZE=2000/d; /HISTTIMEFORMAT="/d; /export HISTTIMEFORMAT/d' /etc/bashrc
cat >> /etc/bashrc << 'EOF'
HISTFILESIZE=2000
HISTSIZE=2000
HISTTIMEFORMAT="%Y-%m-%d %H:%M:%S "
export HISTTIMEFORMAT
EOF
source /etc/bashrc
# 修改系统最大文件描述符
cat >> /etc/security/limits.conf << 'EOF'
root soft nofile 65535
root hard nofile 65535
* soft nofile 65535
* hard nofile 65535
EOF
# 设置pip国内源
[ -d ~/.pip ] || mkdir ~/.pip
cat > ~/.pip/pip.conf << EOF
[global]
index-url = https://mirrors.aliyun.com/pypi/simple/
[install]
trusted-host=mirrors.aliyun.com
EOF
# 安装 glances mycli
pip3 install --upgrade pip
pip3 install glances mycli
# 设置别名
cat >> ~/.bashrc << EOF
alias n='netstat -ntpl'
alias u='netstat -nupl'
alias a='netstat -natupl'
EOF
# 重启系统使部分配置生效
reboot
This is a banana 🍌