- How to find a computer name in a LAN from the IP address?
- 7 Answers 7
- How to Use the hostname Command in Linux
- hostname Command Syntax
- hostname Command Examples
- Display Hostname
- Display Short Hostname
- Display Alias
- Search for Hostnames
- Force Default Hostname
- Display Domain Name
- Display FQDN
- Display All FQDNs
- Display NIS Domain
- Change NIS Domain Name
- Display Related Network Addresses
- Display All Network Addresses
- Change Hostname Until Reboot
- Change Hostname Permanently
- Additional hostname Options
- How to get the computer name (not its hostname)?
- 5 Answers 5
How to find a computer name in a LAN from the IP address?
My network includes machines running Linux and others running Windows. And my machine is running Linux.
The question is a bit unclear, which resulted in the varying — and good — answers below. Do you want to find the DNS name of a host? Do you want to find the WINS name? Do you want to search the network for all hosts found? All of these questions are answered — but the specific answer desired is unknown.
7 Answers 7
You can find your LAN subnet using ip addr command.
It will show all host name in LAN whether it is Linux or Windows. You also able to see mobile devices, if any present on LAN network. Here you need to make sure that you run command with sudo or root.
On Ubuntu 14.04 this says: «? (192.168.1.2) at 00:22:6b:f2:33:b3 [ether] on wlan0», giving me only info about my router (my laptop is connected to it via wifi, like about other 10 devices atm). Looking at man arp it says also «arp — manipulate the system ARP cache». How should this tool solve the original question?
Same here on Ubuntu 14.04, this just lists a bunch of ? (192.168.55.147) at ac:3a:7a:a4:01:d4 [ether] on eth0
Same here. I can only access the localhost by hostname, nothing else seems to advertise it’s hostname, even linux machines I have set up myself.
sudo aptitude install nmap nmap -sP xxx.xxx.xxx.xxx
Starting Nmap 5.21 ( http://nmap.org ) at 2012-11-03 19:08 CET Nmap scan report for HOST.DOMAIN (xxx.xxx.xxx.xxx) Host is up (0.00052s latency). MAC Address: YY:YY:YY:YY:YY:YY (Manufactor) Nmap done: 1 IP address (1 host up) scanned in 0.11 seconds
Where HOST.DOMAIN is the DNS-name of the machine.
This shows all active IPs, but it only shows domains for machines which explicitly have custom domain configured in my local Bind server. It doesn’t show any local hostnames, which I think is what the OP is trying to accomplish.
-sP may have been changed to -sn depending on your version of nmap. From the nmap 7.12 manual: -sn (No port scan) In previous releases of Nmap, -sn was known as -sP.
Similar to the arp command, I believe this answer only works if you have a local DHCP server and DNS server/proxy that communicate with one another, so that the hostnames recorded with the DHCP leases can be resolved (or reverse-resolved, in this case) via DNS.
A Netbios name reverse lookup might accomplish what you want more than than «hostname» which is a function of DNS and tcp/ip. nmblookup with the -A parameter returns device names as well as mac address. Try something like this:
You can run it in windows
nbtstat -A xxx.xxx.xxx.xxx (where x is the ip address)
on ubuntu you can install nbtscan. You can find more information here : http://www.unixwiz.net/tools/nbtscan.html
Thanks very much, but it seems that nbtscan detects machines running Windows only. I’ve 2 machines running Windows and another 2 running Linux. It detects the two running Windows only.
Sorry, I’ve read the tutorial but couldn’t figure out how to write the command that solves my problem. I can’t see options taking IP as input. Can you write the command for me please?
try nmblookup -A xxx.xxx.x.x where x is the ip address of the machine. So I would do nmblookup -A 192.168.1.6 and it would return the name of my machine. You will need to do that for each machine.
@Brett: (+1) you should convert your comment into an answer, as its the only method that worked for me (both windows and linux boxes). Thanks
it will show me the host name (usually the computer name)
nslookup does DNS searches, so for this to work you need the hosts to be registered in the DNS. If you have a regular Windows network, in a private IP range, without ActiveDirectory configured, it will not work. NetBios names, and DNS names are different beasts.
If your network is running a DHCP server, usually on the modem/router, chances are it will have a way for you to see the DHCP assigned addresses — often by a web page, and that often lists the computer names beside the allocated ip addresses.
To the best of my knowledge, there is no utility available to resolve an IP address into a NetBIOS name. I’d imagine that some sort of a network scoping program or a penetration testing program might be able to give you that sore of information though. However, all of the computers would have to be on the same domain and subnet.
I know this is old. and OP is using Linux, not Windows. but on Windows you can run nbtstat -A
How to Use the hostname Command in Linux
The Linux hostname command is used to view or change a system’s domain and hostname. It can also check a computer’s IP address.
In this tutorial, we will cover all the ways you can use the hostname command on Linux and how to change your computer’s hostname on a Linux system.
hostname Command Syntax
The hostname command uses the following format:
hostname [options] [new_hostname]
Use the [options] parameter to add more specific instructions to the hostname command. Without it, the default output shows your computer’s hostname:
Use the [new_hostname] parameter when you want to change your computer’s hostname.
hostname Command Examples
The hostname command has a number of options you can use for more specific outputs:
- -a , —alias : Displays the alias name of the host.
- -A , —all-fqdns : Displays every FQDN (Fully Qualified Domain Name) of the computer.
- -b , —boot : Always set a hostname.
- -d , —domain : Display DNS domain name.
- -f , —fqdn , —long : Display the FQDN.
- -F , —file : Check a file to recover and display the hostname.
- -h , —help : Print the help message as the output.
- -i , —ip-address : Display the computer’s IP address.
- -I , —all-ip-addresses : Display all of the computer’s network addresses.
- -s , —short : Display the short version of the hostname.
- -v , —verbose : Expand all output to verbose.
- -y , —yp , —nis : Display the NIS domain name.
Display Hostname
Using the hostname command without any additional options displays the computer’s hostname:
Display Short Hostname
Use the -s or —short option to display the short version of the hostname, which is cut off at the first dot:
hostname -s hostname --short
Display Alias
Use the -a or —alias option to display the alias (substitute hostname) of the host if one is set. There are very few cases where this option is useful, and its use is no longer recommended.
hostname -a hostname --alias
Search for Hostnames
Use the -F or —file option to search a specific file (such as hostname or hosts) and change your computer’s hostname to match the content of the file:
sudo hostname -F [file path] sudo hostname --file [file path]
Note: When using the -F or —file options, also specify the path to the file you want to check.
Force Default Hostname
If you don’t want to have a specific file containing the hostname, or want to leave that file empty, use the -b or —boot option. This makes your computer use the default hostname (localhost) until you set a different one:
Display Domain Name
Use the -d or —domain option to display the name of your DNS domain:
hostname -d hostname –domain
Display FQDN
If you want to display a system’s FQDN, use the -f , —fqdn , or —long option:
hostname -f hostname --fqdn hostname --long
A FQDN contains the short hostname and the DNS domain name.
Display All FQDNs
To display each FQDN of your machine, use the -A or —all-fqdns option:
hostname -A hostname --all-fqdns
This option lists out all of your network addresses and their DNS domain names. It skips any addresses that it can’t translate.
Note: Different network addresses may translate to same DNS domain names. This can result in duplicate output entries when using -A or —all-fqdns options.
Display NIS Domain
If you’re using NIS (Network Information Service), you can check your NIS domain name with the -y , —yp , or —nis option:
hostname -y hostname --yp hostname --nis
Change NIS Domain Name
Use this command with the following format to change your NIS domain name:
sudo hostname -y [NIS hostname] sudo hostname --yp [NIS hostname] sudo hostname --nis [NIS hostname]
Display Related Network Addresses
The hostname command also lets your display network addresses tied to a hostname. To do this, use the -i or —ip-address option:
hostname -i hostname --ip-address
This command only works if the hostname can be resolved.
Display All Network Addresses
Use the -I or —all-ip-addresses option to display all of the host’s network addresses. Unlike -i , this option doesn’t depend on hostname resolution:
hostname -I hostname --all-ip-addresses
Change Hostname Until Reboot
You can change your computer’s hostname with the hostname command. To do this, use the following format:
sudo hostname [new hostname]
Any changes to the hostname you make using this command will only last until the next reboot.
Change Hostname Permanently
To change the hostname permanently, use a text editor like Nano to make changes to the hostname and hosts files:
sudo nano /etc/hostname sudo nano /etc/hosts
You can also use the hostnamectl command to permanently change the hostname:
sudo hostnamectl set-hostname [new hostname]
Additional hostname Options
Use the -V or —version option to print out the version of the hostname software package on your Linux system:
hostname -V hostname --version
If you need help with any of the commands, use the -h or —help option to print out a help message:
After following this guide, you now know how to use the hostname command in Linux and all the options that can extend its use.
For more Linux commands, check out our Linux Commands Cheat Sheet.
How to get the computer name (not its hostname)?
All the results of my searches end up having something to do with hostname or uname -n . I looked up the manual for both, looking for sneaky options, but no luck. I am trying to find an equivalent of OSX’s scutil —get ComputerName on Linux systems. On Mac OS X, the computer name is used as a human-readable identifier for the computer; it’s shown in various management screens (e.g. on inventory management, Bonjour-based remote access, . ) and serves as the default hostname (after filtering to handle spaces etc.).
I might be wrong, but defining the name of the computer as its network name means that it doesn’t have a fixed name, right? When you install a Linux or OSX on a machine, you usually choose a name for that computer (which is the default network name I assume). For example my laptop might be named «FooBar» but when I connect to a network at work I get a hostname such as «machine42.work.localnetwork».
@Sh3ljohn what purpose does computername is osx serve? I don’t think there’s any equivalent in linux system.
Did you give some name other than hostname when installing that computer? Try searching for that name in /etc: grep -ri ‘name’ /etc
@Bibek_G I would like to use this to identify the machine on which I am running from a software of mine. UUID is insufficient because there might be several OS’s installed on the same disk which in turn run on the same machine.
5 Answers 5
The closest equivalent to a human-readable (and human-chosen) name for any computer running Linux is the default hostname stored in /etc/hostname . On some (not all) Linux distributions, this name is entered during installation as the computee’s name (but with network hostname constraints, unlike macOS’s computer name). This can be namespaced, i.e. each UTS namespace can have a different hostname.
Systems running systemd distinguish three different hostnames, including a “pretty” human-readable name which is supposed to be descriptive in a similar fashion to macOS’s computer name; this can be set and retrieved using hostnamectl ’s —pretty option. The other two hostnames are the static hostname, which is the default hostname described above, and the transient hostname which reflects the current network configuration.
Systemd also supports a chassis type (e.g. “tablet”) and an icon for the host; see systemd-hostnamed.service .
But this file is often used to set hostname and uname -n , and @Sh3ljohn explicitly wanted to avoid the output of those two commands.