12

How can I get the ip address of a server in linux?

I need to use the bash command in Linux to tell me the IP address.

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
vinyuri
  • 723
  • 2
  • 8
  • 11
  • 1
    A machine can have thousands of IP addresses; how would you pick among them? – sarnold Feb 21 '11 at 07:30
  • Possible duplicate of [Linux command to translate DomainName to IP](http://stackoverflow.com/questions/3963085/linux-command-to-translate-domainname-to-ip) – Chris_Rands Feb 22 '17 at 08:54

2 Answers2

20

If you are trying to get this information from BASH, you probably want to use nslookup. For example:

[michaelsafyan@codemage ~]$ nslookup redmine.org
Server:     8.8.8.8
Address:    8.8.8.8#53

Non-authoritative answer:
Name:   redmine.org
Address: 46.4.36.71

I should add that an IP address does NOT represent a computer, but rather a network interface. And a computer can have any number of network interfaces (and IP addresses). Also, a website or domain may have many machines (and consequently many more network interaces and IP addresses). When querying with nslookup you will get at least one IP address for the given domain name (assuming DNS is working and it doesn't fail for one reason or another), but it won't necessarily give you all the addresses.

Michael Aaron Safyan
  • 93,612
  • 16
  • 138
  • 200
4

If you are just after the IP for a script, the following is a lot cleaner:

dig +short stackoverflow.com

For example:

@felix:~% dig +short stackoverflow.com
198.252.206.16
Tigger
  • 8,980
  • 5
  • 36
  • 40
  • This really should be the answer. I wanted to get just the ip address back, and this works perfectly, thank you. – james-see Jul 25 '16 at 19:51