1

using VPS and Ubuntu (latest version). When I changed ssh port by going into sudo nano /etc/ssh/sshd_config it did not work. When checked via sudo lsof -i -n -P | grep LISTEN, it shows it's still listening to port 22. Then when I tried to login again, console showed SSH connection lost.

Then found this thread: SSH default port not changing (Ubuntu 22.10) and I put the commands as:

mkdir -p /etc/systemd/system/ssh.socket.d

cat >/etc/systemd/system/ssh.socket.d/listen.conf <<EOF

[Socket]

ListenStream=

ListenStream=46876

EOF

sudo systemctl daemon-reload

sudo systemctl restart ssh

When checking systemctl status ssh, it shows failed. Why there is ListenStream two times?

Been at this for too long. Can someone please help me with the commands to change the port?

Thank you.

Kenyo
  • 11

2 Answers2

1

Seems like you are trying to only restart ssh client and not the server after you make the changes. try this to restart the ssh server and new port configured in sshd config should take effect:

sudo systemctl restart sshd

Also editing systemd directory is for when you want the custom port stay the same even after reboot. Once you reboot, your ssh server resets to 22 until you restart it again after boot up, but modifying that directory will prevent this.

Another key point is that you are using listen.conf that I think should be changed to port.conf, please try below:

mkdir /lib/systemd/system/ssh.socket.d
sudo nano /lib/systemd/system/ssh.socket.d/port.conf

Paste the following and save the file (replace 54747 with custom port):

[Socket]
ListenStream=
ListenStream=54747

Reboot the server after making changes or:

sudo systemctl daemon-reload
sudo systemctl restart ssh
sudo systemctl restart sshd
MRK
  • 11
-2

the sshd config file is located at /etc/ssh/sshd_config

just change the port and restart sshd by systemctl restart sshd