ssh

开启ssh服务

service sshd start

或者

/etc/init.d/ssh start

ssh开启秘钥登录

生成秘钥

生成rsa密钥,使用Linux的ssh-keygen,详细参考

ssh-keygen -t rsa -b 4096 -f ~/keys/rsaKey

运行指定的生成密钥命令后会执行以下几步

  1. 如果不指定密钥名称,会有 Enter file in which to save the key (/root/.ssh/id_rsa): 请指定文件位置和名称,默认为 /root/.ssh/id_rsa
  2. Enter passphrase (empty for no passphrase) : 请输入密码,如果不输入默认为空
  3. Enter same passphrase again : 请确认密码,如果不输入默认为空

在服务器上安装公钥

键入以下命令,在服务器上安装公钥

[root@host ~]$ cd .ssh
[root@host .ssh]$ cat id_rsa.pub >> authorized_keys

注意使用附加模式,authorized_keys 可能还有其他公钥

如此便完成了公钥的安装。为了确保连接成功,请保证以下文件权限正确:

[root@host .ssh]$ chmod 600 authorized_keys
[root@host .ssh]$ chmod 700 ~/.ssh

生成秘钥其他几种模式

ssh-keygen -t rsa -b 4096
ssh-keygen -t dsa
ssh-keygen -t ecdsa -b 521
ssh-keygen -t ed25519
  • -t 选择加密算法: 有四种: rsa,dsa,ecdsa,ed25519,网站推荐使用ecdsa
  • -b 选择加密算法的长度,只有rsa何ecdsa有这个选项,rsa分为2048和4096,ecdsa分为256,384,521位。建议521位
  • -f 指定文件名和文件地址,会生成/root/rsaKey私钥和/root/rsaKey.pub公钥

秘钥和密码同时认证

vim /etc/ssh/sshd_config

修改配置

PermitRootLogin yes         #允许root认证登录
PasswordAuthentication yes  #允许密码认证

RSAAuthentication yes       #秘钥认证
PubkeyAuthentication yes    #秘钥认证

重启 SSH 服务:

[root@host .ssh]$ service sshd restart

仅秘钥认证关闭密码登录

修改时注意保持一个连接到服务器的窗口不关闭,防止出现意外。

vim /etc/ssh/sshd_config

修改配置

PermitRootLogin yes         #允许root认证登录
PasswordAuthentication no  #允许密码认证

RSAAuthentication yes       #秘钥认证
PubkeyAuthentication yes    #秘钥认证

重启 SSH 服务:

[root@host .ssh]$ service sshd restart

参考

  1. Linux ssh服务开启秘钥和密码认证
  2. 菜鸟教程 - 设置 SSH 通过密钥登录
powered by GitbookFile Modify: 2022-02-26 17:35:05

results matching ""

    No results matching ""