情况1.
通常正常情况下安装完linux虚拟机,只需要使用桥接并修改配置文件 /etc/sysconfig/network-scripts/ifcfg-eth0 (或者 /etc/sysconfig/network-scripts/ifcfg-ens33),将如下参数值改为如下:
ONBOOT=yes
NM_CONTROLLED=yes
BOOTPROTO=dhcp
然后重启网络服务即可:service network restart
家里的笔记本用这种方式确实可行,可是带到公司后发现就获取不到IP了,而且也重启不了网卡,原来是因为路由器的dhcp功能有问题,如果路由器不调整,此种方式肯定是行不通了。只能将BOOTPROTO设为static,然后手动设置IP了。
手动设置IP方法:
修改文件/etc/sysconfig/network-scripts/ifcfg-eth0(或者 /etc/sysconfig/network-scripts/ifcfg-ens33),内容如下:
TYPE="Ethernet"
PROXY_METHOD="none"
BROWSER_ONLY="no"
BOOTPROTO="static"
DEFROUTE="yes"
IPV4_FAILURE_FATAL="yes"
IPV6INIT="yes"
IPV6_AUTOCONF="yes"
IPV6_DEFROUTE="yes"
IPV6_FAILURE_FATAL="no"
IPV6_ADDR_GEN_MODE="stable-privacy"
NAME="ens33"
UUID="46ad222d-6207-4cff-ae91-936591955b4e"
DEVICE="ens33"
ONBOOT="yes"
IPADDR="192.168.12.168"
PREFIX="24"
GATEWAY="192.168.12.2"
DNS1="192.168.12.2"
IPV6_PRIVACY="no"
HWADDR:"00:0c:29:59:b4:c9"
往ifcfg-eth0(或者 ifcfg-ens33) 里添加的网卡接口MAC地址 ,一定的要与ip addr里的相同
后三行分别为设置的IP,网关和子网掩码。保存后重启网络服务,service network restart。此时发现IP能够获取了,ifconfig结果如下图所示:
情况2.
CentOS7重启网络服务报错: Job for network.service failed because the control process exited with error code. See "systemctl status network.service" and "journalctl -xe" for details
刚上架一台服务器,配置好网络,重启网络服务器报错。
[root@localhost network-scripts]# service network restart
Restarting network (via systemctl): Job for network.service failed because the control process exited with error code. See "systemctl status network.service" and "journalctl -xe" for details. [失败]
[root@localhost network-scripts]# systemctl status network.service
● network.service - LSB: Bring up/down networking
Loaded: loaded (/etc/rc.d/init.d/network; bad; vendor preset: disabled)
Active: failed (Result: exit-code) since 三 2017-07-05 08:09:37 CST; 1min 15s ago
Docs: man:systemd-sysv-generator(8)
Process: 7132 ExecStop=/etc/rc.d/init.d/network stop (code=exited, status=0/SUCCESS)
Process: 8159 ExecStart=/etc/rc.d/init.d/network start (code=exited, status=1/FAILURE)
7月 05 08:09:37 localhost.localdomain network[8159]: RTNETLINK answers: File exists
7月 05 08:09:37 localhost.localdomain network[8159]: RTNETLINK answers: File exists
7月 05 08:09:37 localhost.localdomain network[8159]: RTNETLINK answers: File exists
7月 05 08:09:37 localhost.localdomain network[8159]: RTNETLINK answers: File exists
7月 05 08:09:37 localhost.localdomain network[8159]: RTNETLINK answers: File exists
7月 05 08:09:37 localhost.localdomain network[8159]: RTNETLINK answers: File exists
7月 05 08:09:37 localhost.localdomain systemd[1]: network.service: control process exited, code=exited status=1
7月 05 08:09:37 localhost.localdomain systemd[1]: Failed to start LSB: Bring up/down networking.
7月 05 08:09:37 localhost.localdomain systemd[1]: Unit network.service entered failed state.
7月 05 08:09:37 localhost.localdomain systemd[1]: network.service failed.
[root@localhost network-scripts]# cat /var/log/messages | grep network
Jul 5 08:09:35 localhost systemd: Starting LSB: Bring up/down networking...
Jul 5 08:09:35 localhost network: 正在打开环回接口: [ 确定 ]
Jul 5 08:09:35 localhost network: 正在打开接口 enp1s0f0: RTNETLINK answers: File exists
Jul 5 08:09:37 localhost network: [ 确定 ]
Jul 5 08:09:37 localhost network: 正在打开接口 有线连接_1: 错误:激活连接失败:No suitable device found for this connection.
Jul 5 08:09:37 localhost network: [失败]
Jul 5 08:09:37 localhost network: RTNETLINK answers: File exists
Jul 5 08:09:37 localhost network: RTNETLINK answers: File exists
Jul 5 08:09:37 localhost network: RTNETLINK answers: File exists
Jul 5 08:09:37 localhost network: RTNETLINK answers: File exists
Jul 5 08:09:37 localhost network: RTNETLINK answers: File exists
Jul 5 08:09:37 localhost network: RTNETLINK answers: File exists
Jul 5 08:09:37 localhost network: RTNETLINK answers: File exists
Jul 5 08:09:37 localhost network: RTNETLINK answers: File exists
Jul 5 08:09:37 localhost network: RTNETLINK answers: File exists
Jul 5 08:09:37 localhost systemd: network.service: control process exited, code=exited status=1
Jul 5 08:09:37 localhost systemd: Failed to start LSB: Bring up/down networking.
Jul 5 08:09:37 localhost systemd: Unit network.service entered failed state.
Jul 5 08:09:37 localhost systemd: network.service failed.
[root@localhost network-scripts]#
为什么是打开 有线连接_1 接口而不是 enp1s0f0 ,不管怎样我们配置的是enp1s0f0,所以我们希望他打开enp1s0f0,所以我尝试将有线连接_1 配置注释。
[root@localhost network-scripts]/etc/sysconfig/network-scripts
[root@localhost network-scripts]/mv ifcfg-有线连接_1 backup-ifcfg-有线连接_1
再次重启网络服务就成功了。
[root@localhost network-scripts]# service network restart
Restarting network (via systemctl): [ 确定 ]
还有一些其他的情况,比如虚拟机里边的CentOS7重启之后无法联网了,重启network发现报错。
百度了一圈,都是叫修改mac地址的说法,但是改了之后并没有什么用,最后谷歌解决了。
解决方式:禁用NetworkManager
1. systemctl stop NetworkManager 临时关闭网络管理命令
2. systemctl disable NetworkManager 永久关闭网络管理命令,生产环境常禁用状态
然后service network restart 重启网络服务,能正常联网了!