2.install centos firsttime setup

最近工作關係開始玩 CentOS 了,本次實作環境中採用的是 CentOS 7.4 (1709) Kernel 3.10.0-693.el7.x86_64) 映像檔,也就是新版 CentOS 7.4 最小化安裝版本 (Minimal Install),那麼開始來玩玩吧。💪

實作環境

停止及停用 NetworkManager 系統服務

建立好使用者帳號後接下來便是設定 CentOS 的網路功能,在本文設定中網路功能是以設定固定 IP 位址來進行說明。首先,在 CentOS 7.4 Minimal Install 運作環境中,執行「systemctl list-units --type service |grep running」指令後,可以看到預設仍會啟用 NetworkManager 系統服務,但個人的網路設定習慣不喜歡使用它,所以在組態設定 CentOS 主機網路環境之前,先執行「systemctl stop NetworkManager」指令以便停止 NetworkManager 系統服務,接著執行「systemctl disable NetworkManager」指令以便停用 NetworkManager 系統服務 (以避免 CentOS 主機重新啟動後不會自動執行)。

圖、停止及停用 NetworkManager 系統服務

組態設定主機名稱

預設情況下,採用 CentOS 7.4 Minimal Install 安裝模式後主機名稱為「localhost.localdomain」,你可以使用「hostname」「hostnamectl」指令進行查看。倘若,你希望能夠調整 CentOS 虛擬主機名稱時,可以執行「hostnamectl set-hostname "centos74.weithenn.org" --static」指令即可立即套用生效,後續再次使用「hostname」「hostnamectl」指令進行確認,可以發現 CentOS 主機名稱已經變更為「centos74.weithenn.org」。事實上,該指令將會把組態設定值套用至「/etc/hostname」組態設定檔當中。

圖、組態設定 CentOS 主機名稱

組態設定 CentOS 主機網路資訊

接著,我們直接透過相關組態設定檔的方式,設定 CentOS 主機的網路資訊分別將固定 IP 位址、網路遮罩等相關資訊寫入「/etc/sysconfig/network-scripts/ifcfg-eth0」網卡設定檔中、預設閘道寫入「/etc/sysconfig/network」設定檔中、DNS 名稱解析資訊寫入「/etc/resolve.conf」設定檔中。最後,便可以執行「systemctl restart network」指令重新啟動網路服務,然後再執行「systemctl status network」查看網路服務運作狀態。

# cat /etc/sysconfig/network-scripts/ifcfg-eth0

TYPE=Ethernet

BOOTPROTO=static

IPV6INIT=no

DEVICE=eth0

ONBOOT=yes

IPADDR=10.10.75.7

PREFIX=24

# cat /etc/sysconfig/network

GATEWAY=10.10.75.254

# cat /etc/hosts

127.0.0.1 localhost

# cat /etc/resolv.conf

search weithenn.org

nameserver 168.95.192.1

nameserver 168.95.1.1

nameserver 8.8.8.8

# systemctl restart network

# systemctl status network

● network.service - LSB: Bring up/down networking

Loaded: loaded (/etc/rc.d/init.d/network; bad; vendor preset: disabled)

Active: active (exited) since Mon 2017-10-23 221:41:58 CST; 4min 54s ago

Docs: man:systemd-sysv-generator(8)

Process: 2143 ExecStop=/etc/rc.d/init.d/network stop (code=exited, status=0/SUCCESS)

Process: 2315 ExecStart=/etc/rc.d/init.d/network start (code=exited, status=0/SUCCESS)

Oct 23 22:41:53 centos74.weithenn.org systemd[1]: Starting LSB: Bring up/down networking...

Oct 23 22:41:53 centos74.weithenn.org network[17550]: Bringing up loopback interface: [ OK ]

Oct 23 22:41:53 centos74.weithenn.org network[17550]: Bringing up interface eth0: [ OK ]

Oct 23 22:41:53 centos74.weithenn.org systemd[1]: Started LSB: Bring up/down networking.

圖、組態設定固定 IP 位址

當 CentOS 主機的網路服務重新啟動並套用新的組態設定後,接著便可以使用 ping 指令來判斷主機是否能順利連上網際網路及進行名稱解析的動作,或者藉此判斷此台主機的網路通訊是卡在哪個環節上以便除錯。

# ping -c2 127.0.0.1 //檢查 Loopback

PING 127.0.0.1 (127.0.0.1) 56(84) bytes of data.

64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.031 ms

64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.053 ms

--- 127.0.0.1 ping statistics ---

2 packets transmitted, 2 received, 0% packet loss, time 999ms

rtt min/avg/max/mdev = 0.031/0.042/0.053/0.011 ms

# ping -c2 172.21.11.73 //檢查固定 IP 位址

PING 172.21.11.73 (172.21.11.73) 56(84) bytes of data.

64 bytes from 172.21.11.73: icmp_seq=1 ttl=64 time=0.030 ms

64 bytes from 172.21.11.73: icmp_seq=2 ttl=64 time=0.053 ms

--- 172.21.11.73 ping statistics ---

2 packets transmitted, 2 received, 0% packet loss, time 999ms

rtt min/avg/max/mdev = 0.030/0.041/0.053/0.013 ms

# ping -c2 172.21.11.254 //檢查 CentOS 與預設閘道之間的連線

PING 172.21.11.254 (172.21.11.254) 56(84) bytes of data.

64 bytes from 172.21.11.254: icmp_seq=1 ttl=255 time=0.465 ms

64 bytes from 172.21.11.254: icmp_seq=2 ttl=255 time=0.445 ms

--- 172.21.11.254 ping statistics ---

2 packets transmitted, 2 received, 0% packet loss, time 1000ms

rtt min/avg/max/mdev = 0.445/0.455/0.465/0.010 ms

# ping -c2 168.95.192.1 //檢查 CentOS 是否能夠與指定的 DNS 伺服器連線

PING 168.95.192.1 (168.95.192.1) 56(84) bytes of data.

64 bytes from 168.95.192.1: icmp_seq=1 ttl=248 time=1.72 ms

64 bytes from 168.95.192.1: icmp_seq=2 ttl=248 time=1.46 ms

--- 168.95.192.1 ping statistics ---

2 packets transmitted, 2 received, 0% packet loss, time 1001ms

rtt min/avg/max/mdev = 1.466/1.596/1.726/0.130 ms

# ping -c2 weithenn.org //檢查 CentOS 能否順利進行名稱解析

PING weithenn.org (216.239.38.21) 56(84) bytes of data.

64 bytes from any-in-2615.1e100.net (216.239.38.21): icmp_seq=1 ttl=45 time=7.91 ms

64 bytes from any-in-2615.1e100.net (216.239.38.21): icmp_seq=2 ttl=45 time=8.72 ms

--- weithenn.org ping statistics ---

2 packets transmitted, 2 received, 0% packet loss, time 1000ms

rtt min/avg/max/mdev = 7.918/8.321/8.724/0.403 ms

沒有 ifconfig / netstat / nslookup 指令?

CentOS 5 / 6 版本中,預設都會安裝 net-tools 套件 (包含 ifconfig / netstat 指令)。但是,從 CentOS 7 版本開始預設不會安裝該套件,原因是官方認為 ifconfig 指令無法完整顯示網路卡的 IP 位址組態設定,建議可以改用 ipss 等指令 (詳細資訊請參考 FAQ/CentOS7 - CentOS Wiki)。

倘若,仍希望使用 ifconfig / netstat 指令的話,只要執行「yum -y install net-tools」指令安裝 net-tools 套件即可。此外,預設情況下也已經拿掉 nslookup 指令,只要執行「yum -y install bind-utils」指令安裝 bind-utils 套件即可。如下操作所示,在尚未安裝 net-tools、bind-utils 套件之前,並無法使用 ifconfig / netstat / nslookup 指令,在完成安裝 net-tools、bind-utils 套件後便可以順利執行 ifconfig / netstat / nslookup 指令。

# ifconfig

-bash: ifconfig: command not found

# netstat

-bash: netstat: command not found

# nslookup

-bash: nslookup: command not found

# yum -y install net-tools bind-utils

# ifconfig eth0

eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500

inet 10.10.75.7 netmask 255.255.255.0 broadcast 10.10.75.255

inet6 fe80::215:5dff:feac:5915 prefixlen 64 scopeid 0x20<link>

ether 00:15:5d:ac:59:15 txqueuelen 1000 (Ethernet)

RX packets 20688 bytes 13770965 (13.1 MiB)

RX errors 0 dropped 2 overruns 0 frame 0

TX packets 6043 bytes 509742 (497.7 KiB)

TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

# netstat -tunpl

Active Internet connections (only servers)

Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name

tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 771/sshd

tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1092/master

tcp6 0 0 :::22 :::* LISTEN 771/sshd

tcp6 0 0 ::1:25 :::* LISTEN 1092/master

udp 0 0 127.0.0.1:323 0.0.0.0:* LISTEN 542/chronyd

udp6 0 0 ::1:323 :::* LISTEN 542/chronyd

停用相關服務使用 IPv6

預設情況下,CentOS 主機會同時使用 IPv4 及 IPv6 網路功能,然而目前企業及組織的運作環境仍然相當少使用 IPv6 網路功能。因此,基於節省系統資源的概念下希望停用 IPv6 網路功能,如下操作結果可以看到使用「ifconfig eth0」指令有看到 inet (IPv4) 及 inet6 (IPv6)「netstat -tunpl」指令後,預設 SSH / Postfix / Chronyd 系統服務同時使用 IPv4 / IPv6 網路功能。

# ifconfig eth0

eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500

inet 10.10.75.7 netmask 255.255.255.0 broadcast 10.10.75.255

inet6 fe80::215:5dff:feac:5915 prefixlen 64 scopeid 0x20<link>

ether 00:15:5d:ac:59:15 txqueuelen 1000 (Ethernet)

RX packets 20688 bytes 13770965 (13.1 MiB)

RX errors 0 dropped 2 overruns 0 frame 0

TX packets 6043 bytes 509742 (497.7 KiB)

TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

# netstat -tunpl

Active Internet connections (only servers)

Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name

tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 771/sshd

tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1092/master

tcp6 0 0 :::22 :::* LISTEN 771/sshd

tcp6 0 0 ::1:25 :::* LISTEN 1092/master

udp 0 0 127.0.0.1:323 0.0.0.0:* LISTEN 542/chronyd

udp6 0 0 ::1:323 :::* LISTEN 542/chronyd

停用網卡 IPv6

根據 CentOS Wiki - FAQ/CentOS7 - How do i disable IPv6? 官方文件內容可知,應該在「/etc/sysctl.conf」設定檔中,加入「net.ipv6.conf.all.disable_ipv6 = 1」、「net.ipv6.conf.default.disable_ipv6 = 1」內容,然後執行「sysctl -p」指令以便停用 CentOS 主機的 IPv6 網路功能。

# vi /etc/sysctl.conf //加入下列 2 行參數

net.ipv6.conf.all.disable_ipv6 = 1

net.ipv6.conf.default.disable_ipv6 = 1

# sysctl -p

net.ipv6.conf.all.disable_ipv6 = 1

net.ipv6.conf.default.disable_ipv6 = 1

停用 SSH IPv6

根據 CentOS Wiki - FAQ/CentOS7 - How do i disable IPv6? 官方文件的建議請修改 SSH 組態設定檔內容,讓 SSH 系統服務僅使用 IPv4 網路功能。

# vi /etc/ssh/sshd_config

#AddressFamily any //預設值

AddressFamily inet //修改後 (inet is ipv4 only, inet6 is ipv6 only)

停用 Postfix IPv6

根據 CentOS Wiki - FAQ/CentOS7 - How do i disable IPv6? 官方文件的建議請修改 Postfix 組態設定檔內容,讓 Postfix 系統服務僅使用 IPv4 網路功能。

# vi /etc/postfix/main.cf

#inet_interfaces = localhost //預設值

inet_interfaces = 127.0.0.1 //修改後

停用 Chronyd IPv6

從 CentOS 7 版本開始,預設改為採用 Chrony 套件來擔任 NTP 時間校時的工作,請修改 Chrony 服務組態設定檔內容,以便讓 Chrony 系統服務僅使用 IPv4 網路功能。詳細資訊請參考 16.2. Understanding chrony and Its Configuration - Red Hat Customer Portal

# vi /etc/sysconfig/chronyd

OPTIONS="" //預設值

OPTIONS="-4" //修改後

修改完成後,建議重新啟動主機以便完整的套用生效。當 CentOS 主機重新啟動完成後,再次登入主機後使用「ifconfig eth0」指令發現已經沒有 inet6 (IPv6),而執行「netstat -tunpl」指令會發現預設的 SSH / Postfix / Chrony 系統服務僅使用 IPv4 網路功能。

# ifconfig eth0

eth0: flags=4163 mtu 1500

inet 10.10.75.7 netmask 255.255.255.0 broadcast 10.10.75.255

ether 00:15:5d:4b:10:05 txqueuelen 1000 (Ethernet)

RX packets 20688 bytes 13770965 (13.1 MiB)

RX errors 0 dropped 2 overruns 0 frame 0

TX packets 6043 bytes 509742 (497.7 KiB)

TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

# netstat -tunpl

Active Internet connections (only servers)

Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name

tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 852/sshd

tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1089/master

udp 0 0 127.0.0.1:323 0.0.0.0:* LISTEN 532/chronyd