Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save iambryancs/2a8ce7522d3762b6c4225803f0fdcafb to your computer and use it in GitHub Desktop.
Save iambryancs/2a8ce7522d3762b6c4225803f0fdcafb to your computer and use it in GitHub Desktop.
Update SSH Port on Ubuntu 22.10 and later

Intro

SSHd now uses socket-based activation Ubuntu 22.10 or later. Below are 2 options to change the default SSH port.

A. Revert to service-based

  1. Disable socket-based and enable service-based SSH
    systemctl disable --now ssh.socket
    systemctl enable --now ssh.service
    
  2. Update the Port in /etc/ssh/sshd_config
  3. Restart the SSH service
    sudo systemctl restart ssh.service
    

B. Socket-based

  1. Create ssh socket dir

    mkdir -p /etc/systemd/system/ssh.socket.d
    
  2. Create config

    cat >/etc/systemd/system/ssh.socket.d/listen.conf <<EOF
    [Socket]
    ListenStream=2022
    EOF
    

    This will make SSH listen on ports 2022 and 22. If you want SSH to only listen on port 2022, use the config below.

    cat >/etc/systemd/system/ssh.socket.d/listen.conf <<EOF
    [Socket]
    ListenStream=
    ListenStream=2022
    EOF
    
  3. sudo systemctl daemon-reload

  4. sudo systemctl restart ssh.socket

Refs:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment