How to Find and Use the Public IP of Your Raspberry Pi

The Raspberry Pi is a versatile device for hosting web servers, IoT projects, or remote desktops. To access your Raspberry Pi over the internet, you’ll need its public IP address. This guide walks you through finding your public IP address, understanding its role, and securely using it for remote access.


What is a Public IP Address?

A public IP address is a unique identifier assigned to your network by your Internet Service Provider (ISP). It allows devices outside your local network to communicate with your Raspberry Pi or other devices on your network.


Why You Need a Public IP Address for Raspberry Pi

  • Remote Access: Connect to your Raspberry Pi from anywhere in the world.
  • Host Web Servers: Use your Raspberry Pi to run web servers accessible over the internet.
  • IoT Applications: Control smart devices or monitor sensors remotely.

How to Find the Public IP of Your Raspberry Pi

Method 1: Using a Browser

  1. Open a browser on any device connected to the same network as your Raspberry Pi.
  2. Visit websites like WhatIsMyIP or IPChicken.
  3. Your public IP address will be displayed on the screen.

Method 2: Using Terminal Commands on Raspberry Pi

  1. Open the terminal on your Raspberry Pi.
  2. Use the following command to query an external service:
    curl ifconfig.me
  3. The output will display your public IP address.

Alternatively, use:

wget -qO- https://ipecho.net/plain

Method 3: Check Your Router Settings

  1. Log in to your router’s admin interface.
    • Enter your router’s IP address (usually 192.168.1.1 or 192.168.0.1) into a browser.
    • Use the admin credentials to log in.
  2. Navigate to the Status or WAN section to find your public IP address.

Dynamic IP vs. Static IP

Dynamic IP

Most ISPs assign dynamic IP addresses that change periodically. This can complicate remote access as the IP address may change frequently.

Static IP

A static IP address remains constant. You can request one from your ISP, though it may involve additional costs.


Using Dynamic DNS (DDNS) for Raspberry Pi

To handle changing public IP addresses, use Dynamic DNS (DDNS) to assign a fixed domain name to your Raspberry Pi.

Step 1: Register for a DDNS Service

Popular DDNS providers include:

Step 2: Install DDNS Client on Raspberry Pi

  1. Install the DDNS update client. For example, with No-IP:
    sudo apt install ddclient
  2. Configure the client with your DDNS provider’s credentials and domain name.

Step 3: Update Router Settings

Some routers support built-in DDNS configurations. Log in to your router and configure DDNS under the Dynamic DNS section.


Securely Accessing Raspberry Pi with a Public IP

Step 1: Configure Port Forwarding

To access services hosted on your Raspberry Pi, set up port forwarding on your router.

  1. Log in to your router’s admin interface.
  2. Navigate to the Port Forwarding section.
  3. Forward a port (e.g., 22 for SSH) to your Raspberry Pi’s local IP address.

Example: Forward external port 2222 to internal port 22 on your Raspberry Pi.


Step 2: Secure Your Connection

  1. Change Default SSH Port:
    • Edit the SSH configuration file:
      sudo nano /etc/ssh/sshd_config
    • Replace Port 22 with a custom port (e.g., 2222). Restart SSH:
      sudo systemctl restart ssh
  2. Enable Firewall (UFW):
    • Install and configure UFW:
      sudo apt install ufw
      sudo ufw allow 2222/tcp
      sudo ufw enable
  3. Use SSH Key Authentication:
    • Generate SSH keys on your local device:
      ssh-keygen -t rsa
    • Copy the public key to your Raspberry Pi:
      ssh-copy-id -p 2222 pi@<Public_IP>

Step 3: Test Remote Access

  1. From an external network, test the connection:
    ssh -p 2222 pi@<Public_IP>
  2. Replace <Public_IP> with your actual public IP address or DDNS hostname.

Troubleshooting Tips

  • Can’t Connect to Public IP:
    • Ensure your router’s firewall isn’t blocking incoming connections.
    • Verify port forwarding settings.
  • Dynamic IP Changes Frequently:
    • Use a DDNS service to maintain a consistent hostname.
  • SSH Fails After Changing Ports:
    • Ensure the custom port is open in your Raspberry Pi and router firewall settings.

FAQs

How do I find my Raspberry Pi’s public IP address?
You can use the command curl ifconfig.me on the Raspberry Pi terminal or check it via your router or online tools like WhatIsMyIP.

Can I access my Raspberry Pi without a public IP?
Yes, you can use services like ZeroTier, Tailscale, or Ngrok to establish secure remote connections without a public IP.

What is the difference between public and private IP?
A public IP is assigned by your ISP for external access, while a private IP is used within your local network.

Is it safe to expose my Raspberry Pi’s public IP?
It can be risky without proper security measures like firewalls, custom ports, and SSH key authentication.

What port should I use for secure SSH access?
Avoid using the default port 22. Use a custom port, such as 2222, to reduce exposure to potential attacks.


Conclusion

Knowing your Raspberry Pi public IP and configuring it for remote access unlocks a world of possibilities for IoT projects, web servers, and more. By pairing your public IP with secure practices like SSH key authentication and Dynamic DNS, you can manage your Raspberry Pi from anywhere while keeping your connection safe.