Home / OCI

OCIChecklistBeginner

Add, rotate and revoke SSH keys on running OCI Linux instances: a checklist

The SSH key you gave at launch is only applied once. Use this checklist to add a key, rotate the opc key, give admins their own accounts and revoke access on a running OCI instance without locking yourself out.

Deepak Bhardwaj6 min read

Applies toOCI Compute Linux instances (Oracle Linux 7/8/9, Ubuntu, other cloud-init platform images), OpenSSH, OCI Bastion managed SSH sessions

The goal

You need to change who can SSH into an OCI Linux instance that is already running. Someone joined the team, someone left, or the original launch key was shared too widely. You want the change made today, without a reboot, and without losing your own access.

The key point: the public key you paste at launch is stored in instance metadata as ssh_authorized_keys. cloud-init copies it into the default user's ~/.ssh/authorized_keys on first boot only. After that, authorized_keys on the instance is the only thing that controls access. Changing the key in the console or metadata later does not change who can log in.

Check what was supplied at launch:

curl -s -H "Authorization: Bearer Oracle" \
  http://169.254.169.254/opc/v2/instance/metadata/ssh_authorized_keys

Before you change anything

  • Keep one working SSH session open for the whole change. Never close it until a fresh login with the new key has worked.
  • Know your default user: opc on Oracle Linux and most Oracle images, ubuntu on Ubuntu images. Other images vary. Check the image documentation.
  • Confirm a recovery path exists in case you lock yourself out: the instance console connection (serial console), or Run Command if the Oracle Cloud Agent's Compute Instance Run Command plugin is enabled.
  • Take a copy of the current file: cp -p ~/.ssh/authorized_keys ~/.ssh/authorized_keys.bak.$(date +%F).
Symptom after a change Cause Fix
Permission denied (publickey) for the new key Key pasted with line breaks, or wrong user Each key must be on one line. Check the user name
Works as root, fails as user Wrong ownership or mode on ~/.ssh chmod 700 ~/.ssh; chmod 600 ~/.ssh/authorized_keys and correct owner
Old PuTTY or old client rejected on OL9 Client only offers ssh-rsa (SHA-1) signatures Upgrade the client or use an ED25519 key
SELinux denial after copying the file in Wrong security context restorecon -Rv ~/.ssh

1. Create the new key on the admin's workstation

Generate keys on the person's own machine. The private key never goes to the server or into a ticket.

ssh-keygen -t ed25519 -a 64 -C "jsmith-2026-11" -f ~/.ssh/oci_jsmith
cat ~/.ssh/oci_jsmith.pub

Put a name and date in the comment (-C). That comment is how you find the key again when you revoke it.

2. Add a key for the default user

On the instance, as opc (or ubuntu):

umask 077
mkdir -p ~/.ssh
echo 'ssh-ed25519 AAAAC3Nza...REPLACE... jsmith-2026-11' >> ~/.ssh/authorized_keys
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys
restorecon -Rv ~/.ssh 2>/dev/null || true

Then test from a new terminal while the old session stays open:

ssh -i ~/.ssh/oci_jsmith -o IdentitiesOnly=yes opc@<instance-ip>

3. Better: one account per admin

A shared opc key can't be revoked for one person only. Give each admin a named account:

sudo useradd -m -s /bin/bash jsmith
sudo install -d -m 700 -o jsmith -g jsmith /home/jsmith/.ssh
echo 'ssh-ed25519 AAAAC3Nza...REPLACE... jsmith-2026-11' | \
  sudo tee /home/jsmith/.ssh/authorized_keys >/dev/null
sudo chown jsmith:jsmith /home/jsmith/.ssh/authorized_keys
sudo chmod 600 /home/jsmith/.ssh/authorized_keys

# Sudo through a drop-in file, validated before it is used
echo 'jsmith ALL=(ALL) NOPASSWD:ALL' | sudo tee /etc/sudoers.d/90-jsmith >/dev/null
sudo chmod 440 /etc/sudoers.d/90-jsmith
sudo visudo -cf /etc/sudoers.d/90-jsmith

Use -s /bin/bash and useradd/adduser as your distribution expects. On Ubuntu the sudo group is an alternative to a drop-in file.

Caution

Always run visudo -c on a new sudoers file. A syntax error in /etc/sudoers.d/ can break sudo for every user, including opc. Then your only way back is the serial console.

4. Rotate the shared launch key

When the original launch key has been passed around, replace it:

  1. Add the new key (step 2) and test a fresh login with it.

  2. List the keys and their comments so you remove the right line:

    ssh-keygen -lf ~/.ssh/authorized_keys
    
  3. Remove the old key by its comment or a unique part of the key:

    cp -p ~/.ssh/authorized_keys ~/.ssh/authorized_keys.bak.$(date +%F)
    grep -v 'old-launch-key-comment' ~/.ssh/authorized_keys.bak.$(date +%F) > ~/.ssh/authorized_keys
    ssh-keygen -lf ~/.ssh/authorized_keys
    
  4. Test the new key again in a new session. Then test that the old key fails:

    ssh -i ~/.ssh/old_launch_key -o IdentitiesOnly=yes opc@<instance-ip>   # expect: Permission denied
    

Warning

Some launch keys have an empty comment or the default user@host comment, so grep -v can match more lines than you meant. Check the ssh-keygen -lf output after every edit and before you close your working session.

5. Revoke access for someone who left

  • Remove their key lines from every user's authorized_keys, not just opc. Find them across the box:

    sudo grep -l 'jsmith' /root/.ssh/authorized_keys /home/*/.ssh/authorized_keys 2>/dev/null
    
  • If they had a named account, lock it and expire it: sudo usermod -L -e 1 jsmith. Delete it later with sudo userdel -r jsmith once you've checked for cron jobs and files you need.

  • Remove their sudoers drop-in: sudo rm /etc/sudoers.d/90-jsmith.

  • Look for active sessions and end them: who, then sudo pkill -KILL -u jsmith.

  • Check AuthorizedKeysFile in /etc/ssh/sshd_config and its .d includes. If it points to a non-default location, clean that file too:

    sudo sshd -T | grep -i authorizedkeysfile
    
  • Remove their OCI access as well (API keys, console user, Bastion permissions). See the OCI user offboarding checklist.

6. Locked out? Fix it without SSH

If no working key is left, you still have options that don't need SSH:

  • Run Command: if the Run Command plugin is enabled and the instance has the required policy, push a script that appends a key. Run Command executes as the ocarun user by default, so the script needs sudo rights configured for that user to write into /home/opc/.ssh.
  • Serial console: create an instance console connection, boot into single-user or emergency mode, and edit authorized_keys directly.
  • Rescue instance: stop the instance, detach the boot volume, attach it to another instance, fix the file, then reattach. Use this when the console route isn't possible.

Check the Run Command plugin status from the CLI:

oci instance-agent plugin list --compartment-id <compartment-ocid> \
  --instanceagent-id <instance-ocid> --output table

Prevent it next time

  • Prefer OCI Bastion managed SSH sessions for human access. The session carries a public key for a limited time-to-live, and access is governed by IAM policy instead of files spread across servers.
  • Give each admin a named account. Keep opc for break-glass use with a key stored in your secrets vault.
  • Manage authorized_keys and sudoers with configuration management (Ansible, cloud-init in your custom image) so revocation is one change, not a server-by-server hunt.
  • Put the owner and date in every key comment and review them each quarter with ssh-keygen -lf.
  • Add the SSH key steps to your offboarding runbook next to OCI API key and auth token removal.