メインコンテンツまでスキップ

Ubuntu Server の初期設定

Cardano ユーザーを作成する

現在のユーザーを確認する

echo "現在のユーザーは $(whoami) です"

新しいユーザー名を設定する

NAME="cardano"
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]:

ユーザーを sudo グループに追加する

usermod -G sudo $NAME

ログアウトする

exit

新しいユーザーでログインする

先ほど作成した ユーザー と パスワード で再接続します。

ユーザーの設定をする

ブランケットペーストモードをOFFにする

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

再度ログアウトして再接続する

ログアウトし再接続することでブランケットペーストモードOFFを有効にします。

exit

SSH 鍵認証方式へ切り替え

鍵ペアの作成

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

鍵ペアの確認

.ssh ディレクトリ内に ssh_ed25519 ファイルと ssh_ed25519.pub ファイルが出来ている事を確認します。

cd ~/.ssh
ls

鍵で認証出来るようにする

公開鍵 (ssh_ed25519.pub) を authorized_keys ファイルにコピーすることで秘密鍵 (ssh_ed25519) で認証が出来るようにします。

cat ssh_ed25519.pub >> authorized_keys

鍵ファイルの権限を設定する

セキュリティのためにパーミッションを設定します。

chmod 600 authorized_keys
chmod 700 ~/.ssh

鍵ファイルをダウンロードする

SFTPなどを使用して .ssh フォルダ内の ssh_ed25519.pub ファイルと ssh_ed25519 ファイルをローカルにダウンロードします。

ssh_config を書き換える

SSH の設定ファイル /etc/ssh/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

SSH ポート番号を変更する

SSH のポート番号をデフォルトの 22 番から変更します。 ポート番号は 49513~65535 の間の数値を自由に決めて設定してください。

ポート番号を入力しコマンドをコピーしてください
sudo sed -i.port -E "s/^#Port[[:space:]]*22([[:space:]].*)?$/Port XXXXX/" /etc/ssh/sshd_config

SSH 設定ファイルの構文をチェックする

sudo sshd -t

SSH 設定ファイルを再読み込みする

sudo service sshd reload

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

SSH のポートを確認する

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

SSH のポートを開放する

sudo ufw allow ${SSH_PORT}/tcp

ファイアーウォールを有効にする

sudo ufw enable

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

sudo ufw status numbered

SSH 新規接続

接続中の SSH 接続はそのままにして、秘密鍵を指定、ポート番号を変更して新規接続してみる。

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

パッケージのインストール

sudo apt install unattended-upgrades -y

自動更新を有効にする

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

fail2ban のインストール

fail2ban をインストールする

sudo apt install fail2ban -y

SSH ポート番号を確認する

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

設定ファイルを作成する

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

fail2ban を再起動する

再起動し設定を反映させます。

sudo systemctl restart fail2ban

Chrony のインストール

chrony をインストールする

sudo apt install chrony -y

設定ファイルを変更する

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

ファイアーウォールを開放する

sudo ufw allow 123/udp

設定を有効化する

sudo systemctl restart chronyd