Oliver Pelz和Jonathan Hobson撰寫的CentOS 7 Linux Server Cookbook - Second EditionPackt Publishing於2016年1月出版。近幾天在拜讀此書,學到不少知識,對於提高自己對CentOS7的瞭解大有裨益。

本文介紹的是Chapter2 Configuring the System中Language、Clock、Hostname相關命令介紹和使用。


Prerequisite

因相關操作用到Shell配置文件,故先對CentOS中Shell配置文件進行間要介紹。

Shell登錄可分爲交互式登錄非交互式登錄兩種,有不同的 配置文件配置文件讀取順序

  • 交互式登錄
    • 直接通過終端登錄打開的shell進程
    • 使用su - USERNAME執行的登錄切換
  • 非交互式登錄
    • 使用su USERNAME執行的登錄切換
    • 運行腳本

配置文件也分爲兩種profile類bashrc類 * profile:爲 交互式登錄 的Shell進程提供配置,可用於定義環境變量,執行命令和Shell腳本 * 對全局用戶生效 * /etc/profile * /etc/profiled/*.sh * 僅對當前用戶有效 * ~/.bash_profile * bashrc:爲 非交互式登錄 的Shll進程提供配置,可用於定義本地變量,命令別名 * 對全局用戶生效 * /etc/bashrc * 進對當前用戶有效 * ~/.bashrc

注:僅root用戶可以修改全局配置文件

配置文件讀取順序 * 交互式登錄Shell進程 * /etc/profile–> /etc/profile.d/* –> ~/.bash_profile –> ~/.bashrc –> /etc/bashrc * 非交互式登錄Shell進程 * ~/.bashrc –> /etc/bashrc –> /etc/profile.d/*

如果要對兩種登錄類型都有效,建議在/etc/profile.d/*~/.bashrc/etc/bashrc中任選一種進行操作。

讓配置文件生效,此處的/PATH/PROFILE用具體的配置文件替換 * source /PATH/PROFILE * . /PATH/PROFILE

配置文件修改後,只對新啓動的Shell進程有效。


Setting Locale and Keyboard Layout

localectl - Control the system locale and keyboard layout settings —— man localectl

命令localectl用於控制 系統區域鍵盤佈局 設置

書中出現的命令

#顯示當前系統區域和鍵盤映射設置
localectl status

#列出可用的系統區域
localectl list-locales

#列出可用的鍵盤映射
localectl list-keymaps

#爲當前系統設置系統區域
localectl set-locale LANG=zh_TW.UTF-8

#爲當前系統設置鍵盤映射
localectl set-keymap tw

Personal Operations

以下是個人操作示例

localectl

[flying@lemp ~]$ localectl
   System Locale: LANG=en_US.UTF-8
       VC Keymap: us
      X11 Layout: us

#localectl status與命令localectl顯示相同內容
[flying@lemp ~]$ localectl status
   System Locale: LANG=en_US.UTF-8
       VC Keymap: us
      X11 Layout: us
[flying@lemp ~]$

以下是書中原話

The output of this command showed us that language properties in a CentOS 7 system can be separated into locale (system locale) and keymap (VC keymap and all X11 layout properties) setting.

localectl list-locales

#可用的系統區域數目
[flying@lemp ~]$ localectl list-locales | wc -l
785

#篩選與臺灣有關的條目
[flying@lemp ~]$ localectl list-locales | grep -i tw
nan_TW.utf8@latin
nan_TW@latin
zh_TW
zh_TW.big5
zh_TW.euctw
zh_TW.utf8
[flying@lemp ~]$

locale的格式是Language[_Region][.Encoding][@Modificator],如zh_TW.utf8

localectl set-locale

[flying@lemp ~]$ localectl
   System Locale: LANG=en_US.UTF-8
       VC Keymap: us
      X11 Layout: us

#爲當前系統設置系統區域
[flying@lemp ~]$ sudo localectl set-locale LANG=zh_TW.UTF-8

#再次查看
[flying@lemp ~]$ localectl
   System Locale: LANG=zh_TW.UTF-8
       VC Keymap: us
      X11 Layout: us
[flying@lemp ~]$

localectl list-keymaps

#可用的鍵盤映射數目
[flying@lemp ~]$ localectl list-keymaps | wc -l
513

#列出與tw相關的鍵盤映射
[flying@lemp ~]$ localectl list-keymaps | grep -i tw
ttwin_alt-UTF-8
ttwin_cplk-UTF-8
ttwin_ct_sh-UTF-8
ttwin_ctrl-UTF-8
tw
tw-indigenous
tw-saisiyat
[flying@lemp ~]$

localectl set-keymap

[flying@lemp ~]$ localectl
   System Locale: LANG=en_US.UTF-8
       VC Keymap: us
      X11 Layout: us

#爲當前系統設置鍵盤映射
[flying@lemp ~]$ sudo localectl set-keymap tw

#再次查看
[flying@lemp ~]$ localectl
   System Locale: LANG=en_US.UTF-8
       VC Keymap: tw
      X11 Layout: us
[flying@lemp ~]$

localectl set-x11-keymap

修改x11-keymap

[flying@lemp ~]$ localectl
   System Locale: LANG=en_US.UTF-8
       VC Keymap: us
      X11 Layout: us

#設置x11的鍵盤映射
[flying@lemp ~]$ sudo localectl set-x11-keymap --no-convert tw-asisiyat
[sudo] password for flying:

#再次查看
[flying@lemp ~]$ localectl status
   System Locale: LANG=en_US.UTF-8
       VC Keymap: us
      X11 Layout: tw-asisiyat
[flying@lemp ~]$

以下是書中原話

If you want different mappings for X11 than for the console, use localectl --no-convert set-x11-keymap cz-qwerty, where we use cz-qwerty for the keymap code to a Czec qwerty keyboard model (change this accordingly).

如果不加參數--no-convert,則會同時更改VC Keymap的值

[flying@lemp ~]$ localectl
   System Locale: LANG=en_US.UTF-8
       VC Keymap: us
      X11 Layout: us

#設置鍵盤映射
[flying@lemp ~]$ sudo localectl set-x11-keymap tw

#再次查看,X11和VC的值同時被修改
[flying@lemp ~]$ localectl
   System Locale: LANG=en_US.UTF-8
       VC Keymap: tw
      X11 Layout: tw
[flying@lemp ~]$

Different Language Settings

以下是書中原話

Sometimes, single system users need different language settings than the system’s locale (which can only be set by the root user), according to their regional keyboard differences and for interaction with the system in their preferred human language.

不用用戶設置不同的語言,主要通過在配置文件中添加相關變量實現。

Temporarily change

只在當前shell生效

[flying@lemp ~]$ ls !
ls: cannot access !: No such file or directory
[flying@lemp ~]$ export LANG="zh_TW.UTF-8"
[flying@lemp ~]$ ls !
ls: 無法存取 !: 沒有此一檔案或目錄
[flying@lemp ~]$

新打開一個Shell進程則仍提示 ls: cannot access !: No such file or directory

Permanently change

將變量LANG寫入~/.bashrc

#permanently change
[flying@lemp ~]$ echo "export LANG='zh_TW.UTF-8'" >> ~/.bashrc
[flying@lemp ~]$ tail -1 ~/.bashrc
export LANG='zh_TW.UTF-8'
[flying@lemp ~]$ . ~/.bashrc
[flying@lemp ~]$ ls !
ls: 無法存取 !: 沒有此一檔案或目錄
[flying@lemp ~]$

新打開一個Shell進程,顯示的是正體中文


Clock

Synchronizing the system clock with NTP and the chrony suite

以下是書中原話

chrony suite: use chrony service to manage time synchronization

The chrony daemon chronyd, sets and maintains system time through a process of synchronization with a remote server using the NTP protocol for communication

chronyc is a useful command for interacting and controlling a chrony daemon (locally or remotely)

chrony is a pair of programs for maintaining the accuracy of computer clocks. chronyd is a background daemon program that can be started at boot time.

CentOS 7中對系統時鐘的管理主要通過NTPchrony套件

檢查系統是否安裝chrony

[flying@lemp ~]$ rpm -qa | grep -i chrony
chrony-2.1.1-1.el7.centos.x86_64
[flying@lemp ~]$

如果chrony未安裝,可使用yum安裝

#安裝chrony
sudo yum -y install chrony

#啓動chronyd服務
sudo systemctl start chronyd

#查看chronyd服務狀體
sudo systemctl status chronyd

#設置chronyd服務開機啓動
sudo systemctl enable chronyd

查看NTP是否基於網路時間同步

[flying@lemp ~]$ timedatectl
      Local time: Mon 2016-02-22 22:31:30 CST
  Universal time: Mon 2016-02-22 14:31:30 UTC
        RTC time: Mon 2016-02-22 22:31:30
       Time zone: Asia/Shanghai (CST, +0800)
     NTP enabled: yes
NTP synchronized: yes
 RTC in local TZ: yes
      DST active: n/a

Warning: The system is configured to read the RTC time in the local time zone.
         This mode can not be fully supported. It will create various problems
         with time zone changes and daylight saving time adjustments. The RTC
         time is never updated, it relies on external facilities to maintain it.
         If at all possible, use RTC in UTC by calling
         'timedatectl set-local-rtc 0'.

#篩選NTP synchronized相關信息
[flying@lemp ~]$ timedatectl | grep "NTP synchronized"
NTP synchronized: no

如果是no,則可執行如下操作

timedatectl set-ntp	yes

#可修改時間同步服務器
vim /etc/chrony.conf

#更改後重啓chronyd服務
sudo systemctl restart chronyd
# check whether chrony is taking care of your system time synchronization
#查看chronyc是否處理系統時間同步
[flying@lemp ~]$ chronyc tracking
Reference ID    : 202.112.29.82 (dns1.synet.edu.cn)
Stratum         : 3
Ref time (UTC)  : Mon Feb 22 14:31:07 2016
System time     : 0.000907068 seconds fast of NTP time
Last offset     : +0.000812938 seconds
RMS offset      : 0.002520143 seconds
Frequency       : 8.838 ppm slow
Residual freq   : +3.642 ppm
Skew            : 0.603 ppm
Root delay      : 0.022900 seconds
Root dispersion : 0.007659 seconds
Update interval : 64.6 seconds
Leap status     : Normal
[flying@lemp ~]$

#check the network sources chrony uses for synchronization
#查看chrony用於同步的網路資源
[flying@lemp ~]$ chronyc sources
210 Number of sources = 4
MS Name/IP address         Stratum Poll Reach LastRx Last sample
===============================================================================
^- ntp-sz.chl.la                 2   6   377     9  -1650us[-1650us] +/-   95ms
^* dns1.synet.edu.cn             2   6   377    10  +2453us[+4200us] +/-   12ms
^? news.neu.edu.cn               0   7     0   10y     +0ns[   +0ns] +/-    0ns
^+ dns2.synet.edu.cn             2   6   175     8   +781us[ +781us] +/-   13ms
[flying@lemp ~]$

chrony的配置文件爲/etc/chrony.conf,可指定具體的時間服務器,默認是

# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
server 0.centos.pool.ntp.org iburst
server 1.centos.pool.ntp.org iburst
server 2.centos.pool.ntp.org iburst
server 3.centos.pool.ntp.org iburst

可在 http://www.pool.ntp.org/ 查看、選擇自己所在地區的時間服務器,將其更新進/etc/chrony.conf並重啓chronyd服務即可。

建議使用至少3個及以上的時間服務器,以提高NTP服務的準確性。

Timezones

常看當前系統時區

[flying@lemp ~]$ timedatectl | grep "Time zone"
       Time zone: Asia/Shanghai (CST, +0800)
[flying@lemp ~]$

timedatectl list-timezones

時區文件在目錄/usr/share/zoneinfo/

使用timedatectl list-timezones可列出系統中的可用時區

# list available time zones
[flying@lemp ~]$ timedatectl list-timezones | wc -l
417
[flying@lemp ~]$ timedatectl list-timezones | grep -i taipei
Asia/Taipei
[flying@lemp ~]$

timedatectl set-timezone

設置系統時區

#查看當前系統時區
[flying@lemp ~]$ timedatectl | grep "Time zone"
       Time zone: Asia/Shanghai (CST, +0800)

#設置新時區
[flying@lemp ~]$ sudo timedatectl set-timezone Europe/Rome
[sudo] password for flying:

#再次查看系統時區
[flying@lemp ~]$ timedatectl | grep "Time zone"
       Time zone: Europe/Rome (CET, +0100)

#查看具體信息
[flying@lemp ~]$ timedatectl
      Local time: Mon 2016-02-22 15:40:32 CET
  Universal time: Mon 2016-02-22 14:40:32 UTC
        RTC time: Mon 2016-02-22 15:40:32
       Time zone: Europe/Rome (CET, +0100)
     NTP enabled: yes
NTP synchronized: yes
 RTC in local TZ: yes
      DST active: no
 Last DST change: DST ended at
                  Sun 2015-10-25 02:59:59 CEST
                  Sun 2015-10-25 02:00:00 CET
 Next DST change: DST begins (the clock jumps one hour forward) at
                  Sun 2016-03-27 01:59:59 CET
                  Sun 2016-03-27 03:00:00 CEST

#將當前系統時間同步到硬件時鐘

# Set the Hardware Clock to the current System Time.
hwclock --systohc或-w

Hostname

Setting hostname and resolving network

以下是書中原文

Domain Name System (DNS)

Fully Qualified Domain Name (FQDN)

A hostname is a unique label created to identify a machine on a network. A DNS server is used to translate domain names to IP addresses.

hostnamectl

查看系統主機名和相關設置

[flying@lemp ~]$ hostnamectl
   Static hostname: lemp
         Icon name: computer-laptop
           Chassis: laptop
        Machine ID: 49e6b198519f4995bcbe770f382be43b
           Boot ID: a1d8f1f52aa2445eabab63e488c1adbf
  Operating System: CentOS Linux 7 (Core)
       CPE OS Name: cpe:/o:centos:centos:7
            Kernel: Linux 3.10.0-327.10.1.el7.x86_64
      Architecture: x86-64

# hostnamectl status 命令同 hostnamectl
[flying@lemp ~]$ hostnamectl status
   Static hostname: lemp
         Icon name: computer-laptop
           Chassis: laptop
        Machine ID: 49e6b198519f4995bcbe770f382be43b
           Boot ID: a1d8f1f52aa2445eabab63e488c1adbf
  Operating System: CentOS Linux 7 (Core)
       CPE OS Name: cpe:/o:centos:centos:7
            Kernel: Linux 3.10.0-327.10.1.el7.x86_64
      Architecture: x86-64
[flying@lemp ~]$

hostnamectl set-hostname

設置主機名

[flying@lemp ~]$ hostname
lemp
[flying@lemp ~]$ sudo hostnamectl set-hostname Lover
[sudo] password for flying:
[flying@lemp ~]$ hostname
lover
[flying@lemp ~]$ hostnamectl
   Static hostname: lover
   Pretty hostname: Lover
         Icon name: computer-laptop
           Chassis: laptop
        Machine ID: 49e6b198519f4995bcbe770f382be43b
           Boot ID: a1d8f1f52aa2445eabab63e488c1adbf
  Operating System: CentOS Linux 7 (Core)
       CPE OS Name: cpe:/o:centos:centos:7
            Kernel: Linux 3.10.0-327.10.1.el7.x86_64
      Architecture: x86-64
[flying@lemp ~]$

使用hostname --fqdn可獲取FQDN名稱

--fqdn: Display the FQDN (Fully Qualified Domain Name).

[flying@lemp ~]$ hostname --fqdn
lover
[flying@lemp ~]$

另:在CentOS 7中,修改主機名也可通過修改/etc/hostname文件實現,但需要重啓系統。

Extended Reading


Change Log

  • 2016.02.22 23:10 Mon Asia/Beijing
    • 完成初稿,上傳至lempstacker部落格
  • 2016.02.23 08:03 Tue Asia/Beijing
    • 更新hostnamectl部分,添加/etc/hostname修改實現
  • 2016.02.25 14:04 Thu Asia/Beijing
    • 添加Extended Reading
  • 2016.04.07 10:01 Thu Asia/Beijing
    • 添加/etc/chrony.conf配置

  • Note Time:2016.02.22 23:10 Mon
  • Note Location: Asia/Beijing
  • Writer: lempstacker