November 6, 2019

Make a Server on Raspberry Pi 4

By Andy Gasparini

Make a Server on Raspberry Pi 4

Let’s think of all those times that you thought, “I wish I had a personal storage device, with all sorts of goodies that I can freely grab any time anywere and that I own”. By no means this should replace a cloud service, but if you are looking to learn how a server is set up or how to manage a server, or just do geeky “laissez faire” fun stuff this is the best you can get. Thus, let's satisfy all your nerd cravings for making stuff by making a server, with some additional goodies to steer you and everyone else toward the path of making what would be similar to a very scaled down cloud or hosting service.

First what you need:

  1. A Raspberry Pi (any version)
  2. Power supply 5V 2A
  3. A SD card with at least 8Gb
  4. An ethernet cable
  5. An SD card reader
  6. Software for imaging (I am using Balena Etcher)
  7. Software for ssh (I am using PuTTY or cmder cmder)
  8. Network discovery software (I am using Advanced IP Scanner but you don’t need it you can log in into your router and check what is connected to what)
  9. An operating system compatible with ARM (I used Raspbian Buster Lite, link below)
That’s it!

I am currently using a Raspberry Pi 4 with a 4Gb of ram that came from Vilros complete of everything with heatsinks, fan and case.

The package What's in the package

The heatsinks and everything additional that has not been mentioned that you see here are not necessary, but can help make your life easier, overall this package costed $100 on Amazon. If you have fans and case connect them like so:

Hook up that fan!

Downloads

Download the following:

  • Imaging software (I used Balena Etcher you can find it on the link above)
  • Your ssh software, I usually use PuTTY or cmder
  • Download a Raspberry Pi headless or lite (same thing), I used Raspbian Buster Lite you can find it here

The SD card

Get the SD card and put it in your sd card reader connected to your pc. Go to This Pc and click on the drive that represents the SD card and format (you can also use SD Card Formatter), make sure quick format is checked and that Fat32 is selected.

Format the sd card

Unzip the image and then run your imaging software and load the image (Raspbian Buster Lite in my case) that you just download. (Make sure you download a Lite or Headless version if you want a server)

Format the sd card

Use your imager software, in this case Balena Etcher and install the image on the os card like shown below.

What's in the package

If you get an error don’t worry, pull out your SD card, put it back in and browse the SD card, add a file called ssh and eject your card.

Onward to the Raspberry Pi

Carefully place the SD card on the bottom of the Raspberry Pi like the image.

SD setup

Plug in the power supply and ethernet cable you should see a red and green light appear.

Ethernet and power setup

Run Advanced IP Scanner, check what’s the address of your pi (press start at the top left).

Ip scanner

Go to your computer, get your ssh capable software and run the following command (in my case cmder).

ssh pi@Your pi address

The pi is asking for your password so here are the pi default credentials:

Username: pi Password: raspberry

VOILÀ!

From here you can literally do anything, you can host a website, you can make your pi public and discoverable externally, you can run programs, you can crawl the internet or conquer the world…The last one not really! Here are some fun ideas for projects with links, and then at the bottom of the post there is a fun get started way to programmatically exchange files between your computer and the pi.

Tips: Raspberry Pi is great and all although you have to be careful about compatibility. If you are a programmer, I do suggest the following languages:

  • Java. Use Maven if you are using Java, it will really decrease your development time!
  • Python
  • Rust

If you want to take it a step further to increase compatibility use something like docker. Furthermore if you like to just use it without programming on it you can use FileZilla to transfer files.

Some fun ideas:

  • Git server on raspeberry pi just run apt-get install git

    git init --bare your_project

    In your version control:

    ssh pi@your_pi_address:folder_path

  • Make 24/7 crawler (blog post coming)

  • Make a storage device > Hook up a hard drive

    Apt-get install exfuse

    • Enables usb, and use FileZilla for sftp or ftp to transfer files!
  • Make a Jenkins server for a personal CI/CD solution!

  • Make a Nexus server to work with Jenkins and deploy packages!
  • Make a Factorio server

Fun things to do!

The first time I received an embedded system like a Raspberry Pi I always wanted to make my own way of doing file transfer, thus my suggestion to you to understand basic file transfer is to give you a small implementation of FTP, SFTP and socket transfer that you can use, in Java. Let’s start with the most basic one, mind that this is the faster option among all the other 3! Here is the implementation:

Server

import java.io.DataInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class Server  extends Thread {

private ServerSocket serverSocket;

    public Server(int port) {
        try {
            serverSocket = new ServerSocket(port);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void run() {
        System.out.println("Waiting...");
        while (true) {
            try {
                Socket clientSocket = serverSocket.accept();
                saveFile(clientSocket);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private void saveFile(Socket clientSocket) throws IOException {
        DataInputStream dis = new DataInputStream(clientSocket.getInputStream());
        // The System.getProperty will allow you to get the current folder of the project, you can change this if you wish
        FileOutputStream fos = new FileOutputStream(System.getProperty("user.dir") + File.separator + "YOUR FILES FOLDER" + File.separator + "YOUR FILE");
        byte[] buffer = new byte[4096];

        // Split file
        int filesize = 15123;
        int read = 0;
        int totalRead = 0;
        int remaining = filesize;
        while((read = dis.read(buffer, 0, Math.min(buffer.length, remaining))) > 0) {
            totalRead += read;
            remaining -= read;
            System.out.println("read " + totalRead + " bytes.");
            fos.write(buffer, 0, read);
        }

        fos.close();
        dis.close();
    }
}

Client

import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.Socket;

public class Client {
    private Socket socket;

    public Client(String host, int port, String file) {
        try {
            socket = new Socket(host, port);
            sendFile(file);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void sendFile(String file) throws IOException {
        System.out.println("Sending...");
        DataOutputStream dataOutputStream = new DataOutputStream(socket.getOutputStream());
        FileInputStream fileOutputStream = new FileInputStream(file);
        byte[] buffer = new byte[4096];

        while (fileOutputStream.read(buffer) > 0) {
            dataOutputStream.write(buffer);
        }

        fileOutputStream.close();
        dataOutputStream.close();
        System.out.println("Done");
    }
}

One strategy you can use to use sockets and make it safe is to implement a encryption protocol or have an encryption protocol.

For example one thing you can do is to use RSA/ECB then use 256-AES to encrypt that key that is transfered and stored between the two servers.

Please be mindful the sole purpose of this is so you can or think about software and how embedded systems and iot works, I would not actually use it in a professional context.

Let's tackle the SFTP, I would recommend using Jsch, you can find the maven repo here.

This is the implementation:

import java.io.File;
import java.io.InputStream;

import com.braintobytes.iot.messenger.user.LogInfo;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpException;
import com.jcraft.jsch.UserInfo;

public class SFTP {

    private String host;
    private int port;

    public SFTP(String host, int port) {
        this.host = host;
        this.port = port;
    }


    public boolean sendFile(File file, String target) throws JSchException {
        ChannelSftp channelSftp = makeSftp();

        try {
            channelSftp.put(file.getAbsolutePath(), target);
        }catch(SftpException ex) {
            return false;
        }

        return true;
    }

    public boolean receiveFile(String targetToReceive, String fileDestination) throws JSchException {
        ChannelSftp channelSftp = makeSftp();

        try {
            channelSftp.put(targetToReceive, fileDestination);
        }catch(SftpException ex) {
            return false;
        }

        return true;
    }

    private ChannelSftp makeSftp() throws JSchException {
        JSch shell = new JSch();

        LogInfo userInfo = new LogInfo("yourUser");
        userInfo.promptPassword("Please input password: ");

        Session session = shell.getSession(userInfo.getUser(), host, port);


        session.setUserInfo(userInfo);
        session.connect();

        Channel channel = session.openChannel("sftp");
        channel.connect();
        return (ChannelSftp) channel;
    }
}

Now said that I think the only thing remaining to say it’s HAPPY GEEKING!


Andy Gasparini - Full Stack Software Developer and Consultant

About Andy Gasparini

Andy Gasparini is Full Stack Developer and Consultant at Centare. Andy is mainly focused on C# and Angular, but also loves Java and React. He is an Embedded Systems, IOT Enthusiast and loves cloud-based systems like AWS and Azure. In his free time he likes to learn about mathematics, create libraries and work with Embedded Systems. Some of his projects are available at www.braintobytes.com