<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Linux Explore &#187; chroot</title>
	<atom:link href="https://blog.linuxexplore.com/tag/chroot/feed/" rel="self" type="application/rss+xml" />
	<link>https://blog.linuxexplore.com</link>
	<description>Exploring Linux</description>
	<lastBuildDate>Mon, 07 Apr 2014 00:30:50 +0000</lastBuildDate>
	<language>en-US</language>
		<sy:updatePeriod>hourly</sy:updatePeriod>
		<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=4.0.38</generator>
	<item>
		<title>Chroot SFTP Users for Web Hosting Server.</title>
		<link>https://blog.linuxexplore.com/2013/02/20/chroot-sftp-users-for-web-hosting-server/</link>
		<comments>https://blog.linuxexplore.com/2013/02/20/chroot-sftp-users-for-web-hosting-server/#comments</comments>
		<pubDate>Tue, 19 Feb 2013 20:37:25 +0000</pubDate>
		<dc:creator><![CDATA[linuxexplore]]></dc:creator>
				<category><![CDATA[Linux Explore How to]]></category>
		<category><![CDATA[centos 6]]></category>
		<category><![CDATA[chroot]]></category>
		<category><![CDATA[chroot jail]]></category>
		<category><![CDATA[chroot sftp]]></category>
		<category><![CDATA[featured]]></category>
		<category><![CDATA[internal-sftp]]></category>
		<category><![CDATA[kernel version]]></category>
		<category><![CDATA[Linux Security]]></category>
		<category><![CDATA[openssh]]></category>
		<category><![CDATA[openssh-server]]></category>
		<category><![CDATA[openssh-server-5.3]]></category>
		<category><![CDATA[public_html]]></category>
		<category><![CDATA[restorecon]]></category>
		<category><![CDATA[selinux chroot]]></category>
		<category><![CDATA[setsebool]]></category>
		<category><![CDATA[SFTP]]></category>
		<category><![CDATA[SFTP CentOS]]></category>
		<category><![CDATA[sftp chroot]]></category>
		<category><![CDATA[software]]></category>
		<category><![CDATA[ssh public key]]></category>
		<category><![CDATA[SSH server]]></category>
		<category><![CDATA[sshd_config]]></category>
		<category><![CDATA[ssh_chroot_rw_homedirs]]></category>
		<category><![CDATA[technology]]></category>
		<category><![CDATA[unix operating systems]]></category>
		<category><![CDATA[web hosting]]></category>

		<guid isPermaLink="false">http://linuxexplore.com/?p=717</guid>
		<description><![CDATA[A chroot on Unix operating systems is an operation that changes the apparent root directory for the current running process and its children. A program that is run in such a modified environment cannot name (and therefore normally not access) files outside the designated directory tree. The term &#8220;chroot&#8221; may refer to the chroot(2) system&#8230;]]></description>
				<content:encoded><![CDATA[<p>A <b>chroot</b> on Unix operating systems is an operation that changes the apparent root directory for the current running process and its children. A program that is run in such a modified environment cannot name (and therefore normally not access) files outside the designated directory tree. The term &#8220;chroot&#8221; may refer to the <tt>chroot(2)</tt> system call or the <tt>chroot(8)</tt> wrapper program. The modified environment is called a &#8220;chroot jail&#8221;. From <a title="Chroot" href="http://en.wikipedia.org/wiki/Chroot" target="_blank">Wikipedia</a>.</p>
<p>Why it is required? If you want to set up your Linux box as a web hosting server for its users, you may need to give SFTP access. But they can get access to whole system Linux tree, just for reading but still very unsecure. So it is mandatory to lock them in their home directory.</p>
<p>There are many other applications, it&#8217;s just a common example, so lets start its configuration.</p>
<h2>Linux Box Detail:</h2>
<p>Its mine Linux Box, your Linux system may vary. Only thing to take care is the openssh-server version, because openssh-server-5.3p1 support SFTP chroot. Older version supports but its tricky, please let me k now if you want to know that too.</p>
<p lang="en-IN"><strong>Operating System:</strong> CentOS 6.3/x86_64</p>
<p lang="en-IN"><strong>Kernel Version:</strong> 2.6.32-279.19.1.el6/x86_64</p>
<p lang="en-IN"><strong>Openssh Server Version:</strong> openssh-server-5.3p1-81.el6_3/x86_64</p>
<pre><a href="http://linuxexplore.files.wordpress.com/2013/02/chroot-ori12.png"><img class="aligncenter size-large wp-image-728" alt="chroot-ori1" src="http://linuxexplore.files.wordpress.com/2013/02/chroot-ori12.png?w=610" width="610" height="328" /></a></pre>
<h2>sshd Server Configuration:</h2>
<p>Add the following tail output to your Linux box&#8217;s SSH</p>
<p>server configuration file /etc/ssh/sshd_config.</p>
<pre>[rahulpanwar@myhost ~]# tail -6 /etc/ssh/sshd_config
#Subsystem sftp /usr/libexec/openssh/sftp-server
Subsystem sftp internal-sftp
Match Group www-hosting
ChrootDirectory %h
ForceCommand internal-sftp
AllowTcpForwarding no</pre>
<p lang="en-IN">Then restart sshd service to enable this configuration.</p>
<pre>[rahulpanwar@myhost ~]# sudo /etc/init.d/sshd restart</pre>
<h2>Create Chroot Users:</h2>
<pre>[rahulpanwar@myhost ~]# sudo mkdir /etc/skel/public_html
[rahulpanwar@myhost ~]# sudo groupadd www-hosting
[rahulpanwar@myhost ~]# sudo useradd -s /sbin/nologin -g www-hosting linuxexplore.com</pre>
<h2>Setting Permissions:</h2>
<pre>[rahulpanwar@myhost ~]# sudo chown root:www-hosting /home/linuxexplore.com
[rahulpanwar@myhost ~]# sudo chmod 755 /home/linuxexplore.com</pre>
<p lang="en-IN">That’s all now create multiple users for web hosting, and offer the secure sftp access to your customers.</p>
<h2>Shell Script to Create Web Hosting Users:</h2>
<pre>#!/bin/bash
HOSTING_DIR="/etc/skel/public_html"
CHROOT_GRP="www-hosting"
USR_NAME="$1"

[ ! -d "$HOSTING_DIR" ] &amp;&amp; mkdir -p $HOSTING_DIR
grep ^"${CHROOT_GRP}:" /etc/group || /usr/sbin/groupadd www-hosting
grep ^"${USR_NAMEP}:" /etc/passwd || /usr/sbin/useradd -s /sbin/nologin -g $CHROO_GRP $USR_NAME
chown root:$CHROOT_GRP /home/$USR_NAME
chmod 755 /home/$USR_NAME</pre>
<h2>Selinux Configuration:</h2>
<p lang="en-IN"><a title="Selinux disable temporarily or permanently" href="http://linuxexplore.com/2012/08/04/selinux-disable-temporarily-or-permanently/" target="_blank">Disable the selinux permanently</a> or configure it for read write user&#8217;s home directory in SSH chroot.</p>
<pre>[rahulpanwar@myhost ~]# sudo setsebool -P ssh_chroot_rw_homedirs on
<span style="color:#333333;"><span style="font-family:Monaco, Consolas, Andale Mono, DejaVu Sans Mono, monospace;"><span style="font-size:small;">[rahulpanwar@myhost ~]# sudo restorecon -R /home/$USERNAME</span></span></span></pre>
<h2>Troubleshooting</h2>
<p lang="en-IN">From: <span style="color:#0000ff;"><span style="text-decoration:underline;"><a href="https://wiki.archlinux.org/index.php/SFTP-chroot">https://wiki.archlinux.org/index.php/SFTP-chroot</a></span></span></p>
<pre>sshd[3505]: fatal: bad ownership or modes for chroot directory "/home/linuxexplore.com"</pre>
<p>It&#8217;s ChrootDirectory ownership problem, sshd will reject sftp connections to accounts that are set to chroot into any directory that has ownership/permissions that sshd doesn&#8217;t consider secure. sshd&#8217;s apparently strict ownership/permissions requirements dictate that every directory in the chroot path must be owned by root and only writable for the owner. So, for example, if the chroot environment is in a user&#8217;s home directory both /home and /home/username must be owned by root and have permissions like 755 or 750 ( group ownership should allow user to access ).</p>
<p>If you are using sftp with public key check the following link:</p>
<p><a href="http://www.centos.org/modules/newbb/viewtopic.php?topic_id=37903&amp;forum=59">http://www.centos.org/modules/newbb/viewtopic.php?topic_id=37903&amp;forum=59</a></p>
<p>If chroot environment is in user&#8217;s home directory, make sure user have access to its home directory, or user would not be able to access its publickey, produce the error given in above <a title="CentOS 6" href="http://centos.org" target="_blank">CentOS</a> forum link.</p>
]]></content:encoded>
			<wfw:commentRss>https://blog.linuxexplore.com/2013/02/20/chroot-sftp-users-for-web-hosting-server/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>
