Skip to content

Instantly share code, notes, and snippets.

@Azzaare
Last active September 19, 2024 06:38
Show Gist options
  • Save Azzaare/da88b086977d9fe507b91e8c539b0bf6 to your computer and use it in GitHub Desktop.
Save Azzaare/da88b086977d9fe507b91e8c539b0bf6 to your computer and use it in GitHub Desktop.
Add a new julia user with ssh public keys
#!/bin/bash
# Check if sufficient arguments are provided
if [ $# -lt 3 ]; then
echo "Usage: $0 <username> <password> <ssh_key_string1> [<ssh_key_string2> ...]"
exit 1
fi
# Command-line arguments
USERNAME="$1"
PASSWORD="$2"
shift 2 # Shift arguments so the rest are SSH key strings
# Other variables
HOMEDIR="/home/$USERNAME"
SHELL="/usr/bin/zsh"
SSH_DIR="$HOMEDIR/.ssh"
sudo apt install zsh
# Check if user already exists
if id "$USERNAME" &>/dev/null; then
echo "User $USERNAME already exists."
exit 1
fi
# Create the user with home directory and zsh shell
useradd -m -d "$HOMEDIR" -s "$SHELL" "$USERNAME"
# Set the user's password (provided as an argument)
echo "$USERNAME:$PASSWORD" | chpasswd
# Set up SSH directory
mkdir -p "$SSH_DIR"
chmod 700 "$SSH_DIR"
touch "$SSH_DIR/authorized_keys"
chmod 600 "$SSH_DIR/authorized_keys"
# Add each provided SSH key string to the authorized_keys
for KEYSTRING in "$@"; do
echo "$KEYSTRING" >> "$SSH_DIR/authorized_keys"
done
# Set correct ownership of the home directory and SSH keys
chown -R "$USERNAME:$USERNAME" "$HOMEDIR"
# Install Julia using JuliaUp in the user's environment
echo "Installing Julia as $USERNAME..."
sudo -u "$USERNAME" zsh -c "curl -fsSL https://install.julialang.org | zsh"
# Add Julia versions using JuliaUp
echo "Configuring Julia versions for $USERNAME..."
sudo -u "$USERNAME" bash -c "
export PATH=\"\$HOME/.juliaup/bin:\$PATH\"
juliaup add lts
juliaup add rc
juliaup add nightly
juliaup add 1.6
juliaup add 1.7
juliaup add 1.8
juliaup add 1.9
juliaup add 1.10
juliaup default rc
"
# Add zsh aliases and JuliaUp initialization block to .zshrc
echo "Setting up Zsh aliases and JuliaUp initialization block for $USERNAME..."
ZSHRC="$HOMEDIR/.zshrc"
{
echo ""
echo "# >>> juliaup initialize >>>"
echo "# !! Contents within this block are managed by juliaup !!"
echo "case \":\$PATH:\" in"
echo " *:/home/$USERNAME/.juliaup/bin:*)"
echo " ;;"
echo ""
echo " *)"
echo " export PATH=/home/$USERNAME/.juliaup/bin\${PATH:+:\${PATH}}"
echo " ;;"
echo "esac"
echo "# <<< juliaup initialize <<<"
echo ""
echo "# JuliaUp Aliases"
echo "alias uberjuliaup='juliaup self update; juliaup update'"
echo "alias uberjulia='uberjuliaup; julia'"
} >> "$ZSHRC"
# Set correct ownership of .zshrc
chown "$USERNAME:$USERNAME" "$ZSHRC"
# Verify the setup
echo "User $USERNAME created with zsh shell, home directory, SSH keys, and Julia installed."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment