Skip to content

Instantly share code, notes, and snippets.

@elifiner
Last active August 29, 2015 14:12
Show Gist options
  • Save elifiner/cf757e3a56c25f87a4a2 to your computer and use it in GitHub Desktop.
Save elifiner/cf757e3a56c25f87a4a2 to your computer and use it in GitHub Desktop.
Fabric script to properly set up a user on a remote Ubuntu.
from crypt import crypt
from fabric.api import *
from fabric.contrib.files import append, uncomment
env.user = 'root'
env.use_ssh_config = True
def create_user(name, password):
with hide('stdout'):
run('useradd -m %s' % name)
run('usermod --password %s %s' % (crypt(password, 'salt'), name))
run('mkdir -p ~%s/.ssh' % name)
run('cp ~/.ssh/authorized_keys ~%s/.ssh' % name)
run('chown -R %s:%s ~%s/.ssh' % (name, name, name))
run('chsh -s /bin/bash %s' % name)
run('cp /etc/sudoers /tmp/sudoers.new')
append('/tmp/sudoers.new', '%s ALL=NOPASSWD: ALL' % name, use_sudo=True)
run('visudo -c -f /tmp/sudoers.new')
run('EDITOR="cp /tmp/sudoers.new" visudo')
uncomment('~%s/.bashrc' % name, '#force_color_prompt=yes')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment