Skip to content

自分用・検証用 CentOS8 作成手順メモ(2019/10/02 版)

以前に以下のメモを書きました。

CentOS8 がリリースされた為、改めて「検証用 Linux の作り方」メモをアップデートしておきます。 以下を前提にしています。

  • vSphere 上に作成する仮想マシンである
  • ベースは CentOS8 64bit とする
  • CentOS8 は最小構成でインストールする
  • 作成した仮想マシンはテンプレート化する

SSH 接続時の警告時を表示しない

別のホストへ SSH 接続する際、警告が表示されないように設定しておきます。 また「Unable to negotiate with 192.168.1.1 port 22: no matching key exchange method found. Their offer: diffie-hellman-group1-sha1」等のエラーを回避する為に、必要な鍵交換アルゴリズムを追加しておきます。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
mkdir ~/.ssh/
chmod 700 ~/.ssh/
cat << 'EOS' > ~/.ssh/config
KexAlgorithms +diffie-hellman-group1-sha1
Ciphers aes128-cbc

host *
        StrictHostKeyChecking no
        UserKnownHostsFile=/dev/null
EOS
chmod 600 ~/.ssh/config

SSH 公開鍵をコピーする

ログイン処理をセキュア、且つ簡略化する為に公開鍵をコピーしておきます。

1
2
3
4
cat << 'EOS' >> ~/.ssh/authorized_keys
(公開鍵)
EOS
chmod 600 ~/.ssh/authorized_keys

/root に存在する不要ファイルを削除する

インストール直後は /root 配下に anaconda-ks.cfg が存在します。 不要であれば削除しておきます。

1
rm /root/anaconda-ks.cfg

NTP サーバを指定する

NTP サーバを指定しておきます。 /etc/chrony.conf に時刻同期したいサーバを指定します。 検証環境に併せたサーバを指定すれば良いと思いますが、特に指定が無く、インターネットにアクセス可能な場合は Google Time Server 等で良いと思います (設定変更を反映したい場合は chronyd を再起動しておきます)。

1
2
3
4
server 216.239.35.12 iburst
server 216.239.35.8 iburst
server 216.239.35.4 iburst
server 216.239.35.0 iburst

NTP サーバとの同期状態は chronyc sources で確認します。

1
2
3
4
5
6
7
8
# chronyc sources
210 Number of sources = 4
MS Name/IP address         Stratum Poll Reach LastRx Last sample
===============================================================================
^* time4.google.com              1   8   377   216   +795us[ +368us] +/-   18ms
^+ time3.google.com              1   8   377    26  +8126us[+8126us] +/-   25ms
^+ time2.google.com              1   8   377    89  +3367us[+3367us] +/-   24ms
^+ time1.google.com              1   8   377   410  +2815us[+2366us] +/-   21ms

firewalld を停止する

firewalld は停止しておきます。

1
2
systemctl stop firewalld
systemctl disable firewalld

SELinux を停止する

SELinux は停止しておきます。

1
sed -i -e "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/sysconfig/selinux

プロンプトの色を変更する

一般ユーザのプロンプトは緑色に、root ユーザのプロンプトは赤色に変更します。

1
2
echo "export PS1='\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '" >> /etc/profile
echo "export PS1='\[\033[01;31m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\\$ '" >> /root/.bashrc

対話的な alias を無効化する

検証環境ではコマンドを実行する度に対話的な確認は不必要なので、alias を無効化しておきます。

1
2
3
sed -i -e "s/alias rm='rm -i'/#alias rm='rm -i'/g" /root/.bashrc
sed -i -e "s/alias cp='cp -i'/#alias cp='cp -i'/g" /root/.bashrc
sed -i -e "s/alias mv='mv -i'/#alias mv='mv -i'/g" /root/.bashrc

GRUB のタイムアウト時間を短くする

デフォルトでは GRUB のタイムアウト時間が 5 秒になっています。 検証環境の仮想マシンは少しでも早く起動して欲しいので、タイムアウト時間を 1 秒に縮めます。

1
2
sed -i -e "s/^GRUB_TIMEOUT=5/GRUB_TIMEOUT=1/g" /etc/default/grub
grub2-mkconfig -o /boot/grub2/grub.cfg

基本的なパッケージを追加する

普段、よく使うであろうパッケージを追加しておきます。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
dnf -y install \
    bash-completion \
    bind-utils \
    git \
    lsof \
    tcpdump \
    telnet \
    tree \
    unzip \
    vim-enhanced \
    wget \
    yum-utils \
    zip

vi → vim の alias を追加する

root ユーザでも vi とタイプしたら vim が起動するようにエイリアスを設定しておきます。

1
echo "alias vi='vim'" >> /root/.bashrc

EPEL からパッケージを追加する

必要であれば、EPEL からパッケージを追加しておきます。 以下は fping や nmap を追加する例です。

1
2
3
4
dnf -y install epel-release
dnf -y install \
    fping \
    nmap

パッケージを最新にする

インストールされているパッケージを最新にします。 カーネルがアップデートされている場合を想定し、併せて再起動しておきます。

1
2
3
4
5
6
dnf -y update
dnf clean all
sync
sync
sync
reboot

古い世代のカーネル等、不要になったパッケージは削除します。

1
dnf -y remove $(dnf repoquery --installonly --latest-limit=-1 -q)

pyenv をインストールする

CentOS8 の最小インストールでは Python がインストールされていません。 pyenv をインストールし、任意の Python バージョンを利用できるようにします。 まず、必要なパッケージをインストールし、続いて pyenv をインストールします。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
dnf -y install \
  bzip2 \
  bzip2-devel \
  gcc \
  git \
  libffi-devel \
  make \
  openssl \
  openssl-devel \
  readline \
  readline-devel \
  sqlite \
  sqlite-devel \
  zlib-devel
curl -L https://raw.githubusercontent.com/pyenv/pyenv-installer/master/bin/pyenv-installer | bash
cat << 'EOS' >> ~/.bashrc
export PATH="/root/.pyenv/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"
EOS
source ~/.bashrc

pyenv install --list でインストール可能な Python バージョンを確認します。 現時点では 3.7.4 が最新でした。

1
pyenv install --list

今回は 3.7.4 をインストールします。

1
pyenv install 3.7.4

インストールした Python バージョンが利用されるように設定しておきます。

1
2
pyenv global 3.7.4
pyenv rehash

pip をインストールする

pyenv をインストール後、更に必要であれば pip もインストールしておきます。

1
curl -L https://bootstrap.pypa.io/get-pip.py | python

現時点では pip 19.2.3 がインストールされました。

1
2
# pip --version
pip 19.2.3 from /root/.pyenv/versions/3.7.3/lib/python3.7/site-packages/pip (python 3.7)

キャッシュ / コマンド実行履歴を削除しておく

テンプレート化する際には不要なので dnf のキャッシュとシェルのコマンド実行履歴を削除しておきます。 ここまで完了したら仮想マシンの電源をオフにします。

1
2
3
dnf clean all
history -c
poweroff

VM から DVD メディアを取り出しておく

テンプレート化する前に仮想マシンの光学ドライブから CentOS8 のインストールに利用した DVD メディアを取り出しておきます。

テンプレート化する

ここまで用意が出来たら仮想マシンをテンプレート化します。 以降、検証用の仮想マシンを作成したい場合はこのテンプレートから複製します。

補足

以下は補足です。

sshd は設定変更しない

DNS が利用できない環境でテストする場合は ssnd を UseDNS = no にしておく方がタイムアウトを待たずに済み、効率が良いかも知れません。 しかし、CentOS8 の初期状態では UseDNS = no になっていました。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# sshd -T -f /dev/null | sort
addressfamily any
allowagentforwarding yes
allowstreamlocalforwarding yes
allowtcpforwarding yes
authenticationmethods any
authorizedkeyscommand none
authorizedkeyscommanduser none
authorizedkeysfile .ssh/authorized_keys .ssh/authorized_keys2
authorizedprincipalscommand none
authorizedprincipalscommanduser none
authorizedprincipalsfile none
banner none
challengeresponseauthentication yes
chrootdirectory none
ciphers chacha20-poly1305@openssh.com,aes128-ctr,aes192-ctr,aes256-ctr,aes128-gcm@openssh.com,aes256-gcm@openssh.com
clientalivecountmax 3
clientaliveinterval 0
compression yes
disableforwarding no
exposeauthinfo no
fingerprinthash SHA256
forcecommand none
gatewayports no
gssapiauthentication no
gssapicleanupcredentials yes
gssapienablek5users no
gssapikexalgorithms gss-gex-sha1-,gss-group14-sha1-
gssapikeyexchange no
gssapistorecredentialsonrekey no
gssapistrictacceptorcheck yes
hostbasedacceptedkeytypes ecdsa-sha2-nistp256-cert-v01@openssh.com,ecdsa-sha2-nistp384-cert-v01@openssh.com,ecdsa-sha2-nistp521-cert-v01@openssh.com,ssh-ed25519-cert-v01@openssh.com,rsa-sha2-512-cert-v01@openssh.com,rsa-sha2-256-cert-v01@openssh.com,ssh-rsa-cert-v01@openssh.com,ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521,ssh-ed25519,rsa-sha2-512,rsa-sha2-256,ssh-rsa
hostbasedauthentication no
hostbasedusesnamefrompacketonly no
hostkey /etc/ssh/ssh_host_ecdsa_key
hostkey /etc/ssh/ssh_host_ed25519_key
hostkey /etc/ssh/ssh_host_rsa_key
hostkeyagent none
hostkeyalgorithms ecdsa-sha2-nistp256-cert-v01@openssh.com,ecdsa-sha2-nistp384-cert-v01@openssh.com,ecdsa-sha2-nistp521-cert-v01@openssh.com,ssh-ed25519-cert-v01@openssh.com,rsa-sha2-512-cert-v01@openssh.com,rsa-sha2-256-cert-v01@openssh.com,ssh-rsa-cert-v01@openssh.com,ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521,ssh-ed25519,rsa-sha2-512,rsa-sha2-256,ssh-rsa
ignorerhosts yes
ignoreuserknownhosts no
ipqos af21 cs1
kbdinteractiveauthentication yes
kerberosauthentication no
kerberosorlocalpasswd yes
kerberosticketcleanup yes
kerberosuniqueccache no
kerberosusekuserok yes
kexalgorithms curve25519-sha256,curve25519-sha256@libssh.org,ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group-exchange-sha256,diffie-hellman-group16-sha512,diffie-hellman-group18-sha512,diffie-hellman-group14-sha256,diffie-hellman-group14-sha1
listenaddress 0.0.0.0:22
listenaddress [::]:22
logingracetime 120
loglevel INFO
macs umac-64-etm@openssh.com,umac-128-etm@openssh.com,hmac-sha2-256-etm@openssh.com,hmac-sha2-512-etm@openssh.com,hmac-sha1-etm@openssh.com,umac-64@openssh.com,umac-128@openssh.com,hmac-sha2-256,hmac-sha2-512,hmac-sha1
maxauthtries 6
maxsessions 10
maxstartups 10:30:100
passwordauthentication yes
permitemptypasswords no
permitlisten any
permitopen any
permitrootlogin without-password
permittty yes
permittunnel no
permituserenvironment no
permituserrc yes
pidfile /var/run/sshd.pid
port 22
printlastlog yes
printmotd yes
pubkeyacceptedkeytypes ecdsa-sha2-nistp256-cert-v01@openssh.com,ecdsa-sha2-nistp384-cert-v01@openssh.com,ecdsa-sha2-nistp521-cert-v01@openssh.com,ssh-ed25519-cert-v01@openssh.com,rsa-sha2-512-cert-v01@openssh.com,rsa-sha2-256-cert-v01@openssh.com,ssh-rsa-cert-v01@openssh.com,ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521,ssh-ed25519,rsa-sha2-512,rsa-sha2-256,ssh-rsa
pubkeyauthentication yes
rdomain none
rekeylimit 0 0
revokedkeys none
showpatchlevel no
streamlocalbindmask 0177
streamlocalbindunlink no
strictmodes yes
syslogfacility AUTH
tcpkeepalive yes
trustedusercakeys none
usedns no
usepam no
versionaddendum none
x11displayoffset 10
x11forwarding no
x11maxdisplays 1000
x11uselocalhost yes
xauthlocation /usr/bin/xauth