1、Linux学习环境系统快速配置脚本
- 在 Linux 系统安装完成后,通常需要进行一系列的配置和优化操作,以确保系统能够顺利运行并满足特定的使用需求。这个过程可能会涉及到网络设置、SELinux 状态管理、防火墙配置以及安装必要的软件包等。为了简化这一过程,我编写了一个自动化脚本来执行这些常规任务
2、脚本功能概览:
系统信息展示:显示当前系统的发行版、网络接口名称、本机 IP 地址和主机名。 网络配置:检查并设置网络接口为开机自动连接,配置静态 IP地址,并确保网络连通性。
SELinux 管理:自动检测 SELinux 状态,并在必要时将其关闭,以避免潜在的安全策略冲突。
防火墙管理:关闭系统防火墙,以减少不必要的网络通信限制。
软件安装:安装一系列常用的系统管理软件,如 vim、wget、net-tools等,以提高工作效率。
系统操作选项:提供关机、重启或退出的选项,以便用户根据需要进行选择。
3、使用说明:
- 将脚本保存为 .sh 文件,bash file.sh即可。
4、注意:
- 请根据实际系统环境和需求调整脚本中的配置选项。
- 在生产环境中使用前,请确保充分测试脚本以避免意外情况。
- 这个脚本是我个人在学习 Linux 过程中为了提高工作效率而编写的,现在分享出来,希望对同样在学习 Linux 的你有所帮助。如果你有任何改进建议或遇到问题,欢迎交流讨论!
5、脚本内容
5.1 CentOS 7.9初始化脚本
#!/bin/bash
echo_red() { echo -e "\e[31m$1\e[0m"; }
echo_green() { echo -e "\e[32m$1\e[0m"; }
echo_yellow() { echo -e "\e[33m$1\e[0m"; }
interface=$(ip route | grep default | sed -e "s/^.*dev.//" -e "s/.proto.*//")
LocalIP=$(ip addr show $interface | awk '/inet / {print $2}' | cut -d/ -f1 | tail -1)
linux=$(awk -F "(" '{print $1}' /etc/redhat-release)
hostname=$(hostname)
echo_yellow "当前系统发行版为 $linux"
echo_yellow "当前系统网卡名为 $interface"
echo_yellow "本机IP地址为 $LocalIP"
echo_yellow "本机主机名为 $hostname"
echo_yellow "内核版本为 $(uname -r)"
sleep 2
echo_yellow '正在初始化请稍后...'
autoconnect=$(nmcli dev show $interface | grep 'GENERAL.AUTOCONNECT:' | awk '{print $2}')
if [[ "$autoconnect" != "yes" ]]; then
echo_yellow "检查网卡是否为开机自动连接..."
nmcli con mod $interface connection.autoconnect yes &>/dev/null
nmcli con up $interface &>/dev/null
sed -i '/^ONBOOT/ c ONBOOT=yes' /etc/sysconfig/network-scripts/"ifcfg-${interface}" &>/dev/null
systemctl restart network &>/dev/null
echo_green "网卡已设置为开机自动连接"
else
echo_green "网卡已设置为开机自动连接"
fi
ip_mode=$(nmcli dev show $interface | grep 'IP4.ADDRESS[1]' | awk '{print $2}')
gateway_dns=$(ip addr show $interface | awk '/inet / {split($2,a,"."); print a[1]"."a[2]"."a[3]}')
ip_mode2=$(grep BOOTPROTO /etc/sysconfig/network-scripts/"ifcfg-$interface" | awk -F "=" '{print $2}')
if [[ -z "$ip_mode" ]] || [ "$ip_mode2" != static ] ; then
echo_green "正在检查网卡是否为静态IP地址..."
nmcli con mod $interface ipv4.addresses "${LocalIP}/24" &>/dev/null
nmcli con mod $interface ipv4.gateway "${gateway_dns}.2" &>/dev/null
nmcli con mod $interface ipv4.dns "${gateway_dns}.2" &>/dev/null
nmcli con mod $interface ipv4.method manual &>/dev/null
nmcli con up $interface &>/dev/null
sed -i '/^BOOTPROTO/ c BOOTPROTO=static' /etc/sysconfig/network-scripts/"ifcfg-${interface}" &>/dev/null
systemctl restart network &>/dev/null
echo_green "网卡已设置静态IP"
else
echo_green "网卡已设置静态IP"
fi
for i in {1..3}; do
if ping -c 4 -i 0.2 223.5.5.5 &>/dev/null; then
echo_green "网络连通性正常"
break
else
echo_red "网络错误请检查网络配置"
echo_yellow "正在尝试重启网络(尝试次数:$i)"
systemctl restart network &>/dev/null
nmcli con up $interface &>/dev/null
sleep 5
fi
done
manage_selinux_firewall() {
SELINUXSTATUS=$(getenforce)
if [[ "$SELINUXSTATUS" == "Disabled" ]] || [[ "$SELINUXSTATUS" == "Permissive" ]]; then
echo_green "SELinux已成功关闭或当前处于宽容模式"
else
echo_red "SELinux未能关闭,正在尝试手动关闭..."
sed -i '/^SELINUX=/ c\SELINUX=disabled' /etc/selinux/config &>/dev/null
setenforce 0
if [[ "$(getenforce)" == "Disabled" ]] || [[ "$(getenforce)" == "Permissive" ]]; then
echo_green "SELinux已成功关闭"
else
echo_red "SELinux未能关闭,请手动解决"
fi
fi
}
manage_selinux_firewall
FIREWALLSTATUS=$(systemctl is-active firewalld)
if [[ "$FIREWALLSTATUS" == "active" ]]; then
echo_yellow "防火墙状态为开启,正在关闭防火墙..."
systemctl disable --now firewalld &>/dev/null
echo_green "防火墙已关闭"
else
echo_green "防火墙无需操作"
fi
# 备份原有的CentOS-Base.repo
echo_yellow "正在备份原有的yum源配置文件..."
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup &>/dev/null
if [ $? -ne 0 ]; then
echo_red "备份yum源配置文件失败,请检查权限或文件路径"
else
echo_green "备份成功"
fi
# 下载新的CentOS-Base.repo
echo_yellow "正在下载新的yum源配置文件..."
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo &>/dev/null
if [ $? -eq 0 ]; then
echo_green "新的yum源配置文件下载成功"
else
echo_red "下载新的yum源配置文件失败,请检查网络连接或URL"
fi
# 清空yum缓存
echo_yellow "正在清空yum缓存..."
yum clean all &>/dev/null
if [ $? -eq 0 ]; then
echo_green "yum缓存清空成功"
else
echo_red "清空yum缓存失败"
fi
# 检查yum源是否更新成功
echo_yellow "正在检查yum源是否更新成功..."
yum makecache &>/dev/null
if [ $? -eq 0 ]; then
echo_green "yum源更新成功"
else
echo_red "yum源更新失败,请检查配置文件"
fi
PACKAGES="lrzsz ntpdate sysstat net-tools wget vim bash-completion dos2unix tree psmisc chrony rsync lsof"
echo_yellow "正在安装常用软件..."
yum -y install $PACKAGES &>/dev/null
if [ $? -eq 0 ]; then
echo_green "安装成功"
else
echo_red "安装失败,请检测yum镜像仓库"
fi
echo_yellow "准备开启 $linux 系统的体验吧!!!"
# Prompt for shutdown, reboot, or exit
echo_yellow "请选择一个操作:"
echo_yellow " 关机 : 1"
echo_yellow " 重启 : 2"
echo_yellow " 退出 : 3"
read -p "请输入您的选择: " choice
delete_script() {
echo_yellow "脚本执行完毕,正在删除自身..."
rm -f "$0"
echo_green "脚本已删除。"
}
case $choice in
1)
echo_yellow "正在执行关机操作..."
delete_script
init 0
# 关机前删除脚本自身
;;
2)
echo_green "正在执行重启操作..."
delete_script
reboot
# 重启前删除脚本自身
;;
3)
echo_yellow "请继续操作,脚本将退出。"
# 退出前删除脚本自身
delete_script
exit 0
;;
*)
echo_red "无效的输入"
# 退出前删除脚本自身
delete_script
exit 1
;;
esac
5.2 openEuler 初始化脚本
#!/bin/bash
echo_red() { echo -e "\e[31m$1\e[0m"; }
echo_green() { echo -e "\e[32m$1\e[0m"; }
echo_yellow() { echo -e "\e[33m$1\e[0m"; }
interface=$(ip route | grep default | sed -e "s/^.*dev.//" -e "s/.proto.*//")
LocalIP=$(ip addr show $interface | awk '/inet / {print $2}' | cut -d/ -f1 | tail -1)
linux=$(cat /etc/openEuler-release)
hostname=$(hostname)
echo_yellow "当前系统发行版为 $linux"
echo_yellow "当前系统网卡名为 $interface"
echo_yellow "本机IP地址为 $LocalIP"
echo_yellow "本机主机名为 $hostname"
echo_yellow "内核版本为 $(uname -r)"
sleep 2
echo_yellow '正在初始化请稍后...'
autoconnect=$(nmcli dev show $interface | grep 'GENERAL.AUTOCONNECT:' | awk '{print $2}')
if [[ "$autoconnect" != "yes" ]]; then
echo_yellow "检查网卡是否为开机自动连接..."
nmcli con mod $interface connection.autoconnect yes &>/dev/null
nmcli con up $interface &>/dev/null
sed -i '/^ONBOOT/ c ONBOOT=yes' /etc/sysconfig/network-scripts/"ifcfg-${interface}" &>/dev/null
systemctl restart network &>/dev/null
echo_green "网卡已设置为开机自动连接"
else
echo_green "网卡已设置为开机自动连接"
fi
ip_mode=$(nmcli dev show $interface | grep 'IP4.ADDRESS[1]' | awk '{print $2}')
gateway_dns=$(ip addr show $interface | awk '/inet / {split($2,a,"."); print a[1]"."a[2]"."a[3]}')
ip_mode2=$(grep BOOTPROTO /etc/sysconfig/network-scripts/"ifcfg-$interface" | awk -F "=" '{print $2}')
if [[ -z "$ip_mode" ]] || [ "$ip_mode2" != static ] ; then
echo_green "正在检查网卡是否为静态IP地址..."
nmcli con mod $interface ipv4.addresses "${LocalIP}/24" &>/dev/null
nmcli con mod $interface ipv4.gateway "${gateway_dns}.2" &>/dev/null
nmcli con mod $interface ipv4.dns "${gateway_dns}.2" &>/dev/null
nmcli con mod $interface ipv4.method manual &>/dev/null
nmcli con up $interface &>/dev/null
sed -i '/^BOOTPROTO/ c BOOTPROTO=static' /etc/sysconfig/network-scripts/"ifcfg-${interface}" &>/dev/null
systemctl restart network &>/dev/null
echo_green "网卡已设置静态IP"
else
echo_green "网卡已设置静态IP"
fi
for i in {1..3}; do
if ping -c 4 -i 0.2 223.5.5.5 &>/dev/null; then
echo_green "网络连通性正常"
break
else
echo_red "网络错误请检查网络配置"
echo_yellow "正在尝试重启网络(尝试次数:$i)"
systemctl restart network &>/dev/null
nmcli con up $interface &>/dev/null
sleep 5
fi
done
manage_selinux_firewall() {
SELINUXSTATUS=$(getenforce)
if [[ "$SELINUXSTATUS" == "Disabled" ]] || [[ "$SELINUXSTATUS" == "Permissive" ]]; then
echo_green "SELinux已成功关闭或当前处于宽容模式"
else
echo_red "SELinux未能关闭,正在尝试手动关闭..."
sed -i '/^SELINUX=/ c\SELINUX=disabled' /etc/selinux/config &>/dev/null
setenforce 0
if [[ "$(getenforce)" == "Disabled" ]] || [[ "$(getenforce)" == "Permissive" ]]; then
echo_green "SELinux已成功关闭"
else
echo_red "SELinux未能关闭,请手动解决"
fi
fi
}
manage_selinux_firewall
FIREWALLSTATUS=$(systemctl is-active firewalld)
if [[ "$FIREWALLSTATUS" == "active" ]]; then
echo_yellow "防火墙状态为开启,正在关闭防火墙..."
systemctl disable --now firewalld &>/dev/null
echo_green "防火墙已关闭"
else
echo_green "防火墙无需操作"
fi
PACKAGES="lrzsz ntpdate sysstat net-tools wget vim bash-completion dos2unix tree psmisc chrony rsync lsof"
echo_yellow "正在安装常用软件..."
yum -y install $PACKAGES &>/dev/null
if [ $? -eq 0 ]; then
echo_green "安装成功"
else
echo_red "安装失败,请检测yum镜像仓库"
fi
echo_yellow "准备开启 $linux 系统的体验吧!!!"
# Prompt for shutdown, reboot, or exit
echo_yellow "请选择一个操作:"
echo_yellow " 关机 : 1"
echo_yellow " 重启 : 2"
echo_yellow " 退出 : 3"
read -p "请输入您的选择: " choice
delete_script() {
echo_yellow "脚本执行完毕,正在删除自身..."
rm -f "$0"
echo_green "脚本已删除。"
}
case $choice in
1)
echo_yellow "正在执行关机操作..."
delete_script
init 0
# 关机前删除脚本自身
;;
2)
echo_green "正在执行重启操作..."
delete_script
reboot
# 重启前删除脚本自身
;;
3)
echo_yellow "请继续操作,脚本将退出。"
# 退出前删除脚本自身
delete_script
exit 0
;;
*)
echo_red "无效的输入"
# 退出前删除脚本自身
delete_script
exit 1
;;
esac