Score:2

How to get the ip address without ifconfig or ip command in Ubuntu?

bh flag

Today I got a new Ubuntu server. I tried to get its IP address with the ifconfig or ip commands.

But I got a command not found error. I searched the errors on Google, and it looks like I need to install package to run these commands.

But I don't have sudo access. Is there any other way to get the IP address without these commands?

muru avatar
us flag
You can try some of the solutions in https://unix.stackexchange.com/q/365225/70524. Is `networkctl` available? Try `networkctl status`
user535733 avatar
cn flag
All supported releases of Ubuntu include the `ip` command, so it's confusing which "*new Ubuntu server*" you got. Can you shed some light on that?
Score:2
lu flag

You can try using Python. It is installed by default on most distributions. It is very easy to use.

$python
>>> import netifaces as ni
>>> ni.interfaces()
['lo', 'eth0','eth1','eth2']
>>> ni.ifaddresses('eth0')
{17: [{'addr': '02:93:07:be:da:d9', 'broadcast': 'ff:ff:ff:ff:ff:ff'}], 2: [{'addr': '10.0.0.76', 'netmask': '255.255.255.0', 'broadcast': '10.0.0.255'}], 10: [{'addr': 'fe80::93:7ff:febe:dad9%eth0', 'netmask': 'ffff:ffff:ffff:ffff::'}]}

In the above example, there are four interfaces lo, eth0, eth1, eth2. The ip address for eth0 is 10.0.0.76.

A Linux server can have more than one network interface. You can check out my post on my website here to get more info about this.

This Python script displays the mac address, ip address, netmask for each network interface.

Here's another way to get the IP address without using a python package:

import socket
import fcntl
import struct

def get_ip_address(ifname):
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    return socket.inet_ntoa(fcntl.ioctl(
        s.fileno(),
        0x8915,  # SIOCGIFADDR
        struct.pack('256s', ifname[:15])
    )[20:24])

get_ip_address('eth0')  # '192.168.0.110'

You can refer to this post to get more about how to use Python to get ip address.

NotTheDr01ds avatar
vn flag
Just a heads-up that an auto-check system here flagged your answer since you linked to a website that includes your username. While it is okay to do this, Stack Exchange rules do require that you [explicitly disclose in your answer](https://meta.stackexchange.com/help/promotion) that you are the author of (or have an affiliation with) the link. Please [edit](https://askubuntu.com/a/1443598/1165986) your post to include any affiliation with external links. Thanks!
NotTheDr01ds avatar
vn flag
On a related note, I'm guessing this *isn't* the reason that your post was downvoted (since the downvoter probably didn't notice that), but rather that (to me at least) it appears to be an over-engineered solution ;-).
howtouselinux avatar
lu flag
Any idea what I should add to the answer? .. Thanks.
NotTheDr01ds avatar
vn flag
What you added is fine. I tweaked it slightly just to help it flow better. Thanks!
Score:1
vn flag

If you are simply a user on an Ubuntu server, and the administrator has disabled your access to certain tools, it may be difficult to determine which tools you have available. The ip command is typically available to end-users, but perhaps there's a path issue (unlikely):

/usr/bin/ip addr show
# or
/bin/ip addr show
# or (could be restricted)
/sbin/ip addr show

Alternatively, you can try using curl to access an external service that will detect your public IP. There are quite a few out there - I just went with the first search result I found:

curl ifconfig.me/all
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.