How to Find Your Ubuntu Server IP Address for SSH Connection Using Bash Commands


2 views

When you need to SSH into your Ubuntu machine, the first step is identifying its IP address. Here are the most reliable terminal commands:

# Display all network interfaces and their IPs
ip addr show

# Filter for IPv4 addresses only
hostname -I

# Alternative using ifconfig (deprecated but still common)
ifconfig | grep "inet " | grep -v 127.0.0.1

For headless servers or when you need to parse the output programmatically:

# Get just the primary IP address
ip -4 addr show eth0 | grep -oP '(?<=inet\s)\d+(\.\d+){3}'

# When you have multiple interfaces (e.g., eth0, wlan0)
ip route get 1 | awk '{print $7;exit}'

# For cloud instances that might use different interface names
ip -br -4 a | awk '{print $3}' | cut -d'/' -f1

Here's how to incorporate these commands into bash scripts:

#!/bin/bash

# Store IP in a variable for later use
SERVER_IP=$(hostname -I | awk '{print $1}')

# Example usage in an automated SSH script
if [ -z "$SERVER_IP" ]; then
    echo "Error: Could not determine IP address" >&2
    exit 1
else
    echo "Connecting to $SERVER_IP..."
    ssh user@$SERVER_IP
fi

If you're not seeing expected results:

# Check if network service is running
systemctl status systemd-networkd

# Verify network interface names have changed (common in Ubuntu 18.04+)
ls /sys/class/net/

# Test basic connectivity
ping -c 4 8.8.8.8

Sometimes the standard commands might not work in all environments:

# Using nmcli (NetworkManager)
nmcli -p device show | grep IP4.ADDRESS

# Using Python (when shell commands aren't available)
python3 -c "import socket; print([(s.connect(('8.8.8.8', 53)), s.getsockname()[0], s.close()) for s in [socket.socket(socket.AF_INET, socket.SOCK_DGRAM)]][0][1])"

The most straightforward way to get your IP address in Ubuntu is using the ip command:

ip a

This displays all network interfaces and their associated IP addresses. Look for entries starting with "inet" under your active network interface (usually eth0 for wired or wlan0 for wireless).

For a cleaner output showing only the IP address, you can use:

hostname -I | awk '{print $1}'

Or alternatively:

ip route get 1 | awk '{print $7}' | head -1

If you need the public IP for SSH access from outside your local network:

curl ifconfig.me

Or using a different service:

wget -qO- icanhazip.com

For regular SSH access, consider these approaches:

1. Assign a static IP in your router's DHCP settings

2. Set up Dynamic DNS if your public IP changes frequently

3. For local networks, add an entry to your /etc/hosts file:

192.168.1.100  ubuntu-box

Here's a useful bash script to display comprehensive network information:

#!/bin/bash
echo "=== Network Interfaces ==="
ip -br a
echo -e "\n=== Routing Table ==="
ip route
echo -e "\n=== Public IP ==="
curl -s ifconfig.me

Save this as network_info.sh, make it executable with chmod +x network_info.sh, and run it when needed.

1. When using ifconfig, remember it might not be installed by default on newer Ubuntu versions

2. For headless servers, the IP might be assigned via DHCP - check your router's admin interface if needed

3. Firewall rules might block SSH access even with the correct IP