- #!/bin/bash
- ## 初始化网络 (可在虚拟网络编辑器查看自己的网段)
- ## 方法一: 静态初始化
- read -p "输入你当前 Linux 的 IP 地址:" ip
- ETH=` ifconfig -a | head -1 | awk -F ":" '{print $1}'`
- GATE="`echo $ip | awk -F"."'{print $1"."$2"."$3"."}'`.2"
- cat> /etc/sysconfig/network-scripts/ifcfg-$ETH <<EOF
- TYPE=Ethernet
- BOOTPROTO=static
- NAME=$ETH
- DEVICE=$ETH
- IPADDR="$ip"
- GATEWAY=$GATE
- ONBOOT=yes
- DNS1=8.8.8.8
- EOF
- ## 方法 2:dhcp 分配
- cat> /etc/sysconfig/network-scripts/ifcfg-$ETH <<EOF
- TYPE=Ethernet
- BOOTPROTO=dhcp
- NAME=$ETH
- DEVICE=$ETH
- ONBOOT=yes
- EOF
- ## 更改主机名 (RHEL6)
- cat> /etc/sysconfig/network <<EOF
- NETWORKING=yes
- HOSTNAME=server-123
- EOF
- ## 更改主机名 (RHEL7)
- hostnamectl --static set-hostname server-123
- ## 关闭防火墙 (RHEL6)
- iptables -t filter -F
- iptables -t nat -F
- service iptables save
- service iptables stop
- chkconfig iptables off
- chkconfig ip6tables off
- chkconfig ip6tables off
- ## 关闭防火墙 (RHEL7)
- iptables -t filter -F
- iptables -t nat -F
- systemctl save iptables
- systemctl stop iptables
- systemctl disabled iptables
- ## 禁用 SELinux
- sed -ir '/SELINUX/{s/enforcing/disabled/}' /etc/selinux/config
- ## 将 sr0 永久性挂载
- mkdir /iso chmod u+x /etc/rc.d/rc.local echo "mount /dev/sr0 /iso">> /etc/rc.d/rc.local
- ## 配置 yum 源
- touch /etc/yum.repos.d/base.repo
- cat> /etc/yum.repos.d/base.repo << EOF
- [base]
- name=mybase
- baseurl=file:///iso/
- enabled=1
- gpgcheck=0
- EOF
- ## 适用于 REHL6,RHEL7
- ## 执行脚本后虚拟机会关机重启
- init 6
来源: http://www.bubuko.com/infodetail-2761407.html