- Download the script and make a folder called keys
- add all your keys to your newly created key folder
- add the servers to line 27's array [container id]="username"
ex:
servers+=( [235]="root" [421]="root" [123]="root" [124]="root" [321]="noroot" )
- Thats it! If your keys are correct and you had ssh enabled, it should work :D
Last active
February 6, 2022 19:52
-
-
Save Hackerjef/4fe50d3bd20ca44e6979941df2d34a4f to your computer and use it in GitHub Desktop.
Add many ssh keys to proxmox containers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# add_keys.sh - add public keys to many proxmox containers | |
# Copyright (C) 2022 Nadie (August S.) | |
# This program is free software: you can redistribute it and/or modify | |
# it under the terms of the GNU General Public License as published by | |
# the Free Software Foundation, either version 3 of the License, or | |
# (at your option) any later version. | |
# This program is distributed in the hope that it will be useful, | |
# but WITHOUT ANY WARRANTY; without even the implied warranty of | |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
# GNU General Public License for more details. | |
# You should have received a copy of the GNU General Public License | |
# along with this program. If not, see <https://www.gnu.org/licenses/>. | |
set -e | |
if ! [ $(id -u) = 0 ]; then | |
echo "Please run script as root" | |
exit 1 | |
fi | |
declare -A servers | |
servers+=( ) | |
tmpfile=$(mktemp /tmp/copykeys.XXXXXX) | |
for keyfile in keys/*; do | |
echo "# keyfile: $keyfile" >> "$tmpfile" | |
echo "$(cat "$keyfile")" >> "$tmpfile" | |
echo -e "\n" >> "$tmpfile" | |
done | |
echo "Keys to add:" | |
cat "$tmpfile" | |
for key in ${!servers[@]}; do | |
UENV="" | |
HOMEDIR="" | |
echo "Processing ${servers[${key}]}@$key" | |
UENV=$(pct exec "$key" getent passwd "${servers[${key}]}") | |
HOMEDIR=$(echo ${UENV} | cut -f6 -d:) | |
echo "${servers[${key}]}:${servers[${key}]} | $HOMEDIR" | |
pct exec $key -- mkdir -p "$HOMEDIR/.ssh" | |
pct push "$key" "$tmpfile" "$HOMEDIR/.ssh/authorized_keys" | |
pct exec $key -- chmod 700 "$HOMEDIR/.ssh" | |
pct exec $key -- chmod 600 "$HOMEDIR/.ssh/authorized_keys" | |
pct exec $key -- chown -R "${servers[${key}]}":"${servers[${key}]}" "$HOMEDIR/.ssh" | |
echo "Added keys to ct $key" | |
done | |
rm "$tmpfile" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment