Skip to content

Instantly share code, notes, and snippets.

@eevans
Created August 26, 2010 19:07
Show Gist options
  • Save eevans/551999 to your computer and use it in GitHub Desktop.
Save eevans/551999 to your computer and use it in GitHub Desktop.
from fabric.api import put, sudo
from os.path import join, basename
HOME = '/home'
SHELL = '/bin/bash'
SUDO_GROUP = 'sudo'
def adduser(username, sshpubkey=None, admin=False):
sudo('adduser --gecos \',,,\' --home %s --shell %s --disabled-login %s' % \
(join(HOME, username), SHELL, username))
# If a public key was given, copy it over.
if sshpubkey: copy_sshpubkey(username, sshpubkey)
# Give admins sudo access
if admin: sudo_access(username)
def copy_sshpubkey(username, sshpubkey):
path = '/home/%s/.ssh' % username
sudo('mkdir -p %s' % path)
src = join('/tmp', basename(sshpubkey))
put(sshpubkey, join('/tmp', basename(sshpubkey)))
# Permissions / ownership
dst = join(path, 'authorized_keys')
sudo('mv %s %s' % (src, dst))
sudo('chmod 700 %s' % path)
sudo('chmod 600 %s' % dst)
sudo('chown -R %s: %s' % (username, path))
def sudo_access(username):
sudo('adduser %s %s' % (username, SUDO_GROUP))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment