How to get the IP address of a Linux system from the command line

One way to to get the IP address of a Linux system from the Linux command line is this:

$ hostname -I

That’s the hostname command, followed by a capital letter i as a command line parameter. On my Raspberry Pi system, this command returns its IP address — and only its IP address — like this:

10.0.1.9

It’s nice that this command returns only the IP address, because that means I don’t have to pipe together several commands to get what I need.

A Python script that uses hostname

I just used that command in a Python script, where I want to access the IP address of the local Linux system:

#!/usr/bin/python

from subprocess import check_output

ipAddr = check_output(["hostname", "-I"])
print 'IP Address: ' + ipAddr + "\n"

The script is actually much more complicated than that; I just wanted to show the parts relevant to getting the Linux IP address with the hostname command.

Add new comment

Anonymous format

  • Allowed HTML tags: <em> <strong> <cite> <code> <ul type> <ol start type> <li> <pre>
  • Lines and paragraphs break automatically.