以前に以下のメモを書きました。
CentOS8 がリリースされた為、改めて「検証用 Linux の作り方」メモをアップデートしておきます。 以下を前提にしています。
目次
- 目次
- SSH 接続時の警告時を表示しない
- SSH 公開鍵をコピーする
- /root に存在する不要ファイルを削除する
- NTP サーバを指定する
- firewalld を停止する
- SELinux を停止する
- プロンプトの色を変更する
- 対話的な alias を無効化する
- GRUB のタイムアウト時間を短くする
- 基本的なパッケージを追加する
- vi → vim の alias を追加する
- EPEL からパッケージを追加する
- パッケージを最新にする
- pyenv をインストールする
- pip をインストールする
- キャッシュ / コマンド実行履歴を削除しておく
- VM から DVD メディアを取り出しておく
- テンプレート化する
- 補足
SSH 接続時の警告時を表示しない
別のホストへ SSH 接続する際、警告が表示されないように設定しておきます。 また「Unable to negotiate with 192.168.1.1 port 22: no matching key exchange method found. Their offer: diffie-hellman-group1-sha1
」等のエラーを回避する為に、必要な鍵交換アルゴリズムを追加しておきます。
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 公開鍵をコピーする
ログイン処理をセキュア、且つ簡略化する為に公開鍵をコピーしておきます。
cat << 'EOS' >> ~/.ssh/authorized_keys (公開鍵) EOS chmod 600 ~/.ssh/authorized_keys
/root に存在する不要ファイルを削除する
インストール直後は /root
配下に anaconda-ks.cfg
が存在します。 不要であれば削除しておきます。
rm /root/anaconda-ks.cfg
NTP サーバを指定する
NTP サーバを指定しておきます。 /etc/chrony.conf
に時刻同期したいサーバを指定します。 検証環境に併せたサーバを指定すれば良いと思いますが、特に指定が無く、インターネットにアクセス可能な場合は Google Time Server 等で良いと思います (設定変更を反映したい場合は chronyd を再起動しておきます)。
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
で確認します。
# 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 は停止しておきます。
systemctl stop firewalld
systemctl disable firewalld
SELinux を停止する
SELinux は停止しておきます。
sed -i -e "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/sysconfig/selinux
プロンプトの色を変更する
一般ユーザのプロンプトは緑色に、root ユーザのプロンプトは赤色に変更します。
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 を無効化しておきます。
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 秒に縮めます。
sed -i -e "s/^GRUB_TIMEOUT=5/GRUB_TIMEOUT=1/g" /etc/default/grub grub2-mkconfig -o /boot/grub2/grub.cfg
基本的なパッケージを追加する
普段、よく使うであろうパッケージを追加しておきます。
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 が起動するようにエイリアスを設定しておきます。
echo "alias vi='vim'" >> /root/.bashrc
EPEL からパッケージを追加する
必要であれば、EPEL からパッケージを追加しておきます。 以下は fping や nmap を追加する例です。
dnf -y install epel-release dnf -y install \ fping \ nmap
パッケージを最新にする
インストールされているパッケージを最新にします。 カーネルがアップデートされている場合を想定し、併せて再起動しておきます。
dnf -y update
dnf clean all
sync
sync
sync
reboot
古い世代のカーネル等、不要になったパッケージは削除します。
dnf -y remove $(dnf repoquery --installonly --latest-limit=-1 -q)
pyenv をインストールする
CentOS8 の最小インストールでは Python がインストールされていません。 pyenv をインストールし、任意の Python バージョンを利用できるようにします。 まず、必要なパッケージをインストールし、続いて pyenv をインストールします。
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 が最新でした。
pyenv install --list
今回は 3.7.4 をインストールします。
pyenv install 3.7.4
インストールした Python バージョンが利用されるように設定しておきます。
pyenv global 3.7.4 pyenv rehash
pip をインストールする
pyenv をインストール後、更に必要であれば pip もインストールしておきます。
curl -L https://bootstrap.pypa.io/get-pip.py | python
現時点では pip 19.2.3 がインストールされました。
# pip --version pip 19.2.3 from /root/.pyenv/versions/3.7.3/lib/python3.7/site-packages/pip (python 3.7)
キャッシュ / コマンド実行履歴を削除しておく
テンプレート化する際には不要なので dnf のキャッシュとシェルのコマンド実行履歴を削除しておきます。 ここまで完了したら仮想マシンの電源をオフにします。
dnf clean all history -c poweroff
VM から DVD メディアを取り出しておく
テンプレート化する前に仮想マシンの光学ドライブから CentOS8 のインストールに利用した DVD メディアを取り出しておきます。
テンプレート化する
ここまで用意が出来たら仮想マシンをテンプレート化します。 以降、検証用の仮想マシンを作成したい場合はこのテンプレートから複製します。
補足
以下は補足です。
sshd は設定変更しない
DNS が利用できない環境でテストする場合は ssnd を UseDNS = no
にしておく方がタイムアウトを待たずに済み、効率が良いかも知れません。 しかし、CentOS8 の初期状態では UseDNS = no
になっていました。
# 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