-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathssh-copy-id
More file actions
48 lines (39 loc) · 1.07 KB
/
Copy pathssh-copy-id
File metadata and controls
48 lines (39 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/bin/sh
# Usage: sh ssh-copy-id [username]
# Determine user
USER_NAME="${1:-$(id -un)}"
USER_HOME="$(eval echo ~"$USER_NAME")"
SSH_DIR="$USER_HOME/.ssh"
AUTH_KEYS="$SSH_DIR/authorized_keys"
# Ensure .ssh directory
if [ ! -d "$SSH_DIR" ]; then
mkdir -p "$SSH_DIR"
chown "$USER_NAME" "$SSH_DIR"
chmod 700 "$SSH_DIR"
fi
# Ensure authorized_keys file
if [ ! -f "$AUTH_KEYS" ]; then
touch "$AUTH_KEYS"
chown "$USER_NAME" "$AUTH_KEYS"
chmod 600 "$AUTH_KEYS"
fi
if command -v curl >/dev/null 2>&1; then
DL="curl -fsSL"
elif command -v wget >/dev/null 2>&1; then
DL="wget -qO-"
else
echo "Error: neither curl nor wget is installed" >&2
exit 1
fi
# Download keys and add if not present
$DL "https://raw.githubusercontent.com/JoyceBabu/dotfiles/refs/heads/master/ssh-keys.pub" | \
while IFS= read -r key; do
[ -n "$key" ] || continue # skip empty lines
key_name=$(echo "$key" | awk '{print $3}')
if grep -qxF "$key" "$AUTH_KEYS"; then
echo "Key already present: $key_name"
else
echo "$key" >> "$AUTH_KEYS"
echo "Key added: $key_name"
fi
done