Issues changing default ssh port number

Hi All,

I set up a ubuntu 22.04 lts vm. I'm trying to change the default port from 22 to XXXX. I've uncommented it from /etc.ssh/sshd_config and change the port. Then updated the FW rules.

But the port is still showing as 22. Any ideas? I've rebooted and started ssh service also.
Latest Kernel and latest version of openssh is installed

4 Replies

I just attempted to replicate this, so please bear with me while I walk through my process.

First, I deployed a brand new Ubuntu 22.04 LTS server, and the first thing I did once I connected was updating via: apt update && apt upgrade -y. This took a little while and alerted me to some "reboot required" upgrades but none related to SSH/SSHD so I moved on.

I modified my SSH Config located /etc/ssh/sshd_config file to specifically use port 2222, and after saving/closing out of the file, I restarted the SSH daemon systemctl restart sshd. I made sure to setup some basic UFW firewall rules:

  • sudo ufw default allow outgoing
  • sudo ufw default deny incoming
  • sudo ufw allow 22 (in case the custom SSH port failed)
  • sudo ufw allow 2222 (my test custom port)

After making these changes I updated my UFW rules using the command: yes | sudo ufw enable. I exit'd out of my instance and then rebooted through Cloud Mananager, and then was able to successfully connect to my server using the command: ssh root@$IP_ADDRESS -p 2222.


When you attempt to connect using your custom/specified port what error message do you receive?

One great way to troubleshoot SSH issues is to use the "Verbose" option flag: ssh root@$IP_ADDRESS -p $PORT -v. You can increase the verbosity by increasing this to -vv and -vvv to get increasingly granularr details about what stage of the connection is failing.

I usually have to sudo reboot before it takes effect.. make sure it's listed in the firewall you use (UFW is a good one) Being a VM might make a difference but i'd try it.

I've blown away that VM since. I'm spinning up a new one now so will test again.

I tired all of the above steps while using UFW so not sure why it wouldn't work. Error is standard - unable to connect over port xxxx

Thanks for the replies

Does your ssh daemon bind on port 2222 at all? Since I've the same issues and I found ssh daemon doesn't bind on port different than 22, even I've modified sshd_config and explicitly said the the port will be different.

lsof -i :2222

what is the output?

Reply

Please enter an answer
Tips:

You can mention users to notify them: @username

You can use Markdown to format your question. For more examples see the Markdown Cheatsheet.

> I’m a blockquote.

I’m a blockquote.

[I'm a link] (https://www.google.com)

I'm a link

**I am bold** I am bold

*I am italicized* I am italicized

Community Code of Conduct