{"id":225,"date":"2020-04-20T05:09:45","date_gmt":"2020-04-20T05:09:45","guid":{"rendered":"http:\/\/shresthabrijan.com.np\/?p=225"},"modified":"2020-04-20T05:10:43","modified_gmt":"2020-04-20T05:10:43","slug":"ssh-in-linux","status":"publish","type":"post","link":"https:\/\/shresthabrijan.com.np\/?p=225","title":{"rendered":"SSH in linux"},"content":{"rendered":"\n<p>SSH, an abbreviation of Secure Shell, is a protocol used to secure communication between two devices in an insecure network channel. It supports a wide range of encryption technology including custom made ones. <\/p>\n\n\n\n<p>SSH server is an application which uses SSH protocol to establish connection from remote clients. There are different options available for ssh server but OpenSSH, an abbreviation for OpenBSH Secure Shell which was developed by OpenBSD developers and distributed under open source license, is the most popular and widely used ssh server in linux.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">About config<\/h2>\n\n\n\n<p>One should note that after successful installation of SSH server, there is two main config files under the main ssh config directory, viz; ssh_config and sshd_config. The two files represent client and server setting respectively.<\/p>\n\n\n\n<p>sshd_config: Any settings defined inside this file is used by the ssh server running in the host as default. For example, if port 2233 is defined inside the file as non-default port, the ssh server will only listen in the defined port for the connection from client<\/p>\n\n\n\n<p>ssh_config: Entries in this file is used as defaults by ssh client for any remote connection. For example, if port 2211 is defined to be used for ssh in this file, any new connection to remote ssh server will be initiated on 2211 port by default.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"> Securing SSH server<\/h2>\n\n\n\n<p>There are a bunch of measures one can take for hardening SSH server security. <\/p>\n\n\n\n<h5 class=\"wp-block-heading\">Disable root access<\/h5>\n\n\n\n<p>It is no surprise that an ssh server accepting connection in wild i.e. the internet is hit by a number of uninvited guests, human or bot. As a result, allowing a omnipresent linux superuser account <em>root<\/em> access is just a one password crack away disaster waiting to happen. This can be prevented by disabling root use access to ssh server and using non-default user to login and later switch to root. This can be done by setting following parameter in ssh config file:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">     PermitRootLogin no<\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">Use non-default port<\/h5>\n\n\n\n<p>Similar to default user, ssh server listens to default port 22 unless defined otherwise. Anyone trying to hit an ssh server will attempt their first attack on default port 22. So, it is a good practice to use any non-default port for ssh connection. This can be defined using the following parameter:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"> Port 2233<\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">Disable empty password<\/h5>\n\n\n\n<p>Using empty password should be completely discouraged for obvious reason and this can be done by setting following value:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"> PermitEmptyPasswords no<\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">Enforce IP based access<\/h5>\n\n\n\n<p>This is a robust method to secure an ssh server. By defining this parameter, all connection request from IPs except the ones defined is dropped by the ssh server. This can be implemented by defining value as follows:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">ListenAddress 1.2.3.4<\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">Use passwordless login<\/h5>\n\n\n\n<p>SSH supports login using public-private key authentication method. Following steps can be used to setup ssh keys for connection:<\/p>\n\n\n\n<p>On client machine, run:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ ssh-keygen\nGenerating public\/private rsa key pair. Enter file in which to save the key (\/home\/&lt;username>\/.ssh\/id_rsa):<\/pre>\n\n\n\n<p>After running the command, the tool will ask for the location to save the keys which will be stored at ~\/.ssh directory. This will generate private key, id_rsa, and public key, id_rsa.pub. It will also ask if you want to use a passphrase for authentication and if provided will ask for the passphrase on every login attempt.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Enter passphrase (empty for no passphrase):\nEnter same passphrase again:\nYour identification has been saved in \/home\/infokonn\/.ssh\/id_rsa.\nYour public key has been saved in \/home\/infokonn\/.ssh\/id_rsa.pub.\nThe key fingerprint is:\nSHA256:8OVEF6bnTZ5XGRD9i1efr4kNxVSmHJju6Q7oWbURZlQ infokonn@helpdesk.infokonn.com\nThe key's randomart image is:\n+---[RSA 2048]----+\n| . =BE.o|\n| . =o. *o|\n| . +.= =.o|\n| o + =.B .+|\n| S ..+.B *|\n| . .o= =.|\n| . o.o . .|\n| . o ..+ ..|\n| o .o +. |\n+----[SHA256]-----+<\/pre>\n\n\n\n<p>If you list the files in your location, you will find the two keys:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ ls .ssh<br>id_rsa id_rsa.pub<\/pre>\n\n\n\n<h6 class=\"wp-block-heading\"><strong>Copy public key to ssh server<\/strong><\/h6>\n\n\n\n<p>After the successful key generation, the public key should be copied to the ssh server&#8217;s key directory, usually .ssh as authorized_keys<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ scp ~\/.ssh\/id_rsa.pub user@ssh-server:~\/.ssh\/authorized_keys<\/pre>\n\n\n\n<p>This can be also be done using the utility SSH-Copy-ID <\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ ssh-copy-id user@ssh-server<\/pre>\n\n\n\n<p>After this, try login into ssh server and you will be able to login without a password or using passphrase if you used one.<\/p>\n\n\n\n<p>Since you don&#8217;t need to enter password anymore, disable the password authentication so that you can login using just the keys or passphrase in future by setting following value in ssh configuration:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">PasswordAuthentication no<\/pre>\n\n\n\n<p>Finally, restart the ssh server after making changes to the configuration files so that the new settings will be applied.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>SSH, an abbreviation of Secure Shell, is a protocol used to secure communication between two devices in an insecure network channel. It supports a wide range of encryption technology including custom made ones. SSH server is an application which uses SSH protocol to establish connection from remote clients. There are different options available for ssh [&hellip;]<\/p>\n<div class=\"read-more\"><a href=\"https:\/\/shresthabrijan.com.np\/?p=225\" class=\"btn btn-primary btn-lg\">Read More<\/a><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[],"class_list":["post-225","post","type-post","status-publish","format-standard","hentry","category-system-administration"],"_links":{"self":[{"href":"https:\/\/shresthabrijan.com.np\/index.php?rest_route=\/wp\/v2\/posts\/225","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/shresthabrijan.com.np\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/shresthabrijan.com.np\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/shresthabrijan.com.np\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/shresthabrijan.com.np\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=225"}],"version-history":[{"count":3,"href":"https:\/\/shresthabrijan.com.np\/index.php?rest_route=\/wp\/v2\/posts\/225\/revisions"}],"predecessor-version":[{"id":228,"href":"https:\/\/shresthabrijan.com.np\/index.php?rest_route=\/wp\/v2\/posts\/225\/revisions\/228"}],"wp:attachment":[{"href":"https:\/\/shresthabrijan.com.np\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=225"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/shresthabrijan.com.np\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=225"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/shresthabrijan.com.np\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=225"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}