SSH私钥登录失败及Linux服务器Sato用户.ssh目录迁移至Mac后的登录咨询
Hey there! Let's work through your two SSH issues one by one—they're super common, so we'll get you sorted out quickly.
If you're stuck on private key login failures, here are the most likely fixes to check:
Fix permissions first (this is the #1 culprit)
SSH is strict about file permissions to stay secure. On your Mac, make sure your private key (usuallyid_rsa) has read/write access only for you:chmod 600 ~/.ssh/id_rsaAnd the
.sshdirectory itself should be set to700:chmod 700 ~/.sshOn the server side, the sato user's
.sshdirectory also needs700permissions, and theauthorized_keysfile should be600.Verify the private key matches the server's public key
Double-check that the public key corresponding to your private key is present in the server's~sato/.ssh/authorized_keysfile. You can print your local public key with:cat ~/.ssh/id_rsa.pubCompare that string to what's in the server's
authorized_keys—they need to be identical.Check SSH server configuration
On the Linux server, ensure the SSH daemon allows key-based authentication. Open/etc/ssh/sshd_config(you'll need sudo access) and confirm these lines are set and uncommented:PubkeyAuthentication yes AuthorizedKeysFile .ssh/authorized_keys .ssh/authorized_keys2After making changes, restart the SSH service:
sudo systemctl restart sshdTest with verbose mode to debug
If you're still stuck, run SSH with verbose output to see exactly where it's failing:ssh -v sato@your-server-ipLook for lines like "Offering public key" or "Permission denied" to pinpoint the issue.
Copying the server's .ssh directory over can work, but you need to fix permissions and set things up correctly:
Move the copied .ssh directory to your Mac's user home
Assuming you copied the directory to your Mac's Downloads folder, move it to your home directory (replaceyour-mac-usernamewith your actual Mac username):mv ~/Downloads/.ssh ~/Note: If you already have an existing
.sshdirectory on your Mac, back it up first before overwriting!Fix permissions immediately
As mentioned earlier, SSH won't work with incorrect permissions. Run these commands on your Mac:chmod 700 ~/.ssh chmod 600 ~/.ssh/id_rsa chmod 600 ~/.ssh/authorized_keys chmod 644 ~/.ssh/id_rsa.pubTest the SSH login
Now try connecting to the server—you shouldn't need a password if everything's set right:ssh sato@your-server-ipIf you get a "host key verification failed" error, you can remove the old entry from your Mac's
known_hostsfile:ssh-keygen -R your-server-ipThen try connecting again.
Optional: Set up SSH config for easier login
To avoid typing the full command every time, create aconfigfile in your.sshdirectory:nano ~/.ssh/configAdd this content (customize the values):
Host myserver HostName your-server-ip User sato IdentityFile ~/.ssh/id_rsaSave and exit (Ctrl+O, then Ctrl+X in nano), then you can just run:
ssh myserver
内容的提问来源于stack exchange,提问作者Sato

