原文网址:http://blog.csdn.net/cmzsteven/article/details/49777667
**CentOS7中已经取消了ifconfig,用nmcli进行了代替,服务管理也升级为systemd。所以之前在6.x版本上的网络配置操作在7.x上行不通了。
下面介绍一下在CentOS7.x上进行网络配置的方法。**
环境准备:新安装的CentOS7.x,最小化安装,没有进行网络配置。
1、查看当前网卡信息
[wangdeqiang@localhost ~]$ nmcli connection show
名称 UUID 类型 设备
enp0s3 ae1646b6-4867-4aa8-ac04-a45551f0e4ea 802-3-ethernet enp0s3
virbr0 967c8dc2-0a07-46a1-a3bb-b07a5c6ab662 bridge
终端截图:
CentOS7中对网上的命名规则有所变更,具体规则如下:
eno1 :代表由主板 BIOS 內建的网卡
ens1 :代表由主板 BIOS 內建的 PCI-E 界面的网卡
enp2s0 :代表 PCI-E 界面的独立网卡,可能有多个网卡接口,因此会有 s0, s1... 的编号
eth0 :如果上述的名称都不适用,就回到原本的预设网卡编号
由于现在网卡没有启用,也没有建立相关的配置文件,因此在DEVICE 栏位里显示的是”—”。在配置完成,重启网络服务后就会显示相应的设备名,在我的虚拟机上显示的是“enp0s3”
**2、使用nmcli命令手工配置网卡
需要配置的网络信息:**
IP地址:172.20.31.221/24
网关:172.20.31.240
DNS:202.96.69.38
命令:
[wangdeqiang@localhost ~]$ nmcli connection modify enp0s3
> connection.autoconnect yes
> ipv4.method manual
> ipv4.address 172.20.31.221/24
> ipv4.gateway 172.20.31.240
> ipv4.dns 202.96.64.68
终端截图
*autoconnect 是设置开机自动连接
ipv4.method 是设置手工配置IPV4地址*
重启网络服务
systemctl restart network
终端截图
3、查看配置后的网卡配置信息
[wangdeqiang@localhost ~]$ nmcli connection show enp0s3
connection.id: enp0s3
connection.uuid: ae1646b6-4867-4aa8-ac04-a45551f0e4ea
connection.stable-id: --
connection.interface-name: enp0s3
connection.type: 802-3-ethernet
connection.autoconnect: yes
connection.autoconnect-priority: 0
connection.autoconnect-retries: -1 (默认)
connection.timestamp: 1516626418
connection.read-only: no
connection.permissions: --
connection.zone: --
connection.master: --
connection.slave-type: --
connection.autoconnect-slaves: -1 (默认)
connection.secondaries: --
connection.gateway-ping-timeout: 0
connection.metered: 未知
connection.lldp: -1 (default)
802-3-ethernet.port: --
802-3-ethernet.speed: 0
802-3-ethernet.duplex: --
802-3-ethernet.auto-negotiate: no
802-3-ethernet.mac-address: --
802-3-ethernet.cloned-mac-address: --
802-3-ethernet.generate-mac-address-mask:--
802-3-ethernet.mac-address-blacklist: --
802-3-ethernet.mtu: 自动
802-3-ethernet.s390-subchannels: --
802-3-ethernet.s390-nettype: --
802-3-ethernet.s390-options: --
802-3-ethernet.wake-on-lan: 1 (default)
802-3-ethernet.wake-on-lan-password: --
ipv4.method: manual
ipv4.dns: 202.96.64.68
ipv4.dns-search: --
ipv4.dns-options: (默认)
ipv4.dns-priority: 0
ipv4.addresses: 172.20.31.221/24
ipv4.gateway: 172.20.31.240
ipv4.routes: --
ipv4.route-metric: -1
ipv4.ignore-auto-routes: no
ipv4.ignore-auto-dns: no
ipv4.dhcp-client-id: --
ipv4.dhcp-timeout: 0
ipv4.dhcp-send-hostname: yes
ipv4.dhcp-hostname: --
ipv4.dhcp-fqdn: --
ipv4.never-default: no
ipv4.may-fail: yes
ipv4.dad-timeout: -1 (默认)
ipv6.method: auto
ipv6.dns: --
ipv6.dns-search: --
ipv6.dns-options: (默认)
ipv6.dns-priority: 0
ipv6.addresses: --
ipv6.gateway: --
ipv6.routes: --
ipv6.route-metric: -1
ipv6.ignore-auto-routes: no
ipv6.ignore-auto-dns: no
ipv6.never-default: no
ipv6.may-fail: yes
ipv6.ip6-privacy: -1 (未知)
ipv6.addr-gen-mode: stable-privacy
ipv6.dhcp-send-hostname: yes
ipv6.dhcp-hostname: --
ipv6.token: --
proxy.method: none
proxy.browser-only: no
proxy.pac-url: --
proxy.pac-script: --
GENERAL.名称: enp0s3
GENERAL.UUID: ae1646b6-4867-4aa8-ac04-a45551f0e4ea
GENERAL.设备: enp0s3
GENERAL.状态: 已激活
GENERAL.默认: 是
GENERAL.默认6: 否
GENERAL.VPN 参数: 否
GENERAL.区: --
GENERAL.DBUS路径: /org/freedesktop/NetworkManager/ActiveConnection/4
GENERAL.连接路径: /org/freedesktop/NetworkManager/Settings/1
GENERAL.指定对象: --
GENERAL.主路径: --
IP4.地址[1]: 172.20.31.221/24
IP4.网关: 172.20.31.240
IP4.DNS[1]: 202.96.64.68
IP6.地址[1]: fe80::6c87:9541:45b7:f8d8/64
IP6.网关:
终端截图
因为在CentOS7.x中取消了ifconfig命令, 我们使用ip addr来代替。
[wangdeqiang@localhost ~]$ ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> 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: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 08:00:27:dc:63:24 brd ff:ff:ff:ff:ff:ff
inet 172.20.31.221/24 brd 172.20.31.255 scope global enp0s3
valid_lft forever preferred_lft forever
inet6 fe80::6c87:9541:45b7:f8d8/64 scope link
valid_lft forever preferred_lft forever
3: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN qlen 1000
link/ether 52:54:00:75:ff:72 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: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast master virbr0 state DOWN qlen 1000
link/ether 52:54:00:75:ff:72 brd ff:ff:ff:ff:ff:ff
[wangdeqiang@localhost ~]$
终端截图
4、设置双IP
[wangdeqiang@localhost ~]$ cd /etc/sysconfig/network-scripts/
[wangdeqiang@localhost network-scripts]$ sudo cp ifcfg-enp0s3 ifcfg-enp0s3:1
[sudo] wangdeqiang 的密码:
[wangdeqiang@localhost network-scripts]$ vi ifcfg-enp0s3:1
终端截图
将下列内容进行修改:
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=none
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=enp0s3
UUID=ae1646b6-4867-4aa8-ac04-a45551f0e4ea
DEVICE=enp0s3
ONBOOT=yes
IPADDR=172.20.31.221
PREFIX=24
GATEWAY=172.20.31.240
DNS1=202.96.64.68
终端截图
*保存退出后,生词网络服务,然后查看配置信息
CentOS7怎么保存退出vi编辑
保存命令
按ESC键 跳到命令模式,然后:*
:w 保存文件但不退出vi
:w file 将修改另外保存到file中,不退出vi
:w! 强制保存,不推出vi
:wq 保存文件并退出vi
:wq! 强制保存文件,并退出vi
q: 不保存文件,退出vi
:q! 不保存文件,强制退出vi
:e! 放弃所有修改,从上次保存文件开始再编辑
终端截图
保存退出后,生词网络服务,然后查看配置信息
[wangdeqiang@localhost network-scripts]$ nmcli connection show enp0s3
connection.id: enp0s3
connection.uuid: ae1646b6-4867-4aa8-ac04-a45551f0e4ea
connection.stable-id: --
connection.interface-name: enp0s3
connection.type: 802-3-ethernet
connection.autoconnect: yes
connection.autoconnect-priority: 0
connection.autoconnect-retries: -1 (默认)
connection.timestamp: 1516628218
connection.read-only: no
connection.permissions: --
connection.zone: --
connection.master: --
connection.slave-type: --
connection.autoconnect-slaves: -1 (默认)
connection.secondaries: --
connection.gateway-ping-timeout: 0
connection.metered: 未知
connection.lldp: -1 (default)
802-3-ethernet.port: --
802-3-ethernet.speed: 0
802-3-ethernet.duplex: --
802-3-ethernet.auto-negotiate: no
802-3-ethernet.mac-address: --
802-3-ethernet.cloned-mac-address: --
802-3-ethernet.generate-mac-address-mask:--
802-3-ethernet.mac-address-blacklist: --
802-3-ethernet.mtu: 自动
802-3-ethernet.s390-subchannels: --
802-3-ethernet.s390-nettype: --
802-3-ethernet.s390-options: --
802-3-ethernet.wake-on-lan: 1 (default)
802-3-ethernet.wake-on-lan-password: --
ipv4.method: manual
ipv4.dns: 202.96.64.68
ipv4.dns-search: --
ipv4.dns-options: (默认)
ipv4.dns-priority: 0
ipv4.addresses: 172.20.31.221/24
ipv4.gateway: 172.20.31.240
ipv4.routes: --
ipv4.route-metric: -1
ipv4.ignore-auto-routes: no
ipv4.ignore-auto-dns: no
ipv4.dhcp-client-id: --
ipv4.dhcp-timeout: 0
ipv4.dhcp-send-hostname: yes
ipv4.dhcp-hostname: --
ipv4.dhcp-fqdn: --
ipv4.never-default: no
ipv4.may-fail: yes
ipv4.dad-timeout: -1 (默认)
ipv6.method: auto
ipv6.dns: --
ipv6.dns-search: --
ipv6.dns-options: (默认)
ipv6.dns-priority: 0
ipv6.addresses: --
ipv6.gateway: --
ipv6.routes: --
ipv6.route-metric: -1
ipv6.ignore-auto-routes: no
ipv6.ignore-auto-dns: no
ipv6.never-default: no
ipv6.may-fail: yes
ipv6.ip6-privacy: -1 (未知)
ipv6.addr-gen-mode: stable-privacy
ipv6.dhcp-send-hostname: yes
ipv6.dhcp-hostname: --
ipv6.token: --
proxy.method: none
proxy.browser-only: no
proxy.pac-url: --
proxy.pac-script: --
GENERAL.名称: enp0s3
GENERAL.UUID: ae1646b6-4867-4aa8-ac04-a45551f0e4ea
GENERAL.设备: enp0s3
GENERAL.状态: 已激活
GENERAL.默认: 是
GENERAL.默认6: 否
GENERAL.VPN 参数: 否
GENERAL.区: --
GENERAL.DBUS路径: /org/freedesktop/NetworkManager/ActiveConnection/4
GENERAL.连接路径: /org/freedesktop/NetworkManager/Settings/1
GENERAL.指定对象: --
GENERAL.主路径: --
IP4.地址[1]: 172.20.31.221/24
IP4.网关: 172.20.31.240
IP4.DNS[1]: 202.96.64.68
IP6.地址[1]: fe80::6c87:9541:45b7:f8d8/64
IP6.网关: --
终端截图
查看全部网络设备信息
[wangdeqiang@localhost network-scripts]$ ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> 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: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 08:00:27:ef:69:36 brd ff:ff:ff:ff:ff:ff
inet 172.20.31.221/24 brd 172.20.31.255 scope global enp0s3
valid_lft forever preferred_lft forever
inet 172.20.31.222/24 brd 172.20.31.255 scope global secondary enp0s3:1
valid_lft forever preferred_lft forever
inet6 fe80::a00:27ff:feef:6936/64 scope link
valid_lft forever preferred_lft forever
3: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN qlen 1000
link/ether 52:54:00:75:ff:72 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: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast master virbr0 state DOWN qlen 1000
link/ether 52:54:00:75:ff:72 brd ff:ff:ff:ff:ff:ff
[wangdeqiang@localhost network-scripts]$
在enp0s3中会出现二个”inet”,包含配置过的二个IP的信息。
**5、配置hostname
使用hostnamectl来配置主机的hostname**
[wangdeqiang@localhost network-scripts]$ hostnamectl set-hostname www.cmzsteven.com
[wangdeqiang@localhost network-scripts]$ cat /etc/hostname
www.cmzsteven.com
终端截图
**6、自动获取IP地址
如果局域网里有DHCP服务器,那么可以使用自动获取来取得主机的IP等相关信息**
[wangdeqiang@localhost network-scripts]$ nmcli connection modify enp0s3
> connection.autoconnect yes
> ipv4.method auto
终端截图