summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2022-03-06 15:21:07 +0000
committerLars Wirzenius <liw@liw.fi>2022-03-06 15:21:07 +0000
commit86991387198200e5d0c066600175650fbcdbc952 (patch)
treedeb7ede92c323ee5fcb2839cb77a68c6bb0ff6a2
parentc1dd1e3685275c2ad9b537f23ed5c34be57b4abf (diff)
parent9b2ecee0ff83e38d2faac77a6247b3418d43ff98 (diff)
downloadv-i-86991387198200e5d0c066600175650fbcdbc952.tar.gz
Merge branch 'set-authz-keys-script' into 'main'
feat: add script to install SSH authorized_keys file See merge request larswirzenius/v-i!17
-rwxr-xr-xset-authorized-keys31
1 files changed, 31 insertions, 0 deletions
diff --git a/set-authorized-keys b/set-authorized-keys
new file mode 100755
index 0000000..2151bc9
--- /dev/null
+++ b/set-authorized-keys
@@ -0,0 +1,31 @@
+#!/bin/bash
+
+set -eu -o pipefail
+
+die() {
+ echo "ERROR: $*" 1>&2
+ exit 1
+}
+
+cleanup() {
+ umount "$drive" || true
+ rmdir "$mnt"
+}
+
+trap cleanup EXIT
+
+drive="$1"
+pubkey="$2"
+
+[ -e "$drive" ] || die "$drive does not exist"
+[ -e "$pubkey" ] || die "$pubkey does not exist"
+
+mnt="$(mktemp -d)"
+mount "$drive" "$mnt"
+if [ ! -e "$mnt/root/.ssh" ]; then
+ install -d "$mnt/root/.ssh"
+fi
+
+authz="$mnt/root/.ssh/authorized_keys"
+[ ! -e "$authz" ] || die "$authz already exists"
+install -m 0600 "$pubkey" "$authz"