public_notes/content/20240211171220 sshのポートを変更する.md

59 lines
2.3 KiB
Markdown
Raw Permalink Normal View History

# 20240211171220 sshのポートを変更する
#ssh #server
## 変更する理由
[[20240211171652 なぜsshをポート22で実行してはいけないのか|なぜsshを22で実行してはいけないのか]]
## 変更の方法
### サーバー側
2024-04-09 18:15:10 +09:00
Raspberry piの場合、 20240409 では、`/etc/ssh/sshd_config` を編集すればよい。
x86-64でUbuntu/Debianを使用していて、下記の方法をやるには、以下実行が必要(非推奨)
```
# systemctl disable --now ssh.socket
# rm -f /etc/systemd/system/ssh.service.d/00-socket.conf
# rm -f /etc/systemd/system/ssh.socket.d/addresses.conf
# systemctl daemon-reload
# systemctl enable --now ssh.service
```
2024-04-09 18:30:20 +09:00
これを実行すると、 `/etc/ssh/sshd_config` の設定が反映されるようになる。
ただしい設定をしたい場合、
`/usr/share/doc/openssh-server/README.Debian.gz` を参照
2024-02-21 10:20:02 +09:00
`# Port 22` とコメントアウトされている所があるので、 `#`を削除。
22を別のポートに設定する。
sshdを再起動
```
$ sudo service ssh restart
$ service ssh status
● ssh.service - OpenBSD Secure Shell server
Loaded: loaded (/lib/systemd/system/ssh.service; enabled; preset: enabled)
Active: active (running) since Sun 2024-02-11 17:31:52 JST; 30s ago
Docs: man:sshd(8)
man:sshd_config(5)
Process: 1448 ExecStartPre=/usr/sbin/sshd -t (code=exited, status=0/SUCCESS)
Main PID: 1449 (sshd)
Tasks: 1 (limit: 3918)
CPU: 77ms
CGroup: /system.slice/ssh.service
└─1449 "sshd: /usr/sbin/sshd -D [listener] 0 of 10-100 startups"
Feb 11 17:31:51 saipi4 systemd[1]: Starting ssh.service - OpenBSD Secure Shell server...
Feb 11 17:31:52 saipi4 sshd[1449]: Server listening on 0.0.0.0 port *****. # (ここの数字が設定したポート番号になっているかを確認。22のままなら何かが変)
Feb 11 17:31:52 saipi4 sshd[1449]: Server listening on :: port *****.
Feb 11 17:31:52 saipi4 systemd[1]: Started ssh.service - OpenBSD Secure Shell server.
```
### クライアント側
ポートを指定してアクセスして、うまくいくか確認。
```
$ ssh <host> -p [上記の番号]
```
うまくいったら、毎回ポートを指定するのが面倒なので、 `.ssh/config` に、こんな感じで書いておく。
```
Host <host>
Port *****
(...その他)
```