CentOS6或7最小安装后设置联网

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

centos6:

1. 虚拟机网络连接方式设置成NAT。

2. window系统下的两个服务VMwareDHCP Service和VMware NAT Service需要启动。

3. 用vi编辑文件/etc/sysconfig/network-scripts/ifcfg-eth0,我的系统中没有双引号,如下所示:

BOOTPROTO=dhcp(没变)
ONBOOT=yes(修改之前是no)
NM_CONTROLLED=no(修改之前是yes)

修改完之后,ZZ保存退出。

4. 再输入service network restart。

 

设置静态ip
编辑网卡文件

vi /etc/sysconfig/network-scripts/ifcfg-eth0

设置网卡eth0的IPV4信息,需要注意的是,设置的IPADDR需要和局域网中其他机器处于同一网段。需设置容如下:

DEVICE=eth0

BOOTPROT=static

IPADDR=192.168.1.101

GATEWAY=192.168.1.1

NETMASK=255.255.255.0

ONBOOT= yes

网卡文件内容示例说明:

DEVICE=eth0                  // 指出设备名称

BOOTPROT=static              // 启动类型 dhcp|static

BROADCAST=192.168.1.203      // 广播地址

HWADDR=00:06:5B:FE:DF:7C     // 硬件Mac地址

IPADDR=192.168.0.2           //IP 地址

NETMASK=255.255.255.0        // 子网掩码

NETWORK=192.168.0.0          // 网络地址

GATEWAY=192.168.0.1          // 网关地址

ONBOOT= yes                   // 是否启动应用

TYPE=Ethernet                // 网络类型

重启网卡

service network restart

重启网卡时,shell中可能会有重新配置IP的相关信息出现在shell中。

通过ping局域网中其他主机的IP来测试配置是否成功

ping 192.168.1.10

注意:如果是在VirtualBox中配置,需要把网络选项卡中的连接方式改为桥接。

 

CentOS7.2 设置静态ip

先找到配置文件:

[root@localhost bin]# cd /etc/sysconfig/network-scripts
[root@localhost network-scripts]# ls
ifcfg-eno16777736  ifdown-ppp       ifup-ib      ifup-Team
ifcfg-lo           ifdown-routes    ifup-ippp    ifup-TeamPort
ifdown             ifdown-sit       ifup-ipv6    ifup-tunnel
ifdown-bnep        ifdown-Team      ifup-isdn    ifup-wireless
ifdown-eth         ifdown-TeamPort  ifup-plip    init.ipv6-global
ifdown-ib          ifdown-tunnel    ifup-plusb   network-functions
ifdown-ippp        ifup             ifup-post    network-functions-ipv6
ifdown-ipv6        ifup-aliases     ifup-ppp
ifdown-isdn        ifup-bnep        ifup-routes
ifdown-post        ifup-eth         ifup-sit
[root@localhost network-scripts]# gedit ifcfg-eno16777736

开始配置是:

TYPE="Ethernet"
BOOTPROTO="dhcp"
DEFROUTE="yes"
PEERDNS="yes"
PEERROUTES="yes"
IPV4_FAILURE_FATAL="no"
IPV6INIT="yes"
IPV6_AUTOCONF="yes"
IPV6_DEFROUTE="yes"
IPV6_PEERDNS="yes"
IPV6_PEERROUTES="yes"
IPV6_FAILURE_FATAL="no"
NAME="eno16777736"
UUID="157c8fb5-23b3-414c-8519-25bef9b20a54"
DEVICE="eno16777736"
ONBOOT="yes"

修改成:

TYPE="Ethernet"
#BOOTPROTO="dhcp"
DEFROUTE="yes"
PEERDNS="yes"
PEERROUTES="yes"
IPV4_FAILURE_FATAL="no"
IPV6INIT="yes"
IPV6_AUTOCONF="yes"
IPV6_DEFROUTE="yes"
IPV6_PEERDNS="yes"
IPV6_PEERROUTES="yes"
IPV6_FAILURE_FATAL="no"
NAME="eno16777736"
UUID="157c8fb5-23b3-414c-8519-25bef9b20a54"
DEVICE="eno16777736"
#ONBOOT="yes"

#static assignment
NM_CONTROLLED=no #表示该接口将通过该配置文件进行设置,而不是通过网络管理器进行管理
ONBOOT=yes #开机启动
BOOTPROTO=static #静态IP
IPADDR=192.168.1.122 #本机地址
NETMASK=255.255.255.0 #子网掩码
GATEWAY=192.168.1.1 #默认网关

修改/etc/sysconfig/network

# Created by anaconda
NETWORKING=yes
GATEWAY=192.168.1.1
DNS1=119.29.29.29
DNS2=182.254.116.116

然后重启服务即可

service network restart

一般这样就可以,但是如果有问题

必须要在network-scripts里面的默认的文件里面添加HWADDR

[root@localhost ~]# ip addr
1: lo:  mtu 65536 qdisc noqueue state UNKNOWN qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eno16777736:  mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:a7:a0:8c brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.104/24 brd 192.168.1.255 scope global dynamic eno16777736
       valid_lft 7141sec preferred_lft 7141sec
    inet 192.168.1.122/24 brd 192.168.1.255 scope global secondary eno16777736
       valid_lft forever preferred_lft forever
    inet6 fe80::ac30:2bcb:e910:3ae/64 scope link 
       valid_lft forever preferred_lft forever
3: virbr0:  mtu 1500 qdisc noqueue state DOWN qlen 1000
    link/ether 52:54:00:74:cb:1a brd ff:ff:ff:ff:ff:ff
    inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0
       valid_lft forever preferred_lft forever
4: virbr0-nic:  mtu 1500 qdisc pfifo_fast master virbr0 state DOWN qlen 1000
    link/ether 52:54:00:74:cb:1a brd ff:ff:ff:ff:ff:ff

先查看具体的 link/ether

再赋值到eno16777736中

比如:

TYPE="Ethernet"
#BOOTPROTO="dhcp"
DEFROUTE="yes"
PEERDNS="yes"
PEERROUTES="yes"
IPV4_FAILURE_FATAL="no"
IPV6INIT="yes"
IPV6_AUTOCONF="yes"
IPV6_DEFROUTE="yes"
IPV6_PEERDNS="yes"
IPV6_PEERROUTES="yes"
IPV6_FAILURE_FATAL="no"
NAME="eno16777736"
UUID="157c8fb5-23b3-414c-8519-25bef9b20a54"
DEVICE="eno16777736"
#ONBOOT="yes"

#static assignment
NM_CONTROLLED=no #表示该接口将通过该配置文件进行设置,而不是通过网络管理器进行管理
ONBOOT=yes #开机启动
BOOTPROTO=static #静态IP
IPADDR=192.168.1.122 #本机地址
NETMASK=255.255.255.0 #子网掩码
GATEWAY=192.168.1.1 #默认网关

HWADDR=00:0c:29:a7:a0:8c

设定开机启动一个名为NetworkManager-wait-online服务,命令为:

systemctl enable NetworkManager-wait-online.service

重启服务。

=======================================================================

错误的经验:

误以为是桥接模式,后来发现是NAT,没有设置虚拟机的 虚拟网络编辑器,坑!

修改成:

TYPE="Ethernet"
BOOTPROTO="static"
NM_CONTROLLED=no #表示该接口将通过该配置文件进行设置,而不是通过网络管理器进行管理

DEFROUTE="yes"
PEERDNS="yes"
PEERROUTES="yes"
IPV4_FAILURE_FATAL="no"
IPV6INIT="yes"
IPV6_AUTOCONF="yes"
IPV6_DEFROUTE="yes"
IPV6_PEERDNS="yes"
IPV6_PEERROUTES="yes"
IPV6_FAILURE_FATAL="no"
NAME="eno16777736"
UUID="157c8fb5-23b3-414c-8519-25bef9b20a54"
DEVICE="eno16777736"
ONBOOT="yes" #系统将在启动时开启该接口。
 
IPADDR=192.168.1.122 #静态IP  
GATEWAY=192.168.1.1 #默认网关  
NETMASK=255.255.255.0 #子网掩码  
#DNS1=192.168.7.1 #DNS 配置

重启网络服务:

service network restart

重启网络服务以后ok,但是重启系统以后还是变成了动态的,

[root@localhost ~]# ip addr
1: lo:  mtu 65536 qdisc noqueue state UNKNOWN qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eno16777736:  mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:a7:a0:8c brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.104/24 brd 192.168.1.255 scope global dynamic eno16777736
       valid_lft 6995sec preferred_lft 6995sec
    inet 192.168.1.122/24 brd 192.168.1.255 scope global secondary eno16777736
       valid_lft forever preferred_lft forever
    inet6 fe80::9283:cd42:a281:7a9b/64 scope link 
       valid_lft forever preferred_lft forever
3: virbr0:  mtu 1500 qdisc noqueue state DOWN qlen 1000
    link/ether 52:54:00:74:cb:1a brd ff:ff:ff:ff:ff:ff
    inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0
       valid_lft forever preferred_lft forever
4: virbr0-nic:  mtu 1500 qdisc pfifo_fast master virbr0 state DOWN qlen 1000
    link/ether 52:54:00:74:cb:1a brd ff:ff:ff:ff:ff:ff
[root@localhost ~]# systemctl disable NetworkManager.service
Removed symlink /etc/systemd/system/multi-user.target.wants/NetworkManager.service.
Removed symlink /etc/systemd/system/dbus-org.freedesktop.NetworkManager.service.
Removed symlink /etc/systemd/system/dbus-org.freedesktop.nm-dispatcher.service.
[root@localhost ~]# chkconfig network on
[root@localhost ~]# systemctl stop NetworkManager
[root@localhost ~]# systemctl restart network

可以从上面看出两个ip

192.168.1.104

192.168.1.122

解决方法:

systemctl disable NetworkManager.service
chkconfig network on
systemctl stop NetworkManager
systemctl restart network

如果还出现

[hongdada@localhost ~]$ 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-12-03 23:05:01 CST; 2min 45s ago
     Docs: man:systemd-sysv-generator(8)
  Process: 3957 ExecStart=/etc/rc.d/init.d/network start (code=exited, status=1/FAILURE)

12月 03 23:05:01 localhost.localdomain network[3957]: RTNETLINK answers: File exists
12月 03 23:05:01 localhost.localdomain network[3957]: RTNETLINK answers: File exists
12月 03 23:05:01 localhost.localdomain network[3957]: RTNETLINK answers: File exists
12月 03 23:05:01 localhost.localdomain network[3957]: RTNETLINK answers: File exists
12月 03 23:05:01 localhost.localdomain network[3957]: RTNETLINK answers: File exists
12月 03 23:05:01 localhost.localdomain network[3957]: RTNETLINK answers: File exists
12月 03 23:05:01 localhost.localdomain systemd[1]: network.service: control process exited, code=exited status=1
12月 03 23:05:01 localhost.localdomain systemd[1]: Failed to start LSB: Bring up/down networking.
12月 03 23:05:01 localhost.localdomain systemd[1]: Unit network.service entered failed state.
12月 03 23:05:01 localhost.localdomain systemd[1]: network.service fail

那么。。。

查看https://blog.csdn.net/kokuma/article/details/72876106

可以使用journel -xe查看日志,一般就是修改配置文件里面的Name,DEVICE 为ens33

首先使用ip addr查看网卡,然后查看ifconfig

一般如果是第一次的,就是不是克隆的话,网卡名称为eno16777736,这里只要设定静态ip即可,如果是克隆的话,ip addr里面会发现网卡名称变成了ens33,这样的话,我们也必须在/etc/sysconfig/network-scripts/eno167777736文件里面把NAME,DEVICE里面的改成ens33,重新设定静态IP,重启即可。

http://www.cnblogs.com/Leo_wl/p/5484136.html

http://www.bbsmax.com/A/n2d9D39v5D/

http://www.178linux.com/9928

转载于:https://my.oschina.net/rock912/blog/615776

你可能感兴趣的:(shell,操作系统,运维)