首頁 > 軟體

CentOS 7系統設定詳解

2020-06-16 17:04:37

安裝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

二、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 

好吧,這樣初步解決了基礎問題。

更多詳情見請繼續閱讀下一頁的精彩內容http://www.linuxidc.com/Linux/2017-10/147649p2.htm


IT145.com E-mail:sddin#qq.com