Sample Header Ad - 728x90

bash + how to avoiding duplicate entries in authorized_keys (ssh) in bash

5 votes
1 answer
672 views
Popular methods of adding an ssh public key to a remote host’s authorized_keys file include using the ssh-copy-id command, and using bash operators such as >> to append to the file. An issue with ssh-copy-id is that this command does not check if a key already exists. This creates a hassle for scripts and automation because subsequent runs can add duplicate key entries. so I am using the following script ( on rhel server version 7.8 ) more /tmp/test.sh #!/bin/bash PUB_KEY= cat /root/.ssh/id_rsa.pub grep -q -F \"$PUB_KEY\" ~/.ssh/authorized_keys 2>/dev/null || echo \"$PUB_KEY\" >> ~/.ssh/authorized_keys I an not sure if this is the best approach , so I will appreciate to get other ideas out of the box*
Asked by user436442
Nov 3, 2020, 07:24 PM
Last activity: Jan 24, 2025, 09:21 PM