Linux ip lookup

Linux ip lookup DEFAULT

How can I resolve a hostname to an IP address in a Bash script?

The solutions given so far mostly work in the simpler case: the hostname directly resolves to a single IPv4 address. This might be the only case where you need to resolve hostnames, but if not, below is a discussion on some cases that you might need to handle.

Chris Down and Heinzi briefly discussed the case where the hostname resolves to more than one IP addresses. In this case (and others below), basic scripting under the assumption that a hostname directly resolves to a single IP address may break. Below, an example with a hostname resolving to more than a single IP address:

But what is ? This is where the alias case needs to be introduced. Let's check the example below:

So does not directly resolve to IP addresses, but to an alias that itself resolves to multiple IP addresses. For more information on aliases, check here. Of course, the case where an alias has a single IP address is possible, as shown below:

But can aliases be chained? The answer is yes:

I did not find any example where a hostname resolves to an alias that does not resolve to an IP address, but I think the case might occur.

More than multiple IP addresses and aliases, is there some other special cases... what about IPv6? You could try:

Where the hostname is an IPv6-only hostname. What about dual-stack hostnames:

Again about IPv6, if your host is IPv4 only, you can still resolve IPv6 addresses (tested on a IPv4 only WinXP and with, you could try it on Linux). In this case, the resolution succeeds, but a ping fails with an unknown host error message. This might be a case where your scripting fails.

I hope those remarks were useful.



An IP address is a codename assigned to a computer on a network. It works as a postal address, ensuring that network traffic is sent to the correct computer.

In this tutorial you will learn how to find your IP address using a Linux operating system.

Tutorial on how to find your IP address on Linux.


  • A Linux operating system
  • Access to a terminal window/command-line (optional) (CtrlAltT, CtrlAltF2)

Find Your IP Address From the Command Line

There are a few different commands you can use to check your IP address. To run these commands, start by opening a terminal window/command line.

Using the hostname Command

One way to check your IP address from the command-line is by using the following command:

The system will display your internal IP address.

get ip address in linux terminal

Using the ip addr Command

Check your ip address with the command:

The system will scan your hardware, and display the status for each network adapter you have. Look for an entry that says link/ether. Below it, you should see one of the following:

check ip address with ip addr command in linux

The entries will include one for a wired (Ethernet) adapter and a wireless (Wi-Fi) adapter. You may also have an entry for a virtual adapter. Generally, only one entry will have an IP address listed – that is the one you will want.

Note: The number after the slash – /24 and /64 – specifies the size of the network, and it helps with scanning and mapping network size.

Using the ifconfig Command

The third method to find your IP address involves using the ifconfig command. In the command line, enter the following:

The system will display all network connections – including connected, disconnected, and virtual. Look for the one labeled UP, BROADCAST, RUNNING, MULTICAST to find your IP address. This lists both IPv4 and IPv6 addresses.

Note: When you check your IP address, you may notice the term loopback. This refers to an IP address that returns traffic to the same computer. Usually, the loopback address is . If you see that address listed, either you are looking at the wrong line, or you are not connected to the network.

Read phoenixNAP’s ultimate Linux IP command tutorial with 31 examples to learn more!

Finding Your IP Address in Linux With a GUI

If you are using a point-and-click interface to run your Linux system, you can check your IP address by following these steps:

1. Go to the Application menu and type Settings into the search bar.

2. Click on the Settings icon that appears among the results, as in the image below:

search for settings screenshot in ubuntu

3. Next, find the Network tab in the Settings Menu and click on the Advanced Wired Settings icon.

network settings for finding ip address

4. This opens a new pop-up window with details on wired settings. Here you can find both your IPv4 address and your IPv6 address.

check ip address in wired details

How to Find Public IP Address

To find your public IP address, reach out to an external website.

If you are using a browser, navigate to:


If you are using a command-line or terminal, use a retrieval command like or wget to display the external IP address:

example of looking up the public ip

Note: Did you know that when you use curl to connect to an insecure website, the output responds with an error? To resolve it, visit our guide on making curl ignore certificates.

What is a Public/Private IP Address?

Each computer in a network is required to have its own IP address. If two systems have the same IP address, it will generate errors on the network. Most modern networks will detect the problem and prompt you to fix it. However, older networks might get confused, trying to route traffic to both computers.

Most systems will have two different IP addresses:

  • Private IP address: This is the address used on the network. Also, it is the one you will want to use for configuring routing tools. On smaller networks, the internal IP address typically starts with It is dynamically assigned to your computer whenever you connect.
  • Public IP address: This is the IP address that registers on a website when you visit it. When you connect to a small network, your computer connects to a router using an internal IP address. That router, in turn, connects to a bigger network (like an Internet Service Provider), which has its own IP address system.


Now you know how to find a private and public IP address in Linux using multiple methods.

Each option strikes a balance between speed and complexity. Now you have the flexibility to choose, if you only need your internal IP address, you can use the hostname method. If you need more detailed information, view our article on 31 Linux IP Commands with examples.

Was this article helpful?


  1. 3 watts 909
  2. 5800x vs 3800xt
  3. Camaro snowflake wheels
  4. Craigslist dating ohio
  5. 2006 four winns

How to find your IP address in Linux

We all use the Internet Protocol (IP) daily, whether we're aware of it or not. For instance, any time you type a website name or a search term into your web browser, it looks up the IP address of that URL (or of a search engine) and then loads the website.

Let's divide IP addresses into two categories: private and public. Private IP addresses are the ones your WiFi box (and company intranet) provide. They are in the range of 10.x.x.x, 172.16.x.x-172.31.x.x, and 192.168.x.x, where x=0 to 255. Public IP addresses, as the name suggests, are "public" and you can reach them from anywhere in the world. Every website has a unique IP address that can be reached by anyone and from anywhere; that is considered a public IP address.

Furthermore, there are two types of IP addresses: IPv4 and IPv6.

IPv4 addresses have the format x.x.x.x, where x=0 to 255. There are 2^32 (approximately 4 billion) possible IPv4 addresses.

IPv6 addresses have a more complex format using hex numbers. The total number of bits is 128, which means there are 2^128—340 undecillion!—possible IPv6 addresses. IPv6 was introduced to tackle the foreseeable exhaustion of IPv4 addresses in the near future.

As a network engineer, I recommend not sharing your machine’s public IP address with anyone. Your WiFi router has a public IP, which is the WAN (wide-area network) IP address, and it will be the same for any device connected to that WiFi. All the devices connected to the same WiFi have private IP addresses locally identified by the range provided above. For example, my laptop is connected with the IP address, and my phone is connected with These are private IP addresses, but both would have the same public IP address.

The following commands will get you the IP address list to find public IP addresses for your machine:

    The following commands will get you the private IP address of your interfaces:

      Note: Some utilities need to be installed on your system based on the Linux distro you are using. Also, some of the noted commands use a third-party website to get the IP

      Finding your IP address in the GNOME desktop

      If you're using Linux, you can find your IP address using some basic desktop utilities. First, go to your Activities screen and type Network (for a wired connection) or Wi-Fi (for wireless).

      In the Networks settings window, click the Gear icon next to the network you're on.

      Your IP address is listed in the Network profile.

      In this example screenshot, my IPv4 address is and the gateway is

      Public and private IP addresses

      To understand the significance of public and private IP addresses, let's have a look at a concept called NAT (Network Address Translation) and PAT (Port Address Translation).

      Private addresses are used in Local Area Networks (LAN). They are bound to a specific network.

      Public addresses are necessary for establishing external connectivity to other networks, most notably the "Worldwide Web" (www) of the Internet.

      NAT is a translation of a private IP to a public one, and consists of three major types: static, dynamic, and PAT. In static NAT, one private IP is mapped to one public IP. A common example ru ydco is a firewall. In dynamic NAT, a private IP address is mapped to a public IP but from a pool of public IP addresses.

      With a total of 2^32 IPv4 addresses, out of which approximately just 3.7 billion are public addresses, there are literally more people and devices than there are IP addresses. And yet we haven't run out of addresses. That's because of a concept called PAT, which allows for a single public IP address to be translated from multiple (generally all) private IP addresses using port numbers. Here, instead of assigning a public address to each device, a single public address is allocated to the external side, and private addresses are assigned to each device within the internal network. PAT is most widely used in residential wireless routers, which we use to connect to the Internet.

      Private Ipv4 addresses

      • Local addresses are only used within an internal network.

      • The range is 10.x.x.x, 172.16.x.x-172.31.x.x, and 192.168.x.x, (x is a number from 0 to 255).

      • Used in Local Area Networks (LAN).

      • Not globally reachable, and cannot be searched globally.

      • Not unique: the same private IP can be used in two different networks.

      • Each router comes with a private IP address, which multiple devices can connect to. Users don’t need to pay extra to obtain a private address.

      • Some example private IP addresses:,,

      Public Ipv4 addresses

      • Public addresses are used to connect to external networks.

      • Ranges from to, except for private IP addresses and few unusable or reserved IPs.

      • Used in connecting to the Internet.

      • Globally reachable and can be searched. These are often used to determine geolocation.

      • Unique across all of the Internet.

      • A private address costs money because they are unique, limited, and accessible from the Internet. Each website has a domain name mapped to a public IP. For example, the public IP address for is

      Finding your way with IP

      An IP address is the most direct route to a computer over a network. There are other systems, such as DNS and Avahi, to help route one computer to another, but when those are unavailable or undesired for any reason, the IP protocol is what you use. Now you understand why, and more importantly, how to find your own.

      Editor's note: This article was originally published in May 2018 and has been updated.


      nslookup command

      nslookup (name server lookup) is a tool used to perform DNS lookups in Linux. It is used to display DNS details, such as the IP address of a particular computer, the MX records for a domain or the NS servers of a domain.

      nslookup can operate in two modes: interactive and non-interactive. The interactive mode allows you to query name servers for information about various hosts and domains or to print a list of hosts in a domain. The non-interactive mode allows you to print just the name and requested information for a host or domain.

      The interactive mode

      The interactive mode is entered by typing the nslookup command without any arguments:

      linux nslookup interactive mode

      To find the IP address of a host, simply type the hostname:

      linux nslookup hostname

      To perform a reverse DNS lookup, enter the IP address of a host:

      linux nslookup ip address

      To display MX records (the mail servers responsible for accepting email messages on behalf of a recipient’s domain), set the DNS query type to MX:

      linux nslookup mx

      To display NS records, set the DNS query type to NS:

      linux nslookup ns

      The non-interactive mode

      The non-interactive mode is invoked by typing the nslookup command, followed by the name or the IP address of the host to be looked up.

      For example, to display the IP address of a hostname, use the following command:

      linux nslookup command hostname

      To do a reverse DNS lookup, use the following command:

      linux nslookup reverse dns lookup

      To display the MX records, use the -query=mx option:

      linux nslookup command mx

      To display the NS records, use the -query=ns option:

      linux nslookup command ns

      To display the SOA record (information about the domain), use the -query=soa option:

      linux nslookup command soa

      To display all the available DNS records, use the -query=any option:

      linux nslookup command any

      The nslookup program is officially deprecated, meaning that it’s no longer being maintained. You should use host or dig instead.


      Lookup linux ip

      Unix command to find IP address from hostname - Linux example

      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 :

      1. ifconfig command example

      Let's see an example of the ifconfig command to find The IP address of localhost on which you are working :

      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 netmask ffffff00 broadcast

      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. 

      Unix command to find IP address from hostname - Linux example

      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     nyk4035

      grep and hostname command example for IP address

      3. ping command example

      How to find IP address from hostname in UNIXOur plain old pingcommand can also be used to find the IP address of the localhost or any other host provided you know the hostname. pingis used to check whether the target server is alive or not, but while using option -sit also displays the IP address of the corresponding host as well. 

      # ping -s `hostname.`
      PING nyk4035: 56 data bytes
      64 bytes from nyk4035 ( icmp_seq=0. time=0.186 ms

      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. 

      basic Linux networking commands for beginners

      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`       canonical name =

      If you want to learn more about the nslookup command other important Linux commands, I suggest you check these free online courses to learn Linux commands in-depth.

      how to find IP address from hostname in Linux

      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

      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.

      IP Command - Configure Network Interfaces on Linux
      Every system in a TCP/IP network is assigned a unique identifier known as IP address that helps to connect it with other system o the network and all over the internet. All the websites you access on the internet also have unique IP addresses. As it is difficult for everyone to remember the IP addresses of these websites, the DNS system comes which helps to translate these hard to remember IP addresses into human-readable names. With DNS, you no longer have to remember the IP addresses. Instead, you have to just have to remember the domain name and all done. Actually, on the backed, the DNS server takes the hostname and resolves it to an IP address which the browser or application then connects to.

      In this article, we will explain how to resolve a hostname/domain name to an IPv4 and IPv6 address in a Bash script. However, before proceeding towards creating the script, let us review some of the commands that can be used to resolve the hostname/domain name to an IP address.


      Ping is the most simple and built-in tool that is available on almost all operating systems. It is used to verify the reachability of a host in a network. However, we can also used it to find the IP address against any hostname/domain name. Use the following syntax to find the IP address of a targeted hostname/domain name:

      $ ping target-host


      Nslookup is widely used to resolve the hostname to an IP address. In order to use this command for an IP lookup, use the following syntax:

      $ nslookup target-host


      Another command-line utility “host” can be used to find IP address against any hostname/domain name. In order to use this command, use the following syntax:

      $ host target-host


      Dig is another useful command line tool that is used to query various DNS related records. It can be used to find IP address against any hostname/domain name. Use Dig command in the following way to find an IP address against a specific hostname/domain name.

      $ dig target-host +short

      Bash script to resolve a hostname to an IP address

      In order to use the bash script for an IP lookup, follow the below steps:

      1. Create a bash file using any text editor. Here I will be using the Nano editor to create a script named “”.

        $ sudonano

      1. Copy-paste the following lines in your script file. Note that, here in this script, I am specifying Google’s public DNS server for IP lookup. You can specify any other DNS server as per your environment.

        # Specify DNS server
        # function to get IP address
        function get_ipaddr {
            # A and AAA record for IPv4 and IPv6, respectively
            # $1 stands for first argument
          if[-n"$1"]; then
            if[-z"query_type"]; then
            # use host command for DNS lookup operations
            host -t${query_type}  ${hostname}&>/dev/null ${dnsserver}
            if["$?"-eq"0"]; then
              # get ip address
              ip_address="$(host -t ${query_type} ${hostname} ${dnsserver}| awk '/has.*address/{print $NF; exit}')"
        # display ip
        for query in"A-IPv4""AAAA-IPv6"; do
          query_type="$(printf $query | cut -d- -f 1)"
          ipversion="$(printf $query | cut -d- -f 2)"
          address="$(get_ipaddr ${hostname})"
          if["$?"-eq"0"]; then
            if[-n"${address}"]; then
            echo"The ${ipversion} adress of the Hostname ${hostname} is: $address"
            echo"An error occurred"

      2. Once done, use Ctrl+O and Ctrl+X to save and exit the file respectively.
      3. Now to find an IP address against a targeted hostname/domain name, run the script using the following syntax:

        $ ./ target-host

        For instance, to resolve the IP address of “”, the command would be:

        $ ./

        The output would be similar to this:

        Similarly, to resolve the IP address of “”, the command would be:

        $ ./

        The output would be similar to this:

        That is all there is to it! In this article, we have learned to resolve the hostname to an IPv4 and IPv6 address using a bash script. We also learned some other command-line tools such as Ping, Nslookup, Host, and Dig that can be used to perform an IP lookup.


      Now discussing:

      Was inevitable, in view of the strict order of the abbess herself. "We need to think of something to weaken this damned supervision," Xavier often thought, puzzling over this difficult task. In the end, he nevertheless came up with and threw out the trick.

      5247 5248 5249 5250 5251