修改主机名,网卡以及设置固定IP

Margaret ·
更新时间:2024-11-14
· 704 次阅读

修改主机名,网卡以及设置固定IP

其中基于主机centos6,7,8 以及Ubuntu1804

修改主机名 #centos6.10 vim /etc/sysconfig/network 修改HOSTNAME=后面你想设定的主机名 #ubuntu1804和centos7和centos8 hostnamectl set-hostname 主机名 修改网卡名 #centos7 [root@centos7 ~]#vim /etc/sysconfig/network-scripts/ifcfg-ens33 [root@centos7 ~]#cat /etc/sysconfig/network-scripts/ifcfg-ens33 HWADDR=00:0C:29:A9:27:CA TYPE=Ethernet PROXY_METHOD=none BROWSER_ONLY=no BOOTPROTO=dhcp DEFROUTE=yes IPV4_FAILURE_FATAL=no IPV6INIT=yes IPV6_AUTOCONF=yes IPV6_DEFROUTE=yes IPV6_FAILURE_FATAL=no IPV6_ADDR_GEN_MODE=stable-privacy NAME=eth0 #ens33改为eth0 UUID=da5ad812-cee5-401d-b648-54fea87ce051 DEVICE=eth0 #ens33改为eth0 ONBOOT=yes [root@centos7 ~]#mv /etc/sysconfig/network-scripts/ifcfg-ens33 /etc/sysconfig/network-scripts/ifcfg-eth0 [root@centos7 ~]sed -i.bak '/GRUB_CMDLINE_LINUX=/s#"$# net.ifnames=0 biosdevname=0"#' /etc/default/grub [root@centos7 ~]#cat /etc/default/grub GRUB_TIMEOUT=5 GRUB_DISTRIBUTOR="$(sed 's, release .*$,,g' /etc/system-release)" GRUB_DEFAULT=saved GRUB_DISABLE_SUBMENU=true GRUB_TERMINAL_OUTPUT="console" GRUB_CMDLINE_LINUX="spectre_v2=retpoline rhgb quiet net.ifnames=0 biosdevname=0" #改的这一项 GRUB_DISABLE_RECOVERY="true" [root@centos7 ~]#grub2-mkconfig -o /boot/grub2/grub.cfg [root@centos7 ~]#reboot #centos8 [root@centos8 ~]#sed -i.bak '/GRUB_CMDLINE_LINUX=/s#"$# net.ifnames=0"#' /etc/default/grub [root@centos8 ~]#grub2-mkconfig -o /boot/grub2/grub.cfg &> /dev/null [root@centos8 ~]#mv /etc/sysconfig/network-scripts/ifcfg-ens33 /etc/sysconfig/network-scripts/ifcfg-eth0 [root@centos8 ~]#reboot #Ubuntu1804 #因为Ubuntu默认是一般用户不是root 所以改配置文件前面要加sudo,当然也可以使用命令 sudo -i 切换至root zxr@ubuntu1804:~$ sudo sed -i '/GRUB_CMDLINE_LINUX=/s#"$# net.ifnames=0"#' /etc/default/grub zxr@ubuntu1804:~$ sudo update-grub zxr@ubuntu1804:~$ sudo vim /etc/netplan/*.yaml zxr@ubuntu1804:~$ cat /etc/netplan/*.yaml # This file describes the network interfaces available on your system # For more information, see netplan(5). network: version: 2 renderer: networkd ethernets: eth0: #将ens33改为eth0 (注意缩进!!!) dhcp4: yes zxr@ubuntu1804:~$ reboot 修改为固定IP ******************centos6 [root@centos6 ~]#vim /etc/sysconfig/network-scripts/ifcfg-eth0 DEVICE=eth0 HWADDR=00:0C:29:91:7F:5C TYPE=Ethernet UUID=d3f5e5ae-8440-41e9-b12e-777203414f4f ONBOOT=yes NM_CONTROLLED=yes BOOTPROTO=static #修改这一项 #添加以下项 IPADDR=10.0.2.6 NETMASK=255.255.255.0 GATEWAY=10.0.2.2 DNS1=114.114.114.114 #重启服务生效 [root@centos6 ~]#service network restart ***************centos7 [root@centos7 ~]#cat /etc/sysconfig/network-scripts/ifcfg-eth0 HWADDR=00:0C:29:A9:27:CA TYPE=Ethernet PROXY_METHOD=none BROWSER_ONLY=no BOOTPROTO=static #修改这一项 EFROUTE=yes IPV4_FAILURE_FATAL=no IPV6INIT=yes IPV6_AUTOCONF=yes IPV6_DEFROUTE=yes IPV6_FAILURE_FATAL=no IPV6_ADDR_GEN_MODE=stable-privacy NAME=eth0 UUID=da5ad812-cee5-401d-b648-54fea87ce051 DEVICE=eth0 ONBOOT=yes #添加以下项 IPADDR=10.0.2.7 GATEWAY=10.0.2.2 DNS1=114.114.114.114 [root@centos7 ~]#systemctl restart network **************centos8 [root@centos8 ~]#cat /etc/sysconfig/network-scripts/ifcfg-eth0 TYPE=Ethernet PROXY_METHOD=none BROWSER_ONLY=no BOOTPROTO=static #修改这一项 DEFROUTE=yes IPV4_FAILURE_FATAL=no IPV6INIT=yes IPV6_AUTOCONF=yes IPV6_DEFROUTE=yes IPV6_FAILURE_FATAL=no IPV6_ADDR_GEN_MODE=stable-privacy NAME=eth0 UUID=4bf20a0e-09d0-49e2-9e25-2d597e680c98 DEVICE=eth0 ONBOOT=yes #添加以下项 IPADDR=10.0.2.8 GATEWAY=10.0.2.2 DNS1=114.114.114.114 [root@centos8 ~]#nmcli c reload *************Ubuntu # .yaml文件缩进要求严格! root@ubuntu1804:~# cat /etc/netplan/01-netcfg.yaml # This file describes the network interfaces available on your system # For more information, see netplan(5). network: version: 2 renderer: networkd ethernets: ens33: addresses: [192.168.70.140/24] gateway4: 192.168.70.1 nameservers: addresses: [233.6.6.6] #修改完文件后需执行以下命令生效 root@ubuntu1804:~# netplan apply
作者:mecy$



主机名 ip 固定ip

需要 登录 后方可回复, 如果你还没有账号请 注册新账号