Skip to content

Part A. Prepare and harden the Lightsail box (Phase P1)

Do these in order. Do not delete the SSH rule in Lightsail until A.8 has passed, or you lock yourself out. The user name on a Debian Lightsail box is admin.

EXPLAIN — what this does: gives you a text window on the box.

DO: Lightsail console → Instances → the Malaysia instance → Connect using SSH. A browser terminal opens with a prompt like admin@ip-172-26-…:~$.

Terminal window
cat /etc/os-release
uname -r
free -m
df -h /

VERIFY — you should see: PRETTY_NAME=“Debian GNU/Linux 13 (trixie)”; about 1900 MB of memory; a disk of about 60 GB. If it says another system, stop and ask Claude Code.

  • Static IP (recommended): Networking → Create static IP → attach to the instance → name it bideward-box-ip. The address then survives stop and start. It is free while attached. It is never put in DNS or in any document.

  • Automatic snapshots: instance → Snapshots → Automatic snapshots: On, time 19:00 UTC (03:00 in Kuala Lumpur). Snapshots are charged per GB-month (verify at the time); if cost is a problem, keep the setting and shorten nothing else, because the snapshot is the fastest way to recover a whole box.

  • Metadata protection (IMDSv2): from your own computer with the AWS CLI configured for a user that may manage Lightsail:

    aws lightsail update-instance-metadata-options --region ap-southeast-5 --instance-name <your-instance-name> --http-tokens required --http-endpoint enabled --http-put-response-hop-limit 1

EXPLAIN — what this does: blocks the old, easy-to-abuse way a stolen request could read the instance’s credentials. VERIFY — you should see: a JSON answer listing the operation as Succeeded. (Verify the command name in the AWS documentation at the time.)

A.3 Update, set UTC time, install base tools

Section titled “A.3 Update, set UTC time, install base tools”
Terminal window
sudo apt update && sudo apt -y full-upgrade
sudo timedatectl set-timezone UTC
sudo apt install -y ca-certificates curl gnupg git jq ufw chrony unattended-upgrades needrestart zstd
chronyc tracking | head -3

VERIFY — you should see: the update finishing without red errors; chrony reporting a small offset. Accurate time matters: the authenticator codes depend on it. If asked to restart services, accept the default.

A.4 Add a 1 GB swap file (safety net, not a working area)

Section titled “A.4 Add a 1 GB swap file (safety net, not a working area)”
Terminal window
sudo fallocate -l 1G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
echo 'vm.swappiness=10' | sudo tee /etc/sysctl.d/80-swap.conf
sudo sysctl --system > /dev/null
free -m

VERIFY — you should see: a Swap line of about 1023 MB. Swappiness 10 means the box uses swap only under real memory pressure.

Terminal window
sudo adduser --system --group --no-create-home --shell /usr/sbin/nologin bideward-hub
sudo adduser --system --group --no-create-home --shell /usr/sbin/nologin bideward-research
sudo install -d -m 755 -o root -g root /opt/bideward
sudo install -d -m 755 -o root -g root /etc/bideward
sudo install -d -m 750 -o bideward-hub -g bideward-hub /var/lib/bideward-hub
sudo install -d -m 750 -o bideward-research -g bideward-research /var/lib/bideward-research /var/lib/bideward-research/crashes
sudo install -d -m 700 -o root -g root /var/backups/bideward
id bideward-hub; id bideward-research

EXPLAIN — what this does: each service runs as its own limited user with no login shell and no home folder. Code lives in /opt/bideward (owned by root, read-only to the services), settings in /etc/bideward (root only), data in /var/lib, dumps in /var/backups/bideward (root only).

EXPLAIN — why: Tailscale joins your own machines into a private network. SSH to the box will work only over it, so port 22 is never open to the internet. You already use it for your other machines; this adds the box to the same network.

DO: in the browser terminal on the box:

Terminal window
curl -fsSL https://tailscale.com/install.sh | sh
sudo tailscale up --hostname bideward-box

It prints a web address. Open it, sign in with the same Tailscale account you use on your other machines, and approve the box. Make sure Tailscale is running on your own computer too.

VERIFY — you should see: tailscale ip -4 printing an address starting 100. Write it in your table (section 6). The box also appears in the Tailscale admin page under Machines. Do not change key expiry by hand: in A.8a the box gets a tag, and a tagged machine never expires.

A.7 Use the SSH key you already have (no new key), and tighten SSH

Section titled “A.7 Use the SSH key you already have (no new key), and tighten SSH”

EXPLAIN — why: the box was created with a Lightsail key pair and you already use that key (or one of your own) on your other machines. SSH only needs the key to be listed on the box, so nothing new has to be created. Over Tailscale the login works exactly as it did over the public address.

Do (1): test your existing key over Tailscale. On your own computer, replace YOUR_KEY with the path of the private key you use for this box (for a Lightsail key it is the .pem file, which must have mode 600) and 100.x.y.z with the address from A.6:

Terminal window
ssh -i ~/.ssh/YOUR_KEY admin@100.x.y.z

VERIFY — you should see: the box’s prompt with no password asked. The first time it asks “Are you sure you want to continue connecting?”: type yes. If it says “Permission denied (publickey)” the key is not listed on the box: in the browser terminal run cat ~/.ssh/authorized_keys and compare it with your key’s public half (ssh-keygen -y -f ~/.ssh/YOUR_KEY prints it). If you really have no working key, use the “Only if you have no key” box below.

Do (2): check that the key has a passphrase. The key lives on several machines, so a stolen laptop must not hand over the box.

Terminal window
ssh-keygen -y -P "" -f ~/.ssh/YOUR_KEY > /dev/null 2>&1 && echo "NO passphrase: set one now" || echo "has a passphrase"

If it prints NO passphrase, set one (the old passphrase is empty, so just press Enter; then type the new one twice and store it in your password manager):

Terminal window
ssh-keygen -p -f ~/.ssh/YOUR_KEY

Then load the key once per session so you are not asked on every command: ssh-add ~/.ssh/YOUR_KEY (on a Mac: ssh-add --apple-use-keychain ~/.ssh/YOUR_KEY). On Windows use the same commands in PowerShell with the built-in OpenSSH.

VERIFY — you should see: “has a passphrase”, and the key still logs in (repeat the ssh command from step 1).

Do (3): tighten SSH. In the browser terminal on the box (keep it open):

Terminal window
sudo tee /etc/ssh/sshd_config.d/10-bideward.conf > /dev/null <<'EOF'
PasswordAuthentication no
KbdInteractiveAuthentication no
PermitRootLogin no
PubkeyAuthentication yes
AllowUsers admin
MaxAuthTries 3
LoginGraceTime 20
X11Forwarding no
AllowTcpForwarding no
ClientAliveInterval 300
ClientAliveCountMax 2
EOF
sudo sshd -t && sudo systemctl reload ssh

VERIFY — you should see: sshd -t printing nothing (no news is good news). Do not close the browser terminal. Open a new terminal on your computer and run the ssh command from step 1 again; it must still work.

Only if you have no key

on your own computer run ssh-keygen -t ed25519 -C "frank-bideward" (choose a passphrase) and cat ~/.ssh/id_ed25519.pub; copy the line that starts ssh-ed25519. In the browser terminal run mkdir -p ~/.ssh && chmod 700 ~/.ssh, then echo 'PASTE-THE-WHOLE-LINE-HERE' >> ~/.ssh/authorized_keys, then chmod 600 ~/.ssh/authorized_keys. Continue at step 1 with the new key.

A.8 Test the way in through Tailscale (do not skip)

Section titled “A.8 Test the way in through Tailscale (do not skip)”

From your own computer, with Tailscale running:

Terminal window
ssh -i ~/.ssh/YOUR_KEY admin@100.x.y.z

(use the Tailscale address from A.6). VERIFY — you should see: the same prompt as the browser terminal, with no password asked. Do not go on until this works. Keep this window open for the next steps.

A.8a Restrict who may reach the box (Tailscale tag and access rules)

Section titled “A.8a Restrict who may reach the box (Tailscale tag and access rules)”

EXPLAIN — why: by default every machine in your Tailscale network can reach every other one. Your 1 GB staging machine (the proxy server) is in the same network: if it were ever compromised it could try to log in to the Bideward box. The fix is a tag on the box plus an access rule that lets only your own computers open port 22. This edits the rules of your whole Tailscale network, so go slowly and keep a copy.

DO:

  1. Tailscale admin page → Access controls. Copy the whole current policy into a text file on your computer and keep it. This is your undo.

  2. Machines page: write down the Tailscale address of each of your own computers that should be allowed to log in to the box (for example your laptop and your desktop), and the address of the staging machine. Note which of your other machines already carry a tag.

  3. Add these parts to the policy (this is HuJSON: comments allowed, trailing commas allowed). Replace the 100.x addresses and names with yours:

    {
    // Who may put the tag on a machine.
    "tagOwners": { "tag:bideward-prod": ["autogroup:admin"] },
    // Names for your own machines (addresses from step 2).
    "hosts": {
    "my-laptop": "100.x.x.x",
    "my-desktop": "100.y.y.y",
    "staging": "100.z.z.z"
    },
    "acls": [
    // Only your own computers may open SSH on the Bideward box.
    { "action": "accept", "src": ["my-laptop", "my-desktop"], "dst": ["tag:bideward-prod:22"] }
    // KEEP your existing rules for every other machine. If your current rule is the default
    // allow-all ("src": ["*"], "dst": ["*:*"]), change its dst so it no longer covers the
    // Bideward box, for example "dst": ["autogroup:member:*"] plus any other tagged machine you use.
    ],
    // Tailscale refuses to save the policy if one of these tests fails.
    "tests": [
    { "src": "my-laptop", "accept": ["tag:bideward-prod:22"] },
    { "src": "staging", "deny": ["tag:bideward-prod:22"] }
    // Add one accept test for each machine-to-machine path you use today.
    ]
    }
  4. Save. If Tailscale shows a test failure, read it, fix the policy and save again. Nothing changes until a save succeeds. If you lock yourself out of something, paste back the copy from step 1.

  5. On the box (browser terminal or your SSH window) put the tag on it. If the command complains that you must mention flags you used before, copy the full command it prints and add the tag flag to it:

    Terminal window
    sudo tailscale up --hostname bideward-box --advertise-tags=tag:bideward-prod
  6. Machines page: bideward-box now shows the tag tag:bideward-prod and no key expiry.

VERIFY — you should see:

  • From your laptop: ssh -i ~/.ssh/YOUR_KEY admin@100.x.y.z still works.
  • From the staging machine: ssh admin@100.x.y.z must hang and time out (or be refused). If it connects, the rule is wrong: do not go on.
  • From your laptop, every other machine you use still answers (for example ssh to the staging machine). If something broke, the broad rule in step 3 needs the missing machine added.

A.9 Turn on the box’s own firewall (ufw)

Section titled “A.9 Turn on the box’s own firewall (ufw)”
Terminal window
sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw allow in on tailscale0 to any port 22 proto tcp
sudo ufw enable
sudo ufw status verbose

VERIFY — you should see: Status: active, Default: deny (incoming), allow (outgoing), and one rule: 22/tcp on tailscale0. Nothing else. (Type y when asked to confirm.) The browser terminal button stops working now; use your own terminal.

A.10 Delete every inbound rule in the Lightsail firewall

Section titled “A.10 Delete every inbound rule in the Lightsail firewall”

Lightsail → instance → Networking: in IPv4 Firewall delete SSH (22), HTTP (80) and any other rule; in IPv6 Firewall delete all rules too. The list must be empty.

VERIFY — you should see: no rules in either list. Your Tailscale SSH session keeps working (Tailscale connects outward). Rescue if you ever lose Tailscale: temporarily add SSH 22 from your own IP only in the IPv4 firewall, use the browser terminal, fix Tailscale, delete the rule again.

EXPLAIN — why: the firewall now allows nothing from the internet and port 22 only from your own machines. Before you close your last working window, prove that the way in is the one you intend, and that the box is really closed.

  1. Open a second terminal on your computer and log in again with the ssh command from A.8. It must work while the first window is still open.
  2. From the staging machine, the same ssh command must still time out (A.8a).
  3. Turn Tailscale off on your own computer, then run ssh -i ~/.ssh/YOUR_KEY admin@PUBLIC_IP (the address Lightsail shows for the instance). It must hang and fail. Turn Tailscale on again.
  4. Run sudo ufw status verbose on the box once more and compare it with the A.9 result.

VERIFY — you should see: logins work only through Tailscale from your own computers, and nothing answers on the public address. If step 1 fails, do not close the first window: fix it from there (A.9 and A.8a explain each rule).

A.11 Automatic security updates, kernel hardening, log size

Section titled “A.11 Automatic security updates, kernel hardening, log size”
Terminal window
sudo tee /etc/apt/apt.conf.d/20auto-upgrades > /dev/null <<'EOF'
APT::Periodic::Update-Package-Lists "1";
APT::Periodic::Unattended-Upgrade "1";
APT::Periodic::AutocleanInterval "7";
EOF
sudo tee /etc/sysctl.d/90-bideward.conf > /dev/null <<'EOF'
net.ipv4.conf.all.rp_filter = 1
net.ipv4.conf.default.rp_filter = 1
net.ipv4.conf.all.accept_redirects = 0
net.ipv6.conf.all.accept_redirects = 0
net.ipv4.conf.all.send_redirects = 0
net.ipv4.tcp_syncookies = 1
net.core.somaxconn = 1024
kernel.kptr_restrict = 2
kernel.dmesg_restrict = 1
fs.protected_hardlinks = 1
fs.protected_symlinks = 1
EOF
sudo sysctl --system > /dev/null
sudo mkdir -p /etc/systemd/journald.conf.d
printf '[Journal]\nSystemMaxUse=300M\nMaxRetentionSec=1month\n' | sudo tee /etc/systemd/journald.conf.d/bideward.conf
sudo systemctl restart systemd-journald

Security updates install by themselves. The box does not reboot by itself (a reboot drops every bot connection). After a kernel update, reboot it yourself within a week, when no bot is in a recovery or a bet.

From your own computer, scan the box’s public IP (find it in the Lightsail console):

nmap -Pn -p 1-10000 <public-ip>

VERIFY — you should see: every port filtered or closed, nothing open. (If nmap is not installed, skip; the Lightsail firewall list being empty is the proof.)


← Previous: The plan at a glance · Guide index · Next: Part B. PostgreSQL: install, tune, secure (Phase P2) →