Here is the body of the script I am running...
for i in `cat ${hostfile}`
do
echo "Adding user $username on $i"
ssh -q $i /usr/bin/sudo /usr/sbin/adduser $username --gecos "$username,,," --disabled-password;
echo "$username:$password" | /usr/bin/sudo chpasswd;
/usr/bin/sudo mkdir -p /home/$username/.ssh;
/usr/bin/sudo touch /home/$username/.ssh/authorized_keys;
/usr/bin/sudo chmod 600 /home/$username/.ssh/authorized_keys;
/usr/bin/sudo chmod 700 /home/$username/.ssh;
/usr/bin/sudo chown -R $username:$username /home/$username
#ssh -q $i /usr/bin/sudo /usr/sbin/adduser $username --gecos "$username,,," --disabled-password; sudo echo $username:'$password' | chpasswd; /usr/bin/sudo mkdir /home/$username/.ssh; /usr/bin/sudo touch /home/$username/.ssh/authorized_keys; /usr/bin/sudo chmod 600 /home/$username/.ssh/authorized_keys; /usr/bin/sudo chmod 700 /home/$username/.ssh; /usr/bin/sudo chown -R $username:$username /home/$username; adduser $username $group
done
It executes but does not create the .ssh directory.
echo
supposed to be run locally? What is the use of;
at the end of lines? – FedKad May 16 '19 at 18:59for i in $(cat file)
and usewhile IFS= read -r file; do ...; done < file
which also avoids the unnecessary use ofcat
. See https://mywiki.wooledge.org/DontReadLinesWithFor. Granted, this shouldn't be an issue if your file is just a list of host names, but it's better to not get into this habit. – terdon May 16 '19 at 19:30