gpt4 book ai didi

linux - 无法为用户将 key 添加到远程计算机

转载 作者:太空宇宙 更新时间:2023-11-04 12:46:15 25 4
gpt4 key购买 nike

我正在编写一个脚本来将用户添加到 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

我找不到插入这条特定线的方法。我将不胜感激。

最佳答案

我能够解决没有这样的文件或目录的问题。我在看命令的错误部分。我能够使用插值和 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

关于linux - 无法为用户将 key 添加到远程计算机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38635946/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com