General Knowledge of Networking

 

On Internet model, there are 5 layers. They’re Physical Layer, Data Link Layer, Network Layer, Transport Layer and Application Layer. Each of them has its own protocols and addresses.  Each layer can only communicates with its adjacent layers.

Addresses

MAC Address is the address in Data Link Layer (Layer 2). IP Address is the address in Network Layer (Layer 3). Port Number is the address in Transport Layer (Layer 4). Mostly, we’re dealing with IP address and Port number.

IP Address

IP address is used to identify a host. IPv4 address is ranged from 0.0.0.0-255.255.255.255. Each section is from 0-255. An IP address can be private, public or for special use.

Private IP address can be used in local network only, it doesn’t resolve on the internet. LAN IP are often private IP. Private IP are ranged at 10.0.0.0-10.255.255.255, 172.16.0.0-172.31.255.255, 192.168.0.0-192.168.255.255.

Public IP address are used on the internet, WAN IP are often public IP. Public IP is ranged from 1.0.0.0-223.255.255, except for Private IP, 127.0.0.0-127.255.255.255 (loopback address) and 169.254.0.0-169.254.255.255 (link local address).

The remaining addresses are reserved or for special use.

Port Number

Port number indicates which application the packet is sending to. Port number can be ranged from 0-65535. Port number on server side is often static and on client side it’s dynamic. Port 49151-65535 are explicitly for private use (client port).

Well known server ports includes 21 (FTP), 22 (SSH), 23 (Telnet), 25 (SMTP), 53 (DNS), 67/68 (DHCP), 80 (HTTP), 110 (POP3), 137/139 (Samba), 143 (IMAP), 443 (HTTPS), 465/587 (SMTPS), 520 (RIP), 546/547 (DHCPv6), 993 (IMAPS), 995 (POP3S).

NAT

Network Address Translation is widely used in home router. It allows us to use the same public IP address (the one from ISP) with multiple devices. The IP address of our devices is actually a private address, which means it can be used in LAN only.

Let’s see what’s happening when we browse a webpage in LAN:

  • When we open Chrome, the computer assigns a random port number. If we go to google from Chrome, Chrome sends a packet to Google with source destination and its assigned port (Let’s say 192.168.0.3:54321 -> 216.58.220.110:443).
  • Then the router maps the private address and source port to a random port in router, then alters the source address and source port of the packet (e.g. 123.234.321.23:11111) and send it to Google, now the packet becomes 123.234.321.23:11111 -> 216.58.220.110:443.
  • Google replies to the router with the webpage, it’s destination is 123.234.321.23:11111, the router maps back to the original private address and rewrites the destination to 192.168.0.3:54321. Then your computer knows port 54321 is for Chrome and sent the data to it.

That’s it for what we need to configure our device working on LAN. With the basic concept of how network works we can now proceed to implement access to Raspberry Pi.

Connect to Wifi

The Raspberry Pi Model 3B has built-in 802.11n wireless network card, which means you can connect to internet over wifi. For all other model, you may purchase a USB wifi dongle.

The wifi network is controlled by wpa_supplicant, edit the file with the following command:

  • sudo nano /etc/wpa_supplicant/wpa_supplicant.conf

Enter the network detail as the following format at the end of the file:

  • network={
  •     ssid=”<wifi SSID>”
  •     psk=”<wifi Password>”
  • }

Note that the quotation marks are compulsory. Save and exit nano, then restart wireless service by the following command:

  • sudo ifdown wlan0
  • sudo ifup wlan0

You should be see the IP address on “ifconfig wlan0” under inet address. If not, check your SSID and password.

 

Static IP Address

As the device asks DHCP server for IP address when it connects, the IP address changes over time. Static IP allows the device reserves an IP address and doesn’t change over time. It is not compulsory but highly recommended. A static address allows quick access if you would like to setup remote access in LAN, and it’s essential if you would like to run a server and allow access from internet via NAT.

Static IP can be configured on router or on Raspberry Pi. To eliminate the possibility of IP address conflict, configure on router is recommended.

To configure on router:

First, check your MAC address by the following command:

  • ifconfig <interface>

If you’re using ethernet cable, the interface would be “eth0”.

If you’re connected to wifi, the interface would be “wlan0”.

The HWaddr is the MAC address and the inet addr is the IP address we need, record these addresses.

Second, login to your router, look for static IP settings. It’s usually under or close to DHCP Server settings. Enter the MAC address and IP address recorded before and save. If you like (and you know how it works), you may also bound the device to another IP address.

This IP address now permanently belongs to your pi on your network. Reboot the device to make it in effect.

To configure on Raspberry Pi:

First, check your default gateway and DNS settings by the following commands:

  • ifconfig <interface>
  • route

If you’re using ethernet cable, the interface would be “eth0”.

If you’re connected to wifi, the interface would be “wlan0”.

For the first command, The inet addr is the device IP address and mask is subnet mask we need, record these address. We also need the Bcast address.

For the second command, the gateway address for default destination is the IP address of the router, we need these as well.

Second, edit the interface file by command:

  • sudo nano /etc/network/interface

Find the appropriate interface, Enter the detail as the following format under it:

  • iface <interface> inet manual static
  •     address <IP address of Raspberry Pi>
  •     netmask <subnet mask>
  •     gateway <IP address of router>
  •     network <Bcast address, replace the last 255 with 0>
  •     broadcast <Bcast address>

Originally, The last word of the first line is manual and we need to replace it with static. You may also use another IP address if you like (and know what you’re doing).

Third, we also need to update DNS address on resolv.conf:

  • sudo nano /etc/resolv.conf

Enter the detail as the following format at the end:

  • nameserver <IP address of router>

If you’re using another DNS server, you can use the one you prefer.

Note that this method may result in IP conflict, therefore it’s not recommended. Reboot your Raspberry Pi to make them in effect.

Remote Login – SSH

Raspberry Pi can be accessed remotely by Secure Shell (SSH) by default (Default to be off since Sep 2016 release). You can connect your PC to Raspberry Pi via SSH and use the command line just the same way as you’re on the Pi. To use SSH, you need the IP address of the Raspberry Pi (that’s why static IP is recommended), and your username and password, or you can create a certificate later and use it instead of a password.

Other than the IP address, Raspberry Pi can also be connected by hostname. The default hostname is “raspberrypi.local”. If you don’t know the IP address, this is a replacement of it.

The default username is “pi” and the default password is “raspberry”. If you have changed your password on earlier setup (and you should), use your own password.

On Windows, PuTTY is the the most popular client for remote access. It can be downloaded at http://www.putty.org.

Run PuTTY as administrator, select SSH for connection type, enter the IP address and login information, then connect.

On Mac OS, we use terminal to connect to Raspberry Pi. Type in the one of following command:

  • ssh <username>@<IP address>

Then type in your password and get connected.

SSH can also be used to transfer files between Raspberry Pi and your PC. This is called Secure Copy (SCP).

On Windows, you need to download PSCP from PuTTY ebsites . Open Command Line and use the following command:

  • If you want to copy file from your PC to Raspberry Pi:
    • pscp <source files> <username>@<IP address>:<destination path>
  • If you want to copy file from your Raspberry Pi to your PC:
    • pscp <username>@<IP address>:<source files> <destination path>

On Mac OS, scp is integrated in terminal. The command is as following:

  • If you want to copy file from your Mac to Raspberry Pi:
    • scp <source files> <username>@<IP address>:<destination path>
  • If you want to copy file from your Raspberry Pi to your Mac:
    • scp <username>@<IP address>:<source files> <destination path>

We also need to disable root login via SSH, especially if you would like to setup access via internet, as root password cannot be change. This is configured by sshd_config, edit the file with the following command:

  • sudo nano /etc/ssh/sshd_config

Find the following line:

  • PermitRootLogin yes no

Change from yes to no to disable root login. Then save and exit, and restart SSH service:

  • sudo service ssh restart

If you plan to make a headless pi and access via SSH all the time, SSH is off as default from Sep 2016. You may create an empty file named ssh under boot partition to override this setting after install the system to SD card.

With SSH, we no longer need a monitor and a keyboard to access Raspberry Pi. It’s a huge save of money for an extra monitor, Hurray!

 

 

SSH with Certificates

Using certificates to login via SSH is safer than using a password. And it’s more convenient if you’re using the same device to login. Here is the steps:

First, we need to generate certificates. You can generate SSH key pairs directly from PuTTY with PuTTYgen on Windows, or use OpenSSH integrated in the terminal for Mac OS.

On Windows, Run PuTTYgen as administrator, select SHA-1 RSA for key type and 2048 bits for length, and generate a key pair. Then fill in key comment and a passphase. A passphase is like a password for your private key, it’s not compulsory but recommended. Then save both of you public key and private key.

On Mac OS, use the following command in the terminal to generate key pairs:

  • ssh-keygen -t rsa

Leave the save location blank to save at default location, which is recommended (or you can specify a location if you want). Enter a passphase and verify (or you can leave them blank for no passphase). The key will be saved automatically.

Second, we need to copy the public key from PC to Raspberry Pi. Login to Raspberry Pi with SSH, then edit authorszed_keys file:

  • nano ~/.ssh/authorized_keys

Open your public key file. Paste the key in the file and save and exit nano. You can have multiple public keys each line in the file if you need to login to your Raspberry Pi from more than one device.

On Mac OS, the public key ends with “.pub”. You can use cat command to view and copy:

  • cat ~/.ssh/id_rsa.pub (if you save at default location)

Never share your private key!

Third, we need to edit sshd_config of Raspberry Pi, to disable login via password, open the file with the following command:

  • sudo nano /etc/ssh/sshd_config

Find the following lines:

  • #PasswordAuthentication yes no

We would like to delete the hash key at the front, and change yes to no. Then save and exit, and restart ssh service:

  • sudo service ssh restart

Now we can login to Pi using certificate. Only client with certificate that is registered on authorized_keys is allowed.

On Windows, you need to select your private key as login credential. Select SSH – Auth on the left menu and choose your private key, then enter your passphase if you have one. Then connect as normal.

On Mac OS, terminal will automatically use your private key to attempt to login, if there’s one in the default location. If you saved you private key in a different location, use “-i <private key file>” parameter on SSH command.

Note that if you lost all your keys, you will lose access via SSH. In such case, you need to use a keyboard and monitor to access just like the first time setup. You can edit the sshd_config to allow password login again.

Remote Desktop – VNC

We were dealing with command line from the start. In fact Raspberry Pi also has desktop environment. It can be started one-off by command “startx”, or it can be launched on boot, which can be set on raspi-config:

  • sudo raspi-config

Select Option 3 Boot Option, go to Desktop/CLI and choose Desktop. Exit raspi-config and reboot, now we can see it boots into desktop environment.

Virtual Network Computing (VNC) is the remote login to desktop. The latest disk image has RealVNC preinstalled, but it’s not turned on by default. This is also set on raspi-config. Go to Advance Option, select VNC and choose yes to allow remote desktop. Then reboot the device.

On the PC side, RealVNC client is available at https://www.realvnc.com/download/vnc/. After installation of VNC Viewer, we can connect to Raspberry Pi with its IP address, username and password.

Now we can access command line and desktop remotely, which is a lot more flexible than sitting on a monitor. For even more flexibility, we can  setup access over the internet next.

Access from the Internet

You might find all the settings before work only on LAN. We cannot access our LAN from internet because our router has NAT and firewall. To go over this limit, we can use port forwarding/port mapping. Port forwarding is to bind a port to a host and all packets arrives at that port is redirected to that host. This is set in the router.

First, login to your router and select port forwarding, this is normal in advance option near firewall.

Then, add some new rules in. You’ll need to specific incoming port, destination private address and destination port. You may also specific source IP address to prevent malicious attack.

For security, open minimal ports to the internet. For public servers, such as web server and mail server, we may use its standard port. For other servers, never use its standard port. Never ever open port 22 for SSH or port 5900 for VNC from the internet, use some others (I used 2200, but don’t try it because it’s closed now) and map it to port 22.

Now if you try to access your public IP address from the internet (you may use cellular connection to test, remember to use correct port number as well), you should be able to connect as you were in your LAN. If you don’t know your public IP, you may look at WAN address in your router. Note that if your WAN address is also a private address, you cannot configure to connect from the internet.

You’ll also need to check whether your public IP is static or dynamic. You may check with your ISP or reboot your router to see if WAN IP changed. If it’s static, that’s it we’re done, remember to update port forward when new service comes online. If your IP address is dynamic, it’s recommended to set up DDNS on router as well. There are many free DDNS service provider, such as https://www.noip.com. After you register and select a domain name, login to your account on your router and you can gain access from the internet using the domain name. This also applies to static WAN IP if you don’t want to use your IP address.

Now we have anywhere access from the internet. We can start to launch some services to the internet.