- How can I get the IP address of a remote Unix/Linux machine whose name I know?
- 5 Answers 5
- Use linux Dig command to find the full ip address range of a domain?
- 2 Answers 2
- How to Find the IP Address of a Website in Linux
- Commands to find IP address of a website in Linux
- Method 1: Get website IP address with dig command
- Method 3: Get IP address of website using host command
- Method 4: Get website’s IP address with ping command in Linux
- Conclusion
- Get ip address from hostname in C with Linux sockets
- 1. gethostbyname
- 2. getaddrinfo
- 17 Comments
How can I get the IP address of a remote Unix/Linux machine whose name I know?
I have a Unix/Linux machine aster-star-98 in our company but it’s on a different network. How can I get the IP address of that machine? If I could enter the complete address of the machine and ping it I might get the IP address, but I don’t know the complete address, only the simple name aster-star-98 . I’m sure that it’s running.
5 Answers 5
Query the local name-service. For example:
nslookup aster-star-98.example.com host aster-star-98.example.com dig aster-star-98.example.com
telnet or ssh to aster-star-98.example.com then
If you don’t know the fully qualified domain name and have no way to open a session then you’ll have to visit the computer or enlist the help of someone near the computer.
If you run ping aster-star-98 , you should see the real IP.
Check the mac address of the network adapter then ping the broadcast address of the subnet (from within said subnet). broadcast address on /24 is .255, then run arp -a from the windows machine and find the mac address.
1: lo: mtu 16436 qdisc noqueue state UNKNOWN link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: eth0: mtu 1500 qdisc pfifo_fast state DOWN qlen 1000 link/ether b8:88:e3:95:eb:c0 brd ff:ff:ff:ff:ff:ff 3: wlan0: mtu 1500 qdisc mq state UP qlen 1000 link/ether 20:68:9d:f1:66:88 brd ff:ff:ff:ff:ff:ff inet ***192.168.12.182***/24 brd 192.168.12.255 scope global wlan0 inet6 fe80::2268:9dff:fef1:6688/64 scope link valid_lft forever preferred_lft forever
Use linux Dig command to find the full ip address range of a domain?
and gives me the following data. First, what does this mean. I have all of google IP addresses by the way. But not sure about amazonses.com. I also thing the IP addresses is giving me is very limited.
"v=spf1 ip4:69.53.224.0/19 ip4:165.193.233.164/30 ip4:205.139.44.20 ip4:66.150.112.120 ip4:205.139.45.20 ip4:209.177.164.2 ip4:54.84.21.177 ip4:54.85.33.189 include:_spf.google.com include:amazonses.com -all"
Instead of relying to domain name system which is not meant to be nothing but a phone book translating IP addresses to human readable names, you should be looking at various WHOIS databases of various IP service providers.
2 Answers 2
Short answer: You can’t do what it sounds like you want.
Longer answer: Getting all IP addresses for a certain website is simply a question of issuing an A and an AAAA DNS query for its name. That will give you all the published addresses for that site. But, and I’m guessing here, it sounds like that’s not what you want. If what you want is to find out which IP ranges are assigned to Netflix the corporation, you can find that by looking it up in the various RIR databases (easiest via whois, as Sami says in a comment up there). Most of those addresses probably aren’t being used for their web servers (but for mail, VPN, internal communications and such). Also, it’s likely that a lot of their web presence aren’t on those IP addresses, but on addresses belonging to some CDN.
You need to ask a better question if you want to get a useful answer.
That big string is Netflix’ SPF record. It’s an email thing and tells the world what servers it should expect netflix.com email from.
Finding all of a website’s IP addresses can be a pretty tough thing in the general case. One answer that’s often enough is to just trust whatever the DNS server is giving you as the A record for that domain at the time:
% dig netflix.com netflix.com. 74 IN A 69.53.236.17
Some websites will have several records, and will let you know those up front:
% dig google.com google.com. 205 IN A 173.194.33.103 google.com. 205 IN A 173.194.33.110 google.com. 205 IN A 173.194.33.96 google.com. 205 IN A 173.194.33.105 google.com. 205 IN A 173.194.33.100 google.com. 205 IN A 173.194.33.97 google.com. 205 IN A 173.194.33.99 google.com. 205 IN A 173.194.33.102 google.com. 205 IN A 173.194.33.98 google.com. 205 IN A 173.194.33.104 google.com. 205 IN A 173.194.33.101
Depending on the site, things can get tricky in a hurry. Many sites, especially larger ones, will give you a different set of records at different times (or for each time you ask):
% dig indeed.com indeed.com. 19 IN A 50.97.195.27 % dig indeed.com indeed.com. 30 IN A 50.97.35.152
And some will give you a different address depending on which part of the world you’re in. Like wordpress.com from India:
Seattle WA, United States: 192.0.78.9, 192.0.78.17 Montreal QC, Canada: 192.0.78.9, 192.0.78.17 Paris, France: 192.0.78.9, 192.0.78.17 St. Petersburg, Russia: 192.0.78.9, 192.0.78.17 Beijing, China: 192.0.78.9, 192.0.78.17 Mumbai, India: 203.90.66.98
Some sites will even mix and match those types of responses.
IPv6 throws a wrench into this, as does HTTP redirection.
Probably the thing to start with when considering which answer is best for you: what are you trying to accomplish with this information?
How to Find the IP Address of a Website in Linux
Wondering how to get the IP address of a website in Linux? Here are 3 command line utilities to query the DNS and get you the IP address and other details.
How do I find the IP address of a website using Linux terminal?
Finding the public IP address of a website’s server is quite easy. In fact, there are several command line tools you can use to find the IP address of a website.
In an earlier article, I showed you how to find IP address in Linux. In this one, I am going to show you some of those commands here.
Commands to find IP address of a website in Linux
Do note that you might have to install some of these utilities mentioned here. Good news is that they are available in the default repositories of most Linux distributions. You can easily install them using the package manager of your distribution.
Method 1: Get website IP address with dig command
Dig is a DNS lookup utility. The sole purpose of this command is to perform DNS lookup and display the answers returned by the nameserver of the queried website.
A sample output for the dig command looks like this:
dig facebook.com ; > DiG 9.11.3-1ubuntu1.5-Ubuntu > facebook.com ;; global options: +cmd ;; Got answer: ;; ->>HEADER
You can see the IP address of the website in the ‘ANSWER SECTION’.
Facebook has tons of servers and you may see a different IP address based on your geo-location and time. You shouldn’t find it unusual.
Method 2: Use nslookup command to find IP address of website in Linux
Like Dig, nslookup command is also used for querying the DNS records. nslookup stands for ‘name server lookup’.
You might need to install nslookup. If you have sudo access, you can use your Linux distribution’s installation command to install this tool. On Debian and Ubuntu, the command below will install nslookup:
sudo apt install nslookup
Once you have the tool, just use it in the following fashion:
For example, if I try to get the IP address of Facebook.com, this is the output I get:
nslookup facebook.com Server: 127.0.0.53 Address: 127.0.0.53#53 Non-authoritative answer: Name: facebook.com Address: 157.240.25.35 Name: facebook.com Address: 2a03:2880:f10c:83:face:b00c:0:25de
Method 3: Get IP address of website using host command
Like the above two, host is also a DNS lookup utility. But unlike the above two commands, the output of the host command is neat and precise. It just displays the IPv4 and IPv6 addresses of a website.
Sample output would be like this:
host facebook.com facebook.com has address 157.240.13.35 facebook.com has IPv6 address 2a03:2880:f139:83:face:b00c:0:25de facebook.com mail is handled by 10 msgin.vvv.facebook.com.
Method 4: Get website’s IP address with ping command in Linux
Let’s see how to get the IP address of a website with the ping command in Linux.
Ping command is used to check whether the remote host is up or not. You can use either the server’s IP address or the URL. The syntax is pretty much the same.
A sample output for facebook.com looks like this:
ping facebook.com PING facebook.com (157.240.24.35) 56(84) bytes of data. 64 bytes from edge-star-mini-shv-01-sin2.facebook.com (157.240.24.35): icmp_seq=1 ttl=52 time=203 ms 64 bytes from edge-star-mini-shv-01-sin2.facebook.com (157.240.24.35): icmp_seq=2 ttl=52 time=163 ms 64 bytes from edge-star-mini-shv-01-sin2.facebook.com (157.240.24.35): icmp_seq=3 ttl=52 time=248 ms ^C --- facebook.com ping statistics --- 4 packets transmitted, 3 received, 25% packet loss, time 3001ms rtt min/avg/max/mdev = 163.965/205.339/248.902/34.713 ms
You’ll have to use Ctrl+C to stop the ping command.
Conclusion
So, you just saw three networking tools that would give you the same result. I hope this quick Linux tip helped you find a website's IP address in Linux terminal.
If you have any questions or suggestions, please leave a comment below. Don’t forget to subscribe to the newsletter and get all the new tutorials in your inbox, for free.
Get ip address from hostname in C with Linux sockets
Socket applications often need to convert hostnames like google.com to their corresponding ip address. This is done through dns requests.
The socket api in linux provides functions like gethostbyname and getaddrinfo that can be used to perform the dns requests and get the ip address.
1. gethostbyname
The first method uses the traditional gethostbyname function to retrieve information about a hostname/domain name.
#include //printf #include //memset #include //for exit(0); #include #include //For errno - the error number #include //hostent #include int hostname_to_ip(char * , char *); int main(int argc , char *argv[]) < if(argc <2) < printf("Please provide a hostname to resolve"); exit(1); >char *hostname = argv[1]; char ip[100]; hostname_to_ip(hostname , ip); printf("%s resolved to %s" , hostname , ip); printf("\n"); > /* Get ip from domain name */ int hostname_to_ip(char * hostname , char* ip) < struct hostent *he; struct in_addr **addr_list; int i; if ( (he = gethostbyname( hostname ) ) == NULL) < // get the host info herror("gethostbyname"); return 1; >addr_list = (struct in_addr **) he->h_addr_list; for(i = 0; addr_list[i] != NULL; i++) < //Return the first one; strcpy(ip , inet_ntoa(*addr_list[i]) ); return 0; >return 1; >
$ gcc hostname_to_ip.c && ./a.out www.google.com www.google.com resolved to 74.125.235.16 $ gcc hostname_to_ip.c && ./a.out www.msn.com www.msn.com resolved to 207.46.140.34 $ gcc hostname_to_ip.c && ./a.out www.yahoo.com www.yahoo.com resolved to 98.137.149.56
2. getaddrinfo
The second method uses the getaddrinfo function to retrieve information about a hostname/domain name. The getaddrinfo supports ipv6 better.
#include //printf #include //memset #include //for exit(0); #include #include //For errno - the error number #include //hostent #include int hostname_to_ip(char * , char *); int main(int argc , char *argv[]) < if(argc <2) < printf("Please provide a hostname to resolve"); exit(1); >char *hostname = argv[1]; char ip[100]; hostname_to_ip(hostname , ip); printf("%s resolved to %s" , hostname , ip); printf("\n"); > /* Get ip from domain name */ int hostname_to_ip(char *hostname , char *ip) < int sockfd; struct addrinfo hints, *servinfo, *p; struct sockaddr_in *h; int rv; memset(&hints, 0, sizeof hints); hints.ai_family = AF_UNSPEC; // use AF_INET6 to force IPv6 hints.ai_socktype = SOCK_STREAM; if ( (rv = getaddrinfo( hostname , "http" , &hints , &servinfo)) != 0) < fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv)); return 1; >// loop through all the results and connect to the first we can for(p = servinfo; p != NULL; p = p->ai_next) < h = (struct sockaddr_in *) p->ai_addr; strcpy(ip , inet_ntoa( h->sin_addr ) ); > freeaddrinfo(servinfo); // all done with this structure return 0; >
$ gcc hostname_to_ip.c && ./a.out www.google.com www.google.com resolved to 74.125.235.19 $ gcc hostname_to_ip.c && ./a.out www.yahoo.com www.yahoo.com resolved to 72.30.2.43 $ gcc hostname_to_ip.c && ./a.out www.msn.com www.msn.com resolved to 207.46.140.34
A Tech Enthusiast, Blogger, Linux Fan and a Software Developer. Writes about Computer hardware, Linux and Open Source software and coding in Python, Php and Javascript. He can be reached at [email protected] .
17 Comments
- thisisbazz August 17, 2014 at 6:06 am Actually, having learned a little more, I’ve reverted to the newer inet_ntop. Simpler to use, I’m unsure if any power is lost from using it. I think it will be alright. Just thought I would leave evidence of my trail.
- Silver MoonMarch 23, 2013 at 10:10 pm try changing the following line hints.ai_family = AF_UNSPEC; // use AF_INET6 to force IPv6 to this hints.ai_family = AF_INET;
- Yu Jing March 24, 2013 at 4:04 am Thank you, It works !
In the manual,
“The value AF_UNSPEC indicates that getaddrinfo() should return socket addresses for any address family (either IPv4 or IPv6, for example) that can be used with node and service.”
What is the basis of the judgments using IPv4 or IPv6? The server support ipv6 ? or just my pc support IPv6?
- Yu Jing March 26, 2013 at 8:08 am Thanks for your patient , while ,what makes me confuse is … I mean,
if the ai_family’s value is “AF_UNSPEC”, how the code judge using IPv4 or IPv6 ? For example , if only my pc support IPv6 , will the code trying return the IPv6 address ?
- Silver MoonMarch 6, 2013 at 9:09 pm check your internet connectivity by pinging the host first.
use the following command at terminal/console ping hostname if ping replies come correctly, then try the program again and let me know the results.