Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have an Ubuntu 10.04 server setup remotely that I setup a while back. While I recorded the username and password, I seem to have been clever and changed the usual ssh port from 22 to... something else.

How do I find out what that port might be?

I do have access to the server via the hosting company's back door, so I can execute whatever Unix commands are needed - but I cannot log in using a normal putty shell on my machine.

share|improve this question
This is probably silly, and you have good answers already, but if you have connected in the past, you could check your terminal history and/or ~/.ssh/config. – Sparhawk Aug 8 '14 at 4:46

First check on the config file which port is configured:

$ sudo grep Port /etc/ssh/sshd_config
Port 22

Then either restart ssh to make sure it loads the config you just saw or find out on which port ssh is running:

$ sudo netstat -tpln | egrep '(Proto|ssh)'
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0    *               LISTEN      12586/sshd

That's a normal ssh running on port 22.

share|improve this answer

If you have access to the server through other means, just run:

$ sudo grep Port /etc/ssh/sshd_config 
Port NNN

That will return a line like the one shown above where NNN will be the port you chose.

share|improve this answer

The easier way is just looking at the SSH server configuration files:

➜  ~  sudo grep Port /etc/ssh/sshd_config 
   Port 22

There's also checking for listening ports per process with lsof:

sudo lsof -Pi | grep ssh

or any other ports listing command like netstat -lntu.

share|improve this answer

If you don't have access to the server's console, you'll have to probe all the ports from a distant host. These utilities are like nmap, however the network layer must relay traffic to the specific combination of host (IP address) AND port.

share|improve this answer

If you don't have login access to the server, you can find the SSH port using nmap's "version scan" feature:

nmap -sV -p- <insert target's IP here>

The -sV option means "version scan", and -p- means "scan all ports". If you've got a good connection and are sure you won't be upsetting someone's firewall or IDS, you can add -T5 for "scan as fast as possible". The results will look something like this:

1422/tcp open  ssh      (protocol 2.0)
share|improve this answer
$ sudo netstat -lntp
[sudo] password for XXX:
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address    Foreign Address  State        PID/Program name
tcp   0      0*        LISTEN       2799/sshd
tcp6  0      0      :::22            :::*             LISTEN       2799/sshd
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.