Mobai's Blog

Go Back
Last updated on

快捷ssh密钥登陆


快捷ssh密钥登陆

  1. 配置公钥和私钥

    ssh-keygen -t rsa -C '-C这里用来添加你的注释信息'
  2. 传输公钥

    scp ~/.ssh/id_rsa.pub <用户>@<ip地>:/home/id_rsa.pub
  3. 添加权限

    mkdir -p ~/.ssh && chmod 700 ~/.ssh 
    cat /home/id_rsa.pub >> ~/.ssh/authorized_keys 
    chmod 600 ~/.ssh/authorized_keys
  4. 本地配置用户

    vim  ~/.ssh/config
    
    # 模版如下
    Host host_name
      HostName host_ip
      User root
      Port 22
      IdentityFile ~/.ssh/id_rsa
  5. SSH服务端

    vim /etc/ssh/sshd_config

    将前面的注释符删掉才能生效。

    PermitRootLogin yes
    PubkeyAuthentication yes
    AuthorizedKeysFile .ssh/authorized_keys
    # 只接收公钥认证
    PasswordAuthentication no
    systemctl restart sshd