Ubuntuのセットアップ
表1 : Ubuntu バージョン要件
| Ubuntu Version |
|---|
| Ubuntu 22.04.* LTS |
ヒント
Ubuntuのバージョン確認
lsb_release -d
sudo権限を持つ非rootユーザーの作成
- 新規ユーザーを作成します。
ユーザーアカウント名については任意ですので変更してくださ い。
adduser cardano
sudoグループへ追加します。
usermod -aG sudo cardano
rootユーザーからログアウトします。
exit
- ターミナルソフトでのユーザーとパスワードを上記で設定したものに書き換え、再接続します。
ヒント
ブラケットペーストモードOFF
echo "set enable-bracketed-paste off" >> ~/.inputrc
デーモン再起動自動化
echo "\$nrconf{restart} = 'a';" | sudo tee /etc/needrestart/conf.d/50local.conf
echo "\$nrconf{blacklist_rc} = [qr(^cardano-node\\.service$) => 0, qr(^cnode-blocknotify\\.service$) => 0, qr(^cnode-cncli-sync\\.service$) => 0,];" | sudo tee -a /etc/needrestart/conf.d/50local.conf
SSHパスワード認証を無効にしてSSHキーのみを使用
SSHペアキーを生成します。
ssh_ed25519(秘密鍵)とssh_ed25519.pub(公開鍵)が.sshフォルダに作成されます。
ssh-keygen -t ed25519 -N '' -C ssh_connect -f ~/.ssh/ssh_ed25519
- パーミッションの変更をします。
cd ~/.ssh
cat ssh_ed25519.pub >> authorized_keys
chmod 600 authorized_keys
chmod 700 ~/.ssh
SSHキーファイルをダウンロードします。ssh_ed25519(秘密鍵)とssh_ed25519.pub(公開鍵)をダウンロードしてssh_ed25519(秘密鍵)をターミナルソフトに設定してください。
ヒント
FileZillaを用いてダウンロードすると便利です。
- 上記完了したら再度SSH接続して、
01-custom-ssh.confの編集をします。
備考
- 以下のコマンドで
49513 ~ 65535までのランダムな数字を出力します。
ssh_ports=$(shuf -i 49513-65535 -n 1)
echo ${ssh_ports}
- 上記で出力されたポート番号を使用して、
01-custom-ssh.confを編集します。
sudo tee /etc/ssh/sshd_config.d/01-custom-ssh.conf > /dev/null << EOF
PasswordAuthentication no
KbdInteractiveAuthentication no
PermitRootLogin no
PermitEmptyPasswords no
Port ${ssh_ports}
EOF
SSH構文をチェックします。
sudo sshd -t
- 有効設定の確認
sudo sshd -T | grep -iE 'passwordauthentication|kbdinteractiveauthentication|permitrootlogin|port'
- エラーがないことを確認し、
SSHプロセスを再起動します。
sudo systemctl restart ssh
システムの更新
- システムを更新します。
sudo apt update -y && sudo apt full-upgrade -y
sudo apt autoremove -y
sudo apt autoclean -y
- 自動更新を
有効に設定します。
sudo apt install unattended-upgrades -y
sudo dpkg-reconfigure --priority=low unattended-upgrades
rootアカウントの無効化設定
rootアカウントを無効に設定します。
- 無効化
- 有効化
sudo passwd -l root
危険
🔥 何らかの理由で有効化する場合のみ有効化します。
sudo passwd -u root
SSHの2要素認証の設定
ヒント
必須ではありません。
導入には「Google 認証システムアプリ」が必要です。
SSHの2FA設定
警告
設定に失敗するとログインできなくなる場合があるので、設定前に二つ目のウィンドウでサーバーにログインしておいてください。
万が一ログインできなくなった場合、復旧できます。
- システムを更新して、
libpam-google-authenticatorをインストールします。
sudo apt update -y
sudo apt install libpam-google-authenticator -y
google-authenticatorコマンド実行
google-authenticator
推奨される構成は次のとおりです。
一連の質問が表示されます。
備考
Do you want authentication tokens to be time-based (y/n) : y
Do you want me to update your
"/home/cardano/.google_authenticator" file? (y/n): y
Do you want to disallow multiple uses of the same authentication
token? This restricts you to one login about every 30s, but it increases
your chances to notice or even prevent man-in-the-middle attacks (y/n): y
By default, a new token is generated every 30 seconds by the mobile app.
In order to compensate for possible time-skew between the client and the server,
we allow an extra token before and after the current time. This allows for a
time skew of up to 30 seconds between authentication server and client. If you
experience problems with poor time synchronization, you can increase the window
from its default size of 3 permitted codes (one previous code, the current
code, the next code) to 17 permitted codes (the 8 previous codes, the current
code, and the 8 next codes). This will permit for a time skew of up to 4 minutes
between client and server.
Do you want to do so? (y/n): n
If the computer that you are logging into isn't hardened against brute-force
login attempts, you can enable rate-limiting for the authentication module.
By default, this limits attackers to no more than 3 login attempts every 30s.
Do you want to enable rate-limiting? (y/n) : y
警告
緊急時のスクラッチコードやQR Codeは必ず保存しておきましょう。
- Google Authenticator PAMを有効化します。
grep -q '^auth required pam_google_authenticator.so' /etc/pam.d/sshd \
|| sudo sed -i '/^@include common-auth$/a auth required pam_google_authenticator.so nullok' /etc/pam.d/sshd
- @include common-authの直下に追加されていることを確認します。
grep -n '^auth required pam_google_authenticator\.so' /etc/pam.d/sshd
grep -n -A1 '^@include common-auth$' /etc/pam.d/sshd
- 02-2fa.confファイルに設定します。
sudo tee /etc/ssh/sshd_config.d/02-2fa.conf > /dev/null << EOF
KbdInteractiveAuthentication yes
UsePAM yes
AuthenticationMethods publickey,keyboard-interactive
EOF
- 構文をチェックし、有効化
sudo sshd -t
sudo systemctl reload ssh
補足:SSH 2FAの必須化設定この手順を実施すると、Google Authenticator未設定のユーザーは、SSHログインできなくなります。 必ず以下を満たしてから実施してください。
対象ユーザー全員が google-authenticatorの初期設定を完了している 公開鍵認証(publickey)による SSH ログインが正常に行える 予備の SSH セッションを開いた状態で作業している
必須化するためにnullokを削除します。
sudo sed -i \
's/^auth required pam_google_authenticator\.so nullok$/auth required pam_google_authenticator.so/' \
/etc/pam.d/sshd
設定内容の確認をします。
grep -n '^auth required pam_google_authenticator\.so' /etc/pam.d/sshd
構文チェックし、サービス読み込み
sudo sshd -t
sudo systemctl reload ssh