设置主机名、网络、防火墙、selunx、镜像仓库地址
    vim init.sh

    1. #!/bin/bash
    2. echo -e "\n===========\e[32m设置hostname\e[0m=============="
    3. # 设置主机名
    4. read -p "Enter hostname(`hostname`):" hostname
    5. if [[ $hostname ]];then
    6. hostnamectl set-hostname $hostname
    7. echo -e "\e[32m设置主机名:$hostname 成功!\e[0m"
    8. else
    9. echo -e "\e[32m设置主机名:`hostname` 成功!\e[0m"
    10. fi
    11. echo -e "\n===========\e[32m设置network\e[0m==============="
    12. # 正则
    13. cidr_reg="^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])/([1-9]|[1-2][0-9]|3[12])$"
    14. ip_reg="^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$"
    15. # 设置网络ip
    16. read -p "设置网络ip(eg:192.168.1.1/24):" address
    17. address=`echo $address | grep -Eoe ${cidr_reg}`
    18. # 设置网关
    19. read -p "设置网关(eg:192.168.1.254):" gateway
    20. gateway=`echo $gateway | grep -Eoe ${ip_reg}`
    21. if [[ $address && $gateway ]];then
    22. nmcli connection modify eth0 \
    23. connection.autoconnect yes \
    24. ipv4.method manual \
    25. ipv4.addresses $address \
    26. ipv4.gateway $gateway
    27. # 激活网卡
    28. nmcli connection up eth0
    29. else
    30. echo -e "\e[31m输入的参数无效!\e[0m"
    31. fi
    32. # 是否关闭防火墙
    33. echo -e "\n===========\e[32m设置firewalld\e[0m============="
    34. read -p "是否关闭防火墙?[y/n]:" action
    35. case $action in
    36. y|Y|YES|yes)
    37. systemctl disable firewalld && systemctl stop firewalld && echo -e "\e[32m防火墙关闭成功!\e[0m"
    38. ;;
    39. n|N|NO|no)
    40. systemctl enable firewalld && systemctl restart firewalld && echo -e "\e[32m防火墙启动成功!\e[0m"
    41. ;;
    42. esac
    43. # 是否关闭selinux
    44. echo -e "\n===========\e[32m设置selinux\e[0m==============="
    45. read -p "是否关闭selinux?[y/n]:" action
    46. case $action in
    47. y|Y|YES|yes)
    48. setenforce 0 && sed -i 's/^SELINUX=.*/SELINUX=disabled/' /etc/selinux/config && echo -e "\e[32mselinux关闭成功!\e[0m"
    49. ;;
    50. n|N|NO|no)
    51. setenforce 1 && sed -i 's/^SELINUX=.*/SELINUX=enforcing/' /etc/selinux/config && echo -e "\e[32mselinux开启成功!\e[0m"
    52. ;;
    53. esac
    54. echo -e "\n=============\e[32m设置yum\e[0m================="
    55. read -p "设置yum镜像仓库(default:192.100.3.221):" yum_repository
    56. yum_repository=`echo $yum_repository | grep -Eoe ${ip_reg}`
    57. if [[ ! $yum_repository ]];then
    58. yum_repository=192.100.3.221
    59. fi
    60. if [[ `grep "yum_repository" /etc/hosts` ]];then
    61. sed -ri "/yum_repository/"c"$yum_repository yum_repository" /etc/hosts
    62. else
    63. echo "$yum_repository yum_repository" >> /etc/hosts
    64. fi
    65. echo -e "\n======\e[32m测试镜像仓库的网络连通性\e[0m======"
    66. ping yum_repository -w 3