0

So i am not sure if its the right place to ask because its my first time using this platform.

Just for security reasons, i wanted to change the port number of my ssh server runnin on Pi3 and then i came up with the following question:

If i choose a random port number would that be ok? Even if another program uses that port? If not then how can i check available/unused port on my machine?

  • i am not sure if its the right place to ask IMHO: no. This is a general Linux (or actually networking) question. Not a Pi specific issue. Maybe you should read the Tour first – Dirk Jan 25 at 19:23
  • netstat -t -l should list you all TCP ports currently in use. – Dmitry Grigoryev Jan 26 at 7:26
  • BTW scanning for active TCP ports on a remote host takes seconds, so it won't even slow down the attack in any considerable way. – Dmitry Grigoryev Jan 28 at 14:46
2

If i choose a random port number would that be ok?

Well, almost always OK

Even if another program uses that port?

This is when it won't be OK

If not then how can i check available/unused port on my machine?

netstat command - specifically

netstat -t -l -n

This lists -tcp ports (since ssh uses tcp protocol) that are -listening (since you want ssh to listen on a port) - showing the ports as -numbers (useful if don't you know which service uses which default port)

You'll get an output like

Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State
tcp        0      0 0.0.0.0:5269            0.0.0.0:*               LISTEN
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN
tcp        0      0 0.0.0.0:1883            0.0.0.0:*               LISTEN
tcp        0      0 127.0.0.1:445           0.0.0.0:*               LISTEN
tcp        0      0 192.168.1.19:445        0.0.0.0:*               LISTEN
tcp        0      0 0.0.0.0:5222            0.0.0.0:*               LISTEN
tcp        0      0 0.0.0.0:8200            0.0.0.0:*               LISTEN
tcp        0      0 127.0.0.1:139           0.0.0.0:*               LISTEN
tcp        0      0 192.168.1.19:139        0.0.0.0:*               LISTEN
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN
tcp6       0      0 :::5269                 :::*                    LISTEN
tcp6       0      0 :::22                   :::*                    LISTEN
tcp6       0      0 ::1:25                  :::*                    LISTEN
tcp6       0      0 :::1883                 :::*                    LISTEN
tcp6       0      0 :::5222                 :::*                    LISTEN
tcp6       0      0 :::111                  :::*                    LISTEN
tcp6       0      0 :::80                   :::*                    LISTEN

The number after the : in the Local Address column is the ports you can't use - because they're already in use

Alternatively

netstat -t -l -n | awk '{ print $4 }' | grep ':' | sed -e 's/.*://' | sort -n -u

Will give you a list of port numbers to avoid

As mentioned in another answer, there are also other ports you should avoid - a list of common ports can be found in this wikipedia page - also you'll find similar information in /etc/services on your pi.

That's not to say you can't use one of those ports for any purpose you want, of course. For example port 524 is "NetWare" - I'm guessing 50% of people reading this wouldn't even know what that is :p

2

While the accepted answer will enable you to avoid ports currently in use on your system, you should ensure you do not use any of the common ports, especially the Official IANA port allocations. See https://en.wikipedia.org/wiki/List_of_TCP_and_UDP_port_numbers.

If you are going to use a non-standard port it is normal to use a port >1024.

PS using a non-standard port provides little security. At best it would slow down a dedicated hacker. It is common for all ports <1024 to be scanned.

  • yes, I should've added that – Jaromanda X Jan 26 at 9:29

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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