12

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.

  • 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
23

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 0.0.0.0:22              0.0.0.0:*               LISTEN      12586/sshd

That's a normal ssh running on port 22.

  • 1
    Just a note: I don't think you need sudo – piertoni Aug 6 at 16:31
  • In the first command you need sudo because usually the sshd config file is not world readable. In the second you need sudo to be able to resolve the Program name, otherwise you can only see the name for the processes runing under your own user. – Marcos Dione Aug 8 at 15:56
  • I agree with @piertoni, you don't need to run this command with "SUDO". – Amintabar Sep 23 at 11:55
8

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.

3

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.

3

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.

2

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:

PORT     STATE SERVICE VERSION
1422/tcp open  ssh      (protocol 2.0)
1
$ 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      0.0.0.0:22       0.0.0.0:*        LISTEN       2799/sshd
tcp6  0      0      :::22            :::*             LISTEN       2799/sshd

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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