How to install and configure SSH server on Debian Linux?

Installing and testing the SSH Server

You will need to be root to do the following…

su

  1. Install SSH Server

    apt install openssh-server
     

  2. Check if the server process exists. This indicates the install went well.

    ps -A | grep sshd

    արտադրանք (or similar): 1753 ? 00:00:00 sshd

     

  3. Install SSH Client to check the SSH Server connection

    apt install openssh-client

    Connect to localhost to test connection

    ssh localhost

    Also login with the interface IP address

    Get IP Address

    ip addr

    SSH to the ip address

    ssh {ipaddress here}
     

Securing the SSH Server

It is a good idea to secure the SSH server by disallowing root to login and configuring login only by public key i.e. login using passwords is not allowed

Ensure to execute the following commands as your user and not as root

  1. Navigate to the user home directory

    cd
     

  2. Check if .ssh exists

    ls -a
     

  3. If .ssh does not exist, create it

    mkdir .ssh
     

  4. Set the folder permission to 700

    chmod 700 .ssh
     

  5. Create the authorized_keys file

    touch ./.ssh/authorized_keys
     

  6. Change permission on the authorized_keys file to 600

    chmod 600 ./.ssh/authorized_keys
     

  7. Բացեք authorized_keys ֆայլը

    nano ./.ssh/authorized_keys
     

  8. Կպցնել Ձեր հանրային բանալին դեպի ֆայլը

    Դուք կարող եք ստանալ Ձեր հանրային բանալին է SSH հաճախորդի. Եթե ​​հաճախորդը Windows 10 ապա դա պետք է, որ %USERPROFILE%/.ssh/id_rsa.pub ֆայլ. Եթե ​​հաճախորդը Debian, ապա հանրային բանալին ~/.ssh/id_rsa.pub.

    Եթե ​​ձեր հանրային բանալին գոյություն չունի, առաջացնում այն ​​հաճախորդի մեքենայի

    ssh-keygen -t rsa -C "email@domain.com"
     

  9. Փոփոխություն է արմատի մասին

    su

  10. Կարգավորել ձեր ssh սերվեր է ընդունել միայն հանրային առանցքային logins եւ կանխել մուտքի համար արմատից

    nano /etc/ssh/sshd_config

    Uncomment եւ / կամ խմբագրել հետեւյալ տողերը

    PermitRootLogin no
    StrictModes yes
    PubkeyAuthentication yes
    AuthorizedKeysFile .ssh/authorized_keys .ssh/authorized_keys2
    PasswordAuthentication no
     

  11. Վերագործարկեք sshd ծառայությունը

    service sshd restart
     

  12. Փորձեք մուտք գործելու, ինչպես նաեւ արմատից Ձեր SSH հաճախորդի օգտագործելով գաղտնաբառը

    ssh root@{ssh server ip address}

    արտադրանք: Permission denied (publickey).

     

  13. Try logging in as username from your SSH client using password

    ssh username@{ssh server ip address}

    արտադրանք: Permission denied (publickey).

     

  14. Try logging in as root from your SSH client using the public key

    ssh root@{ssh server ip address} -i .ssh/id_rsa

    արտադրանք: Permission denied (publickey).

     

  15. Log in as username from your SSH client using the public key

    ssh username@{ssh server ip address} -i .ssh/id_rsa
     

If your SSH client has a static IP address then you may want to consider only your IP address can login to the SSH server.