unix:linux:sysadmin:syncusers
Differences
This shows you the differences between two versions of the page.
| — | unix:linux:sysadmin:syncusers [2022/01/17 18:40] (current) – created - external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ====== Synchronizing Users ====== | ||
| + | For now, we're not going to go into LDAP or anything. Assuming you have a small shop with a small number of machines and a small number of users, and you just want things to be similar across the systems. | ||
| + | |||
| + | ===== Standardizing Users ===== | ||
| + | |||
| + | Ok, this part is not for synchronizing users, but actually for creating a list of standard users. The following Perl script is very insecure as it stores passwords and public keys, but at least they are encrypted. | ||
| + | |||
| + | The two variables at the top of the page, // | ||
| + | |||
| + | It does **not** set the UID, and sets primary group to //users//. All users are members of the group sudo, which gives them sudo rights. See line in middle of sub addAUser to modify that. | ||
| + | |||
| + | The passwords are encrypted using the command <code bash> | ||
| + | |||
| + | Use with caution, but it has worked well for us in the past. It is not well documented. | ||
| + | |||
| + | <code perl fixusers.pl> | ||
| + | #! / | ||
| + | |||
| + | use strict; | ||
| + | use warnings; | ||
| + | use Data:: | ||
| + | |||
| + | # set this to 1 to not really do anything, but to only print what we | ||
| + | # would have done. | ||
| + | my $TEST = 0; | ||
| + | |||
| + | |||
| + | # we use this to know to change usernames, in other words, if | ||
| + | # the username user exists on the server, we need to change it | ||
| + | # to user1, while baduser is removed if it exists. | ||
| + | # if the $changeTo is empty, we simply remove the user. | ||
| + | my %fixUserNames = ( | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ); | ||
| + | |||
| + | # ' | ||
| + | # echo ' | ||
| + | # 'ssh key' is the value found in ~/ | ||
| + | # in the following example, ' | ||
| + | # and their password is changed to ' | ||
| + | # nothing is done to their ssh key. | ||
| + | # user2 is treated the same way, but their ssh public key is added to their authorized_keys file | ||
| + | my %passwords = ( | ||
| + | ' | ||
| + | ' | ||
| + | 'ssh key' | ||
| + | }, | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | }, | ||
| + | ); | ||
| + | |||
| + | |||
| + | my $tempFile = '/ | ||
| + | |||
| + | sub runCommand { | ||
| + | my $command = shift; | ||
| + | if ( $TEST == 1 ) { | ||
| + | print " | ||
| + | return; | ||
| + | } else { | ||
| + | system($command); | ||
| + | return $? >> 8; | ||
| + | } | ||
| + | } | ||
| + | |||
| + | sub getUserHomeDir { | ||
| + | my $user = shift; | ||
| + | | ||
| + | } | ||
| + | |||
| + | |||
| + | # checks to see if username is on this system | ||
| + | # returns true if username exists, false if not | ||
| + | sub userExists { | ||
| + | my $username = shift; # get whatever username they pass in | ||
| + | | ||
| + | | ||
| + | } | ||
| + | |||
| + | # create a user on the system | ||
| + | # if $makeHomeDir is true, will create home directory | ||
| + | sub addAUser { | ||
| + | my $username = shift; # the username we want to add | ||
| + | my $makeHomeDir = shift; # choose whether to make homedir | ||
| + | # makes root of homedir if not there | ||
| + | & | ||
| + | # build our command | ||
| + | my $command = " | ||
| + | # add flag to not create homedir if they told us to | ||
| + | if ( $makeHomeDir ) { | ||
| + | $command .= ' --create-home'; | ||
| + | } else { | ||
| + | $command .= ' --no-create-home' | ||
| + | } | ||
| + | # add the username | ||
| + | | ||
| + | # execute the command | ||
| + | & | ||
| + | # check if it works | ||
| + | my $success = & | ||
| + | # see if worked | ||
| + | | ||
| + | } | ||
| + | |||
| + | # rename a user | ||
| + | sub renameUsers { | ||
| + | my $lookingFor = shift; # this is the account we want to rename | ||
| + | my $changeTo = shift; # this is what we want to rename it to | ||
| + | # does the account to rename exist? | ||
| + | if ( & | ||
| + | print " | ||
| + | # does the account we want to rename to not exist? | ||
| + | if ( not userExists( $changeTo ) ) { | ||
| + | # then, add the new user account | ||
| + | if ( $changeTo ) { | ||
| + | if ( & | ||
| + | # mv the old home directory to the new home directory name | ||
| + | & | ||
| + | # change the ownership of all files to new user | ||
| + | & | ||
| + | } else { # we failed to add the user! | ||
| + | | ||
| + | } # if..else | ||
| + | } # if changeTo exists | ||
| + | print " | ||
| + | } # if | ||
| + | # we succeeded, so delete the original user account | ||
| + | & | ||
| + | & | ||
| + | } # if $lookingFor exist | ||
| + | | ||
| + | } | ||
| + | |||
| + | sub setUpSSH { | ||
| + | my $user = shift; | ||
| + | my $passphrase = shift; | ||
| + | |||
| + | | ||
| + | my $dir = & | ||
| + | | ||
| + | | ||
| + | & | ||
| + | & | ||
| + | open KEY,"> | ||
| + | print KEY $passphrase; | ||
| + | close KEY; | ||
| + | & | ||
| + | & | ||
| + | | ||
| + | } | ||
| + | |||
| + | |||
| + | # check for and rename users in the fixUserNames hash | ||
| + | # removes any users who have an empty username | ||
| + | foreach my $user (keys %fixUserNames ) { | ||
| + | my $worked = & | ||
| + | if ( not $worked ) { | ||
| + | warn "Could not rename $user to $fixUserNames{$user}\n"; | ||
| + | } | ||
| + | } | ||
| + | |||
| + | # add any accounts which do not exist. Also, build a password file | ||
| + | # for setting passwords | ||
| + | open PASS, "> | ||
| + | foreach my $user ( keys %passwords ) { | ||
| + | | ||
| + | warn "Could not add user $user\n" | ||
| + | } | ||
| + | my $ssh = & | ||
| + | warn "User $user already has an authorized_keys file\n" | ||
| + | warn "User $user does not have a home directory\n" | ||
| + | warn "User $user has no ssh key set in config\n" | ||
| + | # print "User $user set up for ssh public key\n" if $ssh == 2; | ||
| + | if ( & | ||
| + | print PASS join( ':', | ||
| + | } | ||
| + | } | ||
| + | close PASS; | ||
| + | |||
| + | # change all passwords in $tempFile | ||
| + | & | ||
| + | #unlink $tempFile; | ||
| + | |||
| + | 1; | ||
| + | |||
| + | </ | ||
unix/linux/sysadmin/syncusers.1642464486.txt.gz · Last modified: (external edit)
