4 ‐ Recover Access to an OCI Instance When SSH Keys Are Lost (Password Authentication Not Set) - SanjeevOCI/Ocidocs GitHub Wiki
Recover Access to an OCI Instance When SSH Keys Are Lost (Password Authentication Not Set)
Applies to: OCI Linux instances (opc user)
Goal: Regain access to a locked OCI instance if both the SSH key pair is lost and password authentication was never enabled.
🧭 Overview
There are two ways to regain access:
- Using Cloud Shell (recommended) — Create a Console Connection to the instance and access it through the Cloud Shell serial console.
- Using a healthy instance — If another instance exists in the same subnet, use it to reach the locked instance.
✅ Preferred Method: Cloud Shell (Console Connection)
This avoids boot volume detach/attach and preserves instance identity.
1️⃣ Using Cloud Shell (Recommended)
Step 1: Navigate to the Instance
- ☰ → Compute → Instances
- Select the problematic instance.
- Click Cloud Shell on the instance page.
Step 2: Open Cloud Shell Session
Click the Cloud Shell icon at the bottom of the OCI Console. This will open an interactive shell session in your browser, authenticated with your OCI identity.
Step 3: Create a Console Connection
- In the Instance Details page → Resources (left panel) → click Console Connections.
- Click Create Console Connection.
- Paste your new public key (from a keypair generated on your local machine or Cloud Shell).
- OCI will generate an SSH command to connect to the serial console using the matching private key.
Continue through the creation flow:
Step 4: SSH to Serial Console
OCI will display an SSH command. Run it exactly in Cloud Shell or locally, using the private key that matches the public key you provided earlier.
Once connected, you’ll get a serial console shell for the instance.
Step 5: Restore Access
You can now:
Option A — Add a new public key
sudo su - opc
mkdir -p ~/.ssh && chmod 700 ~/.ssh
echo "<your-new-public-key>" >> ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys
Option B — Temporarily enable password auth
sudo systemctl reload sshd
sudo passwd opc
✅ After you restore access, you can SSH normally to the instance again.
2️⃣ Using a Healthy Instance in the Same Subnet
- If Instance 2 (healthy) exists in the same VCN & subnet as Instance 1 (problematic), you can use Instance 2 to reach Instance 1:
- SSH into the healthy instance using your existing key.
- Use private IP of the problematic instance to access it internally, or mount/edit its boot volume if needed.
- Add your new public key to the authorized_keys file of the problematic instance user.
This method requires both instances to be reachable over the same subnet and proper NSG/SecList rules.
📝 Key Notes
- Cloud Shell Console Connection is safest and doesn't require stopping or detaching volumes.
- If using the helper instance method, remember to unmount and detach the volume properly before reattaching.
- Always test SSH with the new key after modification.
✅ Summary
We have regained access to a locked OCI instance after both the SSH key pair is lost and password authentication was never enabled.