0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Conoha VPS Ubuntu Server 24.04の設定

Last updated at Posted at 2025-08-19

VPS初期設定(セキュリティ&運用準備)

自分のメモ用に記録しているだけなのでわかりにくいと思いますが...

1. VPSに接続しよう

SSHでサーバーに接続する基本的なコマンド

ssh ユーザー名@ホスト名(またはIPアドレス)

2. パッケージの更新(最優先)

sudo apt update && sudo apt upgrade -y

この中の-y は:
yes を自動で答えるyes の略)
apt upgrade を実行すると、普通は「これこれのパッケージをアップグレードします。続行しますか? [Y/n]」けど-yをつけると、すべて「yes(Y)」で自動進行してくれる。

The default action is to keep your current version.
*** cloud.cfg (Y/I/N/O/D/Z) [default=N] ?  

Nと入力してenter


3. 新しい一般ユーザー作成(rootのまま運用しない)

sudo adduser [任意のユーザー名]
3.1 一般ユーザーでログインが出来るか確認
login

login:[ユーザー名]
Password:[パスワード]
3.2 一般ユーザーに管理者権限を追加
usermod -aG sudo [作成した一般ユーザー名]

4. Rootログインを禁止

sudo vi /etc/ssh/sshd_config
  • PermitRootLogin no にする
sudo systemctl restart ssh

5. SSHポートの変更(セキュリティ強化)

Port番号の変更
元の /lib/systemd/system/ssh.socket を直接触るのは危険だから、
/etc/systemd/system/ssh.socket.d/override.conf に書く。

やり方(viで開く場合):

sudo mkdir -p /etc/systemd/system/ssh.socket.d
sudo vi /etc/systemd/system/ssh.socket.d/override.conf

内容はシンプルに:

[Socket]
ListenStream=
ListenStream=0.0.0.0:[ポート番号]
ListenStream=[::]:[ポート番号]

解説

  • ListenStream= ← 元の設定を 一旦クリアする
  • その後に IPv4 と IPv6 の両方を明示的に指定

保存したら反映:

sudo systemctl daemon-reload
sudo systemctl restart ssh.socket
sudo systemctl restart ssh
sudo systemctl enable ssh.socket

確認:

sudo systemctl status ssh.socket
ss -ltnp | grep [ポート番号]

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

sudo ufw enable

Command may disrupt existing ssh connections. Proceed with operation (y|n)? y
Firewall is active and enabled on system startup

状況を確認

sudo ufw status

ファイアウォールを追加

sudo ufw allow 決めたポート番号/tcp  # SSH用(変更したポートに合わせて)
sudo ufw allow 80,443/tcp  # Webサーバー用

ファイアウォールをリロード

sudo ufw reload

7. SSH公開鍵認証の導入

鍵作成の流れ

7.1. さっき作った一般ユーザーでログイン
ssh -p [ポート番号] [ユーザー名]@[IPアドレス]
7.2. 鍵ペアの生成コマンドを実行
ssh-keygen -t rsa -b 4096  # ローカルで鍵生成
ssh-copy-id -p [任意のポート番号] ユーザー名@VPSのIP

RSA鍵の長さはビット単位で表される。この場合4096ビットの鍵が作られる。

任意の名前で作りたい場合:

ssh-keygen -t rsa -b 4096 -f ~/.ssh/my_vps_key
7.3. 鍵の保存場所の指定:
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/your_username/.ssh/id_rsa):

特に指定がなければ、デフォルトの保存場所(~/.ssh/id_rsa)で問題なし。そのままEnterキーを押す。

7.4. パスフレーズの入力:

秘密鍵を保護するためのパスフレーズを入力。セキュリティのために必ず設定することを強く推奨されている。
⤷ Geminiが言ってた。

パスフレーズなしでも作成できるがその分、秘密鍵が漏洩した場合のリスクが高まる。
パスフレーズはログイン時に入力が必要になるらしい(作ったことないからしらん)

7.5. 鍵ペアの確認:

以下のファイルが作成される。

~/.ssh/id_rsa:
これが秘密鍵
絶対に他人に知られないように厳重に保管!

7.6. VPSに公開鍵をコピー
ssh-copy-id -p [任意のポート番号] ユーザー名@VPSのIP
7.7. ホストで.sshフォルダを作成
mkdir .ssh
7.8. パーミッション権限を「700」に設定
chmod 700 .ssh
7.9. 公開鍵を.sshに移動して、ファイル名をauthorized_keysに変更。
mv id_rsa.pub .ssh/authorized_keys
7.10. authorized_keysのパーミッション権限を「600」に設定
chmod 600 .ssh/authorized_keys

8. Passwordログインを封鎖

8.2. 設定ファイルの構造確認

Ubuntu 24.04 の SSH 設定は:

  • 本体: /etc/ssh/sshd_config
  • Drop-in: /etc/ssh/sshd_config.d/*.conf

昇順で読み込まれ、最後の値が優先される。
50-cloud-init.confPasswordAuthentication yes が残っているため、それを上書きする必要がある。


8.3. ハードニング用設定ファイルを追加

最後に必ず勝つファイルを作成する。

sudo vi /etc/ssh/sshd_config.d/99-hardening.conf

内容:

# SSH Hardening Final Overrides
PubkeyAuthentication yes
KbdInteractiveAuthentication no
PasswordAuthentication no
AuthenticationMethods publickey

保存して終了。

補足:cloud-init の再適用対策
Ubuntu 24.04 では、/etc/ssh/sshd_config.d/50-cloud-init.conf が cloud-init によって自動生成され、
そこに PasswordAuthentication yes が書き込まれることがある。
このため sshd_config 本体で no を設定しても、再適用のタイミングで上書きされる可能性があるのが注意点。

そこで 99-hardening.conf のような番号の大きい drop-in ファイルを作成して、
最後に必ず PasswordAuthentication no を読み込ませるのが安全策。
これにより cloud-init が再度 yes を書き込んでも、最終的には no が優先されるため、
パスワードログインが復活することはない。


9. 構文チェック

sudo sshd -t

(エラーが出なければOK)

10. サービス再起動

sudo systemctl restart ssh

11. 設定確認

sudo sshd -T | grep -E 'passwordauthentication|pubkeyauthentication|kbdinteractiveauthentication|authenticationmethods'

期待値:

pubkeyauthentication yes
passwordauthentication no
kbdinteractiveauthentication no
authenticationmethods publickey

12. 接続テスト(必ず別ターミナルから)

12.1 鍵でログイン(成功するはず)
ssh -p [ポート番号] [ユーザー名]@<サーバIP>
12.2 パスワードログインを強制(拒否されるはず)
ssh -o PreferredAuthentications=password -o PubkeyAuthentication=no -p 55225 k-hifumi@<サーバIP>

期待結果:

Permission denied (publickey).
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?