Running Claude Code Securely on Your Own VPS
A hardened Debian VPS keeps Claude Code sessions permanently available. This guide covers everything from user accounts and SSH keys to firewalls, data hygiene, tmux, and secure access from an iPhone.
A Claude Code session on your own computer can end unexpectedly as soon as the laptop sleeps or the network connection drops. A VPS keeps running and can be accessed from multiple devices. At the same time, it is permanently connected to the public internet and is automatically scanned shortly after startup.
This guide combines both requirements: Claude Code remains available in a tmux session, while the Debian server offers only an SSH connection protected by keys to the outside world. The hardening is not Claude-specific and is also suitable for other publicly accessible Linux servers.
Why a VPS can make sense
Compared to a purely local installation, the server offers three practical advantages:
- Persistence. In a
tmuxsession, Claude keeps running even when the SSH connection is disconnected. A task that takes ten minutes or an hour finishes without the laptop having to stay open. - Accessibility. The same session is accessible from a desktop, laptop, and iPhone. You start a task at your desk and check the result while on the go.
- Data control. You decide what resides on the server. No sync service, no accidentally backed-up credentials—provided you proceed carefully during migration (see below).
tmux is purely an availability and convenience feature, not a security measure. The real work lies in securing the system.
Starting point
The basis is Debian 13 (Trixie), installed minimally, without a desktop and without additional network services. The provider supplies an upstream firewall that applies independently of the operating system. The goal is a server on which only SSH is reachable from the outside, and even that only with passphrase-protected keys.
1. Update the system
Immediately after installation, update all packages:
sudo apt update
sudo apt full-upgrade
Unlike upgrade, full-upgrade also resolves dependencies that require packages to be added or removed. On a fresh system, this is the right way to install all available security updates. Restart once after kernel updates.
2. Use a dedicated user instead of root
Working as root is unnecessarily risky: every typo affects the entire system, and direct root login is the first thing automated attacks try. Therefore, create a dedicated user (here claude) with sudo privileges for cases where they are needed:
sudo adduser claude
sudo usermod -aG sudo claude
From now on, all administration is performed through claude and sudo, no longer through direct root access.
3. Passphrase-protected Ed25519 keys, one per device
Login should work exclusively through SSH keys, not passwords. Ed25519 is the current standard: short, fast, and cryptographically sound. The key point is that the key is generated on the client—that is, on the PC, not the server—and protected with a passphrase. The passphrase is the second line of defense should the private key ever fall into the wrong hands.
On the PC:
ssh-keygen -t ed25519 -C "pc-thinkpad"
The comment (-C) identifies the device. This pays off later: generate a separate key for every device—one for the PC and a separate one for the iPhone. If a device is lost, remove its public key specifically from ~/.ssh/authorized_keys without having to redeploy all other access.
Only the public key belongs on the server. The private key never leaves the device. In authorized_keys, only public keys remain, each with its device comment:
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAA...pc pc-thinkpad
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAA...ios iphone-15
Transfer the public PC key initially. As long as password login is still active, this is easiest with:
ssh-copy-id claude@SERVER
Then test that key-based login works before disabling password login in the next step. File permissions must be correct; otherwise sshd ignores the file: ~/.ssh at 700, authorized_keys at 600.
4. Harden SSH: no root, no password
The server configuration is located in /etc/ssh/sshd_config and (on Debian 13) in drop-in files under /etc/ssh/sshd_config.d/. Changes belong in a dedicated drop-in file; this leaves the main file untouched and package updates overwrite nothing. Create file /etc/ssh/sshd_config.d/99-haertung.conf:
PermitRootLogin no
PasswordAuthentication no
KbdInteractiveAuthentication no
PubkeyAuthentication yes
This disables direct root login and password login. From now on, only someone who possesses a matching private key can get in. Before reloading, check the configuration syntax:
sudo sshd -t
If sshd -t reports nothing, the file is valid. Only then reload:
sudo systemctl reload ssh
Important: Leave the existing SSH session open and test the new access in a second terminal. Only once key-based login demonstrably works there may you close the old session. This precaution reduces the risk of locking yourself out to virtually zero. A configuration error would otherwise cost you all access.
5. Move SSH to an uncommon port
The standard port 22 is probed by bots around the clock. Switching to a high, freely chosen port (in this example 61417) causes most of this automated noise to go nowhere. This is explicitly not a security improvement in the strict sense: changing the port does not replace strong authentication; it only reduces log volume and scanning load. The key requirement from step 4 remains the actual security measure.
Which port to use is not arbitrary. IANA distinguishes three ranges: 0–1023 (well-known ports) are reserved for standard services (SSH itself on 22, HTTP on 80, HTTPS on 443), require root to bind, and have no place on a self-chosen SSH port; scanners expect these ports, as do standard services installed later. 1024–49151 (registered ports) are assigned to individual applications on request, such as 3306 (MySQL), 5432 (PostgreSQL), 6379 (Redis), or 8080/8443 as common HTTP alternatives; a randomly selected port from this range can easily conflict later with software that expects its registered port. 49152–65535 (dynamic/private ports) are not assigned to any service according to IANA and are intended for temporary, private purposes—the right range for a permanent, self-chosen port.
One caveat remains: many Linux systems, including Debian, use part of this same range as source ports for their own outgoing connections (net.ipv4.ip_local_port_range, by default around 32768–60999). A permanently listening service does not truly conflict with this, as the kernel does not assign an already bound port, but a port above 60999 also avoids this theoretical ambiguity. The example in this article (61417) is deliberately in that range. Before switching, also use ss -lntup (see step 7) to check that the selected port is not already in use on your server.
There is a peculiarity in Debian 13: SSH may be started through systemd socket activation. If that is the case, the Port setting in sshd_config is simply ignored; the port must then be set on the socket. First check which case applies:
systemctl is-enabled ssh.socket
If the command responds with enabled, SSH runs through the socket. Then change the port there:
sudo systemctl edit ssh.socket
Enter the following lines in the editor. The first, empty ListenStream= line clears the preset port 22; the second sets the new one:
[Socket]
ListenStream=
ListenStream=61417
Then apply the changes:
sudo systemctl daemon-reload
sudo systemctl restart ssh.socket
If socket activation is not active (disabled), add Port 61417 to the drop-in file from step 4 instead, followed by sudo sshd -t and sudo systemctl restart ssh.
The same applies here: first open the new port in the firewall (next step), then connect and test it, and leave the old session open until access through the new port is confirmed.
6. Firewall: deny by default
The upstream provider firewall is the most effective boundary because it intercepts packets before they even reach the operating system. Two basic rules apply:
- Set the default inbound action to DROP. Everything not explicitly permitted is discarded silently, without feedback to the sender.
- One single exception: inbound TCP to destination port
61417. Nothing else needs to be reachable from the outside.
Outbound traffic remains allowed. This is intentional: the server must download packages, synchronize time, and reach the API for Claude Code. Restrictive outbound filtering provides little additional protection for a single server but makes operation noticeably more cumbersome.
If you want additional defense in depth, you can duplicate the same rules on the host with nftables or ufw. For the setup described, the provider firewall is sufficient.
7. Check the attack surface
After hardening, check what the server actually offers to the outside. Two commands are enough. First: which services are listening on which addresses?
sudo ss -lntup
The address column is crucial: a service on 0.0.0.0 or [::] is reachable from the outside, while one on 127.0.0.1 or [::1] is local only. In the secured state, only SSH should appear publicly. Services such as chronyd (time synchronization) may appear, but only bound to local addresses. If chronyd listens exclusively on 127.0.0.1 and ::1, it cannot be reached from the outside and is therefore not an issue.
Second: are there failed system services that indicate a configuration problem?
systemctl --failed
The response should be 0 loaded units listed, with not a single failed service. Failed units are not only an operational issue but potentially a security issue as well if they involve a partially started, misconfigured network service.
8. Install and run Claude Code
Claude Code does not require a separate Node.js runtime environment. Anthropic recommends the native installer (curl -fsSL https://claude.ai/install.sh | bash), which updates Claude Code itself in the background; for Debian, a signed apt repository is also available and is updated through normal system updates. After installation, authenticate the CLI fresh on the server rather than uploading local credentials (more on that shortly).
For persistent operation, use tmux:
tmux new -s claude
Start Claude within the session. Press Ctrl-b, then d to detach from the session without ending it; Claude keeps running. Return with:
tmux attach -t claude
This lets a running task survive disconnected connections, device changes, and the laptop sleeping overnight.
9. Data hygiene during migration
The most sensitive part of moving to the server is not the technology but deciding what to bring along. Three rules:
- No private keys on the server. Only public keys belong in
authorized_keys. Private keys remain on endpoint devices. - Do not copy credentials indiscriminately. Sensitive local files such as a
.credentials.jsondo not belong on the VPS without review. Instead, authenticate fresh on the server. - Put configuration in a migration folder first. Do not write existing Claude memories and configuration directly to active configuration paths. First transfer them to a separate migration folder and review what should actually be retained. Anything no longer needed, such as old MCP entries or orphaned settings, is intentionally left behind rather than moved blindly.
10. Web previews through an SSH tunnel
For web previews, such as a local development server started by Claude, it is tempting to simply open another port. You should not do that. Every additional open port is additional attack surface. Instead, the preview runs through an encrypted SSH port tunnel: the service listens only locally on the server, and SSH forwards it to the client.
Make a service running locally on port 4321 accessible from the PC:
ssh -p 61417 -L 4321:localhost:4321 claude@SERVER
Then open http://localhost:4321 in the local browser. All traffic runs through the existing, authenticated SSH connection, without having to open even one additional port in the firewall.
Access from an iPhone
Access while on the go follows the same security model as from the PC. You only need an SSH client with key management. Common options are Termius, Blink Shell, and Secure ShellFish; all can generate Ed25519 keys and store them in the iOS keychain, in some cases protected by Face ID.
The process corresponds to step 3, only on the iPhone:
- Generate a dedicated Ed25519 key for the iPhone in the SSH client; do not copy the PC key. The private key remains in the device keychain.
- Add the iPhone’s public key as an additional line in
~/.ssh/authorized_keyson the server, with a descriptive comment (iphone-15). - Create the connection in the client: server address, user
claude, port61417, and the iPhone key for authentication.
This is exactly why a separate key per device is worthwhile: if the iPhone is lost, delete the one iphone-15 line from authorized_keys on the server, and that device is locked out while PC access and all other keys continue unaffected.
After connecting, resume the running Claude session with tmux attach -t claude and continue where you left off at your desk. The port tunnel from step 10 also works from iOS; Termius and Secure ShellFish support port forwarding.
Checklist
In summary, the complete process:
- Debian 13 installed and fully updated with
apt full-upgrade. - Dedicated user
claudewith sudo privileges; direct root login no longer used. - Passphrase-protected Ed25519 keys, one per device, with only public keys in
authorized_keys. - sshd hardened:
PermitRootLogin no,PasswordAuthentication no; checked withsshd -tbefore reloading, and left the existing session open until testing was complete. - SSH moved to port 61417, set on
ssh.socketwhen socket activation is used, otherwise in the sshd configuration. - Provider firewall: inbound default DROP, sole exception TCP 61417; outbound allowed.
- Attack surface checked with
ss -lntup(only SSH public,chronydlocal) andsystemctl --failed(no errors). - Claude Code freshly authenticated on the server, running in a
tmuxsession. - Data hygiene: no private keys or credentials on the server; configuration reviewed through a migration folder first.
- No additional ports; web previews run through an SSH tunnel.
After this setup, only SSH on the specified port is reachable from the outside, and even there only with a passphrase-protected key. Claude Code runs independently of the endpoint device in tmux; web previews remain accessible through SSH tunnels without opening an additional port.
Change log
- October 25, 2026: Corrected: Claude Code does not require a separate Node.js runtime environment; added the recommended native installer and the signed apt repository for Debian. Updated source links to the new Claude Code documentation addresses.
Comments
Comments are loaded from GitHub / Giscus.