OCI SSH and port forwarding

 How to Lock Down an Oracle Cloud (OCI) Ubuntu VM with SSH Keys, WireGuard VPN, and firewalld

Public cloud servers are scanned by automated bots within minutes of going online. Leaving Port 22 (SSH) or internal web dashboards exposed to 0.0.0.0/0 invites constant brute-force attacks—and locking SSH down to a single home IP breaks every time your ISP rotates your dynamic IP address.
This complete, production-tested guide walks you through hardening an Oracle Cloud Infrastructure (OCI) Ubuntu VM from start to finish:
  1. Enforcing SSH key-only authentication and blocking root login.
  2. Setting up a full-tunnel WireGuard VPN so your PC keeps full internet access from any network while connecting to a permanent private IP (10.8.0.1).
  3. Configuring firewalld and OCI Security Lists so Port 22 and internal dashboards (like OpenClaw or Ollama) are completely invisible to the public internet.
  4. Accessing localhost-bound dashboards securely via SSH port forwarding.

Architecture Overview
Once configured, your connection flow looks like this:
  • Public Internet → OCI VM: Only UDP Port 51820 (WireGuard) is reachable. TCP Port 22 and internal app ports are completely closed.
  • Your PC → WireGuard Tunnel (10.8.0.2 ↔ 10.8.0.1): All your PC's traffic routes securely through the VM (giving you full internet access), and SSH is permitted only inside the wg0 tunnel.
  • SSH Tunnel → Local Dashboards: Services bound to 127.0.0.1 on the VM are accessed cleanly on your PC via http://localhost:<port>.

Step 1: Fix & Harden SSH Authentication
1. Ensure Your Public Key Is in authorized_keys
In OCI, downloading a new key pair from the web console after an instance is already created does not push the public key into the running VM. If you ever need to add a key manually:
  1. Extract the public key string from your private .key file on your local PC:
    bash
    ssh-keygen -y -f /path/to/your-private-key.key
    
    Use code with caution.
  2. Append that ssh-rsa ... (or ssh-ed25519 ...) output to ~/.ssh/authorized_keys on the VM and enforce strict permissions:
    bash
    mkdir -p ~/.ssh
    echo "PASTE_YOUR_PUBLIC_KEY_HERE" >> ~/.ssh/authorized_keys
    chmod 700 ~/.ssh
    chmod 600 ~/.ssh/authorized_keys
    chown -R ubuntu:ubuntu ~/.ssh
    
    Use code with caution.
2. Disable Password Login and Root Access
On modern Ubuntu (24.04+), drop-in files in /etc/ssh/sshd_config.d/ (such as 60-cloudimg-settings.conf) are evaluated in alphabetical order—first match wins. Create a 00-hardening.conf file so your rules load first and override cloud-init defaults:
bash
sudo tee /etc/ssh/sshd_config.d/00-hardening.conf > /dev/null << 'EOF'
# Enforce SSH key authentication only
PubkeyAuthentication yes
PasswordAuthentication no
KbdInteractiveAuthentication no
ChallengeResponseAuthentication no
UsePAM yes

# Block direct root login and restrict allowed users
PermitRootLogin no
AllowUsers ubuntu

# Harden session limits
MaxAuthTries 3
LoginGraceTime 20
X11Forwarding no
AllowAgentForwarding no
EOF
Use code with caution.
Test the syntax, restart SSH, and verify the active configuration:
bash
sudo sshd -t && sudo systemctl restart ssh
sudo sshd -T | grep -E "passwordauthentication|permitrootlogin|pubkeyauthentication|allowusers"
Use code with caution.
Confirm the output shows passwordauthentication no and permitrootlogin no.

Step 2: Open UDP Port 51820 in OCI Console
Before installing WireGuard, allow WireGuard's UDP port in your VCN firewall:
  1. Open OCI Console → Compute → Instances → click your instance.
  2. Click your Subnet → click your Security List (e.g., Default Security List).
  3. Click Add Ingress Rules:
    • Source Type: CIDR
    • Source CIDR: 0.0.0.0/0
    • IP Protocol: UDP
    • Destination Port Range: 51820
  4. Click Add Ingress Rules. (Keep TCP Port 22 open until you confirm WireGuard works).

Step 3: Set Up a Full-Tunnel WireGuard VPN on the VM
1. Install WireGuard and Generate Keys
Run these commands on your OCI VM:
bash
sudo apt update && sudo apt install -y wireguard

wg genkey | tee ~/server_private.key | wg pubkey > ~/server_public.key
wg genkey | tee ~/client_private.key | wg pubkey > ~/client_public.key
chmod 600 ~/*.key
Use code with caution.
2. Create the Server Configuration (/etc/wireguard/wg0.conf)
Detect your VM's primary network interface (commonly ens3 or enp0s6 on OCI) and generate wg0.conf:
bash
SERVER_PRIV=$(cat ~/server_private.key)
CLIENT_PUB=$(cat ~/client_public.key)
NET_IF=$(ip route get 8.8.8.8 | awk '{print $5; exit}')

sudo tee /etc/wireguard/wg0.conf > /dev/null << EOF
[Interface]
Address = 10.8.0.1/24
ListenPort = 51820
PrivateKey = $SERVER_PRIV
PostUp = sysctl -w net.ipv4.ip_forward=1; iptables -I INPUT 1 -p udp --dport 51820 -j ACCEPT; iptables -A FORWARD -i %i -j ACCEPT; iptables -A FORWARD -o %i -j ACCEPT; iptables -t nat -A POSTROUTING -o $NET_IF -j MASQUERADE
PostDown = iptables -D INPUT -p udp --dport 51820 -j ACCEPT; iptables -D FORWARD -i %i -j ACCEPT; iptables -D FORWARD -o %i -j ACCEPT; iptables -t nat -D POSTROUTING -o $NET_IF -j MASQUERADE

[Peer]
PublicKey = $CLIENT_PUB
AllowedIPs = 10.8.0.2/32
EOF

sudo chmod 600 /etc/wireguard/wg0.conf
Use code with caution.
(Note: Including iptables -I INPUT 1 -p udp --dport 51820 -j ACCEPT ensures Oracle's default host iptables REJECT rules do not block WireGuard handshakes).
3. Enable IP Forwarding & Start WireGuard
bash
echo "net.ipv4.ip_forward=1" | sudo tee /etc/sysctl.d/99-wireguard.conf
sudo sysctl --system
sudo systemctl enable --now wg-quick@wg0
Use code with caution.
4. Export Your Client Config
Print the client configuration block (replace <YOUR_VM_PUBLIC_IP> with your instance's public IP):
bash
cat << EOF
[Interface]
PrivateKey = $(cat ~/client_private.key)
Address = 10.8.0.2/24
DNS = 1.1.1.1, 8.8.8.8
MTU = 1380

[Peer]
PublicKey = $(cat ~/server_public.key)
Endpoint = <YOUR_VM_PUBLIC_IP>:51820
AllowedIPs = 0.0.0.0/0
PersistentKeepalive = 25
EOF
Use code with caution.
Copy that entire block, then remove the temporary key files from your VM:
bash
rm -f ~/server_*.key ~/client_*.key
Use code with caution.

Step 4: Connect Your Local PC to the VPN
On your local Ubuntu/Linux PC:
  1. Install WireGuard and resolvconf:
    bash
    sudo apt update && sudo apt install -y wireguard resolvconf
    
    Use code with caution.
  2. Save the client config you copied into /etc/wireguard/oci-vpn.conf:
    bash
    sudo nano /etc/wireguard/oci-vpn.conf
    sudo chmod 600 /etc/wireguard/oci-vpn.conf
    
    Use code with caution.
  3. Bring up the tunnel:
    bash
    sudo wg-quick up oci-vpn
    
    Use code with caution.
  4. Verify your internet routes through the VM and SSH works over the private tunnel IP (10.8.0.1):
    bash
    curl -4 ifconfig.me
    # Should return your OCI VM's public IP
    
    ssh -i /path/to/your-private-key.key ubuntu@10.8.0.1
    
    Use code with caution.
    (To disconnect later, run sudo wg-quick down oci-vpn).

Step 5: Lock Down firewalld & Close Public Port 22
Oracle Ubuntu images ship with firewalld enabled at boot (which also integrates cleanly with Docker without conflicts). Configure firewalld on your VM so SSH (22/tcp) and internal apps are accessible only via the wg0 interface:
bash
# Ensure firewalld is running
sudo systemctl enable --now firewalld

# Allow WireGuard UDP traffic publicly
sudo firewall-cmd --permanent --zone=public --add-port=51820/udp

# Enable NAT masquerading so VPN clients have full internet access
sudo firewall-cmd --permanent --zone=public --add-masquerade

# Place wg0 in the trusted zone (permits SSH on 22/tcp & forwarding)
sudo firewall-cmd --permanent --zone=trusted --add-interface=wg0

# Remove SSH (and any private app ports like 11434 or 18789) from public
sudo firewall-cmd --permanent --zone=public --remove-service=ssh

# Reload to apply
sudo firewall-cmd --reload
Use code with caution.
Verify your active zones:
bash
sudo firewall-cmd --zone=public --list-all
sudo firewall-cmd --zone=trusted --list-all
Use code with caution.
Finally, go back to the OCI Console → VCN → Security List → Ingress Rules and delete the TCP Port 22 (0.0.0.0/0) rule, leaving only UDP Port 51820 open.

Step 6: Access Localhost Dashboards Cleanly via ~/.ssh/config
Many self-hosted tools (such as OpenClaw on port 18789 or Ollama on port 11434) bind strictly to 127.0.0.1 for security. Instead of exposing them to the network, map them automatically using SSH LocalForward in ~/.ssh/config on your PC:
bash
cat << 'EOF' >> ~/.ssh/config
Host oci
    HostName 10.8.0.1
    User ubuntu
    IdentityFile /path/to/your-private-key.key
    # Forward VM localhost ports to your PC
    LocalForward 18790 127.0.0.1:18789
    LocalForward 11434 127.0.0.1:11434
EOF
chmod 600 ~/.ssh/config
Use code with caution.
(Tip: If a local port like 18789 is already in use on your PC, map it to a free local port like 18790 as shown above).
Now, whenever WireGuard is active:
  • Run ssh oci to open a shell and tunnel all your dashboards (http://localhost:18790, http://localhost:11434, etc.).
  • Or run ssh -N oci to keep the tunnels active in the background without opening a terminal shell.

Final Security Checklist
  • Public Port Scan: With WireGuard disconnected (sudo wg-quick down oci-vpn), ssh ubuntu@<PUBLIC_IP> should completely time out.
  • Password Brute-Force: PasswordAuthentication no and PermitRootLogin no ensure no password prompts are ever served, even inside the VPN.
  • Emergency Recovery: If you ever misconfigure WireGuard or firewalld, you can still access the VM anytime via OCI Console → Compute → Instances → Console Connection (Cloud Shell).

Comments

Popular posts from this blog

HOW TO INSTALL DOCKER IN UBUNTU Ubuntu 22.04 LTS

Fixing Git Portable SSL Certificate Errors on Windows

Installing VM Ware Workstation required tools from build vmmon and vmnet