{"id":552,"date":"2023-01-26T17:08:42","date_gmt":"2023-01-26T17:08:42","guid":{"rendered":"https:\/\/merserver.com\/?p=552"},"modified":"2023-01-26T17:08:43","modified_gmt":"2023-01-26T17:08:43","slug":"how-to-setup-an-sftp-server-with-rsa-public-key-authentication-mechanism","status":"publish","type":"post","link":"http:\/\/merserver.com\/index.php\/2023\/01\/26\/how-to-setup-an-sftp-server-with-rsa-public-key-authentication-mechanism\/","title":{"rendered":"How to setup an SFTP server with RSA public key authentication\u00a0mechanism"},"content":{"rendered":"\n<p><em>Why SFTP over FTP? The reason is visualized in its name: \u201cS\u201d, that means Security. Using SSH will employ a client-server model to authenticate two parties and encrypt the data between them. This topic will guide you through how to setup an&nbsp;<\/em>SFTP<em>&nbsp;authentication mechanism using&nbsp;public key cryptography, the working OS is CentOS version 6.9. Let\u2019s get started!<\/em><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">1. Make sure ssh and ssh-server are installed<\/h4>\n\n\n\n<pre class=\"wp-block-preformatted\">user@localhost:$which ssh\n\/usr\/bin\/ssh<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">2. Create a new user and a new group in server<\/h4>\n\n\n\n<p>To be easier and clarified in permission managing, we should create a separated group for SFTP and add the corresponding user to the group. To acquire this, using the following groupadd and useradd command under root:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">user@localhost:$sudo groupadd sftp_users \nuser@localhost:$sudo useradd <strong>sftp_user1 <\/strong>\nuser@localhost:$sudo passwd sftp_user1\nuser@localhost:$sudo usermod -G sftp_users sftp_user1<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">3. Generate RSA public and private key<\/h4>\n\n\n\n<p>Let\u2019s make a recall how public key cryptography works. This&nbsp;<a href=\"https:\/\/www.digitalocean.com\/community\/tutorials\/understanding-the-ssh-encryption-and-connection-process\" target=\"_blank\" rel=\"noreferrer noopener\">link<\/a>&nbsp;for detail. In short,&nbsp;SSH key pairs can be used to authenticate a client to a server. The client creates a key pair and then uploads the public key to any remote server it wishes to access. This is placed in a file called&nbsp;<code>authorized_keys<\/code>&nbsp;within the&nbsp;<code>~\/.ssh<\/code>&nbsp;directory in the user account\u2019s home directory on the remote server.<\/p>\n\n\n\n<p>If you\u2019re under *nix based OS, you can use ssh-keygen to generate keys as being described below. <\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">user@localhost:$ssh-keygen -t rsa -f sftp_rsa<\/pre>\n\n\n\n<p>Then, copy the public key to the server within the ~\/.ssh folder (<strong>corresponding to which user will be authenticated<\/strong>).<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">user@localhost:$cd \/home\/<strong>sftp_user1<\/strong>\/ \nuser@localhost:$mkdir .ssh # In case of no .ssh folder inside\nuser@localhost:$ls -a\n...\n<strong>.ssh<\/strong>\n...\nuser@localhost:$cd .ssh # &lt;= Copy the public key to this folder<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">4. Correct permissions and owner<\/h4>\n\n\n\n<pre class=\"wp-block-preformatted\">user@localhost:$cd \/home\/<strong>sftp_user1<\/strong>\/\nuser@localhost:$chmod 700 .ssh \nuser@localhost:$chown sftp_user1:sftp_user1 .ssh \nuser@localhost:$cd .ssh mv sftp_rsa.pub authorized_keys\nuser@localhost:$chmod 600 authorized_keys\nuser@localhost:$chown sftp_user1:sftp_user1 authorized_keys<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">5. Change SSH configurations<\/h4>\n\n\n\n<pre class=\"wp-block-preformatted\">user@localhost:$vi \/etc\/ssh\/sshd_config<\/pre>\n\n\n\n<p>Check the following configurations (uncomment these settings by removing # if needed):<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">RSAAuthentication yes\nPubkeyAuthentication yes\nAuthorizedKeysFile .ssh\/authorized_keys\nPasswordAuthentication no<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">6. Restart the service<\/h4>\n\n\n\n<pre class=\"wp-block-preformatted\"> user@localhost:$service sshd restart<\/pre>\n\n\n\n<p>Now you can check if it works by using any program that supports SFTP.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Why SFTP over FTP? The reason is visualized in its name: \u201cS\u201d, that means Security. Using SSH will employ a client-server model to authenticate two parties and encrypt the data between them. This topic will guide you through how to setup an&nbsp;SFTP&nbsp;authentication mechanism using&nbsp;public key cryptography, the working OS is CentOS version 6.9. Let\u2019s get&#8230;<\/p>\n<p class=\"more-link-wrap\"><a href=\"http:\/\/merserver.com\/index.php\/2023\/01\/26\/how-to-setup-an-sftp-server-with-rsa-public-key-authentication-mechanism\/\" class=\"more-link\">Read More<span class=\"screen-reader-text\"> &ldquo;How to setup an SFTP server with RSA public key authentication\u00a0mechanism&rdquo;<\/span> &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[],"class_list":["post-552","post","type-post","status-publish","format-standard","hentry","category-it"],"_links":{"self":[{"href":"http:\/\/merserver.com\/index.php\/wp-json\/wp\/v2\/posts\/552","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/merserver.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/merserver.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/merserver.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/merserver.com\/index.php\/wp-json\/wp\/v2\/comments?post=552"}],"version-history":[{"count":1,"href":"http:\/\/merserver.com\/index.php\/wp-json\/wp\/v2\/posts\/552\/revisions"}],"predecessor-version":[{"id":553,"href":"http:\/\/merserver.com\/index.php\/wp-json\/wp\/v2\/posts\/552\/revisions\/553"}],"wp:attachment":[{"href":"http:\/\/merserver.com\/index.php\/wp-json\/wp\/v2\/media?parent=552"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/merserver.com\/index.php\/wp-json\/wp\/v2\/categories?post=552"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/merserver.com\/index.php\/wp-json\/wp\/v2\/tags?post=552"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}