Skip to content

Instantly share code, notes, and snippets.

@gmiroshnykov
Last active December 29, 2015 11:08
Show Gist options
  • Save gmiroshnykov/7661240 to your computer and use it in GitHub Desktop.
Save gmiroshnykov/7661240 to your computer and use it in GitHub Desktop.
fab create_user
from fabric.api import *
# Usage: fab -H <hostname> -u <ssh_username> create_user:<username>
def create_user(username):
run('adduser --disabled-password --gecos "" {0}'.format(username))
run('echo "{0} ALL=(ALL) NOPASSWD:ALL" > /etc/sudoers.d/{0}'.format(username))
run('chmod 0440 /etc/sudoers.d/{0}'.format(username))
run('mkdir -p /home/{0}/.ssh'.format(username))
run('chmod 0755 /home/{0}/.ssh'.format(username))
run('chown {0}:{0} /home/{0}/.ssh'.format(username))
run('cp ~/.ssh/authorized_keys /home/{0}/.ssh/authorized_keys'.format(username))
run('chmod 0644 /home/{0}/.ssh/authorized_keys'.format(username))
run('chown {0}:{0} /home/{0}/.ssh/authorized_keys'.format(username))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment