创建密钥 1 2 3 4 5 6 7 8 9 10 cd ~/.sshssh-keygen -t ecdsa -C "Key_for_Github" -f ~/.ssh/id_rsa mv id_rsa.pub authorized_keys
此时在 ~/.ssh/ 目录下生成了2个文件,id_rsa为私钥,id_rsa.pub为公钥。 私钥自己下载到本地电脑妥善保存(丢了服务器可就没法再登陆了), 为安全,建议删除服务器端的私钥。公钥则可以任意公开。
修改配置文件 1 2 3 4 5 6 sudo vim /etc/ssh/sshd_configPasswordAuthentication no PubkeyAuthentication yes PermitRootLogin no PermitEmptyPasswords no
重启SSH服务 1 2 sudo systemctl reload sshd.service
其他设置 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 Port 22 ListenAddress IP LoginGraceTime 2m HostKey UseDNS no PermitRootLogin yes MaxAuthTries 6 MaxSessions 10 StrictModes yes PubkeyAuthentication yes AuthorizedKeysFile .ssh/authorized_keys PasswordAuthentication yes PermitEmptyPasswords no GatewayPorts no ChallengeResponseAuthentication no ClientAliveCountMax 3 ClientAliveInterval 10 MaxStartups 10:30:100 Banner /path/file GSSAPIAuthentication no AllowUsers username DenyUsers AllowGroups DenyGroups
登陆方式
直接指定私钥
ssh -i /pi/.ssh/id_rsa pi@192.168.1.1 -p 22
默认私钥路径
SSH默认的公钥私钥路径为 /.ssh/ 下的id_rsa和id_rsa.pub文件。 把公钥放置到远端服务器的/.ssh/authorized_keys中然后直接ssh会认证失败的 需要把它的私钥放置到路径~/.ssh/id_rsa,然后再SSH才能成功:ssh pi@192.168.1.1