7

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?

2
  • You can try some of the solutions in unix.stackexchange.com/q/365225/70524. Is networkctl available? Try networkctl status
    – muru
    Commented Dec 2, 2022 at 11:41
  • 7
    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?
    – user535733
    Commented Dec 2, 2022 at 11:44

4 Answers 4

5

If you're lucky (and hostnames are set up properly), hostname -i will provide the local IP address. If not, or if you have multiple IP addresses, it will probably return the localhost address 127.0.0.1. The command hostname -I will report all machine-local IP (v4 and v6) addresses.

2

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.

0
1

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
0

Here's the solution based on @howtouselinux's answer that you can directly copy and paste.

python3 <<HEREDOC
import netifaces as ni
import pprint
interfaces = ni.interfaces()
print("\n", "Interfaces:", interfaces)
for iface in interfaces:
    ifconfig = ni.ifaddresses(iface)
    pprint.pprint(ifconfig)
HEREDOC

Output

Interfaces: ['lo', 'enp0s1']
{2: [{'addr': '127.0.0.1', 'netmask': '255.0.0.0', 'peer': '127.0.0.1'}],
 10: [{'addr': '::1',
       'netmask': 'ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128'}],
 17: [{'addr': '00:00:00:00:00:00', 'peer': '00:00:00:00:00:00'}]}
{2: [{'addr': '192.168.1.215',
      'broadcast': '192.168.1.255',
      'netmask': '255.255.255.0'}],
 10: [{'addr': 'fe80::b:d8ff:fe61:58a%enp0s1',
       'netmask': 'ffff:ffff:ffff:ffff::/64'}],
 17: [{'addr': '02:0b:d8:65:0b:8a', 'broadcast': 'ff:ff:ff:ff:ff:ff'}]}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .