コンテンツにスキップ

Ubuntu Server の構築手順

1. 作業用ユーザーアカウントを作成

1-1. 現在のユーザーを確認する

ME=$(whoami)
echo "現在のユーザーは ${ME} です。"

1-2. 新しいユーザー名を設定する

NAME=cardano

1-3. 新しいユーザーを作成する

adduser $NAME
sudo adduser $NAME

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グループに追加する

usermod -G sudo $NAME
sudo usermod -G sudo $NAME

1-5. ログアウトする

exit

1-6. 新しいユーザーでログインする

ターミナルのユーザーとパスワードを先ほど作成したユーザーとパスワードに書き換えて再接続します。

2. ユーザーの設定をする

2-1. ブランケットペーストモードをOFFに設定します。

echo "set enable-bracketed-paste off" >> ~/.inputrc

2-2. 再度ログアウトして、再接続する

exit

3. SSH鍵認証方式へ切り替え

3-1. ペアキーの作成

ssh-keygen -t ed25519 -N '' -C ssh_connect -f ~/.ssh/ssh_ed25519

3-2. ペアキーの確認

cd ~/.ssh
ls

3-3. 公開鍵を利用出来るようにする

cd ~/.ssh/
cat ssh_ed25519.pub >> authorized_keys

3-4. 鍵ファイルの権限を設定する

chmod 600 authorized_keys
chmod 700 ~/.ssh

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をポート番号に変更して実行する

SSH_PORT=xxxxx

3-8. 設定ファイルを書き換える

sudo sed -i.port -E "s/^#Port[[:space:]]*22([[:space:]].*)?$/Port ${SSH_PORT}/" /etc/ssh/sshd_config

3-9. 設定ファイルの構文をチェックする

sudo sshd -t

3-10. 設定ファイルを再読み込みする

sudo service sshd reload

4. ファイアーウォールを有効化する

4-1. SSHのポートを確認する

SSH_PORT=`grep "Port" /etc/ssh/sshd_config | sed -e 's/[^0-9]//g'`
echo "SSHのポート番号は ${SSH_PORT} です"

4-2. SSHのポートを開放する

sudo ufw allow ${SSH_PORT}/tcp

4-3. ファイアーウォールを有効化

sudo ufw enable

4-4. ファイアーウォールの状態を確認する

sudo ufw status numbered

4-5. IPv6が有効な場合は(v6)の項目を削除する

sudo ufw delete 2

4-6. 再接続

ポートと鍵を設定して再接続する

5. システムの自動更新を有効にする

5-1. パッケージのインストール

sudo apt install unattended-upgrades -y

5-2. 自動更新を有効にする

sudo dpkg-reconfigure --priority=low unattended-upgrades

6. Fail2banをインストールする

6-1. fail2banをインストールする

sudo apt install fail2ban -y

6-2. SSHのポート番号

SSH_PORT=`grep "Port" /etc/ssh/sshd_config | sed -e 's/[^0-9]//g'`
echo "SSHのポート番号は ${SSH_PORT} です"

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を再起動する

sudo systemctl restart fail2ban

7. Chrony

7-1. chronyをインストールする

sudo apt install chrony -y

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

7-3. ファイアーウォールを開放する

sudo ufw allow 123/udp

7-4. 設定を有効にする

sudo systemctl restart chronyd