Skip to content

Instantly share code, notes, and snippets.

@pleasemarkdarkly
Last active January 1, 2020 13:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pleasemarkdarkly/b578fc7df870240dc3a8e7a77d528a83 to your computer and use it in GitHub Desktop.
Save pleasemarkdarkly/b578fc7df870240dc3a8e7a77d528a83 to your computer and use it in GitHub Desktop.
Script to setup users and SSH on a Ubuntu Server 19.04 Raspberry Pi 4 SD card image. By default Ubuntu Server does not enable SSH. So to enable this and create a few users, this script was created and adapted from instructions from the referenced website. This script should be used on a host Linux machine, Ubuntu to see the writeable directory o…
#!/bin/sh
# based on instructions from
# https://www.berthon.eu/2017/installing-ubuntu-server-on-raspberry-pi-headless/
session=$(date +"%Y%m%d%H%M%S")
array_users=(
rooot
)
# Bail out if the temp directory wasn't created successfully.
if [ ! -e $TMPDIR ]; then
>&2 echo "Failed to create temp directory"
exit 1
fi
# Make sure it gets removed even if the script exits abnormally.
trap "exit 1" HUP INT PIPE QUIT TERM
trap 'rm -rf "$TMPDIR"' EXIT
#--------------------------------------------------------------------------------------------------
# Bash Utils Section Section
function verify_log4bash () {
if [[ ! -e ./log4bash.sh ]]; then
echo "function: verify_log4bash"
echo "./log4bash.sh not found...fetching remotely"
wget http://pretty.pleasemarkdarkly.com:8080/jP8Nd/log4bash.sh
else
source "./log4bash.sh"
log_info "function: verify_log4bash"
log_info "log4bash will be used"
verify_log4bash_output
fi
}
function verify_log4bash_output () {
log "example log outputs"
log "log output";
log_info "log_info output";
log_success "log_success output";
log_warning "log_warning output";
log "end example log outputs"
}
# Bash Utils Section Section
#--------------------------------------------------------------------------------------------------
function log_output () {
log_info "connect usb/sd card to linux pc"
}
function wait_five_seconds () {
for i in {5..1..1}; do echo -n "$i..." && sleep 1; done
log_warning "ready to party"
}
function wait_three_seconds () {
for i in {3..1..1}; do echo -n "$i..." && sleep 1; done
}
function map_drive () {
[ -e /mnt/rpi ] log_info "/mnt/rpi mounted" | ln -s /media/$(whoami)/writable/ /mnt/rpi
}
function create_ssh () {
log_info "function: create_ssh user for ubuntu 19.01 sd card"
[ -e /mnt/rpi ] log_info "found properly mounted drive" | log_error "rpi mount not found"; return;
userid=1000
log_info "starting userid: $userid"
[ -e /mnt/rpi/etc/passwd ] log_info "found mounted /etc/passwd"; log_warning "adding users..."
log_info "you have five seconds to change your mind"
wait_five_seconds
for user in "${array_users[@]}"
do
log_warning "creating user: $user, with userid: $userid"
log_warning "5 seconds to cancel"
wait_three_seconds
echo "$(user):x:$(userid):$(userid):<Full Name>:/home/$(user):/bin/bash" | sudo tee -a /mnt/rpi/etc/passwd
echo "$(user):x:$(userid):" | sudo tee -a /mnt/rpi/etc/group
echo "$(user):*::$(user)" | sudo tee -a /mnt/rpi/etc/gshadow
echo "$(user)::0:0:99999:7:::" | sudo tee -a /mnt/rpi/etc/shadow
echo "$(user) ALL=(ALL) ALL" | sudo tee /mnt/rpi/etc/sudoers.d/20_$(user)_superuser
log_warning "current user: $(whoami) .ssh/id_rsa.pub keys being added to created users"
sudo cp -R /mnt/rpi/etc/skel /mnt/rpi/home/$(user)
sudo chmod 0750 /mnt/rpi/home/$(user)
sudo mkdir -m 0700 /mnt/rpi/home/$(user)/.ssh
cat ~/.ssh/id_rsa.pub | sudo tee -a /mnt/rpi/home/$(user)/.ssh/authorized_keys
sudo chmod 0600 /mnt/rpi/home/$(user)/.ssh/authorized_keys
sudo chown -R $(userid):$(userid) /mnt/rpi/home/$(user)
log_info "created user: $(user)"
((userid=userid+1))
done
}
function enable_systemd_enabling_ssh () {
log_info "function: enable_systemd_enabling_ssh"
cd /mnt/rpi/lib/systemd/system
sudo rm -vf default.target
sudo ln -s multi-user.target default.target
cd /mnt/rpi/etc/systemd/system/multi-user.target.wants
ln -s /lib/systemd/system/ssh.service ssh.service
log_warning "last command failing is fine"
}
function main () {
log_info "function: main"
[ "$(whoami]" == "root" ] log_info "script runs at root, please review the source to be sure" | log_info "must run as root"; sudo
[ ! -f ~/.ssh/id_rsa.pub ] log_info "couldn't find ssh/id_rsa.pub. see ssh-keygen" | cat ~/.ssh/id_rsa.pub; log_info "found ./ssh/id_rsa...continuing"
[ -f ./env ] log_info "overriding default values for users"; source ~/.env | \
log_info "using internal script array_users: "; for user in "${array_users[@]}"; do echo "$user, "; done
log_warning "creating users from array including account for rooot"
create_ssh
enable_systemd_enabling_ssh
}
verify_log4bash
main "[@]"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment