脚本

  1. #!/bin/bash
  2. eth0=eth0
  3. # 正则
  4. 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])$"
  5. 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])$"
  6. # 设置网络ip
  7. read -p "set ip(eg:192.168.1.1/24):" address
  8. address=`echo $address | grep -Eoe ${cidr_reg}`
  9. # 设置网关
  10. read -p "set gateway(eg:192.168.1.254):" gateway
  11. gateway=`echo $gateway | grep -Eoe ${ip_reg}`
  12. # 设置dns
  13. read -p "set dns(eg:8.8.8.8):" dns
  14. dns=`echo $dns | grep -Eoe ${ip_reg}`
  15. if [[ $address && $gateway ]];then
  16. if [[ $dns ]];then
  17. nmcli connection modify $eth0 \
  18. connection.autoconnect yes \
  19. ipv4.method manual \
  20. ipv4.addresses $address \
  21. ipv4.gateway $gateway \
  22. ipv4.dns $dns
  23. else
  24. nmcli connection modify $eth0 \
  25. connection.autoconnect yes \
  26. ipv4.method manual \
  27. ipv4.addresses $address \
  28. ipv4.gateway $gateway
  29. fi
  30. # 激活网卡
  31. nmcli connection up $eth0
  32. else
  33. echo -e "\e[31m invalid parameter!\e[0m"
  34. fi

1.nmcli命令设置

设置网络:1.手动分配ip

[network-scripts]# nmcli connection modify ens36 \
> connection.autoconnect yes \
> ipv4.method manual \
> ipv4.addresses 192.168.190.133/24 \
> ipv4.gateway 192.168.190.2 \
> ipv4.dns 8.8.8.8

设置网络:2.自动分配ip

[network-scripts]# nmcli connection modify ens36 \
> connection.autoconnect yes \
> ipv4.method auto

激活

[root@ceph-node-1 network-scripts]# nmcli connection up ens36
连接已成功激活(D-Bus 活动路径:/org/freedesktop/NetworkManager/ActiveConnection/3)

查看

[root@ceph-node-1 network-scripts]# nmcli connection show ens36

2.手动修改配置文件

vim /etc/sysconfig/network-scripts/ifcfg-ens36

  1. TYPE=Ethernet
  2. PROXY_METHOD=none
  3. BROWSER_ONLY=no
  4. BOOTPROTO="static"
  5. DEFROUTE=yes
  6. IPV4_FAILURE_FATAL=no
  7. IPV6INIT=yes
  8. IPV6_AUTOCONF=yes
  9. IPV6_DEFROUTE=yes
  10. IPV6_FAILURE_FATAL=no
  11. IPV6_ADDR_GEN_MODE=stable-privacy
  12. NAME=ens36
  13. DEVICE=ens36
  14. ONBOOT=yes
  15. IPADDR=192.168.190.133
  16. PREFIX=24
  17. GATEWAY=192.168.190.2
  18. DNS1=8.8.8.8
  19. UUID=418da202-9a8c-b73c-e8a1-397e00f3c6b2
  20. IPV4_ROUTE_METRIC=0 #路由优先级

3.nmtui图形化设置