I am interested in finding out which port number the SSH client used to send connect/authentication request to server (default on port 22).

How can I discover it?


For me client uses some arbitrary ports to connect

  • 1
    During the connection setup? From a log file? From the server side or the client side? – Jeff Schaller Sep 9 at 20:23
  • From the client-side on at the time of connection setup, is there a way I can find out which port did SSH client used? I can guess it might be 80, but how can I be sure? – Avi Mehenwal Sep 9 at 20:26
  • 1
    After connecting, you have $SSH_CLIENT which contains the client's IP, client's port, and the server's port. Do you imagine using a second terminal (or escaping out of the current ssh connection) to investigate the client port? – Jeff Schaller Sep 9 at 20:35
  • I am on ubuntu 18.04, unfortunately I do not see environment variable by name SSH_CLIENT. I grepped all environment variables for ssh env | grep ssh and the only environment variable I see is SSH_AUTH_SOCK – Avi Mehenwal Sep 9 at 20:40
  • 1
    My comment addressed the remote end of the SSH connection; I wasn't sure where you were "looking" for the port information -- whether at the client-side or server-side (and within or without of the ssh connection itself). From within the connection, after logging in, you'd have SSH_CLIENT. – Jeff Schaller Sep 9 at 20:48

If you want to know what port a particular ssh connection will use, you can look in /etc/ssh/ssh_config for any system/global hosts configured, and in ~/.ssh/config for any user/local hosts.

If you want to know what port will be used by default, you can use the command getent services ssh. The standard is to connect to port 22.

If you want to know what ports are actively in use, the command netstat --tcp --programs --numeric | grep ssh should show you.

  • 1
    Won't the client use a high-numbered port to connect to the server's port 22? – Jeff Schaller Sep 9 at 20:32
  • @wyrm solution worked superfine. Netstat is what I was looking for. I observed that for each ssh connection client uses some arbitrary port number (I may be wrong but I do not see any patter). Client port (for me) lies in range like 53924, 53930 etc. – Avi Mehenwal Sep 9 at 20:53

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.