This commit is contained in:
陆柯 2022-11-18 16:37:37 +08:00
parent 77e27d795f
commit c0c4adefc4

View File

@ -35,11 +35,15 @@ fi
if [ "$2" == 'root' ] if [ "$2" == 'root' ]
then then
HomePath=/root HomePath=/root
TheUser=root
elif [ "$2" ] elif [ "$2" ]
then then
HomePath=/home/$2 HomePath=/home/$2
TheUser=$2
else else
# 可能是 root 或其他
HomePath=~ HomePath=~
TheUser=`whoami`
fi fi
if [ -d "$HomePath" ] if [ -d "$HomePath" ]
@ -66,29 +70,34 @@ then
done done
echo echo
mkdir -p $HomePath/.ssh if [ "$TheUser" != 'root' ]
chmod 700 $HomePath/.ssh
# 注意,由 root 为新用户创建的配置文件的 owner 是 root而不是新用户导致新用户无法读取该文件而密钥登录失败因此要重设 owner。
chown $2:$2 $HomePath
if [ -e '$HomePath/.ssh/authorized_keys' ]
then then
mv $HomePath/.ssh/authorized_keys $HomePath/.ssh/authorized_keys.backup-$(date +%Y%m%d%H%M%S)
fi
echo "=== Append or link or omit [$HomePath/.ssh/authorized_keys] to config ssh server? <a> for append, <l> for link, <<anything else>> for omit:" mkdir -p $HomePath/.ssh
read -p ">>> " CopyOrLinkOrOmitAuthorizedKeys chmod 700 $HomePath/.ssh
# 注意,由 root 为新用户创建的配置文件的 owner 是 root而不是新用户导致新用户无法读取该文件而密钥登录失败因此要重设 owner。
chown $2:$2 $HomePath
if [ -e '$HomePath/.ssh/authorized_keys' ]
then
mv $HomePath/.ssh/authorized_keys $HomePath/.ssh/authorized_keys.backup-$(date +%Y%m%d%H%M%S)
fi
if [ "$CopyOrLinkOrOmitAuthorizedKeys" = 'l' ] echo "=== Append or link or omit [$HomePath/.ssh/authorized_keys] to config ssh server? <a> for append, <l> for link, <<anything else>> for omit:"
then read -p ">>> " CopyOrLinkOrOmitAuthorizedKeys
echo "--- Linking $SourcePath/authorized_keys to $HomePath/.ssh/authorized_keys ..."
ln -s $SourcePath/.ssh/authorized_keys $HomePath/.ssh/authorized_keys if [ "$CopyOrLinkOrOmitAuthorizedKeys" = 'l' ]
elif [ "$CopyOrLinkOrOmitAuthorizedKeys" = 'a' ] then
then echo "--- Linking $SourcePath/authorized_keys to $HomePath/.ssh/authorized_keys ..."
echo "--- Copying $SourcePath/authorized_keys to $HomePath/.ssh/authorized_keys ..." ln -s $SourcePath/.ssh/authorized_keys $HomePath/.ssh/authorized_keys
cat $SourcePath/.ssh/authorized_keys >> $HomePath/.ssh/authorized_keys elif [ "$CopyOrLinkOrOmitAuthorizedKeys" = 'a' ]
chmod 600 $HomePath/.ssh/authorized_keys then
echo "--- Copying $SourcePath/authorized_keys to $HomePath/.ssh/authorized_keys ..."
cat $SourcePath/.ssh/authorized_keys >> $HomePath/.ssh/authorized_keys
chmod 600 $HomePath/.ssh/authorized_keys
fi
echo
fi fi
echo
popd popd
else else