Linux 初始化
第一次写博客,主要是记录自己学习过程中的一些知识点,分析给大家,欢迎大家一起交流!
-----------------------------
1. 使用的环境主要是workstation,运行Linux虚拟机;
2. Linux版本:rocky Linux 8.6
3. 第一篇博客主要介绍的类容是对Linux虚拟机安装完成后,使用脚本自动化的初始化的工作:
vim init-setup.sh
#!/bin/bash
#
set -ue
ENS=`nmcli device show | grep "GENERAL.CONNECTION"`
hostnamectl --static set-hostname nodeX
echo "IPADDR=192.168.2.121" >> /etc/sysconfig/network-scripts/ifcfg-$ENS
sed -i "/BOOTPROTO=dhcp/c BOOTPROTO=none" /etc/sysconfig/network-scripts/ifcfg-$ENS
sed -i "/ONBOOT=no/c ONBOOT=yes" /etc/sysconfig/network-scripts/ifcfg-$ENS
echo "NETMASK=255.255.255.0" >> /etc/sysconfig/network-scripts/ifcfg-$ENS
echo "GATEWAY=192.168.2.1" >> /etc/sysconfig/network-scripts/ifcfg-$ENS
echo "DNS1=114.114.114.114" >> /etc/sysconfig/network-scripts/ifcfg-$ENS
echo "DNS2=223.5.5.5" >> /etc/sysconfig/network-scripts/ifcfg-$ENS
systemctl set-default multi-user.target
sed -i '/SELINUX=enforcing/c SELINUX=disabled' /etc/selinux/config
systemctl stop firewalld.service
systemctl disable firewalld.service
timedatectl set-timezone Asia/Shanghai
## 对旧的repo文件保留,不作删除
mkdir /etc/yum.repos.d/bak
mv /etc/yum.repos.d/* /etc/yum.repos.d/bak/
############################################################################################
echo "[infra]
name=Centos_8 aliyun.com-infra-common
baseurl=https://mirrors.aliyun.com/centos/\$releasever/infra/\$basearch/infra-common/
enabled=1
gpgcheck=0">> /etc/yum.repos.d/infra-common.repo
echo "[epel]
name=Centos_8 aliyun.com-epel
baseurl=https://mirrors.aliyun.com/epel/8/Everything/x86_64/
enabled=1
gpgcheck=0" >> /etc/yum.repos.d/epel8.repo
echo "[extras]
name=Centos_8 mirrors.aliyun.com-extras
baseurl=https://mirrors.aliyun.com/centos/\$releasever/extras/x86_64/os/
enabled=1
gpgcheck=0" >> /etc/yum.repos.d/extras.repo
## stream源;
echo "[core]
name=Centos_8 aliyun.com-core
baseurl=https://mirrors.aliyun.com/centos/8-stream/core/\$basearch/centos-plus/
enabled=1
gpgcheck=0">> /etc/yum.repos.d/core.repo
echo "[base]
name=Centos_8 aliyun.com-base
baseurl=https://mirrors.aliyun.com/centos/8-stream/BaseOS/x86_64/os/
enabled=1
gpgcheck=0">> /etc/yum.repos.d/base.repo
cat > /etc/yum.repos.d/CentOS-AppStream.repo << EOF
[AppStream]
name=Centos_8 aliyun.com-AppStream
baseurl=https://mirrors.aliyun.com/centos/8-stream/AppStream/\$basearch/os/
enabled=1
gpgcheck=0
EOF
cat > /etc/yum.repos.d/CentOS-centosplus.repo << EOF
[centosplus]
name=Centos_8 aliyun.com-Plush
baseurl=https://mirrors.aliyun.com/centos/8-stream/centosplus/\$basearch/os/
enabled=1
gpgcheck=0
EOF
cat > /etc/yum.repos.d/CentOS-Devel.repo << EOF
[Devel]
name=Centos_8 aliyun.com-Devel
baseurl=https://mirrors.aliyun.com/centos/8-stream/Devel/\$basearch/os/
enabled=1
gpgcheck=0
EOF
cat > /etc/yum.repos.d/CentOS-HighAvailability.repo << EOF
[HighAvailability]
name=Centos_8 aliyun.com-HighAvailability
baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos/8-stream/HighAvailability/x86_64/os/
enabled=1
gpgcheck=0
EOF
cat > /etc/yum.repos.d/CentOS-PowerTools.repo << EOF
[PowerTools]
name=Centos_8 aliyun.com-PowerTools
baseurl=https://mirrors.aliyun.com/centos/8-stream/PowerTools/\$basearch/os/
enabled=1
gpgcheck=0
EOF
### echo "[InstallMedia] ###光盘镜像基本用不上;
### name=Centos_8 media-sr0-cdrom
### baseurl=file:////media/BaseOS/
### enabled=1
### gpgcheck=0" >> /etc/yum.repos.d/media.repo
echo "blacklist i2c_piix4" > /etc/modprobe.d/blacklist.conf
############################################################################################
dnf -y install vim-enhanced chrony openssh-clients tcpdump tree net-tools lsof psmisc lrzsz man mtr bash-completion network-scripts wget mtr nmap htop tmux sysstat tmux man-pages glances
systemctl enable network
systemctl restart network
### bash-completion 补全增强-tab补全一切 net-tools里面有ifconfig工具 func, ### mailx 这个没什么用;
sleep 1
systemctl restart chronyd
sed -i '/net.ipv4.ip_forward=0/c net.ipv4.ip_forward=1/' /etc/sysctl.conf #打开转发功能;
echo "net.ipv4.ip_forward=1" >> /etc/sysctl.conf
sysctl -p #让上述配置生效
sed -i '/UseDNS/c UseDNS no' /etc/ssh/sshd_config
sed -i '/GSSAPIAuthentication/c GSSAPIAuthentication no' /etc/ssh/sshd_config
sed -i '/ask$/c StrictHostKeyChecking no' /etc/ssh/ssh_config ###交互式,免输入yes/no;
systemctl restart sshd
### CentOS7启动报 Host SMbus controller not enabled 错;
echo "blacklist i2c_piix4" > /etc/modprobe.d/blacklist.conf
### CentOS7启动报 sd 0:0:0:0: [sda] Assuming drive cache: write through 错误;
sed -i 's/rhgb//g' /etc/default/grub #去掉rhgb参数;
grub2-mkconfig -o /boot/grub2/grub.cfg
echo "alias cdnet='cd /etc/sysconfig/network-scripts'" >> /etc/profile.d/env.sh ###定义进入网卡设备目录;
echo 'export PS1="\[\e[35;40m\][\u@\h \w]\\$ \[\e[m\]"' >> /etc/profile.d/env.sh
---------------------------------
上面的脚本执行的时候,部分是没有幂等性的,可以根据自己需要,手动选择一部分一部分的执行。
欢迎大神提出优化建议
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?