LoginSignup
2
0

【2024年04月版】Ubuntu24.04 で sshdの待ち受けポートの変更【ssh, sshd】

Last updated at Posted at 2024-04-28

はじめに

Ubuntu24.04 で sshdの待ち受けポートを変更するメモ。

ここでは、待受ポートを 22 から 22222 に変更しています

Ubuntu22.10 から、待受ポートの変更は /etc/ssh/sshd_config の変更やオーバーライドでだけはできなくなっています。

環境

  • Ubuntu22.04

変更手順

  • /etc/ssh/sshd_config をオーバライドして設定を変更する
  • systemctl edit を使って、ssh.socket を変更する
  • service を再起動する

変更前の確認

ポート 22 が開いてる

$ ss -anlt
State             Recv-Q             Send-Q                         Local Address:Port                          Peer Address:Port            Process            
LISTEN            0                  4096                              127.0.0.54:53                                 0.0.0.0:*                                  
LISTEN            0                  128                                  0.0.0.0:22                                 0.0.0.0:*                                  
LISTEN            0                  4096                           127.0.0.53%lo:53                                 0.0.0.0:*                                  
LISTEN            0                  128                                     [::]:22                                    [::]:*      

設定の変更

/etc/ssh/sshd_config のオーバーライド

/etc/ssh/sshd_config をコピーして設定ファイルを作る

$ sudo cp /etc/ssh/sshd_config /etc/ssh/sshd_config.d/99_override.conf
$ sudo vim /etc/ssh/sshd_config.d/99_override.conf

必要な行だけ残す、ここではポート番号を変更する行だけ残す

$ cat /etc/ssh/sshd_config.d/99_override.conf
Port 22222

ssh.socket の変更

$ sudo systemctl edit ssh.socket

エディタが上がってくるので以下の部分をコメントアウトを外して変更

[Socket]
ListenStream=22222

全体的には以下の感じ

  GNU nano 7.2                                    /etc/systemd/system/ssh.socket.d/.#override.conf39d759716b0aac37                                              
### Editing /etc/systemd/system/ssh.socket.d/override.conf
### Anything between here and the comment below will become the contents of the drop-in file



### Edits below this comment will be discarded


### /usr/lib/systemd/system/ssh.socket
# [Unit]
# Description=OpenBSD Secure Shell server socket
# Before=sockets.target ssh.service
# ConditionPathExists=!/etc/ssh/sshd_not_to_be_run
# 
[Socket]
ListenStream=22222
# Accept=no
# FreeBind=yes
# 
# [Install]
# WantedBy=sockets.target
# RequiredBy=ssh.service

### /run/systemd/generator/ssh.socket.d/addresses.conf
# # Automatically generated by sshd-socket-generator
# 
                                                                       [ Read 29 lines ]
^G Help         ^O Write Out    ^W Where Is     ^K Cut          ^T Execute      ^C Location     M-U Undo        M-A Set Mark    M-] To Bracket  M-Q Previous
^X Exit         ^R Read File    ^\ Replace      ^U Paste        ^J Justify      ^/ Go To Line   M-E Redo        M-6 Copy        ^Q Where Was    M-W Next

サービスの再起動

$ sudo systemctl restart ssh

変更後の確認

ポート 22222 が開いてる

$ ss -anlt
State             Recv-Q             Send-Q                         Local Address:Port                          Peer Address:Port            Process            
LISTEN            0                  4096                              127.0.0.54:53                                 0.0.0.0:*                                  
LISTEN            0                  128                                  0.0.0.0:22222                              0.0.0.0:*                                  
LISTEN            0                  4096                           127.0.0.53%lo:53                                 0.0.0.0:*                                  
LISTEN            0                  128                                     [::]:22222                                 [::]:*           

さいごに

かんたんでしたね。

関連リンク

SSHd now uses socket-based activation (Ubuntu 22.10 and later)

2
0
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
2
0