Created
October 20, 2016 20:46
-
-
Save jasondewitt/02d4495e4d9e192772177a861e4bdc40 to your computer and use it in GitHub Desktop.
migrate all users from one linux server to another
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
targetsrv=user@ip.add.re.ss | |
for user in $(ssh $targetsrv ls /home) | |
do | |
if ! id $user > /dev/null 2>&1 | |
then | |
echo "create user $user" | |
useradd -m -s /bin/bash $user | |
pass=$(ssh $targetsrv grep $user: /etc/shadow | awk -F: '{print $2}') | |
if [ "$pass" == "!" ] | |
then | |
echo "locking password for $user" | |
passwd -l $user | |
else | |
echo "setting password for $user" | |
usermod -p "$pass" $user | |
fi | |
if sshkey=$(ssh $targetsrv cat /home/$user/.ssh/authorized_keys > /dev/null 2>&1) | |
then | |
mkdir /home/$user/.ssh | |
chmod 700 /home/$user/.ssh | |
echo $sshkey > /home/$user/.ssh/authorized_keys | |
chmod 600 /home/$user/.ssh/authorized_key | |
chown -R $user:$user /home/$user | |
fi | |
fi | |
echo | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment