LAB 68 ‐ Create Console Connection to Troubleshoot OPC Key Lost Issue - DevWithNehaCloud/Oracle-Cloud-Infrastructure-wiki GitHub Wiki

Introduction

This lab demonstrates how to recover access to an Oracle Linux instance when the SSH private key for the opc user is lost.

Instead of creating a new instance, we use an OCI Serial Console Connection to access the server, enter GRUB recovery mode, boot into a Bash shell, generate a new SSH key pair, update the authorized_keys file, and regain SSH access using a new private key.

This is a common troubleshooting scenario used by OCI administrators in production environments.


Lab Objective

In this lab, we will:

  • Create a Console Connection
  • Connect using OCI Cloud Shell
  • Reboot the instance
  • Access the GRUB boot menu
  • Boot into recovery mode using init=/bin/bash
  • Access the Bash recovery shell
  • Generate a new SSH key pair
  • Replace the old SSH public key
  • Reboot the instance
  • Log in using the new private key

Environment Details

Component Value
Cloud Provider Oracle Cloud Infrastructure (OCI)
Operating System Oracle Linux 9.8
Access Method Serial Console Connection
User opc
Recovery Method GRUB + init=/bin/bash

Step 1: Open the OCI Instance

Navigate to:

OCI Console
→ Compute
→ Instances
→ Select Target Instance

Verify:

  • Instance State = Running
  • Console Connection Permission Available

Step 2: Launch Console Connection

Open:

Resources
→ Console Connections

Click:

Launch Cloud Shell Connection

Note: Use Cloud Shell Connection instead of Local Connection for this lab.


Step 3: Open Console Session

Cloud Shell opens automatically.

Press:

Enter
Enter
Enter

until console output appears.


Step 4: Reboot the Instance

Navigate to:

Instance
→ More Actions
→ Reboot

Wait for the reboot process to begin.


Step 5: Access the GRUB Boot Menu

As soon as the reboot starts:

Press:

ESC

repeatedly until the GRUB menu appears.

Example:

Oracle Linux Server 9.8
Advanced options for Oracle Linux

Step 6: Edit the Boot Entry

Highlight the first boot entry.

Press:

e

to edit the boot configuration.


Step 7: Locate the Linux Boot Line

Scroll down until you find a line similar to:

linux ($root)/vmlinuz-...

Example:

linux ($root)/vmlinuz-0-rescue-xxxxxxxx

Step 8: Boot into Recovery Mode

At the end of the Linux boot line add:

init=/bin/bash

Example:

linux ($root)/vmlinuz-0-rescue-xxxxxxxx init=/bin/bash

Boot using:

Ctrl + X

Step 9: Verify Recovery Shell Access

After booting, the system loads a Bash shell.

Expected output:

bash-5.1#

This confirms that the server has booted into recovery mode successfully.


Step 10: Remount the Root Filesystem

Enable read-write access:

mount -o remount,rw /

Verify:

mount | grep " / "

Step 11: Load SELinux Policy

Execute:

/usr/sbin/load_policy -i

Step 12: Navigate to the OPC SSH Directory

Run:

cd /home/opc/.ssh

Verify the SSH key file:

ls -l

Example:

authorized_keys

Step 13: Backup the Existing Authorized Key

Create a backup:

cp authorized_keys authorized_keys.bak

Rename the original file:

mv authorized_keys authorized_keys.old

Verify:

ls -l

Expected:

authorized_keys.bak
authorized_keys.old

Step 14: Generate a New SSH Key Pair

Generate a new RSA key pair:

ssh-keygen -t rsa -b 4096

Press Enter for all prompts.

Example:

Enter file in which to save the key:
/root/.ssh/id_rsa

Enter passphrase:
Enter

Confirm passphrase:
Enter

Output:

Your identification has been saved in:

/root/.ssh/id_rsa

Your public key has been saved in:

/root/.ssh/id_rsa.pub

Step 15: Replace the OPC Public Key

Copy the newly generated public key into the OPC user's authorized keys file:

cat /root/.ssh/id_rsa.pub > /home/opc/.ssh/authorized_keys

Verify:

cat /home/opc/.ssh/authorized_keys

Step 16: Fix File Permissions

Apply the correct permissions:

chmod 600 /home/opc/.ssh/authorized_keys

chown opc:opc /home/opc/.ssh/authorized_keys

Verify:

ls -l /home/opc/.ssh

Expected:

-rw------- authorized_keys

Step 17: Reboot the Instance

Reboot the server:

reboot -f

or

/usr/sbin/reboot -f

The instance exits recovery mode and starts normally.


Step 18: Save the New Private Key

Display the newly generated private key:

cat /root/.ssh/id_rsa

Copy the entire key and save it locally.

Example:

-----BEGIN OPENSSH PRIVATE KEY-----
...
-----END OPENSSH PRIVATE KEY-----

Save it as:

new-opc-key

Step 19: Convert the Key to PPK Format

Open:

PuTTYgen

Select:

Conversions
→ Import Key

Import:

new-opc-key

Leave Passphrase blank.

Click:

Save Private Key

Save as:

new-opc-key.ppk

Step 20: Login Using the New Key

Open PuTTY.

Configure:

Host Name = <Public IP>
User = opc
Private Key = new-opc-key.ppk

Connect to the server.

Successful login confirms that SSH access has been recovered.


Result

Successfully recovered access to an Oracle Linux instance after losing the original SSH private key by using an OCI Serial Console Connection and GRUB recovery mode.


Interview Question

What would you do if the SSH key for an OCI Linux instance is lost?

Answer:

I would create a Serial Console Connection, reboot the instance, access the GRUB boot menu, modify the boot entry by adding init=/bin/bash, boot into recovery mode, generate a new SSH key pair, replace the opc user's authorized_keys file, reboot the instance, and then log in using the newly generated private key.

STEPS -

image

1.click on instance

image

2.click enter 2 or 3 times you will get the login prompt

image

3.Duplicate the browser tab and keep the Cloud Shell session open. In the second tab, reboot the instance. Immediately return to the Cloud Shell console and press Esc repeatedly during startup until the GRUB boot menu appears.

image image

Accessing the Correct GRUB Menu

Initially, after rebooting the instance and repeatedly pressing the Esc key, I was redirected to the GRUB command prompt (grub>) instead of the GRUB boot menu.

The grub> prompt does not allow editing the Linux boot entry required for recovery using init=/bin/bash.

The below screen shows the GRUB command prompt (grub>), which is not the menu required for SSH key recovery.

image

To access the correct GRUB boot menu, I rebooted the instance again. During the boot process, instead of continuously pressing Esc, I pressed the Up Arrow and Down Arrow keys repeatedly. This paused the automatic boot sequence and allowed me to capture the actual GRUB boot menu.

From this menu, I was able to select the Oracle Linux boot entry and press e to edit the boot configuration.

The below screen shows the actual GRUB boot menu used for troubleshooting and recovery. From this menu, the boot entry can be edited by pressing e and adding init=/bin/bash to boot into recovery mode.

image

Press e to edit the GRUB boot entry. Navigate to the Linux boot line, add init=/bin/bash at the end of the line, and press Ctrl + X to boot into recovery mode.

image

After pressing Ctrl + X, the instance booted using the modified GRUB configuration and entered recovery mode.

A Bash shell prompt appeared:

bash-5.1#

This confirms that the instance has successfully booted into the recovery shell.

From this recovery environment, administrative tasks such as resetting SSH keys, modifying system files, updating configurations, and troubleshooting boot issues can be performed without requiring normal SSH access.

bash-5.1#

image

### Prepare the Recovery Environment

Execute the following commands to remount the root filesystem in read-write mode, load the required system policies, and access the SSH configuration directory for key recovery.

mount -o remount,rw /
mount | grep " / "
/usr/sbin/load_policy -i
cd /home/opc/.ssh
ls -l
image

taking the backup of the existing key

cp authorized_keys authorized_keys.bak
ls -l
image

Generate a New SSH Key Pair

Generate a new RSA SSH key pair from the recovery shell using the following command:

ssh-keygen -t rsa -b 4096

press enter again enter

image

Copy the Public Key

Display and copy the newly generated public key:

cat /root/.ssh/id_rsa.pub
image

Backup the Existing SSH Key

Create a backup of the current authorized_keys file before replacing it with the new public key.

cd /home/opc/.ssh
cp authorized_keys authorized_keys.bak
mv authorized_keys authorized_keys.old
ls -l
image

Update the Authorized Keys File

Replace the existing SSH public key with the newly generated public key.

cat /root/.ssh/id_rsa.pub > /home/opc/.ssh/authorized_keys

now verify

cat /home/opc/.ssh/authorized_keys
image

Verify the Updated Public Key

Verify that the contents of the authorized_keys file match the newly generated public key.

The output should be identical to the contents of /root/.ssh/id_rsa.pub, confirming that the new public key has been successfully updated.

Permission Fix

chmod 600 /home/opc/.ssh/authorized_keys
chown opc:opc /home/opc/.ssh/authorized_keys

Interview Answer

chmod 600

Sets secure permissions on the authorized_keys file so that only the owner can read and modify it.

chown opc:opc

Changes the ownership of the authorized_keys file to the opc user and group, ensuring SSH authentication works correctly for the OPC account.

verify it

ls -l /home/opc/.ssh
image

Before Reboot

Run:

cat /root/.ssh/id_rsa

copy the private key and save it to notepad

image

Convert the Private Key to PPK Format

Open PuTTYgen and select:

Conversions → Import Key

Import the saved private key file and click Save Private Key.

Save the file as:

new-opc-key.ppk

No passphrase is required because no passphrase was configured during SSH key generation (Enter was pressed for all prompts).

This creates a new .ppk file that can be used for PuTTY authentication.

now reboot the server

/usr/sbin/reboot -f
image

Verify SSH Key Recovery

The previous PuTTY session will no longer work because it is still using the old private key.

When attempting to reconnect, you may receive an error such as:

Server refused our key

or

Key mismatch

Open a new PuTTY session and configure it to use the newly created:

new-opc-key.ppk

Connect using the new private key to regain SSH access to the instance.

login successful with new opc private key

image

Note to remember :- PuTTY uses the private key for authentication, but the corresponding public key must be present in the server's authorized_keys file. During SSH authentication, the server validates the private key against the stored public key. Therefore, after generating a new key pair, I updated the authorized_keys file with the new public key and used the matching private key in PuTTY.

⚠️ **GitHub.com Fallback** ⚠️