From ea0a7b7e9b552251b9c876bc81b2bb8fc32f82dc Mon Sep 17 00:00:00 2001 From: code-server-usr Date: Mon, 24 Aug 2026 11:54:41 +0800 Subject: [PATCH] =?UTF-8?q?fix(ssh):=20=E9=81=BF=E5=85=8D=E8=A6=86?= =?UTF-8?q?=E7=9B=96=20authorized=5Fkeys=20=E5=B9=B6=E9=98=B2=E6=AD=A2?= =?UTF-8?q?=E9=87=8D=E5=A4=8D=E6=B7=BB=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 1.sh | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/1.sh b/1.sh index 5870281..cdd3edc 100644 --- a/1.sh +++ b/1.sh @@ -224,8 +224,17 @@ modify_authorized_keys() { echo "正在修改 authorized_keys" mkdir -p /root/.ssh/ AUTHORIZED_KEY="ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQDi6fLhEYp2mmUUBOB/6CA0iQYcwhAUVBwpbJJhK0fZmvmtrCrfkDvFtqw4WH0D/DgRtJNyoDfrQ2gm67FtFzu2ZfH0PtIEI6JWrh432qjQnAT5cuKbN3ghrjTHf08vjh0axxilSFtCXf5Gyv+ZnABil7GZAhJB5K/yBUMr6OD5fp4tvjAYe0t6ZIQb8vgd0dCXDGA3Rak1B7W24vDA1Col+Qvx5I7pfqiebkof7BbkfBYX5KZ/ArK0n1RV7wBjf6g/XkwRPvpCR5rz7s+UnMr2FFYqfQ1wkpKjo74KzAahFyy7UicM3e7OKfZvd8EmWBysIdKCcl9s/H9Z4Q/TDZsmnr68KweCvpqFVHfKS9CZ/7w/7zQhrXDhnysqt6nmp/moQLMax6b62++4X6E0gyopc2nlMT+QjIwRPl+DPC2UicWniOSAuSvpPxt8YXF4Zbpsg0efMQvHDrjQF6Ws9UIKVQeTBqij+TJ3w5aUbLEjPhrP5ia7Dtn7u/3IsosxW00= yanhao@yanhaodeMacBook-Pro.local" - echo $AUTHORIZED_KEY >/root/.ssh/authorized_keys - chmod 600 /root/.ssh/authorized_keys + local authorized_keys_file=/root/.ssh/authorized_keys + + touch "$authorized_keys_file" + if ! grep -Fqx -- "$AUTHORIZED_KEY" "$authorized_keys_file"; then + # Keep an existing final line intact when it does not end with a newline. + if [ -s "$authorized_keys_file" ] && [ -n "$(tail -c 1 "$authorized_keys_file")" ]; then + printf '\n' >>"$authorized_keys_file" + fi + printf '%s\n' "$AUTHORIZED_KEY" >>"$authorized_keys_file" + fi + chmod 600 "$authorized_keys_file" # wget -q -O /tmp/sshd_config https://script.yanhao.ren/downloads/sshd_config # mv /tmp/sshd_config /etc/ssh/sshd_config # service sshd restart