Ingap.dev - A Personal Dev Blog

I would spend 55 minutes defining the problem and then five minutes solving it. (Albert Einstein)

Published on Thursday 28 November 2024

Tags: raspberry1 putty1 git1 linux2

How to setup Raspberry Pi Server: SSH Keys, PuTTY, and Git Integration

Setting up a secure headless Raspberry Pi server is becoming more and more intuitive. Here are the key steps and time-saving tricks you need to know. Learn proper SSH key configuration, PuTTY setup tricks, and seamless GitHub repository integration for efficient development.


Table of Content

  • Initial Setup
  • Auto-Login (Optional)
  • How to Find Your Pi from Windows (connected to same LAN)
  • PuTTY Setup
  • GitHub Setup
  • Remote Control via Telegram
  • Setup Steps
  • Automated Updates and Task Scheduling
  • Sample crontab.txt
  • Smart Power Management
  • After setting up multiple Raspberry Pi servers, I've refined my process to a reliable workflow using three main tools: SSH for secure access, PuTTY for easy connections, and Git for managing my code. I use SSH key pairs because I hate dealing with passwords. My Pi holds the public key (like a lock), and I keep the private key (the unique key) on my computer. PuTTY handles these connections for me, while Git takes care of all my code deployments. With this setup, I can:

    • Access my Pi from anywhere on my network
    • Push code changes through GitHub
    • Keep everything secure without password hassles
    • Automate most of my routine tasks

    The best part? Once I've got it running, I never need to plug in a keyboard or monitor - I manage everything remotely. It took me some trial and error to get here, but now my workflow is smooth and secure.

    Initial Setup

    1. Install and open Raspberry Pi Imager
    2. Select Raspberry Pi OS Lite (64-bit) - no desktop
    3. In the options:
      • Set hostname
      • Create username and password
      • Configure WiFi (if needed)
      • Enable SSH with public-key authentication
    4. Write to SD card
    5. Insert SD card into Pi and power on with keyboard and monitor (first time only)
    6. Wait 90 seconds for first boot

    Auto-Login (Optional)

    1. Run sudo raspi-config
    2. System Options → Boot / Auto Login → Console Autologin
    3. Reboot when prompted

    How to Find Your Pi from Windows (connected to same LAN)

    1. Download and run Angry IP Scanner
    2. Scan your network range (e.g., 192.168.1.1-255)
    3. Look for hostname you set
    4. Note the IP address

    PuTTY Setup

    Install PuTTYgen and PuTTY to connect to your Pi through SSH.

    1. Open PuTTYgen
    2. Click Load (select "All Files (.)")
    3. Navigate to C:\Users\YourUsername\.ssh\id_rsa
    4. Save private key as .ppk file
    5. Open PuTTY:
      • Enter Pi's IP or hostname
      • Port: 22
      • Connection → SSH → Auth → Credentials: Browse to previously stored .ppk
      • Save session for future use
    6. Click on Open to connect

    GitHub Setup

    1. Create a new private repository
    2. Go to GitHub → Settings → Developer Settings → Personal Access Tokens → Fine-grained tokens
    3. Generate new token:
      • Read-only repository access
      • Select your repository only
    4. On Pi:
    sudo apt update
    sudo apt install git
    git config --global credential.helper store
    mkdir -p ~/app
    cd ~/app
    git clone https://github.com/your-username/your-repo.git .

    Use token as password when prompted

    Remote Control via Telegram

    Using Telegram's infrastructure provides a solution for remotely controlling or easily getting updates from your Raspberry Pi without exposing it to the internet. Instead of dealing with port forwarding or paying for a static IP, your Pi establishes a secure outbound connection to Telegram's servers. This means you can monitor and control your Pi from anywhere while keeping it safely behind your home network's firewall.

    Setup Steps

    1. Create Telegram Bot:

      • Message @BotFather on Telegram
      • Use /newbot command
      • Save the API token
    2. Install Requirements:

    If you want to use virtual environments:

    # Create a virtual environment
    python -m venv myenv
    
    # Activate the virtual environment
    source myenv/bin/activate

    Note: Create a virtual environment if you plan to work on multiple Python projects that use different versions of the same libraries, as this prevents dependency conflicts. However, if you're just running this script alone and want to save disk space, you can simply install the requests library globally since it's the only dependency needed for the Telegram API.

    pip install requests

    Anyway, the requests module should be installed by default.

    Automated Updates and Task Scheduling

    Through crontab, we can make our Pi self-managing. That is very powerful.

    By adding a periodic git pull command to crontab, the Pi automatically syncs with our GitHub repository every hour (or at any interval we choose).

    Even better, by keeping our crontab configuration in a crontab.txt file within the repository and using crontab crontab.txt command, we can version control our scheduled tasks. This means adding a new scheduled script is as simple as pushing it to GitHub and updating the crontab.txt file - the Pi will pull both the new script and its scheduling instructions automatically.

    Sample crontab.txt

    # Ensure destination log folder exists: mkdir -p /home/pi/log
    
    # Morning system status via Telegram
    0 6 * * *  /usr/bin/python3 /home/pi/app/morning_status.py > /home/pi/log/morning_status.log 2>&1
    
    # Git pull every hour
    0 * * * * cd /home/pi/app && git pull > /home/pi/log/git-pull.log 2>&1
    # Copy the content of crontab.txt to the crontab every hour
    0 * * * * crontab /home/pi/app/crontab.txt > /home/pi/log/crontab-txt.log 2>&1
    
    # Shutdown at Midnight every day (smart plug will turn it off and on again)
    0 0 * * * sudo shutdown -h now
    
    # Leave a final new line

    This crontab configuration:

    • Sends morning system status via Telegram at 6 AM
    • Pulls repository updates every hour
    • Self-updates crontab from repository version
    • Safely shuts down before power cut
    • Logs all operations for debugging

    Note: you should run crontab /home/pi/app/crontab.txt in the terminal the first time!

    Smart Power Management

    Adding a smart plug to the Pi's power supply creates an extra layer of automation. By scheduling the smart plug to cut power during known inactive hours (like midnight to 6 AM) and restore it during working hours, we can save energy and reduce wear on the SD card. Just make sure to add a shutdown script to crontab five minutes before power-off:

    55 23 * * * sudo shutdown -h now