Wednesday, September 22, 2021

Turn an Old Laptop into a Server

Over the years I've garnered a collection of somewhat broken old laptops. One has a broken keyboard and no battery. Another has a faulty SSD and a flaky motherboard. Both are slow when running modern systems.

For general computing, though, old laptops have some advantages:

If you have an old laptop lying around that you'd like to turn into a home Linux server that you control over ssh, here's how I set it up.

Linux USB installer

Download an .iso file of your favorite Debian-based Linux distribution. Ubuntu Server is a tempting choice, but if your laptop lacks a wired ethernet card (like my most recent one), then the server distribution might not have your wireless drivers. Here be dragons.

Instead, we'll install a "desktop" flavored distribution and strip it down afterward.

I used Ubuntu Desktop 20.04.3 and flashed it onto a 32 GB USB flash drive using Startup Disk Creator.

Configure BIOS on old laptop

You need to tell the laptop to boot from the USB flash drive. How to do this will depend on the specific laptop, but it will involve one or more of the following:

With luck, the next boot will start some GUI/shell for the Linux distribution's installer.

On the old laptop

Install the Linux distribution. To keep things simple, I configure the main user to have the same name as the main user on my primary computer ("david"). I like short names for the to-be server (e.g. "lenovo" for my old Yoga 2).

Once that's done and you've restarted the computer without the USB flash drive and verified that it boots the system you installed, you're ready to strip it down.

Our first order of business is installing an SSH server so that you can configure the computer remotely. For example, I have mine set up with the lid closed on a shelf somewhere. Don't close the lid yet, though.

$ sudo apt update
$ sudo apt install -y openssh-server
$ sudo apt upgrade -y

Now you can connect to the laptop remotely over ssh (see below). Keep the lid open, though, so that the laptop doesn't sleep. We'll change that later.

On your real computer

Switch to your favorite computer for computing and connect to your soon-to-be-server laptop. You can use the laptop's local IP address or hostname (if it advertises one). In my case, my user name is david and the laptop's name is lenovo, so:

$ ssh david@lenovo

david@lenovo $ echo 'we are now remoted into the laptop'
we are now remoted into the laptop

It will ask you for a password, and possibly multiple confirmations. The password is whatever you configured for the user (david) on the laptop.

The remaining commands in this section are to be run on the soon-to-be-server laptop, over your ssh connection. I'll use the shell prompt david@lenovo $ to remind you.

Over ssh

Configure non-graphical boot

If I happen to have the lid open when the laptop boots, I don't want to see a splash screen. I want to see Linux's log, and then systemd's once init starts. Here's how:

david@lenovo $ sudo vi /etc/default/grub

That file might have a variable whose value is set to "quiet splash" or similar. If so, remove "quiet" and "splash," e.g. so that value is "".

Save and exit. Then, regenerate the bootloader with the modified configuration:

david@lenovo $ sudo update-grub

Now change the "run level" to the non-graphical one:

david@lenovo $ sudo systemctl enable multi-user.target
david@lenovo $ sudo systemctl set-default multi-user.target

Now your desktop Linux will feel more like a server.

Closing the laptop lid does nothing

Lid open/close events from the hardware are handled by the login service. Let's configure the handlers to ignore the events, rather than whatever they do by default:

david@lenovo $ sudo vi /etc/systemd/logind.conf

That file will have commented-out lines beginning with HandleLid, e.g.

#HandleLidSwitch=suspend
#HandleLidSwitchExternalPower=suspend
#HandleLidSwitchDocked=ignore

Uncomment them and change their values to ignore, e.g.

HandleLidSwitch=ignore
HandleLidSwitchExternalPower=ignore
HandleLidSwitchDocked=ignore

Save and exit.

Then restart the service to pick up the changes:

david@lenovo $ sudo service systemd-logind restart

Now you can close the lid on the laptop, and nothing will happen.

Authorize your public ssh key on the server

It's a pain to type my password every time I want to connect to the server. The ssh server supports a per-user allow list of public keys to permit without falling back to password authentication. That file probably does not yet exist on the server, so let's copy the contents of our public key to that file on the server.

My public key is called ~/.ssh/id_ed25519.pub. Yours might have a different name.

david@lenovo $ mkdir ~/.ssh

Run this next command on your real computer, not on the server.

$ scp ~/.ssh/id_ed25519.pub david@lenovo:/home/david/.ssh/authorized_keys

Now you can connect to the server without a password.

$ ssh lenovo

david@lenovo $

Profit

Congrats, you now have a server in your house, you budding sysadmin, you.

My next steps are usually to:

Happy hacking.

laptop server