Encrypted backup key icon

When you set up SSH key-based authentication, you typically add your SSH public key to the authorized_keys file on the remote server. This allows you to authenticate without having to enter a password every time you connect to the server.

Using two authorized_keys files, one main and one backup, ensures that you always have a working copy of the file. If you accidentally modify or delete the main authorized_keys file, you can simply use the backup file to restore it.

Protecting the backup file from accidental modifications or deletions is also important. This can be done by setting the immutable flag on the file, as described in the previous example. This prevents the file from being modified or deleted, even by the root user.

Step-by-step guide:
  1. Create a ~/.ssh directory if it doesn't exist already, and navigate to it:
  2. mkdir -p ~/.ssh && cd ~/.ssh
  3. Generate a new ed25519 key pair:
  4. ssh-keygen -t ed25519 -C "Your comment here"
  5. Create a authorized_keys_main file:
  6. touch authorized_keys_main
  7. Copy your SSH public key to the authorized_keys_main file. You can do this by opening the file in a text editor and pasting your public key on a new line, or by using the ssh-copy-id command:
  8. ssh-copy-id -i ~/.ssh/id_ed25519.pub user@server
  9. Create a backup of the authorized_keys_main file and name it authorized_keys_backup:
  10. cp authorized_keys_main authorized_keys_backup
  11. Set the execute permission on the authorized_keys_backup file:
  12. chmod +x authorized_keys_backup
  13. Set the immutable flag on the authorized_keys_backup file using the chattr command:
  14. sudo chattr +i authorized_keys_backup

    Note: you may need to use sudo to run the chattr command, depending on your system configuration.

  15. Verify that the authorized_keys_backup file is protected by trying to delete or modify it:
  16. rm authorized_keys_backup

    You should see an error message indicating that the file is protected.