CentOS/Ubuntu修改IP
2021-02-24 09:37:19
## CentOS
vi /etc/sysconfig/network-scripts/ifcfg-enp3s0
```ini
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
# BOOTPROTO=dhcp # 注释掉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=enp3s0
UUID=5508de66-197a-473a-b0cd-3a2fb95e2f2b
DEVICE=enp3s0
ONBOOT=yes # 启用网卡
BOOTPROTO=static
IPADDR=192.168.31.100 # 根据你的网络情况填写
NETMASK=255.255.255.0 # 根据你的网络情况填写
GATEWAY=192.168.31.2 # 根据你的网络情况填写
DNS1=114.114.114.114 # 根据你的网络情况填写
DNS2=8.8.8.8 # 根据你的网络情况填写
```
centos6的网卡重启方法:service network restart
centos7的网卡重启方法:systemctl restart network
## Ubuntu
sudo vi /etc/netplan/00-installer-config.yaml
```yaml
network:
ethernets:
ens33: #配置的网卡的名称
addresses: [192.168.31.100/24] #配置的静态ip地址和掩码
dhcp4: no #关闭DHCP,如果需要打开DHCP则写yes
optional: true
gateway4: 192.168.31.2 #网关地址
nameservers:
addresses: [114.114.114.114,8.8.8.8] #DNS服务器地址,多个DNS服务器地址需要用英文逗号分隔开
version: 2
renderer: networkd #指定后端采用systemd-networkd或者Network Manager,可不填写则默认使用systemd-workd
```
sudo netplan apply
新版本ubuntu gateway4 已被弃用,用下面的方式
```
network:
ethernets:
eno1:
dhcp4: no
addresses: [192.168.31.253/24]
optional: true
routes:
- to: default
via: 192.168.31.1
nameservers:
addresses: [114.114.114.114,8.8.8.8]
version: 2
```