Skip to content

Instantly share code, notes, and snippets.

@mbbx6spp
Created December 7, 2011 06:19
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mbbx6spp/1441693 to your computer and use it in GitHub Desktop.
Save mbbx6spp/1441693 to your computer and use it in GitHub Desktop.
Removing the crazy amount of hoops you need to jump through to create system users on OS X. I can't believe how painful this is. These thirteen commands are just two on Linux and real BSDs. WTF?
# These are bash functions. You can just source them on startup based on uname
# if you use the same dotfiles for all systems.
function listUsers() {
dscl . -list /Users UniqueID;
}
function listGroups() {
dscl . -list /Groups PrimaryGroupID;
}
function lastSystemUID() {
echo $(listUsers | awk '{if ($2 < 500) print $2}' | sort -ug | tail -1);
}
function nextSystemUID() {
echo $(lastSystemUID | awk '{ print $1+1 }');
}
function lastSystemGID() {
echo $(listGroups | awk '{if ($2 < 500) print $2}' | sort -ug | tail -1);
}
function nextSystemGID() {
echo $(lastSystemGID | awk '{ print $1+1 }');
}
function createSystemGroup() {
local grp=$1;
local grpId=${nextSystemGID};
sudo dscl . -create /Groups/${grp};
sudo dscl . -create /Groups/${grp} PrimaryGroupID ${grpId};
sudo dscl . -append /Groups/${grp} RecordName ${grp};
echo "Created system group ${grp}";
}
function createSystemUser() {
local usr=$1;
local grp=$2;
local name=$3;
local usrId=${nextSystemUID};
createSystemGroup ${grp};
sudo dscl . -create /Users/${usr}
sudo dscl . -create /Users/${usr} UniqueID ${usrId};
sudo dscl . -create /Users/${usr} PrimaryGroupID 1;
sudo dscl . -create /Users/${usr} NFSHomeDirectory /Users/${usr};
sudo dscl . -create /Users/${usr} UserShell /bin/bash;
sudo dscl . -create /Users/${usr} RealName "${name}";
sudo dscl . -append /Users/${usr} RecordName ${usr};
sudo dscl . -append /Groups/${grp} GroupMembership ${usr};
echo "Created system user ${usr}";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment