Last active
April 6, 2025 08:50
-
-
Save hayd1n/97e8ee85b9fd5b23bcdd20855f52018c to your computer and use it in GitHub Desktop.
π Auto-installer for ssh-key-sync with systemd timer on Linux. Automatically detects OS and architecture, downloads the correct binary, and sets up periodic GitHub SSH key syncing.
This file contains hidden or 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 | |
set -e | |
# === CONFIGURATION === | |
VERSION="1.7.3" | |
USER_NAME="$(whoami)" | |
GITHUB_USER="hayd1n" # <--- Your GitHub username here | |
# === Detect OS === | |
OS="$(uname | tr '[:upper:]' '[:lower:]')" # linux, darwin | |
# === Detect ARCH === | |
ARCH_RAW="$(uname -m)" | |
case "$ARCH_RAW" in | |
x86_64) ARCH="amd64" ;; | |
aarch64 | arm64) ARCH="arm64" ;; | |
*) | |
echo "β Unsupported architecture: $ARCH_RAW" | |
exit 1 | |
;; | |
esac | |
echo "π Detected OS: $OS" | |
echo "π Detected Architecture: $ARCH" | |
# === Download & Install === | |
cd /tmp | |
FILENAME="ssh-key-sync_${VERSION}_${OS}_${ARCH}.tar.gz" | |
URL="https://github.com/shoenig/ssh-key-sync/releases/download/v${VERSION}/${FILENAME}" | |
echo "β¬οΈ Downloading $FILENAME ..." | |
wget -q "$URL" || { | |
echo "β Download failed. Check if version/arch exists." | |
exit 1 | |
} | |
echo "π¦ Extracting..." | |
sudo tar -C /usr/local/bin -xf "$FILENAME" | |
# === SSH Setup === | |
mkdir -p ~/.ssh | |
chmod 700 ~/.ssh | |
touch ~/.ssh/authorized_keys | |
chmod 600 ~/.ssh/authorized_keys | |
# === Systemd Service Setup === | |
SERVICE_FILE="/etc/systemd/system/ssh-key-sync.service" | |
TIMER_FILE="/etc/systemd/system/ssh-key-sync.timer" | |
echo "π οΈ Setting up systemd service..." | |
sudo bash -c "cat > ${SERVICE_FILE}" <<EOF | |
[Unit] | |
Description=Synchronize ssh authorized keys with public keys from github. | |
[Service] | |
ExecStart=/usr/local/bin/ssh-key-sync -verbose -system-user ${USER_NAME} -github-user ${GITHUB_USER} | |
EOF | |
sudo bash -c "cat > ${TIMER_FILE}" <<EOF | |
[Unit] | |
Description=Run ssh-key-sync every 6 hours | |
[Timer] | |
OnBootSec=5min | |
OnUnitActiveSec=6h | |
Unit=ssh-key-sync.service | |
[Install] | |
WantedBy=timers.target | |
EOF | |
# === Enable and Start Timer === | |
echo "π Enabling and starting systemd timer..." | |
sudo systemctl daemon-reload | |
sudo systemctl enable ssh-key-sync.timer | |
sudo systemctl start ssh-key-sync.service | |
echo "β ssh-key-sync.service started. Checking status..." | |
sudo systemctl status ssh-key-sync.service --no-pager |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment