Skip to main content

Ubuntuのセットアップ

表1 : Ubuntu バージョン要件

Ubuntu Version
Ubuntu 22.04.* LTS
tip

Ubuntuのバージョン確認

lsb_release -d

sudo権限を持つ非rootユーザーの作成

  1. 新規ユーザーを作成します。

ユーザーアカウント名については任意ですので変更してください。

adduser cardano
  1. sudoグループへ追加します。
usermod -aG sudo cardano
  1. rootユーザーからログアウトします。
exit
  1. ターミナルソフトでのユーザーとパスワードを上記で設定したものに書き換え、再接続します。
tip

ブラケットペーストモード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キーのみを使用

  1. SSHペアキーを生成します。

ssh_ed25519(秘密鍵)とssh_ed25519.pub(公開鍵)が.sshフォルダに作成されます。

ssh-keygen -t ed25519 -N '' -C ssh_connect -f ~/.ssh/ssh_ed25519
  1. パーミッションの変更をします。
cd ~/.ssh
cat ssh_ed25519.pub >> authorized_keys
chmod 600 authorized_keys
chmod 700 ~/.ssh
  1. SSHキーファイルをダウンロードします。 ssh_ed25519(秘密鍵)とssh_ed25519.pub(公開鍵)をダウンロードしてssh_ed25519(秘密鍵)をターミナルソフトに設定してください。
tip

FileZillaを用いてダウンロードすると便利です。

  1. 上記完了したら再度SSH接続して、01-custom-ssh.confの編集をします。
info
  • 以下のコマンドで49513 ~ 65535までのランダムな数字を出力します。
ssh_ports=$(shuf -i 49513-65535 -n 1)
echo ${ssh_ports}
  1. 上記で出力されたポート番号を使用して、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
  1. SSH構文をチェックします。
sudo sshd -t
  1. 有効設定の確認
sudo sshd -T | grep -iE 'passwordauthentication|kbdinteractiveauthentication|permitrootlogin|port'
  1. エラーがないことを確認し、SSHプロセスを再起動します。
sudo systemctl restart ssh

システムの更新

  1. システムを更新します。
sudo apt update -y && sudo apt full-upgrade -y
sudo apt autoremove -y
sudo apt autoclean -y
  1. 自動更新を有効に設定します。
sudo apt install unattended-upgrades -y
sudo dpkg-reconfigure --priority=low unattended-upgrades

rootアカウントの無効化設定

  1. rootアカウントを無効に設定します。
sudo passwd -l root

SSHの2要素認証の設定

tip

必須ではありません。
導入には「Google 認証システムアプリ」が必要です。

SSHの2FA設定
warning

設定に失敗するとログインできなくなる場合があるので、設定前に二つ目のウィンドウでサーバーにログインしておいてください。
万が一ログインできなくなった場合、復旧できます。

  1. システムを更新して、libpam-google-authenticatorをインストールします。
sudo apt update -y
sudo apt install libpam-google-authenticator -y

google-authenticatorコマンド実行

google-authenticator

推奨される構成は次のとおりです。

一連の質問が表示されます。

info
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
warning

緊急時のスクラッチコードやQR Codeは必ず保存しておきましょう。

  1. 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
  1. @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
  1. 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
  1. 構文をチェックし、有効化
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

共有メモリの保護

最初に行うべきことの1つは、システムで使用される共有メモリを保護することです。
知らないと、共有メモリは実行中のサービスに対する攻撃に使用される可能性があります。
そのため、システムメモリのその部分を保護する必要があります。

  1. fstabの編集をします。
echo 'tmpfs /dev/shm tmpfs ro,noexec,nosuid 0 0' | sudo tee -a /etc/fstab

追記確認

tail -n 5 /etc/fstab

Fail2banのインストール

tip

特定のIP アドレスから指定された時間内に一定数のログイン失敗が検知された場合、Fail2banはそのIP アドレスからのアクセスをブロックします。

  1. fail2banをインストールします。
sudo apt install fail2ban -y
  1. jail.localの生成をします。
port=`grep "Port" /etc/ssh/sshd_config.d/01-custom-ssh.conf`
ssh_port=${port#"Port"}
output_ssh_port=`echo ${ssh_port} | sed -e 's/[^0-9]//g'`
echo "SSHポート番号 : ${output_ssh_port}"
cat > $HOME/jail.local << EOF
[sshd]
enabled = true
port = ${output_ssh_port}
filter = sshd
logpath = /var/log/auth.log
maxretry = 6
EOF
  1. ファイルを移動します。
sudo mv $HOME/jail.local /etc/fail2ban/
  1. Fail2banの自動起動を有効化し、即時起動
sudo systemctl enable --now fail2ban
  1. Fail2banの起動確認
sudo systemctl status fail2ban --no-pager

ファイアウォールの設定

  1. インバウンドアクセスポートを限定にします。
port=`grep "Port" /etc/ssh/sshd_config.d/01-custom-ssh.conf`
ssh_port=${port#"Port"}
output_ssh_port=`echo ${ssh_port} | sed -e 's/[^0-9]//g'`
echo ${output_ssh_port}
sudo ufw allow ${output_ssh_port}/tcp
  1. ファイアウォールを有効化
sudo ufw enable
  1. ステータスの確認

activeであれば有効化されてます。

sudo ufw status

Swap領域の設定

  1. Swap領域の設定をします。

以下は16GBでのSwap領域の設定です。

sudo fallocate -l 16G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
sudo swapon --show
sudo cp /etc/fstab /etc/fstab.bak
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
echo 'vm.swappiness=10' | sudo tee -a /etc/sysctl.conf
echo 'vm.vfs_cache_pressure=50' | sudo tee -a /etc/sysctl.conf
cat /proc/sys/vm/vfs_cache_pressure
cat /proc/sys/vm/swappiness
  1. サーバーを再起動します。
sudo reboot

Chronyの設定

  1. chronyをインストールします。
sudo apt install chrony -y
  1. chrony.confの生成をします。
cat > $HOME/chrony.conf << EOF
pool time.google.com iburst minpoll 2 maxpoll 2 maxsources 3 maxdelay 0.3
pool time.facebook.com iburst minpoll 2 maxpoll 2 maxsources 3 maxdelay 0.3
pool time.euro.apple.com iburst minpoll 2 maxpoll 2 maxsources 3 maxdelay 0.3
pool time.apple.com iburst minpoll 2 maxpoll 2 maxsources 3 maxdelay 0.3
pool ntp.ubuntu.com iburst minpoll 2 maxpoll 2 maxsources 3 maxdelay 0.3

# This directive specify the location of the file containing ID/key pairs for
# NTP authentication.
keyfile /etc/chrony/chrony.keys

# This directive specify the file into which chronyd will store the rate
# information.
driftfile /var/lib/chrony/chrony.drift

# Uncomment the following line to turn logging on.
#log tracking measurements statistics

# Log files location.
logdir /var/log/chrony

# Stop bad estimates upsetting machine clock.
maxupdateskew 5.0

# This directive enables kernel synchronisation (every 11 minutes) of the
# real-time clock. Note that it can’t be used along with the 'rtcfile' directive.
rtcsync

# Step the system clock instead of slewing it if the adjustment is larger than
# one second, but only in the first three clock updates.
makestep 0.1 -1

# Get TAI-UTC offset and leap seconds from the system tz database
leapsectz right/UTC

# Serve time even if not synchronized to a time source.
local stratum 10
EOF
  1. 既存の設定ファイルをバックアップしてから、新しい設定ファイルを移動させます。
sudo cp /etc/chrony/chrony.conf /etc/chrony/chrony.conf.bak
sudo mv $HOME/chrony.conf /etc/chrony/chrony.conf
  1. UFWの設定をします。
sudo ufw allow 123/udp
  1. chronyd.serviceの再起動
sudo systemctl reload-or-restart chronyd.service

確認コマンド

tip

同期データのソース

chronyc sources

現在のステータス

chronyc tracking