Linux怎么配置IP?CentOS8怎么重启网卡?配置/修改静态IP的方法

正文开始
要修改CentOS Linux 8的IP地址,只需要更改一个文件就OK了,这个文件就是:
/etc/sysconfig/network-scripts/ifcfg-ens***。
由于每台电脑的配置不同,这个网卡文件的名称会有些不一样,在我的电脑上,这个网卡名称是:ifcfg-ens160。

修改网卡内容
我们进入网卡目录执行修改:
cd /etc/sysconfig/network-scripts/
ls
vi ifcfg-ens160

英文输入法下按字母键"A"执行编辑(左下角出现"-- INSERT --"),修改成类似如下的内容:

按"ESC"键后,录入":wq"保存退出
重启网络服务
修改完网卡的内容,需要重新启动一下网络(重启电脑也可以)。CentOS Linux 8以前,修改完IP,一般都是"service network restart"或者"systemctl restart network",把网络服务重新启动一下。

CentOS Linux 8怎么重启网卡呢?
从CentOS Linux 8开始,默认就没有安装network服务,转而使用nmcli来操作(CentOS7就开始用了),这是一种新的操作方法。
重启网卡之前一定要重新载入一下配置文件,不然不能立即生效:
nmcli c reload
然后再重启网卡 (下面的三条命令都可以,任选一个):
nmcli c up ens160
nmcli d reapply ens160
nmcli d connect ens160


修改成功,因为我是远程连接到服务器上的,修改完IP,就失去连接了,只能重新连接新的IP。
总结:重启CentOS Linux 8的网卡,先载入新的配置文件,然后再执行重启网卡命令即可。
这几个命令处于SSH远程连接模式也可以用,如果对网卡只操作一次down,那么就没有办法操作up了,只能要求运维帮忙重启服务器了。
现在新的IP已经配置好了,可以用新IP连接并操作服务器了。
