Bash_Scripts/SSH/Create-Remote-User.bash
2021-10-06 00:11:46 -05:00

44 lines
1.1 KiB
Bash
Executable File

#!/bin/bash --posix
set -e
# Remote System
REMOTE_SSH_SERVER=Kansas-Xeon
REMOTE_HOST_NAME=kansas-xeon.orion-technologies.io
REMOTE_USER_TO_CREATE=Sam
# Local System
SSH_FILE_DIRECTORY=~/.ssh
SSH_FILE_NAME=${REMOTE_USER_TO_CREATE}-id_ed25519
SSH_FILE="${SSH_FILE_DIRECTORY}"/"${SSH_FILE_NAME}"
SSH_HOST=Kansas-Xeon
ssh-keygen -b 512 -t ed25519 -f ${SSH_FILE} -N ""
chmod 600 ${SSH_FILE}
PUB_KEY_CONTENTS=$(cat ${SSH_FILE}.pub)
ssh root@${REMOTE_SSH_SERVER} /bin/bash << EOF
useradd ${REMOTE_USER_TO_CREATE}
mkdir /home/${REMOTE_USER_TO_CREATE}/.ssh
touch /home/${REMOTE_USER_TO_CREATE}/.ssh/authorized_keys
echo ${PUB_KEY_CONTENTS} >> /home/${REMOTE_USER_TO_CREATE}/.ssh/authorized_keys
chown -R ${REMOTE_USER_TO_CREATE}:${REMOTE_USER_TO_CREATE} /home/${REMOTE_USER_TO_CREATE}/.ssh
EOF
cat << EOF >> ~/.ssh/config
Host ${SSH_HOST}
HostName ${REMOTE_HOST_NAME}
Match user ${REMOTE_USER_TO_CREATE} host ${REMOTE_HOST_NAME}
Identityfile ${SSH_FILE}
EOF
ssh ${REMOTE_USER_TO_CREATE}@${SSH_HOST} /bin/bash << EOF
mkdir ~/.bin
echo export PATH=$PATH:~/.bin >> ~/.bash_profile
EOF
echo
echo "Finished, login via ssh with \"${REMOTE_USER_TO_CREATE}@${SSH_HOST}\""