> ## Documentation Index
> Fetch the complete documentation index at: https://docs.gcore.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Recover SSH access in recovery mode

Recovery mode opens a root shell on a Linux Virtual Machine or Bare Metal server from the console when the private key is lost and no console password is set. [Install a public key](#install-a-public-key) when SSH must accept a new key.

<Steps>
  <Step title="Open the server">
    <Tabs>
      <Tab title="Virtual Machine">
        Log in to the [Gcore Customer Portal](https://portal.gcore.com) and navigate to **Cloud** > **Virtual Instances**. Open the Virtual Machine.

        <Frame>
          <img src="https://mintcdn.com/gcore/Fvzh2QEH6v_ao85Z/images/docs/cloud/virtual-instances/troubleshooting/recover-ssh-access-via-recovery-mode/access-to-console.png?fit=max&auto=format&n=Fvzh2QEH6v_ao85Z&q=85&s=a84d67c828d47b60fff056fab6ba01c9" alt="Virtual Instance page with Access to Console and the ubuntu login" width="980" height="200" data-path="images/docs/cloud/virtual-instances/troubleshooting/recover-ssh-access-via-recovery-mode/access-to-console.png" />
        </Frame>
      </Tab>

      <Tab title="Bare Metal">
        Log in to the [Gcore Customer Portal](https://portal.gcore.com) and navigate to **Cloud** > **Bare Metal**. Open the Bare Metal server.

        <Frame>
          <img src="https://mintcdn.com/gcore/Fvzh2QEH6v_ao85Z/images/docs/cloud/virtual-instances/troubleshooting/recover-ssh-access-via-recovery-mode/bare-metal-access-to-console.png?fit=max&auto=format&n=Fvzh2QEH6v_ao85Z&q=85&s=1b9c5756d3a4f70e598f83c3489118a2" alt="Bare Metal page with Access to Console and the ubuntu login" width="980" height="210" data-path="images/docs/cloud/virtual-instances/troubleshooting/recover-ssh-access-via-recovery-mode/bare-metal-access-to-console.png" />
        </Frame>
      </Tab>
    </Tabs>

    The login beside **Access to Console** is the account SSH accepts. On the Ubuntu image that account is `ubuntu`.
  </Step>

  <Step title="Open the console">
    <Frame>
      <img src="https://mintcdn.com/gcore/Fvzh2QEH6v_ao85Z/images/docs/cloud/virtual-instances/troubleshooting/recover-ssh-access-via-recovery-mode/server-page-access-to-console.png?fit=max&auto=format&n=Fvzh2QEH6v_ao85Z&q=85&s=2bbd703fb150bb2f9da79c5ceadad518" alt="Bare Metal server page with Access to Console beside the IP address" width="1794" height="661" data-path="images/docs/cloud/virtual-instances/troubleshooting/recover-ssh-access-via-recovery-mode/server-page-access-to-console.png" />
    </Frame>

    Click **Access to Console** to open the console in a browser tab, with **Send CtrlAltDel** at the top right.

    <Note>
      Not every Bare Metal platform provides **Access to Console**.
    </Note>

    <Frame>
      <img src="https://mintcdn.com/gcore/Fvzh2QEH6v_ao85Z/images/docs/cloud/virtual-instances/troubleshooting/recover-ssh-access-via-recovery-mode/console-connected.png?fit=max&auto=format&n=Fvzh2QEH6v_ao85Z&q=85&s=8f6f0c6911f1bed143ebf3a012611631" alt="VNC console with Send CtrlAltDel and the Ubuntu login" width="1097" height="226" data-path="images/docs/cloud/virtual-instances/troubleshooting/recover-ssh-access-via-recovery-mode/console-connected.png" />
    </Frame>
  </Step>

  <Step title="Open the GRUB menu">
    Open the boot menu before the login prompt:

    1. Click the console screen so keystrokes reach the server.
    2. Click **Send CtrlAltDel**.
    3. Press Escape until **GNU GRUB** appears.

    <Warning>
      The GRUB menu stays hidden, and Escape registers only during `Booting from Hard Disk...`. If the Ubuntu login prompt appears, Escape missed the menu, so click the console screen and repeat this step.
    </Warning>

    <Frame>
      <img src="https://mintcdn.com/gcore/Fvzh2QEH6v_ao85Z/images/docs/cloud/virtual-instances/troubleshooting/recover-ssh-access-via-recovery-mode/grub-menu.png?fit=max&auto=format&n=Fvzh2QEH6v_ao85Z&q=85&s=654ad00715f744cefb5ece3f4b1b035f" alt="GNU GRUB menu with Ubuntu and Advanced options for Ubuntu" width="783" height="498" data-path="images/docs/cloud/virtual-instances/troubleshooting/recover-ssh-access-via-recovery-mode/grub-menu.png" />
    </Frame>
  </Step>

  <Step title="Boot recovery mode">
    On an Ubuntu cloud image, select **Advanced options for Ubuntu** and press Enter. Then select the entry that ends with `(recovery mode)` and press Enter.

    <Frame>
      <img src="https://mintcdn.com/gcore/Fvzh2QEH6v_ao85Z/images/docs/cloud/virtual-instances/troubleshooting/recover-ssh-access-via-recovery-mode/grub-recovery.png?fit=max&auto=format&n=Fvzh2QEH6v_ao85Z&q=85&s=e23c5919303c1fc58c2e3b3231711076" alt="GRUB advanced options with the recovery mode entry highlighted" width="780" height="458" data-path="images/docs/cloud/virtual-instances/troubleshooting/recover-ssh-access-via-recovery-mode/grub-recovery.png" />
    </Frame>
  </Step>

  <Step title="Open a root shell">
    In **Recovery Menu (filesystem state: read-only)**, select **root** (**Drop to root shell prompt**) and press Enter.

    <Frame>
      <img src="https://mintcdn.com/gcore/Fvzh2QEH6v_ao85Z/images/docs/cloud/virtual-instances/troubleshooting/recover-ssh-access-via-recovery-mode/recovery-menu-root.png?fit=max&auto=format&n=Fvzh2QEH6v_ao85Z&q=85&s=4d2777cc699978b0e54026acd58f99e1" alt="Recovery menu with Drop to root shell prompt highlighted" width="784" height="458" data-path="images/docs/cloud/virtual-instances/troubleshooting/recover-ssh-access-via-recovery-mode/recovery-menu-root.png" />
    </Frame>

    At `Press Enter for system maintenance (or press Control-D to continue):`, press Enter. The menu frame can remain on screen after the shell starts, while the shell is active once a prompt ending in `~#` appears at the bottom.
  </Step>
</Steps>

## Install a public key

Use this section when the server must accept a new SSH key. The root shell from the procedure above is already open.

<Steps>
  <Step title="Remount the root filesystem">
    Recovery mounts the root filesystem read-only, so remount it before writing the key:

    ```bash theme={null}
    mount -o remount,rw /
    ```

    When the command returns to the prompt without an error, the root filesystem accepts writes.

    <Frame>
      <img src="https://mintcdn.com/gcore/Fvzh2QEH6v_ao85Z/images/docs/cloud/virtual-instances/troubleshooting/recover-ssh-access-via-recovery-mode/remount-read-write.png?fit=max&auto=format&n=Fvzh2QEH6v_ao85Z&q=85&s=055f35ef6eca8f8a7c5aa3a37502f76b" alt="Root shell after remounting the filesystem read-write" width="973" height="235" data-path="images/docs/cloud/virtual-instances/troubleshooting/recover-ssh-access-via-recovery-mode/remount-read-write.png" />
    </Frame>
  </Step>

  <Step title="Install the public key">
    Write the new public key for the login beside **Access to Console** into `authorized_keys` on the local disk. On Ubuntu the file is `/home/ubuntu/.ssh/authorized_keys`, and when the login label names another account, use that account's home directory.

    ```bash theme={null}
    printf '%s\n' 'PUBLIC_KEY' > /home/ubuntu/.ssh/authorized_keys
    ```

    Replace `PUBLIC_KEY` with the OpenSSH public key on one line.

    <Warning>
      The `>` operator replaces the file, so keys already in it stop authenticating.
    </Warning>
  </Step>

  <Step title="Set ownership and permissions">
    Set ownership and modes, then run `sync` before the reboot:

    ```bash theme={null}
    chown ubuntu:ubuntu /home/ubuntu/.ssh /home/ubuntu/.ssh/authorized_keys
    chmod 700 /home/ubuntu/.ssh
    chmod 600 /home/ubuntu/.ssh/authorized_keys
    sync
    ```

    Keep `/home/ubuntu` at mode `750`, because SSH succeeded with that mode on the Ubuntu image.
  </Step>

  <Step title="Reboot the server">
    Reboot into the default GRUB entry:

    ```bash theme={null}
    reboot
    ```
  </Step>

  <Step title="Confirm SSH access">
    From a client that holds the matching private key, connect with the login from the server page:

    ```bash theme={null}
    ssh -i /path/to/private_key ubuntu@INSTANCE_IP
    ```

    Replacing `authorized_keys` makes the previous private key fail with `Permission denied (publickey)`.
  </Step>
</Steps>

<Tip>
  Add [multiple SSH keys](/cloud/virtual-instances/troubleshooting/troubleshoot-issues-with-ssh-connection#lost-ssh-key-and-cannot-connect) at creation time. Set a console password in user data for a [Virtual Machine](/cloud/virtual-instances/customize-initial-setup-for-your-instance#set-a-password-for-a-virtual-machine) or a [Bare Metal server](/cloud/bare-metal-servers/connect-to-your-bare-metal-server-via-ssh#connect-with-a-password) so the console opens without the SSH key.
</Tip>
