我正在编写一个脚本来将用户添加到aws ec2实例中。下面是shell脚本。
file=$2
user=$1
user1="ec2-user"
basekey="/home/user-home/some-folder/test.pem"
keyfile=$3
file1=`cat /home/user-home/.ssh/id_rsa.pub`
for line in `cat $file`
do
ssh -i $basekey -o strictHostKeyChecking=no $user1@$line sudo useradd -m -d /home/$user $user
ssh -i $basekey -o strictHostKeyChecking=no $user1@$line sudo mkdir /home/$user/.ssh
ssh -i $basekey -o strictHostKeyChecking=no $user1@$line sudo touch /home/$user/.ssh/authorized_keys
ssh -i $basekey -o strictHostKeyChecking=no $user1@$line sudo chmod 777 /home/$user/.ssh
ssh -i $basekey -o strictHostKeyChecking=no $user1@$line sudo chmod 777 /home/$user/.ssh/authorized_keys
scp -i $basekey -o strictHostKeyChecking=no $keyfile $user1@$line:/home/$user1/
sleep 10
ssh -i $basekey -o strictHostKeyChecking=no $user1@$line sudo cat /home/$user1/id_rsa.pub > /home/$user/.ssh/authorized_keys
ssh -i $basekey -o strictHostKeyChecking=no $user1@$line sudo cat /home/$user/.ssh/authorized_keys
echo ssh -i $basekey -o strictHostKeyChecking=no $user1@$line sudo chmod 600 /home/$user/.ssh/authorized_keys
echo ssh -i $basekey -o strictHostKeyChecking=no $user1@$line sudo chown $user.$user /home/$user/.ssh/authorized_keys
echo ssh -i $basekey -o strictHostKeyChecking=no $user1@$line sudo chmod 700 /home/$user/.ssh
echo ssh -i $basekey -o strictHostKeyChecking=no $user1@$line sudo chown $user.$user /home/$user/.ssh
done
虽然文件存在,但我收到的错误低于此。
./dev-enable.sh: line 24: /home/devuser/.ssh/authorized_keys: No such file or directory
我无法找到插入此特定行的方法。我很感激任何帮助。
答案 0 :(得分:0)
我能够解决问题没有这样的文件或目录。我正在查看命令的错误部分。我能够使用插值和shell命令的正确语法来解决它。以下是处理这种情况的正确命令。
ssh -i $basekey -o strictHostKeyChecking=no $user1@$line sudo bash -c "'cat > /home/$user/.ssh/authorized_keys'" < $file1
和file1定义如下
file1=/home/user-home/.ssh/id_rsa.pub