Skip to content

Instantly share code, notes, and snippets.

@felixrabe
Last active January 2, 2016 18:39
Show Gist options
  • Save felixrabe/1c7a0c43817d5c69bcab to your computer and use it in GitHub Desktop.
Save felixrabe/1c7a0c43817d5c69bcab to your computer and use it in GitHub Desktop.
Connect Raspberry Pi via Ubuntu 12.04 and NAT to the Internet
  • Host system: Ubuntu 12.04 LTS with DHCP server: (root)

    • Interfaces:

        eth0 => Raspberry Pi
        wlan0 => Internet (WLAN access point)
      
    • Installation:

        apt-get install isc-dhcp-server
      
    • Configuration: (/etc/dhcp/dhcpd.conf)

        default-lease-time 600;
        max-lease-time 7200;
      
        use-host-decl-names on;
      
        subnet 192.168.2.0 netmask 255.255.255.0 {
        }
      
        group {
          option routers 192.168.2.1;
          # range 192.168.2.2 192.168.2.6;
          host rpi {
            hardware ethernet b8:27:eb:XX:XX:XX;
            fixed-address 192.168.2.2;
            option domain-name-servers 192.168.1.1;
            # option domain-name-servers 123.45.67.89, 123.45.67.90;
          }
        }
      

      Change the hardware ethernet line to contain your device's MAC address.

  • Load 2014-09-09-wheezy-raspbian.zip on SD card:

      tail -F /var/log/syslog
      lsblk
      unzip 2014-09-09-wheezy-raspbian.zip
      sudo cp 2014-09-09-wheezy-raspbian.img /dev/mmcblk0
    
  • Monitoring: (root)

      tcpdump -nettti eth0
    
  • Configuration: (root)

      # keep this running in a separate terminal:
      while true ; do ifconfig eth0 192.168.2.1 ; sleep 2 ; done
      
      $EDITOR /etc/dhcp/dhcpd.conf  # change DNS servers
      service isc-dhcp-server start  # needs eth0 to be up
      echo 1 > /proc/sys/net/ipv4/ip_forward
    
      cat > /tmp/ipt <<-EOT
      *nat
      -A POSTROUTING -o "wlan0" -j MASQUERADE
      COMMIT
      *filter
      :INPUT ACCEPT [0:0]
      :FORWARD ACCEPT [0:0]
      :OUTPUT ACCEPT [0:0]
      -A FORWARD -i "wlan0" -o "eth0" -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT 
      -A FORWARD -i "eth0" -o "wlan0" -j ACCEPT
      -A FORWARD -j LOG
      COMMIT
      EOT
    
      iptables-restore < /tmp/ipt
    

    (Credits: ipt is based on https://help.ubuntu.com/community/Router.)

  • Power up Raspberry Pi

  • It should work:

      ping 192.168.2.2
      # $EDITOR ~/.ssh/known_hosts
      ssh -l pi 192.168.2.2
      # Logins:
      # Raspbian: pi / raspberry
      # Raspbian netinst: root / raspbian
      # Arch Linux: root / root
    
      # on Raspberry Pi:
      ping 192.168.1.1  # configured dns server
      ping 8.8.8.8
      ping lwn.net
      
      sudo systemctl poweroff  # systemd: Debian jessie+, Arch Linux
      sudo poweroff  # Debian wheezy
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment