Is production: true
#migrated

Use public/private key for authentication

Created: 19 Mar 2023 Modified: 19 Mar 2023 Lang: English



[Legacy Link]

[Link]

Instructions

  1. Generate key file

    1
    2
    3
    4
    5
    
    #Generate rsa key pair
    ssh-keygen -t rsa
    
    #Or generate rsa with specific bit "-t" option
    ssh-keygen -b 8192 -t rsa
    
  2. Copy public file to the server aimed to connect through ssh

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    
    scp -P{port} ~/.ssh/id_rsa.pub {user1}@{host1}:~
    
    #ssh to host1
    ssh -P{port} {user1}@{host1}
    
    #make directory if not exists
    mkdir ~/.ssh
    
    #make file is not exists
    cat ~/id_rsa.pub >> ~/.ssh/authorized_keys
    chmod 700 ~/.ssh
    chmod 600 ~/.ssh/authorized_keys
    
    #exit ssh session
    
  3. Test the connection

    1
    
    ssh -i ~/.ssh/id_rsa {user1}@{host1}
    
  4. Disable password authentication

    1
    2
    3
    4
    5
    6
    
    sudo vim /etc/ssh/sshd_config
    #########################
    #change from yes to no
    PasswordAuthentication no
    #########################
    sudo systemctl restart sshd.service