検証用 Amazon Linux2023 初期設定メモ (2024/01/04 版)
以前に以下のメモを書きました。
- 検証用 Amazon Linux2 初期設定メモ(2019/10/10 版)
- 検証用 Amazon Linux 2023 初期設定メモ(2023/05/08 版)
- 自分用・検証用 Ubuntu22.04.2LTS 作成手順メモ(2023/06/05 版)
今回は Amazon Linux2023 ベースで改めて検証用の初期設定手順をメモしておきます。 必ずしもセキュリティには十分な配慮をしておらず、あくまで「検証用途」の設定です。
root ユーザで SSH ログイン出来るようにする¶
/root/.ssh/authorized_keys を以下のように変更し、root ユーザで SSH ログイン出来るようにします。 インターネット側の任意のアドレスから SSH を許可するのは非常に危険である為、私の場合は「所定のアドレスからのみ、SSH ログインを許可する」ようにしています。
変更前¶
no-port-forwarding,no-agent-forwarding,no-X11-forwarding,command="echo 'Please login as the user \"ec2-user\" rather than the user \"root\".';echo;sleep 10;exit 142" ssh-rsa ABC………
変更後¶
設定変更が完了したら ec2-user からログアウトして root ユーザでログインしておきます。
ec2-user ユーザを削除する¶
デフォルトで存在する ec2-user は「デフォルトで存在する = ユーザ名を推測される」為、削除しておきます。
SSH 自動切断の防止¶
SSH 接続した際に自動切断されるのを防ぐ為、ClientAliveInterval や ClientAliveCountMax を設定します。 これらの値は以下の意味を持ちます。
| 項目 | 意味 | デフォルト値 |
|---|---|---|
ClientAliveInterval |
指定した秒数ごとに応答確認を行う間隔 (※ 0 の場合は何もしない) | 0 |
ClientAliveCountMax |
応答確認を行う回数 | 3 |
これらの値をチューニングしつつ、root ユーザでの SSH ログインを許可しておきます。 設定が完了したら sshd を再起動しておきます。 sshd の設定確認には sshd -T を実行します。
sed -i -e "s/#ClientAliveInterval 0/ClientAliveInterval 60/g" /etc/ssh/sshd_config && \
sed -i -e "s/#ClientAliveCountMax 3/ClientAliveCountMax 5/g" /etc/ssh/sshd_config && \
sed -i -e "s/#PermitRootLogin prohibit-password/PermitRootLogin yes/g" /etc/ssh/sshd_config && \
systemctl restart sshd.service
パッケージのアップデート¶
EC2 インスタンス直後はアップデートが無いことが多い (というか常に最新?) のですが念の為、パッケージをアップデートしておきます。
ホスト名を変更する¶
ホスト名を変更しておきます。
タイムゾーンを変更する¶
タイムゾーンはデフォルトで UTC です。 Asia/Tokyo (+9:00) へ変更しておきます。 尚、デフォルトで NTP サーバとの時刻同期設定は実施されている為、追加・修正は不要です。
プロンプトを変更する¶
プロンプトの表示をカスタマイズしておきます。
cat << 'EOF' >> /etc/profile
if [ `id -u` = 0 ]; then
PS1="\[\e[1;31m\]\u@\H \W\\$ \[\e[m\]"
else
PS1="\[\e[1;36m\]\u@\H \W\\$ \[\e[m\]"
fi
EOF
対話的な 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
SELinux を無効にする¶
SELinux には 3 つの設定モードがあります。
| モード | ポリシー違反の扱い | ポリシー違反のロギング |
|---|---|---|
disable |
許可 | ロギングしない |
permissive |
許可 | ロギングする |
enforcing |
拒否 | ロギングする |
Amazon Linux2023 ではデフォルトで SELinux が Permissive に設定されています。 「ポリシー違反があっても動作は許可される」ので検証上の妨げは無いのですが、検証環境なので無効にしてしまいます。 手順は Option to disable SELinux に記載されていますが、grubby を使って起動オプションを変更します。 /etc/selinux/config や /etc/sysconfig/selinux を変更しても無効化は出来ないようです。
パッケージのインストール¶
ここからはパッケージをインストールしていきます。
基本的なパッケージ¶
基本的なパッケージをインストールします。
asdf¶
cat << 'EOF' >> ~/.bashrc
. "$HOME/.asdf/asdf.sh"
. "$HOME/.asdf/completions/asdf.bash"
EOF
source ~/.bashrc
direnv¶
Python3¶
dnf -y install \
gcc \
libffi-devel \
make \
openssl-devel \
patch \
readline-devel \
sqlite \
sqlite-devel \
tk-devel \
xz-devel \
zlib-devel
uncmnt¶
コメントを除外することが出来るツールです。 GitHub の sig9org/uncmnt からインストールしておきます。
curl -L https://github.com/sig9org/uncmnt/releases/download/v0.0.2/uncmnt_v0.0.2_linux_amd64 -o /usr/local/bin/uncmnt && \
chmod 755 /usr/local/bin/uncmnt
lego¶
mkdir tmp
cd tmp
curl -LO https://github.com/go-acme/lego/releases/download/v4.14.2/lego_v4.14.2_linux_arm64.tar.gz
tar zxvf lego_v4.14.2_linux_arm64.tar.gz
mv lego /usr/local/bin/
chmod 755 /usr/local/bin/lego
chown root:root /usr/local/bin/lego
参考¶
# sshd -T | sort
addressfamily any
allowagentforwarding yes
allowstreamlocalforwarding yes
allowtcpforwarding yes
authenticationmethods any
authorizedkeyscommand /opt/aws/bin/eic_run_authorized_keys %u %f
authorizedkeyscommanduser ec2-instance-connect
authorizedkeysfile .ssh/authorized_keys
authorizedprincipalscommand none
authorizedprincipalscommanduser none
authorizedprincipalsfile none
banner none
casignaturealgorithms ecdsa-sha2-nistp256,sk-ecdsa-sha2-nistp256@openssh.com,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521,ssh-ed25519,sk-ssh-ed25519@openssh.com,rsa-sha2-256,rsa-sha2-512
chrootdirectory none
ciphers aes256-gcm@openssh.com,chacha20-poly1305@openssh.com,aes256-ctr,aes128-gcm@openssh.com,aes128-ctr
clientalivecountmax 3
clientaliveinterval 0
compression yes
disableforwarding no
exposeauthinfo no
fingerprinthash SHA256
forcecommand none
gatewayports no
gssapiauthentication yes
gssapicleanupcredentials no
gssapienablek5users no
gssapikexalgorithms gss-curve25519-sha256-,gss-nistp256-sha256-,gss-group14-sha256-,gss-group16-sha512-
gssapikeyexchange no
gssapistorecredentialsonrekey no
gssapistrictacceptorcheck yes
hostbasedacceptedalgorithms ssh-ed25519-cert-v01@openssh.com,ecdsa-sha2-nistp256-cert-v01@openssh.com,ecdsa-sha2-nistp384-cert-v01@openssh.com,ecdsa-sha2-nistp521-cert-v01@openssh.com,sk-ssh-ed25519-cert-v01@openssh.com,sk-ecdsa-sha2-nistp256-cert-v01@openssh.com,rsa-sha2-512-cert-v01@openssh.com,rsa-sha2-256-cert-v01@openssh.com,ssh-rsa-cert-v01@openssh.com,ssh-ed25519,ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521,sk-ssh-ed25519@openssh.com,sk-ecdsa-sha2-nistp256@openssh.com,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,ecdsa-sha2-nistp256-cert-v01@openssh.com,sk-ecdsa-sha2-nistp256@openssh.com,sk-ecdsa-sha2-nistp256-cert-v01@openssh.com,ecdsa-sha2-nistp384,ecdsa-sha2-nistp384-cert-v01@openssh.com,ecdsa-sha2-nistp521,ecdsa-sha2-nistp521-cert-v01@openssh.com,ssh-ed25519,ssh-ed25519-cert-v01@openssh.com,sk-ssh-ed25519@openssh.com,sk-ssh-ed25519-cert-v01@openssh.com,rsa-sha2-256,rsa-sha2-256-cert-v01@openssh.com,rsa-sha2-512,rsa-sha2-512-cert-v01@openssh.com
ignorerhosts yes
ignoreuserknownhosts no
ipqos af21 cs1
kbdinteractiveauthentication no
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-group14-sha256,diffie-hellman-group16-sha512,diffie-hellman-group18-sha512
listenaddress 0.0.0.0:22
listenaddress [::]:22
logingracetime 120
loglevel INFO
macs hmac-sha2-256-etm@openssh.com,hmac-sha1-etm@openssh.com,umac-128-etm@openssh.com,hmac-sha2-512-etm@openssh.com,hmac-sha2-256,hmac-sha1,umac-128@openssh.com,hmac-sha2-512
maxauthtries 6
maxsessions 10
maxstartups 10:30:100
modulifile /etc/ssh/moduli
passwordauthentication no
permitemptypasswords no
permitlisten any
permitopen any
permitrootlogin without-password
permittty yes
permittunnel no
permituserenvironment no
permituserrc yes
persourcemaxstartups none
persourcenetblocksize 32:128
pidfile /var/run/sshd.pid
port 22
printlastlog yes
printmotd no
pubkeyacceptedalgorithms ecdsa-sha2-nistp256,ecdsa-sha2-nistp256-cert-v01@openssh.com,sk-ecdsa-sha2-nistp256@openssh.com,sk-ecdsa-sha2-nistp256-cert-v01@openssh.com,ecdsa-sha2-nistp384,ecdsa-sha2-nistp384-cert-v01@openssh.com,ecdsa-sha2-nistp521,ecdsa-sha2-nistp521-cert-v01@openssh.com,ssh-ed25519,ssh-ed25519-cert-v01@openssh.com,sk-ssh-ed25519@openssh.com,sk-ssh-ed25519-cert-v01@openssh.com,rsa-sha2-256,rsa-sha2-256-cert-v01@openssh.com,rsa-sha2-512,rsa-sha2-512-cert-v01@openssh.com
pubkeyauthentication yes
pubkeyauthoptions none
rekeylimit 0 0
revokedkeys none
securitykeyprovider internal
streamlocalbindmask 0177
streamlocalbindunlink no
strictmodes yes
subsystem sftp /usr/libexec/openssh/sftp-server
syslogfacility AUTHPRIV
tcpkeepalive yes
trustedusercakeys none
usedns no
usepam yes
versionaddendum none
x11displayoffset 10
x11forwarding yes
x11maxdisplays 1000
x11uselocalhost yes
xauthlocation /usr/bin/xauth