Ubuntu Server の構築手順¶
1. 作業用ユーザーアカウントを作成¶
1-1. 現在のユーザーを確認する¶
1-2. 新しいユーザー名を設定する¶
1-3. 新しいユーザーを作成する¶
New Password: # ユーザーのパスワードを入力
Retype new password: # パスワードの確認再入力
Enter the new value, or press ENTER for the default
Full Name []:
Room Number []:
Work Phone []:
Home Phone []:
Other []:
Is the information correct? [Y/n]:y
1-4. ユーザーをsudoグループに追加する¶
1-5. ログアウトする¶
1-6. 新しいユーザーでログインする¶
ターミナルのユーザーとパスワードを先ほど作成したユーザーとパスワードに書き換えて再接続します。
2. ユーザーの設定をする¶
2-1. ブランケットペーストモードをOFFに設定します。¶
2-2. 再度ログアウトして、再接続する¶
3. SSH鍵認証方式へ切り替え¶
3-1. ペアキーの作成¶
3-2. ペアキーの確認¶
3-3. 公開鍵を利用出来るようにする¶
3-4. 鍵ファイルの権限を設定する¶
3-5. 鍵ファイルをダウンロードする¶
~/.ssh フォルダの ssh_ed25519.pub と ssh_ed25519 をダウンロードする。
3-6. sshd_configを書き換える¶
sudo sed -i.bak -E '/^[[:space:]]*#/!{
/^([[:space:]]*)KbdInteractiveAuthentication\b/{ s/^/#/; a\
KbdInteractiveAuthentication no
}
/^([[:space:]]*)PasswordAuthentication\b/{ s/^/#/; a\
PasswordAuthentication no
}
/^([[:space:]]*)PermitRootLogin\b/{ s/^/#/; a\
PermitRootLogin no
}
/^([[:space:]]*)PermitEmptyPasswords\b/{ s/^/#/; a\
PermitEmptyPasswords no
}
}' /etc/ssh/sshd_config
3-7. SSHポートを設定する¶
Info
ポート番号は49513~65535までの数値で設定してください
xxxxxをポート番号に変更して実行する
3-8. 設定ファイルを書き換える¶
sudo sed -i.port -E "s/^#Port[[:space:]]*22([[:space:]].*)?$/Port ${SSH_PORT}/" /etc/ssh/sshd_config
3-9. 設定ファイルの構文をチェックする¶
3-10. 設定ファイルを再読み込みする¶
4. ファイアーウォールを有効化する¶
4-1. SSHのポートを確認する¶
4-2. SSHのポートを開放する¶
4-3. ファイアーウォールを有効化¶
4-4. ファイアーウォールの状態を確認する¶
4-5. IPv6が有効な場合は(v6)の項目を削除する¶
4-6. 再接続¶
ポートと鍵を設定して再接続する
5. システムの自動更新を有効にする¶
5-1. パッケージのインストール¶
5-2. 自動更新を有効にする¶
6. Fail2banをインストールする¶
6-1. fail2banをインストールする¶
6-2. SSHのポート番号¶
6-3. 設定ファイルを作成する¶
sudo tee /etc/fail2ban/jail.local <<EOF >/dev/null
[sshd]
enabled = true
port = $SSH_PORT
filter = sshd
logpath = /var/log/auth.log
maxretry = 3
EOF
6-3. fail2banを再起動する¶
7. Chrony¶
7-1. chronyをインストールする¶
7-2. 設定ファイルを更新する¶
sudo tee /etc/chrony/chrony.conf <<EOF >/dev/null
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