[Raspberry Pi 4] Setting Up a Remote Environment (SSH, RDP)
[Raspberry Pi 4] Setting Up a Remote Environment (SSH, RDP)
Now the Raspberry Pi has a fairly presentable web server environment set up. But handling the Raspberry Pi by directly connecting a monitor, keyboard, and mouse to the device is a bit cumbersome.
In this context, the Raspberry Pi is meant to operate as a secondary web server, so it can't be the main computer. In other words, most work such as development happens on the main computer, with the server only handling deployment. When devices are split this way, both devices need to be able to access each other smoothly.
In short, we need a remote environment. In this chapter, we set up a remote environment on the Raspberry Pi. By setting up SSH and RDP communication, we configure it so the Raspberry Pi can be accessed from anywhere, whether via SSH or a Windows PC.
SSH (Secure SHell) is a protocol that lets you log into a PC connected to a network and perform Shell communication, such as running commands, from a remote PC onto that PC.
By setting up an SSH environment, you can access the Ubuntu Shell remotely. This lets you enter commands remotely to work with Ubuntu. Unlike Windows, almost all programs on Linux operate through commands, so you can work with Ubuntu without much difficulty.
- The openssh-server package
- The SSH (22) service port opened
The OpenSSH package broadly comes in two forms: one is OpenSSH Client, and the other is OpenSSH Server.
- OpenSSH Server - Sets up an SSH service environment on that OS
- OpenSSH Client - Adds the ability to SSH into other PCs
Install OpenSSH Server to configure the environment so you can access Ubuntu via SSH.
BASH
sudo apt-get install -y openssh-server
No additional work is needed after installation.
Open the port on which SSH is served. By default, it uses port 22.
BASH
sudo ufw allow 22
Open port 22 via ufw.
Let's try connecting to Ubuntu from another PC. This works from Windows, or any other Linux-based OS, or any OS/program capable of using the SSH protocol.
BASH
ssh username@xxx.xxx.xxx.xxx
The above command lets you attempt to connect with the entered IP and account name. Enter the Raspberry Pi's IP and the Raspberry Pi's account name. You can also enter a domain instead of an IP.
If the connection information is valid, you'll need to enter a password to log in. Once logged in, you can enter commands in the Shell, letting you work with Ubuntu remotely.
※ This only applies to Ubuntu Desktop, or an OS with an equivalent UI package installed, where Ubuntu has a UI.
Even though Ubuntu can handle most tasks purely through commands, there are occasionally tasks that absolutely require a UI. For example, debugging a web page in a browser on Linux, or watching a video. If you ever want to install Oracle DBMS, you absolutely need an environment where you can use a UI.
Even the Ubuntu we installed comes with a UI, and it's a shame to leave that installed UI unused while only working through SSH. Of course, you could just connect an HDMI cable to the Raspberry Pi and use it that way, but that's just as much of a hassle.
In my case, I use dual monitors, so my situation isn't too bad. But if you only have one monitor, the moment you connect to the Raspberry Pi, your original PC becomes unusable.
Windows has a feature called Remote Desktop Connection. It's a feature that allows remote access between Windows machines — think of it like TeamViewer or AnyDesk. It's essentially a built-in remote program.
Just from the name, it sounds like it would only support communication between Windows machines. I thought so too. But Ubuntu has more tricks up its sleeve than I expected. Installing the xrdp package lets it communicate with Windows' RDP.
- The xrdp package
- The RDP (3389) service port opened
BASH
sudo apt-get install -y xrdp
Enter the above command to install it.
Open the port on which RDP is served. By default, it uses port 3389.
BASH
sudo ufw allow 3389
Open port 3389 via ufw.
Let's try connecting to Ubuntu from another Windows PC. Type [Remote Desktop Connection] into the Start menu to launch the program.
Enter the Ubuntu IP to connect. Once connected successfully, log in using Ubuntu's login credentials.
Once connected successfully, you can control the Ubuntu UI remotely. However, due to various technical limitations and inefficiencies, the performance doesn't seem to be great. There seems to be quite a bit of lag.
Sometimes, even though you've clearly connected successfully, only a black screen appears.
This is a fairly well-known issue that can be fixed by tweaking a few settings.
BASH
sudo vi /etc/xrdp/startwm.sh # Add the following to the very bottom of the file unset DBUS_SESSION_BUS_ADDRESS unset XDG_RUNTIME_DIR test -x /etc/X11/Xsession && exec /etc/X11/Xsession exec /bin/sh /etc/X11/Xsession # Save the file :wq
Add the above content to the /etc/xrdp/startwm.sh file and save it.
BASH
service xrdp restart
Then restart the service and try again — it should connect successfully.
Set up an Ubuntu server on the Raspberry Pi.Run Tomcat to host pages.Apply a domain.Provide HTTPS communication by issuing an SSL certificate.Set up remote communication environments such as SSH and RDP.- Install MariaDB to handle DB communication.
With this, I can now access and control the Raspberry Pi's Ubuntu from anywhere. Since both the Shell and UI can be controlled, you can access it however suits your needs.
