{"id":1342,"date":"2016-11-27T17:46:52","date_gmt":"2016-11-28T01:46:52","guid":{"rendered":"https:\/\/www.cloudinsidr.com\/content\/?p=1342"},"modified":"2018-09-06T14:12:05","modified_gmt":"2018-09-06T21:12:05","slug":"how-to-set-up-remote-access-via-sftp-to-a-web-server-root-directory-on-rhelcentosfedora-using-key-pairs","status":"publish","type":"post","link":"https:\/\/www.cloudinsidr.com\/content\/how-to-set-up-remote-access-via-sftp-to-a-web-server-root-directory-on-rhelcentosfedora-using-key-pairs\/","title":{"rendered":"How to set up remote access via SFTP to a web server root directory on RHEL\/CentOS\/Fedora using key pairs"},"content":{"rendered":"<p>Your users want to access a web server\u00a0instance as\u00a0a staging or production environment for\u00a0DevOps&#8230; They want access to the web server document root of the sites they manage. Your job is to maintain the integrity of the whole system in terms of cyber security.<\/p>\n<p>If you happen to be running a web server\u00a0on Linux\u2014for example in EC2 on Amazon AWS\u2014and need to provide site owners remote access in a secure and responsible manner, here is how to do it.<\/p>\n<p><!--more--><\/p>\n<p>Rule No. 1: Do NOT enable FTP. Unsecured FTP is a can of worms. Use SFTP (Secure FTP over an SSH tunnel).<\/p>\n<p>What you will need: a Linux instance (preferably with SELinux enabled).\u00a0In this tutorial, NGINX is the web server, php-fpm is the PHP interpreter.<\/p>\n<h4>Step 1. Set proper privileges for\u00a0your website administrators<\/h4>\n<p>Each website (or a set of related websites) should be owned by its own Linux user (for information on creating Unix\/Linux users, see <a href=\"https:\/\/www.cloudinsidr.com\/content\/lemp-how-to-set-up-nginx-with-mariadbmysql-and-php-7-x-on-centos-7-rhelfedora\/\">this post<\/a>).<\/p>\n<p>The user needs shell access and a home directory somewhere outside of the web server&#8217;s document root (one good place is \/home\/username).<\/p>\n<p>The website owner also needs to own the php-fpm pool that corresponds to that website (user = websiteowner1 in the configuration file which creates the pool, see <a href=\"https:\/\/www.cloudinsidr.com\/content\/lemp-how-to-set-up-nginx-with-mariadbmysql-and-php-7-x-on-centos-7-rhelfedora\/\">this post<\/a> for more details).<\/p>\n<p>Once this is done, it&#8217;s time to allow access to external software tools\u00a0which support SFTP (such as Adobe Dreamweaver CC, FileZilla and others).<\/p>\n<h4>Step 2. Disallow remote authentication by root and verify the ssh configuration\u00a0of your Linux server<\/h4>\n<p>You need a way of signing in to the remote server using ssh without authenticating as root.\u00a0You should\u00a0assume superuser privileges (sudo su) only after successful authentication with lower-tier credentials (for example, identified as &#8220;administrator&#8221;, &#8220;ec2-user&#8221;, &#8220;centos&#8221; or whatever you choose to name your remote\u00a0user). Do not edit the sudoers file. Instead, add your user to the group wheel:<\/p>\n<pre>usermod -aG wheel <span class=\"highlight\">username<\/span><\/pre>\n<p>and you will be able to sudo su after successful authentication. (By default, all members of the <code>wheel<\/code> group on RHEL\/CentOS\/Fedora can assume\u00a0sudo privileges). Next, verify the ssh setup of your remote machine.<\/p>\n<p>CentOS\/RHEL\/Fedora store SSH server settings in \/etc\/ssh\/sshd_config:<\/p>\n<pre class=\"screen\">nano \/etc\/ssh\/sshd_config<\/pre>\n<p>In order to override the default of &#8220;no subsystems&#8221;, make sure that this line is <strong>not<\/strong> commented out:<\/p>\n<pre>Subsystem sftp \/usr\/libexec\/openssh\/sftp-server<\/pre>\n<p>To disable remote logins with the credentials of the root user, make sure you have the following entry:<\/p>\n<pre># Prevent root logins:\r\nPermitRootLogin no<\/pre>\n<p>When you complete changes to \/etc\/ssh\/sshd_config, restart the sshd service:<\/p>\n<pre>systemctl restart sshd.service<\/pre>\n<p>Now it&#8217;s time to set up access for your site owners.<\/p>\n<h4>Step 3. Create a key pair for remote access<\/h4>\n<p>On the client machine from which you wish a user to connects, create a key pair. This has to be done locally on the computer that is going to initiate a connection; this is the only way to ensure that the private key does not\u00a0traverse a network.<\/p>\n<p>On a Linux desktop, this simple task is best accomplished by running ssh-keygen, for example:<\/p>\n<pre>$ ssh-keygen -t rsa<\/pre>\n<p>On a Windows desktop, you can <a href=\"https:\/\/www.cloudinsidr.com\/content\/?p=1344&amp;preview=true\" target=\"_blank\" rel=\"noopener\">create OpenSSH keys using\u00a0PuTTYgen as described in this post<\/a>.<\/p>\n<p>A key can be protected by a passphrase. The passphrase, however, is non-recoverable. Should it get lost, your user will need new keys and you will have to repeat the setup. Unless you set a passphrase, however, anyone in the possession of the\u00a0private key can gain access to the server. The balance between security and\u00a0usability is a question of <strong>your priorities<\/strong>.\u00a0On systems which represent high-value targets, a passphrase adds tangible value to the setup. This is also the case when the private key resides on a mobile device which can easily be lost\u00a0or stolen.<\/p>\n<p><strong>IMPORTANT: Each public key must be provided as a one-liner on a separate line.<\/strong> This is the\u00a0format PuTTy &#8216;s Key Generator will display right in its application window (it&#8217;s not the same public key\u00a0you obtain when you click on &#8220;Save public key&#8221;! It is important to be aware of this not-so-subtle distinction.)<\/p>\n<figure id=\"attachment_1355\" aria-describedby=\"caption-attachment-1355\" style=\"width: 758px\" class=\"wp-caption alignnone\"><a href=\"https:\/\/www.cloudinsidr.com\/content\/?attachment_id=1355\" rel=\"attachment wp-att-1353\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-1355 size-full\" src=\"https:\/\/www.cloudinsidr.com\/content\/wp-content\/uploads\/2016\/11\/PuTTYGen_save_keys_authorized_keys-2.png\" alt=\"PuTTgen: save your public key for use with OpenSSH\" width=\"758\" height=\"525\" \/><\/a><figcaption id=\"caption-attachment-1355\" class=\"wp-caption-text\">PuTTgen: save your public key for use with OpenSSH<\/figcaption><\/figure>\n<h4>Step 4. Set permissions on the\u00a0private key on your local computer<\/h4>\n<p>Set permissions on your private key (the one which remains stored on your local computer):<\/p>\n<pre>$ chmod 700 ~\/.ssh\r\n$ chmod 600 ~\/.ssh\/id_rsa<\/pre>\n<p>(In the above example, id_rsa is the name of your private key and ~\/.ssh is the containing directory).<\/p>\n<p>On SELinux-enabled systems you also need to make sure that the .ssh directory\u00a0has\u00a0the correct SELinux contexts:<\/p>\n<pre>[root@ip-1-2-3-4 .ssh]# ls -laZ\u00a0\r\ndrwx------. user1 user1 unconfined_u:object_r:ssh_home_t:s0 .ssh<\/pre>\n<p>The same goes for its contents:<\/p>\n<pre>[root@ip-1-2-3-4 .ssh]# ls -laZ\r\ndrwx------. user1 user1 unconfined_u:object_r:ssh_home_t:s0 .\r\ndrwx------. user1 user1 unconfined_u:object_r:user_home_dir_t:s0 ..\r\n-rw-------. user1 user1 unconfined_u:object_r:ssh_home_t:s0 authorized_keys<\/pre>\n<p>In order to apply any corrections based on the system defaults, run restorecon:<\/p>\n<pre>restorecon -R -v \/home\/kreativj\/.ssh<\/pre>\n<h4>Step 5. Install your public key on the server<\/h4>\n<p>Only the public key is transferred to the server; the private key remains on the machine where it was generated.<\/p>\n<p>Install the public key on the remote server\u00a0by appending it to the\u00a0~\/.ssh\/authorized_keys file, which is located in the home directory of the user the key is intended for:<\/p>\n<pre>$ cat id_rsa.pub &gt;&gt; ~\/.ssh\/authorized_keys<\/pre>\n<p>A newly created user may have no .ssh directory. If this is the case, create it and set permissions on it as follows:<\/p>\n<pre>chown -Rf user1:user1 \/home\/user1home\r\nchcon -R unconfined_u:object_r:ssh_home_t:s0  \/home\/user\/.ssh\/<\/pre>\n<p>or permanently:<\/p>\n<pre>semanage fcontext -a unconfined_u:object_r:ssh_home_t:s0 \/home\/user1home\/.ssh\/\r\nsemanage fcontext -a unconfined_u:object_r:user_home_t:s0 \/home\/user1home\/<\/pre>\n<p>Create the key file:<\/p>\n<pre>touch\u00a0.ssh\/authorized_keys file<\/pre>\n<p><a href=\"https:\/\/www.digitalmastersmag.com\/magazine\/how-to-set-up-adobe-dreamweaver-for-uploads-to-amazon-aws-ec2-without-an-ftp-account\/\" target=\"_blank\" rel=\"noopener\">Here is how\u00a0to set up Dreamweaver for access to the Linux server with the keys just created<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Your users want to access a web server\u00a0instance as\u00a0a staging or production environment for\u00a0DevOps&#8230; They want access to the web server document root of the sites they manage. Your job is to maintain the integrity of the whole system in terms of cyber security. If you happen to be running a web server\u00a0on Linux\u2014for example [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1355,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_genesis_hide_title":false,"_genesis_hide_breadcrumbs":false,"_genesis_hide_singular_image":false,"_genesis_hide_footer_widgets":false,"_genesis_custom_body_class":"","_genesis_custom_post_class":"","_genesis_layout":"","footnotes":""},"categories":[16,33,111,143,109,154,6],"tags":[164,165,28,7,70,166],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v14.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>How to set up remote access via SFTP to a web server root directory on RHEL\/CentOS\/Fedora using key pairs - CloudInsidr<\/title>\n<meta name=\"robots\" content=\"index, follow\" \/>\n<meta name=\"googlebot\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<meta name=\"bingbot\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.cloudinsidr.com\/content\/how-to-set-up-remote-access-via-sftp-to-a-web-server-root-directory-on-rhelcentosfedora-using-key-pairs\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to set up remote access via SFTP to a web server root directory on RHEL\/CentOS\/Fedora using key pairs - CloudInsidr\" \/>\n<meta property=\"og:description\" content=\"Your users want to access a web server\u00a0instance as\u00a0a staging or production environment for\u00a0DevOps&#8230; They want access to the web server document root of the sites they manage. Your job is to maintain the integrity of the whole system in terms of cyber security. If you happen to be running a web server\u00a0on Linux\u2014for example [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.cloudinsidr.com\/content\/how-to-set-up-remote-access-via-sftp-to-a-web-server-root-directory-on-rhelcentosfedora-using-key-pairs\/\" \/>\n<meta property=\"og:site_name\" content=\"CloudInsidr\" \/>\n<meta property=\"article:published_time\" content=\"2016-11-28T01:46:52+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2018-09-06T21:12:05+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.cloudinsidr.com\/content\/wp-content\/uploads\/2016\/11\/PuTTYGen_save_keys_authorized_keys-2-e1480297408650.png\" \/>\n\t<meta property=\"og:image:width\" content=\"758\" \/>\n\t<meta property=\"og:image:height\" content=\"367\" \/>\n<meta name=\"twitter:card\" content=\"summary\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.cloudinsidr.com\/content\/#website\",\"url\":\"https:\/\/www.cloudinsidr.com\/content\/\",\"name\":\"CloudInsidr\",\"description\":\"Cyber security, infotech\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":\"https:\/\/www.cloudinsidr.com\/content\/?s={search_term_string}\",\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/www.cloudinsidr.com\/content\/how-to-set-up-remote-access-via-sftp-to-a-web-server-root-directory-on-rhelcentosfedora-using-key-pairs\/#primaryimage\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/www.cloudinsidr.com\/content\/wp-content\/uploads\/2016\/11\/PuTTYGen_save_keys_authorized_keys-2-e1480297408650.png\",\"width\":758,\"height\":367,\"caption\":\"PuTTgen: save your public key for use with OpenSSH\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.cloudinsidr.com\/content\/how-to-set-up-remote-access-via-sftp-to-a-web-server-root-directory-on-rhelcentosfedora-using-key-pairs\/#webpage\",\"url\":\"https:\/\/www.cloudinsidr.com\/content\/how-to-set-up-remote-access-via-sftp-to-a-web-server-root-directory-on-rhelcentosfedora-using-key-pairs\/\",\"name\":\"How to set up remote access via SFTP to a web server root directory on RHEL\/CentOS\/Fedora using key pairs - CloudInsidr\",\"isPartOf\":{\"@id\":\"https:\/\/www.cloudinsidr.com\/content\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.cloudinsidr.com\/content\/how-to-set-up-remote-access-via-sftp-to-a-web-server-root-directory-on-rhelcentosfedora-using-key-pairs\/#primaryimage\"},\"datePublished\":\"2016-11-28T01:46:52+00:00\",\"dateModified\":\"2018-09-06T21:12:05+00:00\",\"author\":{\"@id\":\"https:\/\/www.cloudinsidr.com\/content\/#\/schema\/person\/dd6ee9cb21cf05763fd7cff3d6f11b2b\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.cloudinsidr.com\/content\/how-to-set-up-remote-access-via-sftp-to-a-web-server-root-directory-on-rhelcentosfedora-using-key-pairs\/\"]}]},{\"@type\":[\"Person\"],\"@id\":\"https:\/\/www.cloudinsidr.com\/content\/#\/schema\/person\/dd6ee9cb21cf05763fd7cff3d6f11b2b\",\"name\":\"Cloud Insidr\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/www.cloudinsidr.com\/content\/#personlogo\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/8b2fa1415b3d573b97d818b8f8f83b7c?s=96&d=mm&r=g\",\"caption\":\"Cloud Insidr\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","_links":{"self":[{"href":"https:\/\/www.cloudinsidr.com\/content\/wp-json\/wp\/v2\/posts\/1342"}],"collection":[{"href":"https:\/\/www.cloudinsidr.com\/content\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.cloudinsidr.com\/content\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.cloudinsidr.com\/content\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.cloudinsidr.com\/content\/wp-json\/wp\/v2\/comments?post=1342"}],"version-history":[{"count":17,"href":"https:\/\/www.cloudinsidr.com\/content\/wp-json\/wp\/v2\/posts\/1342\/revisions"}],"predecessor-version":[{"id":2373,"href":"https:\/\/www.cloudinsidr.com\/content\/wp-json\/wp\/v2\/posts\/1342\/revisions\/2373"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.cloudinsidr.com\/content\/wp-json\/wp\/v2\/media\/1355"}],"wp:attachment":[{"href":"https:\/\/www.cloudinsidr.com\/content\/wp-json\/wp\/v2\/media?parent=1342"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.cloudinsidr.com\/content\/wp-json\/wp\/v2\/categories?post=1342"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.cloudinsidr.com\/content\/wp-json\/wp\/v2\/tags?post=1342"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}