Saturday, October 12, 2024

How to set Raspberry Pi 5 Headless Mode

Setting up a Raspberry Pi can be a fun and rewarding experience, but it often requires a monitor, keyboard, and mouse. For those who prefer the command line or lack these peripherals, setting up your Raspberry Pi headlessly – without a visual interface – is a viable and convenient option. This guide will walk you through the process, enabling you to remotely access and configure your Raspberry Pi using only your computer and an Ethernet cable.

What You’ll Need:

  • Raspberry Pi 5 Model B (or similar model with an Ethernet port)

  • Power Supply (for your Raspberry Pi)

  • Micro SD Card (≥ 8GB recommended)

  • Card Reader (that supports Micro SD cards)

  • Computer or Laptop (with Ubuntu operating system and an Ethernet port)

  • Ethernet Cable (with RJ45 connector)

  • Internet Access (Recommended for remote access and updates)

Step 1: Installing and Opening Raspberry Pi Imager

Raspberry Pi Imager is a user-friendly tool that simplifies the process of flashing the operating system onto your SD card. It's available for various operating systems, including Ubuntu.

  • Option 1 - Latest Version: Download the latest Raspberry Pi Imager from the official Raspberry Pi website or use the following command in your Ubuntu terminal:

    wget https://downloads.raspberrypi.org/imager/imager_latest_amd64.deb
    sudo apt install ./imager_latest_amd64.deb
    rm -rf imager_latest_amd64.deb
        

  • Option 2 - Specific Version: If you prefer a specific version, navigate to the official Raspberry Pi GitHub repository and download the desired version. Then, use the following command, replacing <num> with the desired version number:

    wget https://github.com/raspberrypi/rpi-imager/releases/download/v<num>/rpi-imager_<num>_amd64.deb
    sudo apt install ./rpi-imager_<num>_amd64.deb
    rm -rf rpi-imager_<num>_amd64.deb
        

Once installed, open Raspberry Pi Imager from your Ubuntu applications menu.

Step 2: Configuring Raspberry Pi Imager

Insert your Micro SD card into the card reader and connect it to your computer. Now, let's configure Raspberry Pi Imager:

  1. Choose Your Raspberry Pi: Select the appropriate device based on your Raspberry Pi model from the list of supported devices.

  2. Select Operating System: For headless setups, choose "Raspberry Pi OS Lite (64-bit)". This version provides a minimal environment without a graphical interface.

  3. Select Storage Device: Select your Micro SD card as the storage device for writing the operating system.

  4. Click "Next" and Choose "Edit Settings": This opens the customization settings for your Raspberry Pi OS.

  5. Configure Hostname, Username, and Password: Enable the "Set hostname" option and optionally set a custom hostname. You can leave the default "raspberrypi" if desired. Enable the "Set username and password" option and provide a desired username and password. For instance, use "pi" for both the username and password.

  6. Enable SSH: Navigate to the "Services" menu and check the "Enable SSH" option. This enables remote access to your Raspberry Pi via Secure Shell (SSH).

  7. Start Writing: Click "YES" twice to confirm your settings, and Raspberry Pi Imager will begin writing the operating system to your Micro SD card.

  8. Wait for Completion: Allow the process to finish. Once complete, safely eject the Micro SD card from your computer.

Step 3: Connecting to Your Raspberry Pi via SSH over Ethernet

  1. Power Up and Connect: Power on your Raspberry Pi and connect it to your laptop using an Ethernet cable.

  2. Configure Network Settings: Go to your Ubuntu network settings, select "Wired Connection," and open the "Wired Settings."

    • Click the gear icon next to your current Wired profile.

    • Remove the existing profile by clicking "Remove Connection Profile."

    • Click the "+" icon to create a new connection profile, naming it "Raspberry Pi" or something similar.

    • Navigate to the "IPv4" menu, select "Shared to other computers" under "IPv4 Method." This enables your laptop to act as a DHCP server, assigning an IP address to your Raspberry Pi. Click "Add" to save the profile.

  3. Connect via SSH: Open your terminal and use the following command, replacing <username> and <hostname> with the values you configured in Raspberry Pi Imager. In our example, this would be:

          ssh pi@raspberrypi.local
        

    • Type "yes" and press Enter to add the Raspberry Pi's key fingerprint to your known hosts list.

    • Enter your password and press Enter. You should now be connected to your Raspberry Pi via SSH over Ethernet.

Step 4: Connecting to Your Raspberry Pi via SSH over Wi-Fi

While you can access your Raspberry Pi via Ethernet, internet connectivity is required for updates and various other tasks. Here's how to connect your Raspberry Pi to Wi-Fi:

  1. Open NetworkManager Text User Interface: Open your terminal and execute the following command:

          sudo nmtui
        
  2. Select Wi-Fi Network: Use the arrow keys to navigate and select "Activate a connection." Choose your desired Wi-Fi network and press Enter to enter the password.

  3. Enter Password: Type your Wi-Fi password and press Enter. The Raspberry Pi should now connect to your Wi-Fi network.

  4. Verify Internet Connectivity: Return to your terminal and try pinging Google's DNS server (8.8.8.8):

          ping 8.8.8.8
        

    If you receive ping replies, your Raspberry Pi is successfully connected to the internet.

Troubleshooting Connectivity Issues:

If your Raspberry Pi cannot ping 8.8.8.8, it might be due to routing issues. You can check the current routing table using the ip route command. You might see two default gateways: one via your Ethernet connection (eth0) and another via your Wi-Fi connection (wlan0).

If the Ethernet connection has a lower metric value, it will take priority, potentially preventing the Raspberry Pi from using the Wi-Fi connection for internet access.

Finding the Wi-Fi IP Address:

To connect via SSH over Wi-Fi, you'll need your Raspberry Pi's IP address on the Wi-Fi network. Run the following command to get the IP addresses:

      hostname -I
    

You'll see two IP addresses: the one assigned by your laptop (Ethernet) and the one assigned by your router (Wi-Fi). Remember the Wi-Fi IP address.

Switching to Wi-Fi and Connecting via SSH:

  1. Log Out and Disconnect Ethernet: In your SSH session, run logout to disconnect from your Raspberry Pi. Unplug the Ethernet cable.

  2. Connect via SSH over Wi-Fi: Now, use the following command, replacing <ip-address> with the Wi-Fi IP address you previously noted:

          ssh pi@<ip-address>
        

    If you encounter a "Warning: Remote Host Identification has Changed" message, run the following command to remove the old key fingerprint:

          ssh-keygen -R <ip-address>
        

    Try connecting again using the same ssh command. You should now be connected to your Raspberry Pi via SSH over Wi-Fi.

Updating and Upgrading Your Raspberry Pi:

With internet connectivity established, you can update and upgrade your Raspberry Pi's software packages using the following command:

      sudo apt update && sudo apt upgrade
    

This process ensures you have the latest software versions for a smoother and more secure experience.

Congratulations! You have successfully set up your Raspberry Pi headlessly, allowing you to access and control it remotely from your computer. This opens a world of possibilities, enabling you to run scripts, deploy servers, and experiment with countless Raspberry Pi projects without needing a monitor, keyboard, or mouse.

0 comments:

Post a Comment