passwordless-user-setup

by nmelehan
1 deployments · 0 still active · last rev. 5 months ago

Compatible with: Debian 8, Debian 9, Ubuntu 16.04 LTS, Ubuntu 18.04 LTS
						#!/bin/bash

# function set_up_user():
## Accepts a username
## Creates a user under that name **with no password**
## Gives them passwordless sudo
## Adds public key to them

# Script also does a software package update/upgrade
# Installs Zsh
# Adds public key to root user 
# Disables password authentication for SSH
# Disables root login for SSH

# Be sure to edit these lines:
# public_key="your-public-key"
# set_up_user "my_user" 

if hash apt 2>/dev/null; then
    apt update && apt upgrade -y
    apt install zsh -y
else
    yum update -y
    yum install zsh -y
fi

public_key="your-public-key"

function set_up_user() {
        echo "Setting up user: $1"
        useradd -m $1
        echo "$1 ALL=(ALL) NOPASSWD: ALL" | tee -a /etc/sudoers.d/passwordless_sudoers
        mkdir /home/$1/.ssh
        echo "$public_key" | tee /home/$1/.ssh/authorized_keys
        chsh --shell $(which zsh) $1
}

set_up_user "my_user"

mkdir /root/.ssh
echo "$public_key" | tee /root/.ssh/authorized_keys
sed -i 's/#PasswordAuthentication yes/PasswordAuthentication no/' /etc/ssh/sshd_config
sed -i 's/PermitRootLogin yes/PermitRootLogin no/' /etc/ssh/sshd_config
systemctl restart sshd