Unix command to find IP address from hostname - Linux example (original) (raw)
The IP address from hostname in UNIX
In this Unix command tutorial, we will see how to find the IP address of any host in a UNIX based system, like Linux, Solaris, or IBM AIX. In general hostname and IP address are two important things about any host in a UNIX based network. You always need either a hostname or IP address to connect to any host. Sometimes you want to find the IP address of the localhost, sometimes the IP address of another host on the network, etc. We have shown some techniques to find the IP address from hostname in UNIX and here we will see is a particular list of UNIX commands to get the IP address of local host or host on which you are working along with any other host for which we know hostname.
By the way, if you are a beginner in Linux or haven't worked in a Linux host for a long time then I also recommend you to join these hands-on Linux courses to learn some essential Linux commands like this one to become more productive and confident Linux user.
List of UNIX command to find the IP address from hostname
Here is a list of UNIX commands which can be used to find the IP address :
- ifconfig
- nslookup
- hostname
1. ifconfig command example
ifconfig command shows a lot of information about various NIC cards in the system, you can check either for "inet" or "inet addr" for seeing IP address to respective NIC cards :
# /usr/sbin/ifconfig -a
inet 192.52.32.15 netmask ffffff00 broadcast 192.52.32.255
As I said ifconfig command shows a lot of details but I have only included relevant inet one if you have more than one network card attached to that host, you may see more than one inet the address corresponding to each of them.
The main drawback of the ifconfig command in UNIX is that you can only get the IP address of localhost from this command, you can not get the IP address of any other host. . You can further see Learn Linux in 5 Days and Level Up Your Career course on Udemy to learn more options of ifconfig command and other essential Linux commands.
2. grep and hostname example
You can combine grep command and hostname to look at IP address from /etc/hosts file. here`hostname` will return the output of the hostname command and great will then search for that word in /etc/hostname. the benefit of using `hostname` is that you can just copy and run this command on any host without typing hostname by yourself.
# grep `hostname` /etc/hosts
192.52.32.15 nyk4035 nyk4035.unix.com
3. ping command example
Our plain old ping command can also be used to find the IP address of the localhost or any other host provided you know the hostname. ping is used to check whether the target server is alive or not, but while using option -s it also displays the IP address of the corresponding host as well.
# ping -s `hostname.`
PING nyk4035: 56 data bytes
64 bytes from nyk4035 (192.52.32.15):icmp_seq=0.time=0.186ms
Ping is actually one of the 10 useful networking commands in UNIX, see that link for other commands. If you want to improve your Linux skills then I highly recommend you to learn those essential networking commands in Linux.
4. nslookup command example
nslookup is one of the primary UNIX commands to find the IP address from the hostname and again from hostname to IP address. Similar to ping you can also, use the nslookup command to find the IP address of Both localhost and remote host in any UNIX-based system. nslookup is generally available in most UNIX-based systems, like Linux, Solaris, IBM AIX, or any other UNIX system.
# nslookup `hostname`
nyk4035.unix.com canonical name = nyk4035.unix.com
Name: nyk4035.unix.com
Address: 192.52.32.15
That's all on How to find the IP address in UNIX, Linux, and other UNIX-based systems, like Solaris, IBM AIX, BSD, etc. We have seen examples of getting the IP addresses from the hostname by using ipconfig, ping, hostname, and nslookup commands. If one command is not working in your machine, you can always use another command or you can simply cross-check the IP address by using multiple UNIX commands.
Other UNIX command tutorial you may like
- 10 chmod command example in UNIX and Linux
- How to find the size of a directory in Linux with example
- How to create an archive using tar command in UNIX
- How to create sot link in UNIX and Linux operating system
- How to send an email from a Linux host?
- How to set up cron jobs in Linux using the Crontab command?
- My favorite Linux course for Beginners
- 10 examples of lsof command in Linux?
- 10 Example of cURL commands in Linux
- How to kill a process using kill command in UNIX
- Quick guide of file and directory permissions in UNIX and Linux
- How does the nslookup command work in Linux?
Thanks a lot for reading this article so far. If you like these Linux IP address hostname command examples then please share them with your friends and colleagues. If you have any questions or comments then please drop a comment.
P. S. - If you are a new Linux user and looking for some free online training courses to start your Linux journey, you should check out my list of Free Bash Scripting Courses for Programmers, IT Professionals, and System Administrators.