63

I am often on one computer in my house and I would like to SSH to another one, but often don't know the IP address of the one I want to connect to. Is there a way, from the command line, to scan the local network so I can find the computer I want to connect to?

4
  • > but often don't know the IP address of the one I want to connect to Isn't this what DNS was invented for? Apr 5, 2012 at 7:37
  • 1
    If you don't know which computers are connected to your house's network, I think you might have a problem...
    – Massimo
    Apr 5, 2012 at 8:31
  • 1
    ...and how do you know you're sshing into the right one? Time to sort out your ip addresses / name lookups.
    – symcbean
    Apr 5, 2012 at 9:00
  • 4
    In defense of Andrew: yes, it's desirable to set unchanging IPs in the DHCP lease, and to have local names. However, consider the real-world case where I just carried a headless Ubuntu PC into the office and hooked it up. For the first connection, I wanted to find the IP without carrying a keyboard and monitor over to it. To symcbean's question, it was easy to know the correct PC based on the MAC address decoding (automatically done by nmap/Zenmap) to the motherboard manufacturer, and the operating system used. Sometimes you don't know the IP and need to find it.
    – Phrogz
    Nov 12, 2015 at 18:27

6 Answers 6

53

Use "nmap" - this will tell you which hosts are up on a network, and indeed which have port 22 open. You could combine it with a few other tools (like grep) to produce more targeted output if need be.

Note: do this only on YOUR network. Running up nmap or its equivalents on someone else's network is considered bad form.

sudo nmap -p 22 192.168.0.0/24
3
  • bluebitter.de/portscn2.htm Use BluePortScan if you want a more simple thing than nmap
    – Gk.
    Apr 5, 2012 at 7:16
  • 9
    This accepted answer doesn't even have a simple example line
    – hmedia1
    Oct 20, 2018 at 10:32
  • 3
    added an example invocation
    – dmourati
    Oct 9, 2020 at 2:26
107

From the command line you could use:

sudo nmap -sS -p 22 192.168.10.0/24

Substitute for the local address space on your network. I sometimes use this when I plug in a headless rasberry pi and want to find where to ssh to.

2
  • 7
    Exactly one of the use-cases that lead me to ask this question. Thanks!
    – Andrew
    Nov 14, 2015 at 20:27
  • +1 Also using for raspberry pi but: sudo nmap -sS -p 22 192.168.10.0/24
    – Gtx
    Feb 14, 2016 at 18:21
50
nmap -p 22 --open -sV 192.168.178.0/24
4
  • How is this different than the other answers? How do we know that is my local network?
    – chicks
    Feb 16, 2018 at 22:13
  • 1
    It does not require sudo and can be used with Android NetworkMapper Jul 17, 2018 at 15:05
  • 8
    I prefer this answer. The addition of --open removed a lot of crud from the output and actually showed me the machine I was looking for. Oct 27, 2018 at 6:11
  • 1
    adding -oG - cleans up the output even more
    – user512619
    Sep 10, 2020 at 11:56
4

You can manually telnet each ip at port 22.

If successful you should see the OpenSSH version string.

The process of checking each ip in the subnet can be done by means of the 'for' directive.

1
  • could you provide an example?
    – Isaac
    Apr 4 at 4:27
1

Rather than the -oG to file approach (suggested by sitaram). Suppress errors from the output. (Sorry, I cannot comment on answer by Motsel.)

nmap -p 22 --open -sV 192.168.178.0/24 2> /dev/null

1

If you just want the hostnames/ips and don't want the other info:

sudo nmap -sS -p 22 192.168.1.0/24 | grep report

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.