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:~/.ssh
directory if it doesn't exist already, and navigate to it:mkdir -p ~/.ssh && cd ~/.ssh
ssh-keygen -t ed25519 -C "Your comment here"
authorized_keys_main
file:touch authorized_keys_main
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:ssh-copy-id -i ~/.ssh/id_ed25519.pub user@server
authorized_keys_main
file and name it authorized_keys_backup
:cp authorized_keys_main authorized_keys_backup
authorized_keys_backup
file:chmod +x authorized_keys_backup
authorized_keys_backup
file using the chattr
command:sudo chattr +i authorized_keys_backup
Note: you may need to use sudo
to run the chattr
command, depending on your system configuration.
authorized_keys_backup
file is protected by trying to delete or modify it:rm authorized_keys_backup
You should see an error message indicating that the file is protected.