Skip to content

Commit f879bb2

Browse files
locus313Copilot
andauthored
Add initial version of sync script (#1)
* Add initial version of sync script * add per user temp file Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * remove tmp file inside loop Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * add log message function Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * add trap to always clean up temp file Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * check if auth key exists Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * use getent to get user home dir Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * use log_message function * remove temp file clean as trap is already set * replace echo with log_message Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update use of trap Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * replace echo with log_message Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * replace echo with log_message Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * replace echo with log_message Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * replace echo with log_message Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * replace echo with log_message Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * move user id check Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * remove redundant check Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * set temp file as local variable Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * add brackets * remove brackets * remove local * remove comment Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 9f3482b commit f879bb2

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# SSH Key Sync Script
2+
3+
This Bash script pulls `authorized_keys` files from remote URLs and updates SSH access for multiple local users.
4+
5+
## Features
6+
7+
- Pull-based, no Git required
8+
- Supports multiple users
9+
- Designed for use with cron or systemd
10+
11+
## Usage
12+
13+
1. Edit the `USER_KEYS` array in `sync-ssh-keys.sh` to define users and their key URLs.
14+
2. Add to root's crontab:
15+
16+
```cron
17+
*/15 * * * * /usr/local/bin/sync-ssh-keys.sh >> /var/log/ssh-key-sync.log 2>&1
18+
```
19+

sync-ssh-keys.sh

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#!/bin/bash
2+
set -euo pipefail
3+
4+
# === Configuration: user -> remote key file URL ===
5+
declare -A USER_KEYS=(
6+
["ubuntu"]="https://example.com/ssh-keys/ubuntu.authorized_keys"
7+
["devuser"]="https://example.com/ssh-keys/devuser.authorized_keys"
8+
["admin"]="https://example.com/ssh-keys/admin.authorized_keys"
9+
)
10+
11+
log_message() {
12+
local TIMESTAMP
13+
TIMESTAMP="$(date '+%Y-%m-%d %H:%M:%S')"
14+
echo "$TIMESTAMP: $1"
15+
}
16+
17+
TMP_FILES=()
18+
trap 'rm -f "${TMP_FILES[@]}"' EXIT
19+
for USER in "${!USER_KEYS[@]}"; do
20+
TMP_FILE=$(mktemp)
21+
TMP_FILES+=("$TMP_FILE")
22+
URL="${USER_KEYS[$USER]}"
23+
# Ensure user exists
24+
if ! id "$USER" &>/dev/null; then
25+
log_message "User '$USER' does not exist. Skipping."
26+
continue
27+
fi
28+
USER_HOME=$(getent passwd "$USER" | cut -d: -f6)
29+
if [ -z "$USER_HOME" ]; then
30+
log_message "Failed to determine home directory for user '$USER'. Skipping."
31+
continue
32+
fi
33+
AUTH_KEYS="$USER_HOME/.ssh/authorized_keys"
34+
SSH_DIR="$(dirname "$AUTH_KEYS")"
35+
36+
# Create .ssh directory if it doesn't exist
37+
if [ ! -d "$SSH_DIR" ]; then
38+
mkdir -p "$SSH_DIR"
39+
chown "$USER:$USER" "$SSH_DIR"
40+
chmod 700 "$SSH_DIR"
41+
log_message "Created .ssh directory for user '$USER'"
42+
fi
43+
44+
# Fetch remote key file
45+
if curl -fsSL "$URL" -o "$TMP_FILE"; then
46+
if [ ! -f "$AUTH_KEYS" ] || ! cmp -s "$TMP_FILE" "$AUTH_KEYS"; then
47+
cp "$TMP_FILE" "$AUTH_KEYS"
48+
chown "$USER:$USER" "$AUTH_KEYS"
49+
chmod 600 "$AUTH_KEYS"
50+
log_message "Updated authorized_keys for user '$USER'"
51+
else
52+
log_message "No changes for user '$USER'"
53+
fi
54+
else
55+
log_message "Failed to download keys for '$USER' from $URL"
56+
fi
57+
done

0 commit comments

Comments
 (0)