passwordless-user-setup

by nmelehan
0 deployments · 0 still active · last rev. 17 days ago

Sets up a user (change your_username to desired username)
Optionally other users (just call set_up_user again)
Gives them passwordless sudo
Adds public key to them
Also adds public key to root user
Disables password authentication for SSH
Disabled root login for SSH

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

# Sets up a user (change your_username to desired username)
# Optionally other users (just call set_up_user again)
# Gives them passwordless sudo
# Adds public key to them
# Also adds public key to root user 
# Disables password authentication for SSH
# Disables root login for SSH

full_name="full name"
room_number=""
work_phone="your work phone"
home_phone="your home phone"
public_key="your public key"

function set_up_user() {
        echo "Setting up user: $1"
        adduser $1 --disabled-password --gecos "$full_name,$room_number,$work_phone,$home_phone"
        adduser $1 sudo
        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
}

set_up_user "your_username"
# set_up_user "second_user_if_desired"

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 ssh