安装CentOS 7后,一开始有点儿无力吐槽的感觉,变化这么大?
一、Runlevel
首先一条,原来一直用的CentOS-6.5-x86_64-minimal.iso光盘镜像(400M左右无图形系统小巧便捷),而7目前最小的镜像是CentOS-7.0-1406-x86_64-livecd.iso(700M左右),默认安装后是启动图形界面,按原来的习惯要改成启动命令行,结果发现:
[root@localhost ~]# cat /etc/inittab
# inittab is no longer used when using systemd.
#
# ADDING CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM.
#
# Ctrl-Alt-Delete is handled by /etc/systemd/system/ctrl-alt-del.target
#
# systemd uses ‘targets’ instead of runlevels. By default, there are two main targets:
#
# multi-user.target: analogous to runlevel 3
# graphical.target: analogous to runlevel 5
#
# To set a default target, run:
#
# ln -sf /lib/systemd/system/<target name>.target /etc/systemd/system/default.target
#
好吧,systemd一统天下了(它管的东西很多很多,学Centos7首先要搞定systemd)。
[root@localhost ~]# ls -Xl /etc/systemd/system/*.target
lrwxrwxrwx. 1 root root 36 7月 8 23:12 /etc/systemd/system/default.target -> /lib/systemd/system/graphical.target
我们来看看从前的runlevel都变成什么样了?
[root@localhost ~]# ls -Xl /lib/systemd/system
[root@localhost ~]# ls -Xl /lib/systemd/system/runlevel*.target
lrwxrwxrwx. 1 root root 15 7月 5 00:45 /lib/systemd/system/runlevel0.target -> poweroff.target
lrwxrwxrwx. 1 root root 13 7月 5 00:45 /lib/systemd/system/runlevel1.target -> rescue.target
lrwxrwxrwx. 1 root root 17 7月 5 00:45 /lib/systemd/system/runlevel2.target -> multi-user.target
lrwxrwxrwx. 1 root root 17 7月 5 00:45 /lib/systemd/system/runlevel3.target -> multi-user.target
lrwxrwxrwx. 1 root root 17 7月 5 00:45 /lib/systemd/system/runlevel4.target -> multi-user.target
lrwxrwxrwx. 1 root root 16 7月 5 00:45 /lib/systemd/system/runlevel5.target -> graphical.target
lrwxrwxrwx. 1 root root 13 7月 5 00:45 /lib/systemd/system/runlevel6.target -> reboot.target
修改默认启动runlevel为 multi-user (貌似runlevel2 3 4 没区别了)
[root@localhost ~]# ln -sf /lib/systemd/system/multi-user.target /etc/systemd/system/default.target
[root@localhost ~]# ll -X /etc/systemd/system/*.target
lrwxrwxrwx. 1 root root 37 7月 10 09:42 /etc/systemd/system/default.target -> /lib/systemd/system/multi-user.target
试试init 6 还好用不。
[root@localhost ~]# init 6
Connection to 192.168.150.180 closed by remote host.
Connection to 192.168.150.180 closed.
成,好用。:)
因为我从桌面版Linux 连接Centos7系统提示变成了中文,所以需要把Centos7 的root用户语言改一下,回到原汁原味。
[root@localhost ~]# cp .bashrc .bashrc-back
[root@localhost ~]# echo ‘export LANG=”en_Us.UTF-8″‘ >> /root/.bashrc
在CentOS 7上利用systemctl添加自定义系统服务 http://www.linuxidc.com/linux/2014-07/104487.htm
CentOS 7.0下编译安装LNMP http://www.linuxidc.com/Linux/2014-07/104247.htm
CentOS 7.0体验与之前版本的不同 http://www.linuxidc.com/Linux/2014-07/104196.htm
CentOS 7 正式版发布下载 http://www.linuxidc.com/Linux/2014-07/104146.htm
U盘安装64位CentOS 6.5方法 http://www.linuxidc.com/Linux/2014-04/100035.htm
Win7安装64位CentOS 6.4双系统详细图文教程 http://www.linuxidc.com/Linux/2014-04/100032.htm
CentOS与Windows双系统安装注意事项记录 http://www.linuxidc.com/Linux/2014-05/101571.htm
更多详情见请继续阅读下一页的精彩内容: http://www.linuxidc.com/Linux/2014-09/107375p2.htm
二、Services
[root@localhost ~]# chkconfig
Note: This output shows SysV services only and does not include native
systemd services. SysV configuration data might be overridden by native
systemd configuration.
If you want to list systemd services use ‘systemctl list-unit-files’.
To see services enabled on particular target use
‘systemctl list-dependencies [target]’.
iprdump 0:off 1:off 2:on 3:on 4:on 5:on 6:off
iprinit 0:off 1:off 2:on 3:on 4:on 5:on 6:off
iprupdate 0:off 1:off 2:on 3:on 4:on 5:on 6:off
livesys 0:off 1:off 2:off 3:on 4:on 5:on 6:off
livesys-late 0:off 1:off 2:off 3:on 4:on 5:on 6:off
netconsole 0:off 1:off 2:off 3:off 4:off 5:off 6:off
network 0:off 1:off 2:off 3:off 4:off 5:off 6:off
rhnsd 0:off 1:off 2:on 3:on 4:on 5:on 6:off
vboxadd 0:off 1:off 2:on 3:on 4:on 5:on 6:off
vboxadd-service 0:off 1:off 2:on 3:on 4:on 5:on 6:off
vboxadd-x11 0:off 1:off 2:off 3:on 4:off 5:on 6:off
SysV已经快退居二线了,想配置服务得用systemctl,先看看默认启动的服务吧。(如果不用grep过滤一下,输出结果有260多行)
[root@localhost ~]# systemctl list-unit-files|grep enabled
tmp.mount enabled
accounts-daemon.service enabled
atd.service enabled
auditd.service enabled
avahi-daemon.service enabled
bluetooth.service enabled
chronyd.service enabled
crond.service enabled
dbus-org.bluez.service enabled
dbus-org.Fedoraproject.FirewallD1.service enabled
dbus-org.freedesktop.Avahi.service enabled
dbus-org.freedesktop.NetworkManager.service enabled
dbus-org.freedesktop.nm-dispatcher.service enabled
display-manager.service enabled
dmraid-activation.service enabled
firewalld.service enabled
gdm.service enabled
getty@.service enabled
irqbalance.service enabled
iscsi.service enabled
kdump.service enabled
libstoragemgmt.service enabled
lvm2-monitor.service enabled
mdmonitor.service enabled
microcode.service enabled
multipathd.service enabled
NetworkManager-dispatcher.service enabled
NetworkManager.service enabled
packagekit-offline-update.service enabled
postfix.service enabled
rngd.service enabled
rsyslog.service enabled
rtkit-daemon.service enabled
smartd.service enabled
spice-vdagentd.service enabled
sysstat.service enabled
systemd-readahead-collect.service enabled
systemd-readahead-drop.service enabled
systemd-readahead-replay.service enabled
tuned.service enabled
avahi-daemon.socket enabled
dm-event.socket enabled
iscsid.socket enabled
iscsiuio.socket enabled
lvm2-lvmetad.socket enabled
default.target enabled
multi-user.target enabled
remote-fs.target enabled
默认居然没有启动sshd,晕!看看监听端口:
[root@localhost ~]# netstat -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 2090/master
果然没有22,试试老办法:
[root@localhost ~]# chkconfig sshd on
Note: Forwarding request to ‘systemctl enable sshd.service’.
ln -s ‘/usr/lib/systemd/system/sshd.service’ ‘/etc/systemd/system/multi-user.target.wants/sshd.service’
能用,但是指令被转发到 systemctl enable sshd.service ,以后控制服务就用这个指令了。试试:
禁用sshd:
[root@localhost ~]# systemctl disable sshd.service
rm ‘/etc/systemd/system/multi-user.target.wants/sshd.service’
启用sshd:
[root@localhost ~]# systemctl enable sshd.service
ln -s ‘/usr/lib/systemd/system/sshd.service’ ‘/etc/systemd/system/multi-user.target.wants/sshd.service’
看看:
[root@localhost ~]# systemctl list-unit-files|grep sshd.service
anaconda-sshd.service static
sshd.service enabled
[root@localhost ~]# netstat -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 2090/master
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 2758/sshd
tcp6 0 0 :::22 :::* LISTEN 2758/sshd
其实启用和禁用服务就是在当前“runlevel”的配置文件目录(/etc/systemd/system/multi-user.target.wants/)里,
建立/usr/lib/systemd/system 里面对应服务配置文件的软链接;禁用服务就是删除此软链接。
有兴趣就自己看看 /usr/lib/systemd/system 里的文件,语法跟旧版/etc/init.d/ 里的服务脚本完全不同,也不能再用 /etc/init.d/sshd restart 之类的指令启动服务器了。
先试试旧方法启动服务:
[root@localhost ~]# service sshd start
Redirecting to /bin/systemctl start sshd.service
用新方法折腾一下:
[root@localhost ~]# systemctl start sshd.service
[root@localhost ~]# systemctl stop sshd.service
[root@localhost ~]# systemctl restart sshd.service
[root@localhost ~]#
如果没有错误,就不会输出任何信息,这个,,,,得习惯一下。
三、网络
setup工具还是保留了,但有区别,没有了网络配置,多了RHN(CentOS到底是跟RedHat一家子了)
[root@localhost ~]# setup
好吧,我们看看网络配置。
[root@localhost ~]# cat /etc/resolv.conf
# Generated by NetworkManager
nameserver 192.168.150.254
[root@localhost ~]# cat /etc/sysconfig/network
# Created by anaconda
[root@localhost ~]# ll /etc/sysconfig/network-scripts/ifcfg-*
-rw-r–r–. 1 root root 298 Jul 9 00:13 /etc/sysconfig/network-scripts/ifcfg-Auto_Ethernet
-rw-r–r–. 1 root root 288 Jul 8 23:12 /etc/sysconfig/network-scripts/ifcfg-enp0s3
-rw-r–r–. 1 root root 254 Apr 2 23:30 /etc/sysconfig/network-scripts/ifcfg-lo
[root@localhost ~]# ifconfig
enp0s3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.150.180 netmask 255.255.255.0 broadcast 192.168.150.255
inet6 fe80::a00:27ff:fe9a:a688 prefixlen 64 scopeid 0x20<link>
ether 08:00:27:9a:a6:88 txqueuelen 1000 (Ethernet)
RX packets 39317 bytes 2487945 (2.3 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 2216 bytes 740115 (722.7 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 0 (Local Loopback)
RX packets 6 bytes 504 (504.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 6 bytes 504 (504.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@localhost ~]#
先改改主机名:
[root@localhost ~]# echo “hostname cenots7.localdomain” >>/etc/sysconfig/network
[root@localhost ~]# reboot
Connection to 192.168.150.180 closed by remote host.
Connection to 192.168.150.180 closed.
xw@Opt9010:~$ ssh root@192.168.150.180
root@192.168.150.180’s password:
Last login: Thu Jul 10 10:01:09 2014
[root@cenots7 ~]#
看看网卡配置文件:
[root@cenots7 ~]# cd /etc/sysconfig/network-scripts/
[root@cenots7 network-scripts]# cat ifcfg-Auto_Ethernet
HWADDR=08:00:27:9A:A6:88
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=”Auto Ethernet”
UUID=76304098-8f46-4185-8337-bb7f0d90423e
ONBOOT=yes
[root@cenots7 network-scripts]# cat ifcfg-enp0s3
HWADDR=08:00:27:9A:A6:88
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=enp0s3
UUID=66a635c2-9600-437b-8cfb-57e9569f68da
ONBOOT=no
看看两个文件有啥不同:
[root@cenots7 network-scripts]# diff ifcfg-Auto_Ethernet ifcfg-enp0s3
14,16c14,16
< NAME=”Auto Ethernet”
< UUID=76304098-8f46-4185-8337-bb7f0d90423e
< ONBOOT=yes
—
> NAME=enp0s3
> UUID=66a635c2-9600-437b-8cfb-57e9569f68da
> ONBOOT=no
两个文件MAC地址一样,ifcfg-Auto_Ethernet ONBOOT=yes,但是ifconfig结果显示的是enp0s3,有点儿费解了。
再看看我复制KVM虚拟机时经常要改的网卡MAC地址:
root@cenots7 network-scripts]# ll /etc/udev/rules.d/
total 8
-rw-r–r–. 1 root root 134 Jul 9 00:47 60-vboxadd.rules
-rw-r–r–. 1 root root 352 Jul 4 00:38 98-kexec.rules
[root@cenots7 network-scripts]# cat /etc/udev/rules.d/98-kexec.rules
SUBSYSTEM==”cpu”, ACTION==”online”, PROGRAM=”/bin/systemctl try-restart kdump.service”
SUBSYSTEM==”cpu”, ACTION==”offline”, PROGRAM=”/bin/systemctl try-restart kdump.service”
SUBSYSTEM==”memory”, ACTION==”add”, PROGRAM=”/bin/systemctl try-restart kdump.service”
SUBSYSTEM==”memory”, ACTION==”remove”, PROGRAM=”/bin/systemctl try-restart kdump.service”
[root@cenots7 network-scripts]# cat /etc/udev/rules.d/60-vboxadd.rules
KERNEL==”vboxguest”, NAME=”vboxguest”, OWNER=”vboxadd”, MODE=”0660″
KERNEL==”vboxuser”, NAME=”vboxuser”, OWNER=”vboxadd”, MODE=”0666″
70-persistent-net.rules呢?
网卡MAC只在网卡配置文件里有,一旦变了我只需要改网卡配置文件?
[root@cenots7 network-scripts]# grep -r “08:00:27:9A:A6:88” /etc/
/etc/sysconfig/network-scripts/ifcfg-enp0s3:HWADDR=08:00:27:9A:A6:88
/etc/sysconfig/network-scripts/ifcfg-Auto_Ethernet:HWADDR=08:00:27:9A:A6:88
改一下网卡MAC 0800279AA688 -> 0800278C3746
改MAC后网卡不认了,测试结果是只需要改网卡配置文件的MAC,而且两个配置文件都要改才行。我顺便把DHCP改成了固定IP。
[root@cenots7 ~]# cat /etc/sysconfig/network-scripts/ifcfg-Auto_Ethernet
HWADDR=08:00:27:8C:37:46
# HWADDR=08:00:27:9A:A6:88
TYPE=Ethernet
BOOTPROTO=static
IPADDR=192.168.150.180
NETMASK=255.255.255.0
GATEWAY=192.168.150.254
DEFROUTE=yes
PEERDNS=yes
PEERROUTES=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=no
IPV6_AUTOCONF=no
IPV6_DEFROUTE=no
IPV6_PEERDNS=no
IPV6_PEERROUTES=no
IPV6_FAILURE_FATAL=no
NAME=”Auto Ethernet”
UUID=76304098-8f46-4185-8337-bb7f0d90423e
ONBOOT=yes
[root@cenots7 ~]# cat /etc/sysconfig/network-scripts/ifcfg-enp0s3
HWADDR=08:00:27:8C:37:46
# HWADDR=08:00:27:9A:A6:88
TYPE=Ethernet
BOOTPROTO=static
IPADDR=192.168.150.180
NETMASK=255.255.255.0
GATEWAY=192.168.150.254
DEFROUTE=yes
PEERDNS=yes
PEERROUTES=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=no
IPV6_AUTOCONF=no
IPV6_DEFROUTE=no
IPV6_PEERDNS=no
IPV6_PEERROUTES=no
IPV6_FAILURE_FATAL=no
NAME=enp0s3
UUID=66a635c2-9600-437b-8cfb-57e9569f68da
ONBOOT=no
好吧,这样初步解决了基础问题。
前面简单介绍了CentOS 7 在服务和网络方面的一点变化,先前很多烂熟于心的操作指令已经不适用了,不管是否习惯,总要接受、熟悉这些变化。
写上篇的时候还没有最小安装的ISO(CentOS-7.0-1406-x86_64-Minimal.iso),后来安装了首先发现ifconfig、netstat、route、arp都没有了,在哪儿呢?
[root@centos7 ~]# yum search ifconfig
……
======================== Matched: ifconfig =========================
net-tools.x86_64 : Basic networking tools
[root@centos7 ~]#
哦,最小安装默认没有包含这些老工具,如果非用不可,就 yum install net-tools 吧,但我就是要看看不用它们我们怎么来管理网络。
我们将要用到的是ip指令,ss指令和NetworkManager的两个工具 nmtui,nmcli。老实说,这几个工具更加强大了,但还真不太容易习惯呢。
一、ip ss指令替代 ifconfig route arp netstat
1、ip 指令入门
ip [ OPTIONS ] OBJECT { COMMAND | help }
OBJECT 和 COMMAND可以简写到一个字母
ip help 可以查到OBJECT列表和OPTIONS,简写 ip h
ip <OBJECT> help 查看针对该OBJECT的帮助,比如 ip addr help,简写 ip a h
ip addr 查看网络接口地址,简写 ip a
查看网络接口地址,替代ifconfig:
[root@centos7 ~]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
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:15:35:d2 brd ff:ff:ff:ff:ff:ff
inet 192.168.150.110/24 brd 192.168.150.255 scope global enp0s3
valid_lft forever preferred_lft forever
inet6 fe80::a00:27ff:fe15:35d2/64 scope link
valid_lft forever preferred_lft forever
[root@centos7 ~]#
网络接口统计信息
[root@centos7 ~]# ip -s link
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
RX: bytes packets errors dropped overrun mcast
0 0 0 0 0 0
TX: bytes packets errors dropped carrier collsns
0 0 0 0 0 0
2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT qlen 1000
link/ether 08:00:27:15:35:d2 brd ff:ff:ff:ff:ff:ff
RX: bytes packets errors dropped overrun mcast
8135366 131454 0 0 0 456
TX: bytes packets errors dropped carrier collsns
646297 2441 0 0 0 0
2、ip route显示和设定路由
显示路由表
[root@centos7 ~]# ip route
default via 192.168.150.254 dev enp0s3 proto static metric 1024
192.168.150.0/24 dev enp0s3 proto kernel scope link src 192.168.150.110
太难看了,格式化一下(显示的是默认网关和局域网路由,两行的内容没有共通性):
[root@centos7 tmp]# ip route|column -t
default via 192.168.150.254 dev enp0s3 proto static metric 1024
192.168.150.0/24 dev enp0s3 proto kernel scope link src 192.168.150.110
添加静态路由
[root@centos7 ~]# ip route add 10.15.150.0/24 via 192.168.150.253 dev enp0s3
[root@centos7 ~]#
[root@centos7 ~]# ip route|column -t
default via 192.168.150.254 dev enp0s3 proto static metric 1024
10.15.150.0/24 via 192.168.150.253 dev enp0s3 proto static metric 1
192.168.150.0/24 dev enp0s3 proto kernel scope link src 192.168.150.110
[root@centos7 ~]#
[root@centos7 ~]# ping 10.15.150.1
PING 10.15.150.1 (10.15.150.1) 56(84) bytes of data.
64 bytes from 10.15.150.1: icmp_seq=1 ttl=63 time=1.77 ms
64 bytes from 10.15.150.1: icmp_seq=1 ttl=63 time=1.08 ms
64 bytes from 10.15.150.1: icmp_seq=1 ttl=63 time=1.57 ms
^C
删除静态路由只需要把 add 替换成 del,或者更简单的只写目标网络
[root@centos7 ~]# ip route del 10.15.150.0/24
[root@centos7 ~]#
但是,ip route 指令对路由的修改不能保存,重启就没了。
设置永久的静态路由的方法RHEL官网文档讲了几种,试验成功的只有一种:
[root@centos7 ~]#echo “10.15.150.0/24 via 192.168.150.253 dev enp0s3” > /etc/sysconfig/network-scripts/route-enp0s3
重启计算机,或者禁用再启用设备enp0s3才能生效,
注意:/etc/sysconfig/static-routes,/etc/sysconfig/network 配置文件都不好用。
3、用 ip neighbor 代替 arp -n
[root@centos7 ~]# ip nei
192.168.150.254 dev enp0s3 lladdr b8:a3:86:37:bd:f8 STALE
192.168.150.100 dev enp0s3 lladdr 90:b1:1c:94:a1:20 DELAY
192.168.150.253 dev enp0s3 lladdr 00:09:0f:85:86:b9 STALE
4、用ss 代替 netstat
对应netstat -ant
[root@centos7 ~]# ss -ant
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 *:22 *:*
ESTAB 0 0 192.168.150.110:22 192.168.150.100:53233
LISTEN 0 100 ::1:25 :::*
LISTEN 0 128 :::22 :::*
对应netstat -antp
[root@centos7 tmp]# ss -antp
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 100 127.0.0.1:25 *:*
users:((“master”,1817,13))
LISTEN 0 128 *:22 *:*
users:((“sshd”,1288,3))
ESTAB 0 0 192.168.150.110:22 192.168.150.100:59413
users:((“sshd”,2299,3))
LISTEN 0 100 ::1:25 :::*
users:((“master”,1817,14))
LISTEN 0 128 :::22 :::*
users:((“sshd”,1288,4))
[root@centos7 tmp]#
看着真的很别扭,不管多宽的终端屏,users:部分都会折到下一行,其实是在一行的。
格式化一下,内容整齐了,但是标题行串了:
[root@centos7 tmp]# ss -antp|column -t
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 100 127.0.0.1:25 *:* users:((“master”,1817,13))
LISTEN 0 128 *:22 *:* users:((“sshd”,1288,3))
ESTAB 0 0 192.168.150.110:22 192.168.150.100:59413 users:((“sshd”,2299,3))
LISTEN 0 100 ::1:25 :::* users:((“master”,1817,14))
LISTEN 0 128 :::22 :::* users:((“sshd”,1288,4))
5、旧的network脚本和ifcfg文件
Centos7 开始,网络由 NetworkManager 服务负责管理,相对于旧的 /etc/init.d/network 脚本,NetworkManager是动态的、事件驱动的网络管理服务。旧的 /etc/init.d/network 以及 ifup,ifdown 等依然存在,但是处于备用状态,即:NetworkManager运行时,多数情况下这些脚本会调用NetworkManager去完成网络配置任务;NetworkManager么有运行时,这些脚本就按照老传统管理网络。
[root@centos7 ~]# /etc/init.d/network start
Starting network (via systemctl): [ OK ]
注意(via systemctl)。
6、网络配置文件:
/etc/sysconfig/network 说是全局设置,默认里面啥也没有
/etc/hostname 用nmtui修改hostname后,主机名保存在这里
/etc/resolv.conf 保存DNS设置,不需要手工改,nmtui里面设置的DNS会出现在这里
/etc/sysconfig/network-scripts/ 连接配置信息 ifcfg 文件
/etc/NetworkManager/system-connections/ VPN、移动宽带、PPPoE连接
二、nmtui配置基本网络连接
nmtui 属于curses-based text user interface(文本用户界面), 类似 CentOS6 的 setup 工具,但只能编辑连接、启用/禁用连接、更改主机名。系统初装之后可以第一时间用nmtui配置网络,挺方便。
看图吧,功能跳转可以用 tab 键或光标键,用空格或回车执行。每个子功能完成了、退出了或取消了会直接回命令行(觉得不方便就试试 nmtui help,有直达的方法)。
在命令行运行 nmtui
添加连接,支持的连接很丰富啊,还有WiFi呢。但是vpn在哪?呵呵,nmtui目前还不支持。
编辑连接。
想编哪里就跳到哪里,如果不喜欢enp0s3这样的连接名,可以在Profile name处改掉,后面会说改名的好处。
注意,编辑复选框只能用空格
启用/禁用连接,远程连接的时候可以试试执行一下 <Deactivate>,然后以泪洗面吧。。。。
改主机名,改完后重启才有效,不想重启就回命令行执行 hostname <你的主机名> ,注销再登录就好了。
三、nmcli和其他网络设置
nmcli的功能要强大、复杂的多
[root@CentOS7 ~]# nmcli help
Usage: nmcli [OPTIONS] OBJECT { COMMAND | help }
OPTIONS
-t[erse] terse output
-p[retty] pretty output
-m[ode] tabular|multiline output mode
-f[ields] <field1,field2,…>|all|common specify fields to output
-e[scape] yes|no escape columns separators in values
-n[ocheck] don’t check nmcli and NetworkManager versions
-a[sk] ask for missing parameters
-w[ait] <seconds> set timeout waiting for finishing operations
-v[ersion] show program version
-h[elp] print this help
OBJECT
g[eneral] NetworkManager’s general status and operations
n[etworking] overall networking control
r[adio] NetworkManager radio switches
c[onnection] NetworkManager’s connections
d[evice] devices managed by NetworkManager
OBJECT和COMMAND可以用全称也可以用简称,最少可以只用一个字母,建议用头三个字母。OBJECT里面我们平时用的最多的就是connection和device,这里需要简单区分一下connection和device。
device叫网络接口,是物理设备
connection是连接,偏重于逻辑设置
多个connection可以应用到同一个device,但同一时间只能启用其中一个connection。
这样的好处是针对一个网络接口,我们可以设置多个网络连接,比如静态IP和动态IP,再根据需要up相应的connection。
上篇说了,/etc/udev/rules.d/70-persistent-net.rules 文件没有了,新添加的网卡NetworkManager自动识别。我们现在试试,给Virtualbox虚拟机增加第二块网卡,虚拟机启动后直接可以看到并且生效了。
[root@centos7 ~]# nmcli con show
NAME UUID TYPE DEVICE
Wired connection 1 0c451785-b098-4c5c-994d-9107171b439d 802-3-ethernet enp0s8
enp0s3 295e1841-0ff3-4db9-8800-4e25fc2944da 802-3-ethernet enp0s3
“Wired connection 1”是新增的网络连接,对应的网络接口叫enp0s8;
“enp0s3”是最初的网络连接,默认与网络接口同名。
我们可以用nmtui把两个连接改成我们熟悉的名字(nmcli也能,但比较麻烦哦)。改完了看看连接:
[root@centos7 ~]# nmcli con show
NAME UUID TYPE DEVICE
eth1 0c451785-b098-4c5c-994d-9107171b439d 802-3-ethernet enp0s8
eth0 295e1841-0ff3-4db9-8800-4e25fc2944da 802-3-ethernet enp0s3
这样就舒服多了!
看看设备:
[root@centos7 ~]# nmcli dev show enp0s3
GENERAL.DEVICE: enp0s3
GENERAL.TYPE: ethernet
GENERAL.HWADDR: 08:00:27:15:35:D2
GENERAL.MTU: 1500
GENERAL.STATE: 100 (connected)
GENERAL.CONNECTION: eth0
GENERAL.CON-PATH: /org/freedesktop/NetworkManager/ActiveConnection/4
WIRED-PROPERTIES.CARRIER: on
IP4.ADDRESS[1]: ip = 192.168.150.110/24, gw = 192.168.150.254
IP4.DNS[1]: 192.168.150.254
IP6.ADDRESS[1]: ip = fe80::a00:27ff:fe15:35d2/64, gw = ::
[root@centos7 ~]#
[root@centos7 ~]# nmcli dev show enp0s8
GENERAL.DEVICE: enp0s8
GENERAL.TYPE: ethernet
GENERAL.HWADDR: 08:00:27:98:D1:B7
GENERAL.MTU: 1500
GENERAL.STATE: 100 (connected)
GENERAL.CONNECTION: eth1
GENERAL.CON-PATH: /org/freedesktop/NetworkManager/ActiveConnection/3
WIRED-PROPERTIES.CARRIER: on
IP4.ADDRESS[1]: ip = 10.0.3.15/24, gw = 10.0.3.2
IP4.DNS[1]: 10.0.3.2
IP6.ADDRESS[1]: ip = fe80::a00:27ff:fe98:d1b7/64, gw = ::
[root@centos7 ~]#
但是试试用连接名查设备呢?
[root@centos7 ~]# nmcli dev show eth0
Error: Device ‘eth0’ not found.
用设备名查连接也是一样出错:
[root@centos7 ~]# nmcli con show enp0s3
Error: enp0s3 – no such connection profile.
这回能看清楚了吧?懂得了连接和设备的区别,在一些指令帮助里面有的参数需要connection,有的需要device,就不会糊涂了。
需要注意的是,enp0s3设备对应的连接名改为eth0,但对应的ifcfg文件并没有改名,我们改改试试,改之前先备份原配置文件到其他目录(注意,不能备份到network-scripts目录!)。
[root@centos7 network-scripts]# cat ifcfg-enp0s3
TYPE=Ethernet
BOOTPROTO=none
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=no
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_PEERDNS=yes
IPV6_PEERROUTES=yes
IPV6_FAILURE_FATAL=no
NAME=eth0
UUID=295e1841-0ff3-4db9-8800-4e25fc2944da
ONBOOT=yes
IPADDR0=192.168.150.110
PREFIX0=24
GATEWAY0=192.168.150.254
DNS1=192.168.150.254
HWADDR=08:00:27:15:35:D2
[root@centos7 network-scripts]#
[root@centos7 network-scripts]# cp ifcfg-enp0s3 /root/
[root@centos7 network-scripts]# mv ifcfg-enp0s3 ifcfg-eth0
[root@centos7 network-scripts]#
[root@centos7 network-scripts]# systemctl restart network
[root@centos7 network-scripts]#
没有问题,配置文件改名成功,强迫症又一次胜利了,耶!
我们还可以看到,ifcfg-eth0里面没有参数指明设备enp0s3,唯一与设备enp0s3相对应的是MAC地址(HWADDR 08:00:27:15:35:D2)。
现在试试增加一个针对新网络接口enp0s8的连接,现有连接是 eth1:
[root@centos7 network-scripts]# cat ifcfg-eth1
HWADDR=08:00:27:98:D1:B7
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=eth1
UUID=0c451785-b098-4c5c-994d-9107171b439d
ONBOOT=yes
嗯,是自动获得IP的。
我们新建一个连接eth1-1,分配固定IP,用nmtui可以很容易的建立
[root@centos7 ~]# nmcli con show
NAME UUID TYPE DEVICE
eth1-1 ef5c3fbc-f07a-4d6e-9f4d-83f239ffc2e9 802-3-ethernet —
eth1 0c451785-b098-4c5c-994d-9107171b439d 802-3-ethernet enp0s8
eth0 295e1841-0ff3-4db9-8800-4e25fc2944da 802-3-ethernet enp0s3
只是增加了一个连接,其他的没有变化。
我们启动新连接 eth1-1
[root@centos7 ~]# nmcli con up eth1-1
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/2)
[root@centos7 ~]#
[root@centos7 ~]# nmcli con show
NAME UUID TYPE DEVICE
eth1-1 ef5c3fbc-f07a-4d6e-9f4d-83f239ffc2e9 802-3-ethernet enp0s8
eth1 0c451785-b098-4c5c-994d-9107171b439d 802-3-ethernet —
eth0 295e1841-0ff3-4db9-8800-4e25fc2944da 802-3-ethernet enp0s3
[root@centos7 ~]#
[root@centos7 ~]# nmcli -t -f IP4.ADDRESS dev show enp0s8
IP4.ADDRESS[1]:ip = 10.0.3.110/24, gw = 10.0.3.2
“nmcli -t -f IP4.ADDRESS dev show enp0s8” 是以脚本友好的方式显示网络接口信息。
现在可以很明显的看到,将连接eth1-1 up了之后,eth1-1占据了原来由eth1占据的DEVICE enp0s8,并且IP地址已经是指定的10.0.3.110/24 了。
[root@centos7 network-scripts]# cat ifcfg-eth1-1
TYPE=Ethernet
BOOTPROTO=none
IPADDR0=10.0.3.110
PREFIX0=24
GATEWAY0=10.0.3.2
DNS1=10.0.3.2
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=no
NAME=eth1-1
UUID=ef5c3fbc-f07a-4d6e-9f4d-83f239ffc2e9
DEVICE=enp0s8
ONBOOT=yes
ifcfg-eth1-1文件里没有HWADDR参数,但是多了一个DEVICE参数,等于enp0s8,你可以对比一下ifcfg-eth1看看区别在哪。
连接的配置文件 ifcfg-*,可以用 DEVICE 指定设备名,也可以用HWADDR指定设备的MAC地址,最终结果都一样的指向某个设备(网络接口)。
我们再试试用nmcli增加连接:
[root@centos7 ~]# nmcli con add con-name eth1-2 ifname enp0s8 type ethernet ip4 10.0.3.120/24 gw4 10.0.3.2
Connection ‘eth1-2’ (468815f5-ce97-43bd-904a-3078e2e59fec) successfully added.
[root@centos7 ~]#
[root@centos7 ~]# nmcli con show
NAME UUID TYPE DEVICE
eth1-1 ef5c3fbc-f07a-4d6e-9f4d-83f239ffc2e9 802-3-ethernet enp0s8
eth1 0c451785-b098-4c5c-994d-9107171b439d 802-3-ethernet —
eth0 295e1841-0ff3-4db9-8800-4e25fc2944da 802-3-ethernet enp0s3
eth1-2 468815f5-ce97-43bd-904a-3078e2e59fec 802-3-ethernet —
[root@centos7 ~]#
[root@centos7 ~]# nmcli con up eth1-2
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/3)
[root@centos7 ~]#
[root@centos7 ~]# nmcli con show
NAME UUID TYPE DEVICE
eth1-1 ef5c3fbc-f07a-4d6e-9f4d-83f239ffc2e9 802-3-ethernet —
eth1 0c451785-b098-4c5c-994d-9107171b439d 802-3-ethernet —
eth0 295e1841-0ff3-4db9-8800-4e25fc2944da 802-3-ethernet enp0s3
eth1-2 468815f5-ce97-43bd-904a-3078e2e59fec 802-3-ethernet enp0s8
[root@centos7 ~]#
[root@centos7 ~]# nmcli -t -f IP4.ADDRESS dev show enp0s8
IP4.ADDRESS[1]:ip = 10.0.3.120/24, gw = 10.0.3.2
[root@centos7 ~]#
再看看新的ifcfg文件:
[root@centos7 network-scripts]# cat ifcfg-eth1-2
TYPE=Ethernet
BOOTPROTO=none
IPADDR0=10.0.3.120
PREFIX0=24
GATEWAY0=10.0.3.2
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_PEERDNS=yes
IPV6_PEERROUTES=yes
IPV6_FAILURE_FATAL=no
NAME=eth1-2
UUID=468815f5-ce97-43bd-904a-3078e2e59fec
DEVICE=enp0s8
ONBOOT=yes
[root@centos7 network-scripts]#
现在up的连接是eth1-2,网络接口enp0s8的IP是10.0.3.120
如果重新up连接eth1-1,nmcli con up eth1-1,网络接口enp0s8的IP变成了10.0.3.110
如果重新up连接eth1, nmcli con up eth1, 网络接口enp0s8的IP就回到原来自动获得的值10.0.3.15
所以,对一个网络接口设置不同的连接,可以快速的切换不同的网络配置,这个真的满厉害的。
编辑连接
用nmtui编辑连接后,ifcfg文件也会有相应的改动;手工修改ifcfg后,nmtui中也能看到。
但是,不论用nmtui还是直接修改ifcfg文件,想让新的配置生效,我们需要load连接配置文件并重新up连接。
举例,我们编辑ifcfg-eth1-1,把IP改为10.0.3.111,然后执行nmcli con reload 或 nmcli con load /etc/sysconfig/network-scripts/ifcfg-eth1-1
结果如下:
[root@centos7 ~]# nmcli con load /etc/sysconfig/network-scripts/ifcfg-eth1-1
[root@centos7 ~]# nmcli con up eth1-1
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/2)
[root@centos7 ~]# ip addr show enp0s8
3: enp0s8: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 08:00:27:98:d1:b7 brd ff:ff:ff:ff:ff:ff
inet 10.0.3.111/24 brd 10.0.3.255 scope global enp0s8
valid_lft forever preferred_lft forever
inet6 fe80::a00:27ff:fe98:d1b7/64 scope link
valid_lft forever preferred_lft forever
[root@centos7 ~]#
新的IP地址生效了。
BONUS:如果有物理WLAN,nmcli能查看wifi,瞅瞅来,好玩吧?(示例来自nmcli-examples(5) man page,指令: man 5 nmcli-examples)
$ nmcli device wifi list
* SSID MODE CHAN RATE SIGNAL BARS SECURITY
netdatacomm_local Infra 6 54 MB/s 37 ▂▄__ WEP
* F1 Infra 11 54 MB/s 98 ▂▄▆█ WPA1
LoremCorp Infra 1 54 MB/s 62 ▂▄▆_ WPA2 802.1X
Internet Infra 6 54 MB/s 29 ▂___ WPA1
HPB110a.F2672A Ad-Hoc 6 54 MB/s 22 ▂___ —
鉴于很多基本配置可以用nmtui做,而且往往设置一次就够了,nmcli命令行其他强大功能等用的时候再查吧,这里不多讲了。
参考:
https://access.RedHat.com/documentation/en-US/Red_Hat_Enterprise_Linux/7/html/Networking_Guide/index.html (这文档写的吧,,,,唉。。。)
nmcli-examples(5) man page — Gives examples of nmcli commands.
ip(8) man page — Describes the ip utility’s command syntax.
nmcli(1) man page — Describes NetworkManager’s command‐line tool.
更多CentOS相关信息见CentOS 专题页面 http://www.linuxidc.com/topicnews.aspx?tid=14
本文永久更新链接地址:http://www.linuxidc.com/Linux/2014-09/107375.htm
一、nmtui手工添加IP
看一下当前网络设备的IP,为了精简指令输出,指令就要复杂一些了:
[root@CentOS7 ~]# nmcli -t -f IP4.ADDRESS dev show enp0s3
IP4.ADDRESS[1]:ip = 192.168.150.110/24, gw = 192.168.150.254
[root@centos7 ~]#
[root@centos7 ~]# ip addr show enp0s3 scope global
2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 08:00:27:15:35:d2 brd ff:ff:ff:ff:ff:ff
inet 192.168.150.110/24 brd 192.168.150.255 scope global enp0s3
valid_lft forever preferred_lft forever
[root@centos7 ~]#
[root@centos7 ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0
……
IPADDR0=192.168.150.110
PREFIX0=24
GATEWAY0=192.168.150.254
DNS1=192.168.150.254
HWADDR=08:00:27:15:35:D2
IPADDR1=192.168.150.111
PREFIX1=24
[root@centos7 ~]#
新IP已经被加入到ifcfg-eth0中,原始地址的关键字是 IPADDR0、PREFIX0,新地址的关键字是 IPADDR1、PREFIX1 。
[root@centos7 ~]# nmcli con load /etc/sysconfig/network-scripts/ifcfg-eth0
[root@centos7 ~]#
[root@centos7 ~]# nmcli -t -f IP4.ADDRESS dev show enp0s3
IP4.ADDRESS[1]:ip = 192.168.150.110/24, gw = 192.168.150.254
[root@centos7 ~]#
[root@centos7 ~]# ip addr show enp0s3 scope global
2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 08:00:27:15:35:d2 brd ff:ff:ff:ff:ff:ff
inet 192.168.150.110/24 brd 192.168.150.255 scope global enp0s3
valid_lft forever preferred_lft forever
[root@centos7 ~]#
load connection 后,NetworkManager 也没有知晓新增IP,新增IP也没有出现在 ip addr show 中。
[root@centos7 ~]# systemctl restart network
[root@centos7 ~]#
[root@centos7 ~]# nmcli -t -f IP4.ADDRESS dev show enp0s3
IP4.ADDRESS[1]:ip = 192.168.150.110/24, gw = 192.168.150.254
IP4.ADDRESS[2]:ip = 192.168.150.111/24, gw = 192.168.150.254
[root@centos7 ~]#
[root@centos7 ~]# ip addr show enp0s3 scope global
2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 08:00:27:15:35:d2 brd ff:ff:ff:ff:ff:ff
inet 192.168.150.110/24 brd 192.168.150.255 scope global enp0s3
valid_lft forever preferred_lft forever
[root@centos7 ~]#
[root@centos7 ~]#
[root@centos7 ~]# ping 192.168.150.111
PING 192.168.150.111 (192.168.150.111) 56(84) bytes of data.
From 192.168.150.110 icmp_seq=1 Destination Host Unreachable
From 192.168.150.110 icmp_seq=2 Destination Host Unreachable
……
[root@centos7 ~]#
重启network服务后,NetworkManager 知晓新增IP了,但新增IP还是没有生效。
[root@centos7 ~]# nmcli dev connect enp0s3
[root@centos7 ~]#
[root@centos7 ~]#
[root@centos7 ~]# nmcli -t -f IP4.ADDRESS dev show enp0s3
IP4.ADDRESS[1]:ip = 192.168.150.110/24, gw = 192.168.150.254
IP4.ADDRESS[2]:ip = 192.168.150.111/24, gw = 192.168.150.254
[root@centos7 ~]#
[root@centos7 ~]#
[root@centos7 ~]# ip addr show enp0s3 scope global
2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 08:00:27:15:35:d2 brd ff:ff:ff:ff:ff:ff
inet 192.168.150.110/24 brd 192.168.150.255 scope global enp0s3
valid_lft forever preferred_lft forever
inet 192.168.150.111/24 brd 192.168.150.255 scope global secondary enp0s3
valid_lft forever preferred_lft forever
[root@centos7 ~]#
[root@centos7 ~]#
[root@centos7 ~]# ping 192.168.150.111
PING 192.168.150.111 (192.168.150.111) 56(84) bytes of data.
64 bytes from 192.168.150.111: icmp_seq=1 ttl=64 time=0.039 ms
64 bytes from 192.168.150.111: icmp_seq=2 ttl=64 time=0.079 ms
……
[root@centos7 ~]#
重新连接设备 enp0s3,新IP生效了。注意:不需要先断开设备(nmcli dev disconnect enp0s3),断开设备的话ssh立刻断线。重新连接设备的过程中,ssh是不会断线的。
nmtui添加IP后,需要:
nmcli con load /etc/sysconfig/network-scripts/ifcfg-eth0
nmcli dev connect enp0s3
二、手工添加IP到ifcfg-eth0
在ifcfg-eth0配置文件中手工添加如下两行:
IPADDR2=192.168.150.112
PREFIX2=24
先执行 nmcli con load /etc/sysconfig/network-scripts/ifcfg-eth0 后,在nmtui中可以看到新增IP。(也可以简单的用 nmcli con reload ,reload 和 load的区别自己脑补一下吧。)
再执行 nmcli dev connect enp0s3 后,IP 192.168.150.112 才生效的。
手工添加IP到ifcfg-eth0后,需要:
nmcli con load /etc/sysconfig/network-scripts/ifcfg-eth0 或 nmcli con reload
nmcli dev connect enp0s3
三、用ip addr add指令添加/删除IP,即刻生效,重启不保留。
在线添加IP:
[root@centos7 ~]# ip addr add 192.168.150.113/24 dev enp0s3
[root@centos7 ~]#
[root@centos7 ~]#
[root@centos7 ~]# nmcli -t -f IP4.ADDRESS dev show enp0s3
IP4.ADDRESS[1]:ip = 192.168.150.110/24, gw = 192.168.150.254
IP4.ADDRESS[2]:ip = 192.168.150.111/24, gw = 192.168.150.254
IP4.ADDRESS[3]:ip = 192.168.150.112/24, gw = 192.168.150.254
IP4.ADDRESS[4]:ip = 192.168.150.113/24, gw = 192.168.150.254
[root@centos7 ~]#
[root@centos7 ~]#
[root@centos7 ~]# ip addr show enp0s3 scope global
2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 08:00:27:15:35:d2 brd ff:ff:ff:ff:ff:ff
inet 192.168.150.110/24 brd 192.168.150.255 scope global enp0s3
valid_lft forever preferred_lft forever
inet 192.168.150.111/24 brd 192.168.150.255 scope global secondary enp0s3
valid_lft forever preferred_lft forever
inet 192.168.150.112/24 brd 192.168.150.255 scope global secondary enp0s3
valid_lft forever preferred_lft forever
inet 192.168.150.113/24 scope global secondary enp0s3
valid_lft forever preferred_lft forever
[root@centos7 ~]#
[root@centos7 ~]#
[root@centos7 ~]# ping 192.168.150.113
PING 192.168.150.113 (192.168.150.113) 56(84) bytes of data.
64 bytes from 192.168.150.113: icmp_seq=1 ttl=64 time=0.098 ms
64 bytes from 192.168.150.113: icmp_seq=2 ttl=64 time=0.255 ms
……
/etc/sysconfig/network-scripts/ifcfg-eth0 配置文件里并没有192.168.150.113,所以重启后该IP不会保留。
在线删除IP:
[root@centos7 ~]# ip addr del 192.168.150.113/24 dev enp0s3
[root@centos7 ~]#
[root@centos7 ~]#
[root@centos7 ~]# nmcli -t -f IP4.ADDRESS dev show enp0s3
IP4.ADDRESS[1]:ip = 192.168.150.110/24, gw = 192.168.150.254
IP4.ADDRESS[2]:ip = 192.168.150.111/24, gw = 192.168.150.254
IP4.ADDRESS[3]:ip = 192.168.150.112/24, gw = 192.168.150.254
[root@centos7 ~]#
[root@centos7 ~]#
[root@centos7 ~]# ip addr show enp0s3 scope global
2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 08:00:27:15:35:d2 brd ff:ff:ff:ff:ff:ff
inet 192.168.150.110/24 brd 192.168.150.255 scope global enp0s3
valid_lft forever preferred_lft forever
inet 192.168.150.111/24 brd 192.168.150.255 scope global secondary enp0s3
valid_lft forever preferred_lft forever
inet 192.168.150.112/24 brd 192.168.150.255 scope global secondary enp0s3
valid_lft forever preferred_lft forever
四、使用子连接配置文件 ifcfg-*:n
子连接配置文件是 Centos6 常用的方法,即:如果当前网卡配置文件为 ifcfg-eth0,则子连接配置文件可以为 ifcfg-eth0:1,并且文件中需要写明 DEVICE=eth0:1 , IPADDR=<新IP地址> 。
我们试试在Centos7 中是否也支持。理论上应该是不支持的,因为在Centos7的网络连接配置文件(即 ifcfg-*)中,DEVICE关键字已经具有了新的定义,即我们上篇说的“设备”。参考前面几页。
首先注释掉ifcfg-eth0中的其他地址,复制 ifcfg-eth0 为 ifcfg-eth0:1,编辑ifcfg-eth0:1, DEVICE 设为eth0:1 ,IP设为 192.168.150.119 ,然后 nmcli con reload , nmcli dev con enp0s3
复制代码
[root@centos7 ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0
TYPE=Ethernet
BOOTPROTO=none
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=no
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_PEERDNS=yes
IPV6_PEERROUTES=yes
IPV6_FAILURE_FATAL=no
NAME=eth0
UUID=295e1841-0ff3-4db9-8800-4e25fc2944da
ONBOOT=yes
IPADDR0=192.168.150.110
PREFIX0=24
GATEWAY0=192.168.150.254
DNS1=192.168.150.254
HWADDR=08:00:27:15:35:D2
#IPADDR1=192.168.150.111
#PREFIX1=24
#IPADDR2=192.168.150.112
#PREFIX2=24
[root@centos7 ~]#
[root@centos7 ~]#
[root@centos7 ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0:1
TYPE=Ethernet
BOOTPROTO=none
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=no
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_PEERDNS=yes
IPV6_PEERROUTES=yes
IPV6_FAILURE_FATAL=no
NAME=eth0:1
DEVICE=eth0:1
UUID=295e1841-0ff3-4db9-8800-4e25fc2944da
ONBOOT=yes
IPADDR0=192.168.150.119
PREFIX0=24
GATEWAY0=192.168.150.254
DNS1=192.168.150.254
HWADDR=08:00:27:15:35:D2
[root@centos7 ~]#
[root@centos7 ~]# nmcli con reload
[root@centos7 ~]# nmcli dev connect enp0s3
[root@centos7 ~]#
[root@centos7 ~]# nmcli -t -f IP4.ADDRESS dev show enp0s3
IP4.ADDRESS[1]:ip = 192.168.150.110/24, gw = 192.168.150.254
[root@centos7 ~]#
[root@centos7 ~]#
[root@centos7 ~]# ip addr show enp0s3 scope global
2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 08:00:27:15:35:d2 brd ff:ff:ff:ff:ff:ff
inet 192.168.150.110/24 brd 192.168.150.255 scope global eth0:1
valid_lft forever preferred_lft forever
[root@centos7 ~]#
可以看到ifcfg-eth0:1里的新IP确实没有生效。
五、总结
•ip addr show 中显示的IP才是有效的。
•ip addr add 能在线添加IP,立即生效,但重启即丢。
•ip addr del 能在线删除IP,立即生效,但重启即丢。
•子连接(ifcfg-*:n)已经无效了。
•nmtui和编辑ifcfg-eth0 文件效果基本相同,��不能立即生效,必须 load 连接再重新connect网络设备,即:
nmcli con load /etc/sysconfig/network-scripts/ifcfg-eth0 或 nmcli reload
nmcli dev connect enp0s3