Dec 27, 2019 - Raspberry PI - Get started

Raspberry PI - Get started

Default tasks when setting up a new Raspberry PI.

Get Raspbian

Download from raspberrypi.org

Transfer it onto the SD card

Plug in the SD card and run fdisk -l to find out which device it is. If it is mounted, unmount it umount /dev/sdc1 I assume for now it is /dev/sdc. You have to use yours, make sure to get this right!!

Then write the image to the SD card:

dd if=2019-09-26-raspbian-buster-full.img of=/dev/sdc bs=512k

Setup Wifi and SSH before the first boot

Most often you don’t want to connect a keyboard and monitor. You just want plug it in the power socket somewhere and get going. Therefore you need to setup Wifi and enable the SSH server before you boot it up.

Simply remove the SD card and put it back in again. Then the /boot partition of the card should get mounted.

Run wpa_passphrase YOUR_SSID and note the output. Then create the file wpa_supplicant.conf in the boot parition and adjust it accordingly:

ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
country="Two letter country code, e.g. GB"

network={
    ssid="Your SSID"
    psk="Your PASSWORD"
}

Enable the SSH server by creating an empty file ssh in the boot partition.

Umount the SD card partitions again, put the card into the Raspberry Pi and start it up.

Change the password

Run nmap -sn 192.168.1.0/24 to find out the IP address of the Pi. Then log in with the default password which is ‘raspberry’: ssh pi@192.168.1.xxx

And change it: passwd

Update system

sudo apt update
sudo apt upgrade

Install X2Go (optional)

If you also want to use the desktop, install the X2Go server.

Create the file /etc/apt/sources.list.d/x2go.list:

# X2Go Repository (release builds)
deb http://packages.x2go.org/raspbian buster main
# X2Go Repository (sources of release builds)
deb-src http://packages.x2go.org/raspbian buster main

Add the key:

sudo apt-key adv --recv-keys --keyserver keys.gnupg.net E1F958385BFE2B6E

Install:

sudo apt update && sudo apt install x2goserver x2goserver-xsession

Start x2goserver and enable it at boot time:

sudo systemctl start x2goserver
sudo systemctl enable x2goserver

Install the client on your PC accordingly, the package is called x2goclient. Then you can remotely log into the desktop running on the Raspberry Pi. The default Raspbian desktop image comes with the ‘LXDE’ desktop. You can skip the ‘Setup’ tasks which pop up on the first desktop login, you’ve already done them.

Oct 24, 2019 - A simple netcat 'pong' service

A simple netcat ‘pong’ service

Given you have a machine outside your home LAN, you can create a very simple ‘pong’ service to get your external IP address. For example if you have to update a dynamic DNS entry for a server which is running inside your LAN.

I just call it ‘pong’, opposed to the wellknown ‘ping’ tool.

It simply uses netcat (you might have to install it first) to listen (-l) on a given port (-p) and dump its verbose (-v) output into a file (this will contain the IP address of the incoming request). Then extract the IP address and return it. Then wait for the next request.

On the external machine

Create the file /usr/local/bin/pong.sh

#!/bin/bash

port=12345

_term() {
  # Catch the interrupt signal to enable
  # a graceful exit
  fuser -k $port/tcp
  exit 0
}

trap _term INT

while true
do
  file=/tmp/$RANDOM.ip
  nc -lvp $port -c "grep connect $file | cut -d'[' -f 3 | cut -d']' -f 1" 2> $file
  rm $file
done

Create a systemd service /etc/systemd/system/pong.service

[Unit]
Description=Pong Service
After=network.target

[Service]
User=nobody
WorkingDirectory=/tmp
ExecStart=/usr/local/bin/pong.sh

[Install]
WantedBy=multi-user.target

Start it:

systemctl start pong

Respectively enable it at boot time:

systemctl enable pong

On your local machine:

Get your external IP address by running

nc [SERVER IP] 12345

Sep 25, 2019 - Virtualbox VM with two network cards

Virtualbox VM with two network cards

A common use case for a virtual machine is that you want to run a server application on your machine locally; because of privacy/security reasons or just for testing purposes.

In this scenario it makes sense to set up the VM with two network cards. One adapter ‘attached to’ the virtual NAT and the other one as ‘host-only adapter’ connected to a virtual net.

The VM will access the internet through the virtual NAT and at the same time will be accessible from the host at a specified address.

  • Create a virtual network in Virtualbox’s ‘Host network manager’, e.g. vboxnet0 with IP range 192.168.1.1/24.

  • In the virtual machine: Set the primary network adapter to NAT, or if you want the machine to directly talk to your network’s router to ‘Bridged’.

  • Add a second network adapter; set to host-only on vboxnet0

Start up the VM.

  • In the VM: Get the name of the second network adapter with ‘ip a’. This is probably something like enp0s8.

  • Setup the network card:

On debian based systems edit /etc/network/interfaces and add:

allow-hotplug enp0s8
	iface enp0s8 inet static           
	address 192.168.1.2/24

(for a statically assigned IP address; alternatively use DHCP if you like)

On redhat based system edit /etc/sysconfig/network-scripts/ifcfg-enp0s8 and add:

ONBOOT=yes
IPADDR=192.168.1.2
NETMASK=255.255.255.0

Reload the network

systemctl restart network

Check that you can still access the general internet in the VM. Check that you can reach your VM from the host on 192.168.1.2.