包括如下内容:
修改主机名
修改ip地址,需要手动将脚本中网关改成自己网关
关闭防护墙,selinux
主机名和ip地址解析
将本地源换成阿里源
安装常用软件,时间同步
注意:安装时间可能过长,请耐心等待,如果是xshell远程连接中运行此脚本,会导致网卡刷新断开xshell连接,建议在虚拟机中运行
#!/bin/bash
# 主机名初始化
read -p "please input hostname: " hostname
hostnamectl set-hostname $hostname
# IP地址初始化
read -p "please input ip address: " ip_address
cat <<EOF > /etc/sysconfig/network-scripts/ifcfg-ens33
TYPE="Ethernet"
BOOTPROTO="none"
NAME="ens33"
DEVICE="ens33"
ONBOOT="yes"
IPADDR=$ip_address
PREFIX=24
GATEWAY=192.168.140.2
DNS1=223.6.6.6
DNS2=114.114.114.114
EOF
nmcli connection reload &> /dev/null
nmcli connection up ens33 &> /dev/null
if [ $? -eq 0 ];then
echo "set $ip_address success!"
else
echo "set $ip_address failure!"
exit 66
fi
# 主机名和IP地址解析
hostname=$(hostname)
ip_address=$(hostname -I | awk '{print $1}')
echo "$ip_address $hostname" >> /etc/hosts
# 永久关闭iptables
systemctl stop firewalld
systemctl disable firewalld
systemctl status firewalld &> /dev/null
if [ $? -eq 0 ];then
echo "firewalld is not stop "
else
echo "firewalld is stopped"
fi
# 永久关闭SELinux
sed -i 's/^SELINUX=.*/SELINUX=disabled/' /etc/selinux/config
#将本地yum源换成阿里源
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
#下载常用软件
yum install -y wget net-tools vim-enhanced bash-completion lftp psmise unzip bzip2 tree
#时间同步
yum install -y ntpdate &> /dev/null
(crontab -l 2>/dev/null; echo "*/30 * * * * /usr/sbin/ntpdate 120.25.115.20 &> /dev/null") | crontab -
#ssh端口改为44444,禁用root用户
#sed -i '/#Port/c \Port 44444' /etc/ssh/sshd_config
#sed -i '/#PermitRootLogin/c \PermitRootLogin no ' /etc/ssh/sshd_config
echo "系统初始化完成。"