<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="ca">
	<id>https://wiki.espai.de/index.php?action=history&amp;feed=atom&amp;title=Jail</id>
	<title>Jail - Historial de revisió</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.espai.de/index.php?action=history&amp;feed=atom&amp;title=Jail"/>
	<link rel="alternate" type="text/html" href="https://wiki.espai.de/index.php?title=Jail&amp;action=history"/>
	<updated>2026-05-27T17:33:50Z</updated>
	<subtitle>Historial de revisió per a aquesta pàgina del wiki</subtitle>
	<generator>MediaWiki 1.39.6</generator>
	<entry>
		<id>https://wiki.espai.de/index.php?title=Jail&amp;diff=1103&amp;oldid=prev</id>
		<title>Marti: Es crea la pàgina amb «==For Ubuntu== From: http://www.brudvik.org/2011/02/chrootjail-sftpscpssh-on-ubuntu/   Sometimes you find yourself in a situation where you would need to make sure that t...».</title>
		<link rel="alternate" type="text/html" href="https://wiki.espai.de/index.php?title=Jail&amp;diff=1103&amp;oldid=prev"/>
		<updated>2012-11-15T03:02:50Z</updated>

		<summary type="html">&lt;p&gt;Es crea la pàgina amb «==For Ubuntu== From: http://www.brudvik.org/2011/02/chrootjail-sftpscpssh-on-ubuntu/   Sometimes you find yourself in a situation where you would need to make sure that t...».&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Pàgina nova&lt;/b&gt;&lt;/p&gt;&lt;div&gt;==For Ubuntu==&lt;br /&gt;
From: http://www.brudvik.org/2011/02/chrootjail-sftpscpssh-on-ubuntu/&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Sometimes you find yourself in a situation where you would need to make sure that the users can’t reach more of the server than you require them to. Often this can be handled by installing some FTP software with SSL/TLS abilities – and that is also what I would do if it would be an option. Your main goal should ALWAYS be to aim for that kind of setup before doing this.&lt;br /&gt;
&lt;br /&gt;
Anyhow, I needed to give user access to SFTP/SCP/SSH, but without giving them access to the entire server, and even more so, no access to other users files either. To me, the logical step would be to make sure they are jailed to their home directories. But how can you achieve this? Sure, you have Restricted Shell (RSSH) but I usually do not like to install more than absolutely necessary, and my paranoia keeps me from installing 3rd-party solutions for SSH access. Yes I know for most part it would probably work out just fine. But let me have my paranoia.&lt;br /&gt;
&lt;br /&gt;
Basically what I needed was a way to setup a CHROOT jail for SFTP/SCP/SSH.&lt;br /&gt;
&lt;br /&gt;
After browsing the internet a bit I found a variety of solutions. Some that didn’t work at all, some that worked to some extent but where you also had to sacrifice your soul to make it work – and when I finally was about to give in, and let RSSH overcome my paranoia, I found a script originally written by Wolfgang Fuschlberger – and the original is available from this article “How to setup a CHROOT jail for SSH / SCP / SFTP with Linux”.&lt;br /&gt;
&lt;br /&gt;
The script didn’t work straight out of the box, but it didn’t need much tweaks to have it running properly. So without further due, here is how you can get this to work as well.&lt;br /&gt;
&lt;br /&gt;
Note that these instructions are for Ubuntu 10.10 (32bit), but any newer version of Ubuntu should work – at least in theory.&lt;br /&gt;
&lt;br /&gt;
*An up-to-date version of Ubuntu 10.10 installed.&lt;br /&gt;
*Do the command: sudo apt-get install mktemp openssh-server&lt;br /&gt;
*Then download script: Jailed User (1430)&lt;br /&gt;
*Unpack the script.&lt;br /&gt;
*Place it where you’d like and make it runnable with chmod u+x jaileduser.sh.&lt;br /&gt;
*Add a new jailed user by doing: ./jaileduser.sh username.&lt;br /&gt;
*Voilá, your new user now have a jailed environment.&lt;br /&gt;
&lt;br /&gt;
By default new users will then be chrooted and jailed to the /home/jail/home/username directory and will only see the contents of /home/jail.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/bin/sh&lt;br /&gt;
#&lt;br /&gt;
# (c) Copyright by Wolfgang Fuschlberger&lt;br /&gt;
#&lt;br /&gt;
#    This program is free software; you can redistribute it and/or modify&lt;br /&gt;
#    it under the terms of the GNU General Public License as published by&lt;br /&gt;
#    the Free Software Foundation; either version 2 of the License, or&lt;br /&gt;
#    (at your option) any later version.&lt;br /&gt;
#&lt;br /&gt;
#    This program is distributed in the hope that it will be useful,&lt;br /&gt;
#    but WITHOUT ANY WARRANTY; without even the implied warranty of&lt;br /&gt;
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the&lt;br /&gt;
#    GNU General Public License for more details.&lt;br /&gt;
#    ( http://www.fsf.org/licenses/gpl.txt )&lt;br /&gt;
#####################################################################&lt;br /&gt;
&lt;br /&gt;
# first Release: 2004-07-30&lt;br /&gt;
RELEASE=&amp;quot;2008-04-26&amp;quot;&lt;br /&gt;
#&lt;br /&gt;
# The latest version of the script is available at&lt;br /&gt;
#   http://www.fuschlberger.net/programs/ssh-scp-sftp-chroot-jail/&lt;br /&gt;
#&lt;br /&gt;
# Feedback is welcome!&lt;br /&gt;
#&lt;br /&gt;
# Thanks for Bugfixes / Enhancements to &lt;br /&gt;
# Michael Prokop &amp;lt;http://www.michael-prokop.at/chroot/&amp;gt;,&lt;br /&gt;
# Randy K., Randy D., Jonathan Hunter and everybody else.&lt;br /&gt;
#####################################################################&lt;br /&gt;
&lt;br /&gt;
#&lt;br /&gt;
# Features:&lt;br /&gt;
# - enable scp and sftp in the chroot-jail&lt;br /&gt;
# - use one directory (default /home/jail/) as chroot for all users&lt;br /&gt;
# - create new accounts&lt;br /&gt;
# - move existing accounts to chroot&lt;br /&gt;
#####################################################################&lt;br /&gt;
&lt;br /&gt;
# path to sshd's config file: needed for automatic detection of the locaten of&lt;br /&gt;
# the sftp-server binary&lt;br /&gt;
SSHD_CONFIG=&amp;quot;/etc/ssh/sshd_config&amp;quot;&lt;br /&gt;
&lt;br /&gt;
# Check if we are called with username or update&lt;br /&gt;
if [ -z &amp;quot;$1&amp;quot; ] ; then&lt;br /&gt;
  echo&lt;br /&gt;
  echo &amp;quot;ERROR: Parameter missing. Did you forget the username?&amp;quot;&lt;br /&gt;
  echo &amp;quot;-------------------------------------------------------------&amp;quot;&lt;br /&gt;
  echo&lt;br /&gt;
  echo &amp;quot;USAGE:&amp;quot;&lt;br /&gt;
  echo &amp;quot;Create new chrooted account or&amp;quot;&lt;br /&gt;
  echo &amp;quot;add existing User to chroot-jail:&amp;quot;&lt;br /&gt;
  echo &amp;quot;-&amp;gt; $0 username&amp;quot;&lt;br /&gt;
  echo&lt;br /&gt;
  echo &amp;quot;or specify the chroot-shell file and path where the jail should be located:&amp;quot;&lt;br /&gt;
  echo &amp;quot;-&amp;gt; $0 username [/path/to/chroot-shell [/path/to/jail]]&amp;quot;&lt;br /&gt;
  echo &amp;quot;Default shell       = /bin/chroot-shell&amp;quot;&lt;br /&gt;
  echo &amp;quot;Default chroot-path = /home/jail&amp;quot;&lt;br /&gt;
  echo &amp;quot;-------------------------------------------------------------&amp;quot;&lt;br /&gt;
  echo&lt;br /&gt;
  echo &amp;quot;Updating files in the chroot-jail:&amp;quot;&lt;br /&gt;
  echo &amp;quot;-&amp;gt; $0 update [/path/to/chroot-shell [/path/to/jail]]&amp;quot;&lt;br /&gt;
  echo &amp;quot;-------------------------------------------------------------&amp;quot;&lt;br /&gt;
  echo&lt;br /&gt;
  echo &amp;quot;To uninstall:&amp;quot;&lt;br /&gt;
  echo &amp;quot; # userdel \$USER&amp;quot;&lt;br /&gt;
  echo &amp;quot; # rm -rf /home/jail&amp;quot;&lt;br /&gt;
  echo &amp;quot; (this deletes all Users' files!)&amp;quot;&lt;br /&gt;
  echo &amp;quot; # rm -f /bin/chroot-shell&amp;quot;&lt;br /&gt;
  echo &amp;quot; manually delete the User's line from /etc/sudoers&amp;quot;&lt;br /&gt;
  exit&lt;br /&gt;
fi&lt;br /&gt;
&lt;br /&gt;
if [ -z &amp;quot;$PATH&amp;quot; ] ; then &lt;br /&gt;
  PATH=/bin:/usr/bin:/usr/local/bin:/sbin:/usr/sbin&lt;br /&gt;
fi&lt;br /&gt;
&lt;br /&gt;
echo&lt;br /&gt;
echo Release: $RELEASE&lt;br /&gt;
echo&lt;br /&gt;
&lt;br /&gt;
echo &amp;quot;Am I root?  &amp;quot;&lt;br /&gt;
if [ &amp;quot;$(whoami &amp;amp;2&amp;gt;/dev/null)&amp;quot; != &amp;quot;root&amp;quot; ] &amp;amp;&amp;amp; [ &amp;quot;$(id -un &amp;amp;2&amp;gt;/dev/null)&amp;quot; != &amp;quot;root&amp;quot; ] ; then&lt;br /&gt;
  echo &amp;quot;  NO!&lt;br /&gt;
&lt;br /&gt;
Error: You must be root to run this script.&amp;quot;&lt;br /&gt;
  exit 1&lt;br /&gt;
fi&lt;br /&gt;
echo &amp;quot;  OK&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
# Check existence of necessary files&lt;br /&gt;
echo &amp;quot;Checking distribution... &amp;quot;&lt;br /&gt;
if [ -f /etc/debian_version ];&lt;br /&gt;
  then echo &amp;quot;  Supported Distribution found&amp;quot;&lt;br /&gt;
       echo &amp;quot;  System is running Debian Linux&amp;quot;&lt;br /&gt;
       DISTRO=DEBIAN;&lt;br /&gt;
elif [ -f /etc/SuSE-release ];&lt;br /&gt;
  then echo &amp;quot;  Supported Distribution found&amp;quot;&lt;br /&gt;
       echo &amp;quot;  System is running SuSE Linux&amp;quot;&lt;br /&gt;
       DISTRO=SUSE;&lt;br /&gt;
elif [ -f /etc/fedora-release ];&lt;br /&gt;
  then echo &amp;quot;  Supported Distribution found&amp;quot;&lt;br /&gt;
       echo &amp;quot;  System is running Fedora Linux&amp;quot;&lt;br /&gt;
       DISTRO=FEDORA;&lt;br /&gt;
elif [ -f /etc/redhat-release ];&lt;br /&gt;
  then echo &amp;quot;  Supported Distribution found&amp;quot;&lt;br /&gt;
       echo &amp;quot;  System is running Red Hat Linux&amp;quot;&lt;br /&gt;
       DISTRO=REDHAT;&lt;br /&gt;
else echo -e &amp;quot;  failed...........\nThis script works best on Debian, Red Hat, Fedora and SuSE Linux!\nLet's try it nevertheless....\nIf some program files cannot be found adjust the respective path in line 98\n&amp;quot;&lt;br /&gt;
#exit 1&lt;br /&gt;
fi&lt;br /&gt;
&lt;br /&gt;
# Specify the apps you want to copy to the jail&lt;br /&gt;
if [ &amp;quot;$DISTRO&amp;quot; = SUSE ]; then&lt;br /&gt;
  APPS=&amp;quot;/bin/bash /bin/cp /usr/bin/dircolors /bin/ls /bin/mkdir /bin/mv /bin/rm /bin/rmdir /bin/sh /bin/su /usr/bin/groups /usr/bin/id /usr/bin/netcat /usr/bin/rsync /usr/bin/ssh /usr/bin/scp /sbin/unix_chkpwd&amp;quot;&lt;br /&gt;
elif [ &amp;quot;$DISTRO&amp;quot; = FEDORA ]; then&lt;br /&gt;
  APPS=&amp;quot;/bin/bash /bin/cp /usr/bin/dircolors /bin/ls /bin/mkdir /bin/mv /bin/rm /bin/rmdir /bin/sh /bin/su /usr/bin/groups /usr/bin/id /usr/bin/nc /usr/bin/rsync /usr/bin/ssh /usr/bin/scp /sbin/unix_chkpwd&amp;quot;&lt;br /&gt;
elif [ &amp;quot;$DISTRO&amp;quot; = REDHAT ]; then&lt;br /&gt;
  APPS=&amp;quot;/bin/bash /bin/cp /usr/bin/dircolors /bin/ls /bin/mkdir /bin/mv /bin/rm /bin/rmdir /bin/sh /bin/su /usr/bin/groups /usr/bin/id /usr/bin/nc /usr/bin/rsync /usr/bin/ssh /usr/bin/scp /sbin/unix_chkpwd&amp;quot;&lt;br /&gt;
elif [ &amp;quot;$DISTRO&amp;quot; = DEBIAN ]; then&lt;br /&gt;
  APPS=&amp;quot;/bin/bash /bin/cp /usr/bin/dircolors /bin/ls /bin/mkdir /bin/mv /bin/rm /bin/rmdir /bin/sh /bin/su /usr/bin/groups /usr/bin/id /usr/bin/rsync /usr/bin/ssh /usr/bin/scp /sbin/unix_chkpwd&amp;quot;&lt;br /&gt;
else&lt;br /&gt;
  APPS=&amp;quot;/bin/bash /bin/cp /usr/bin/dircolors /bin/ls /bin/mkdir /bin/mv /bin/rm /bin/rmdir /bin/sh /bin/su /usr/bin/groups /usr/bin/id /usr/bin/rsync /usr/bin/ssh /usr/bin/scp /usr/sbin/unix_chkpwd&amp;quot;&lt;br /&gt;
fi&lt;br /&gt;
&lt;br /&gt;
# Check existence of necessary files&lt;br /&gt;
echo &amp;quot;Checking for which... &amp;quot; &lt;br /&gt;
#if [ -f $(which which) ] ;&lt;br /&gt;
# not good because if which does not exist I look for an &lt;br /&gt;
# empty filename and get OK nevertheless&lt;br /&gt;
if ( test -f /usr/bin/which ) || ( test -f /bin/which ) || ( test -f /sbin/which ) || ( test -f /usr/sbin/which );&lt;br /&gt;
  then echo &amp;quot;  OK&amp;quot;;&lt;br /&gt;
  else echo &amp;quot;  failed&lt;br /&gt;
&lt;br /&gt;
Please install which-binary!&lt;br /&gt;
&amp;quot;&lt;br /&gt;
exit 1&lt;br /&gt;
fi&lt;br /&gt;
&lt;br /&gt;
echo &amp;quot;Checking for chroot...&amp;quot; &lt;br /&gt;
if [ `which chroot` ];&lt;br /&gt;
  then echo &amp;quot;  OK&amp;quot;;&lt;br /&gt;
  else echo &amp;quot;  failed&lt;br /&gt;
&lt;br /&gt;
chroot not found!&lt;br /&gt;
Please install chroot-package/binary!&lt;br /&gt;
&amp;quot;&lt;br /&gt;
exit 1&lt;br /&gt;
fi&lt;br /&gt;
&lt;br /&gt;
echo &amp;quot;Checking for sudo...&amp;quot; &lt;br /&gt;
if [ `which sudo` ]; then&lt;br /&gt;
  echo &amp;quot;  OK&amp;quot;;&lt;br /&gt;
else &lt;br /&gt;
  echo &amp;quot;  failed&lt;br /&gt;
&lt;br /&gt;
sudo not found!&lt;br /&gt;
Please install sudo-package/binary!&lt;br /&gt;
&amp;quot;&lt;br /&gt;
exit 1&lt;br /&gt;
fi&lt;br /&gt;
&lt;br /&gt;
echo &amp;quot;Checking for dirname...&amp;quot; &lt;br /&gt;
if [ `which dirname` ]; then&lt;br /&gt;
  echo &amp;quot;  OK&amp;quot;;&lt;br /&gt;
else &lt;br /&gt;
  echo &amp;quot;  failed&lt;br /&gt;
&lt;br /&gt;
dirname not found!&lt;br /&gt;
Please install dirname-binary (to be found eg in the package coreutils)!&lt;br /&gt;
&amp;quot;&lt;br /&gt;
exit 1&lt;br /&gt;
fi&lt;br /&gt;
&lt;br /&gt;
echo &amp;quot;Checking for awk...&amp;quot; &lt;br /&gt;
if [ `which awk` ]; then&lt;br /&gt;
  echo &amp;quot;  OK&lt;br /&gt;
&amp;quot;;&lt;br /&gt;
else &lt;br /&gt;
  echo &amp;quot;  failed&lt;br /&gt;
&lt;br /&gt;
awk not found!&lt;br /&gt;
Please install (g)awk-package/binary!&lt;br /&gt;
&amp;quot;&lt;br /&gt;
exit 1&lt;br /&gt;
fi&lt;br /&gt;
&lt;br /&gt;
# get location of sftp-server binary from /etc/ssh/sshd_config&lt;br /&gt;
# check for existence of /etc/ssh/sshd_config and for&lt;br /&gt;
# (uncommented) line with sftp-server filename. If neither exists, just skip&lt;br /&gt;
# this step and continue without sftp-server&lt;br /&gt;
#&lt;br /&gt;
#if  (test ! -f /etc/ssh/sshd_config &amp;amp;&amp;gt; /dev/null); then&lt;br /&gt;
#  echo &amp;quot;&lt;br /&gt;
#File /etc/ssh/sshd_config not found.&lt;br /&gt;
#Not checking for path to sftp-server.&lt;br /&gt;
#  &amp;quot;;&lt;br /&gt;
#else&lt;br /&gt;
if [ ! -f ${SSHD_CONFIG} ]&lt;br /&gt;
then&lt;br /&gt;
   echo &amp;quot;File ${SSHD_CONFIG} not found.&amp;quot;&lt;br /&gt;
   echo &amp;quot;Not checking for path to sftp-server.&amp;quot;&lt;br /&gt;
   echo &amp;quot;Please adjust the global \$SSHD_CONFIG variable&amp;quot;&lt;br /&gt;
else&lt;br /&gt;
  if !(grep -v &amp;quot;^#&amp;quot; ${SSHD_CONFIG} | grep -i sftp-server &amp;amp;&amp;gt; /dev/null); then&lt;br /&gt;
    echo &amp;quot;Obviously no sftp-server is running on this system.&lt;br /&gt;
&amp;quot;;&lt;br /&gt;
  else SFTP_SERVER=$(grep -v &amp;quot;^#&amp;quot; ${SSHD_CONFIG} | grep -i sftp-server | awk  '{ print $3}')&lt;br /&gt;
  fi&lt;br /&gt;
fi&lt;br /&gt;
&lt;br /&gt;
#if !(grep -v &amp;quot;^#&amp;quot; /etc/ssh/sshd_config | grep -i sftp-server /etc/ssh/sshd_config | awk  '{ print $3}' &amp;amp;&amp;gt; /dev/null); then&lt;br /&gt;
APPS=&amp;quot;$APPS $SFTP_SERVER&amp;quot;&lt;br /&gt;
&lt;br /&gt;
# Get accountname to create / move&lt;br /&gt;
CHROOT_USERNAME=$1&lt;br /&gt;
&lt;br /&gt;
if ! [ -z &amp;quot;$2&amp;quot; ] ; then&lt;br /&gt;
  SHELL=$2&lt;br /&gt;
else&lt;br /&gt;
  SHELL=/bin/chroot-shell&lt;br /&gt;
fi&lt;br /&gt;
&lt;br /&gt;
if ! [ -z &amp;quot;$3&amp;quot; ] ; then&lt;br /&gt;
  JAILPATH=$3&lt;br /&gt;
else&lt;br /&gt;
  JAILPATH=/home/jail&lt;br /&gt;
fi&lt;br /&gt;
&lt;br /&gt;
# Exit if user already exists&lt;br /&gt;
#id $CHROOT_USERNAME &amp;gt; /dev/null 2&amp;gt;&amp;amp;1 &amp;amp;&amp;amp; { echo &amp;quot;User exists.&amp;quot;; echo &amp;quot;Exiting.&amp;quot;; exit 1; }&lt;br /&gt;
&lt;br /&gt;
# Check if user already exists and ask for confirmation&lt;br /&gt;
# we have to trust that root knows what she is doing when saying 'yes'&lt;br /&gt;
if ( id $CHROOT_USERNAME &amp;gt; /dev/null 2&amp;gt;&amp;amp;1 ) ; then {&lt;br /&gt;
echo &amp;quot;&lt;br /&gt;
-----------------------------&lt;br /&gt;
User $CHROOT_USERNAME exists. &lt;br /&gt;
&lt;br /&gt;
Are you sure you want to modify the users home directory and lock him into the&lt;br /&gt;
chroot directory?&lt;br /&gt;
Are you REALLY sure?&lt;br /&gt;
Say only yes if you absolutely know what you are doing!&amp;quot;&lt;br /&gt;
  read -p &amp;quot;(yes/no) -&amp;gt; &amp;quot; MODIFYUSER&lt;br /&gt;
  if [ &amp;quot;$MODIFYUSER&amp;quot; != &amp;quot;yes&amp;quot; ]; then&lt;br /&gt;
    echo &amp;quot;&lt;br /&gt;
Not entered yes. Exiting....&amp;quot;&lt;br /&gt;
    exit 1&lt;br /&gt;
  fi&lt;br /&gt;
}&lt;br /&gt;
else&lt;br /&gt;
  CREATEUSER=&amp;quot;yes&amp;quot;&lt;br /&gt;
fi&lt;br /&gt;
&lt;br /&gt;
# Create $SHELL (shell for jailed accounts)&lt;br /&gt;
if [ -f ${SHELL} ] ; then&lt;br /&gt;
  echo &amp;quot;&lt;br /&gt;
-----------------------------&lt;br /&gt;
The file $SHELL exists. &lt;br /&gt;
Probably it was created by this script.&lt;br /&gt;
&lt;br /&gt;
Are you sure you want to overwrite it?&lt;br /&gt;
(you want to say yes for example if you are running the script for the second&lt;br /&gt;
time when adding more than one account to the jail)&amp;quot;&lt;br /&gt;
read -p &amp;quot;(yes/no) -&amp;gt; &amp;quot; OVERWRITE&lt;br /&gt;
if [ &amp;quot;$OVERWRITE&amp;quot; != &amp;quot;yes&amp;quot; ]; then&lt;br /&gt;
  echo &amp;quot;&lt;br /&gt;
Not entered yes. Exiting....&amp;quot;&lt;br /&gt;
  exit 1&lt;br /&gt;
fi&lt;br /&gt;
else&lt;br /&gt;
  echo &amp;quot;Creating $SHELL&amp;quot;&lt;br /&gt;
  echo '#!/bin/sh' &amp;gt; $SHELL&lt;br /&gt;
  echo &amp;quot;`which sudo` `which chroot` $JAILPATH /bin/su - \$USER&amp;quot; \&amp;quot;\$@\&amp;quot; &amp;gt;&amp;gt; $SHELL&lt;br /&gt;
  chmod 755 $SHELL&lt;br /&gt;
fi&lt;br /&gt;
&lt;br /&gt;
# make common jail for everybody if inexistent&lt;br /&gt;
if [ ! -d ${JAILPATH} ] ; then&lt;br /&gt;
  mkdir -p ${JAILPATH}&lt;br /&gt;
  echo &amp;quot;Creating ${JAILPATH}&amp;quot;&lt;br /&gt;
fi&lt;br /&gt;
cd ${JAILPATH}&lt;br /&gt;
&lt;br /&gt;
# Create directories in jail that do not exist yet&lt;br /&gt;
JAILDIRS=&amp;quot;dev etc etc/pam.d bin home sbin usr usr/bin usr/lib&amp;quot;&lt;br /&gt;
for directory in $JAILDIRS ; do&lt;br /&gt;
  if [ ! -d &amp;quot;$JAILPATH/$directory&amp;quot; ] ; then&lt;br /&gt;
    mkdir $JAILPATH/&amp;quot;$directory&amp;quot;&lt;br /&gt;
    echo &amp;quot;Creating $JAILPATH/$directory&amp;quot;&lt;br /&gt;
  fi&lt;br /&gt;
done&lt;br /&gt;
echo&lt;br /&gt;
&lt;br /&gt;
# Comment in the following lines if your apache can't read the directories and&lt;br /&gt;
# uses the security contexts&lt;br /&gt;
# Fix security contexts so Apache can read files&lt;br /&gt;
#CHCON=$(`which chcon`)&lt;br /&gt;
#if [ -n &amp;quot;$CHCON&amp;quot; ] &amp;amp;&amp;amp; [ -x $CHCON ]; then&lt;br /&gt;
#    $CHCON -t home_root_t $JAILPATH/home&lt;br /&gt;
#    $CHCON -t user_home_dir_t $JAILPATH/home/$CHROOT_USERNAME&lt;br /&gt;
#fi&lt;br /&gt;
&lt;br /&gt;
# Creating necessary devices&lt;br /&gt;
[ -r $JAILPATH/dev/urandom ] || mknod $JAILPATH/dev/urandom c 1 9&lt;br /&gt;
[ -r $JAILPATH/dev/null ]    || mknod -m 666 $JAILPATH/dev/null    c 1 3&lt;br /&gt;
[ -r $JAILPATH/dev/zero ]    || mknod -m 666 $JAILPATH/dev/zero    c 1 5&lt;br /&gt;
[ -r $JAILPATH/dev/tty ]     || mknod -m 666 $JAILPATH/dev/tty     c 5 0 &lt;br /&gt;
&lt;br /&gt;
# if we only want to update the files in the jail&lt;br /&gt;
# skip the creation of the new account&lt;br /&gt;
if [ &amp;quot;$1&amp;quot; != &amp;quot;update&amp;quot; ]; then&lt;br /&gt;
&lt;br /&gt;
# Modifiy /etc/sudoers to enable chroot-ing for users&lt;br /&gt;
# must be removed by hand if account is deleted&lt;br /&gt;
echo &amp;quot;Modifying /etc/sudoers&amp;quot;&lt;br /&gt;
echo &amp;quot;$CHROOT_USERNAME       ALL=NOPASSWD: `which chroot`, /bin/su - $CHROOT_USERNAME&amp;quot; &amp;gt;&amp;gt; /etc/sudoers&lt;br /&gt;
&lt;br /&gt;
# Define HomeDir for simple referencing&lt;br /&gt;
HOMEDIR=&amp;quot;$JAILPATH/home/$CHROOT_USERNAME&amp;quot;&lt;br /&gt;
&lt;br /&gt;
# Create new account, setting $SHELL to the above created script and&lt;br /&gt;
# $HOME to $JAILPATH/home/*&lt;br /&gt;
if [ &amp;quot;$CREATEUSER&amp;quot; != &amp;quot;yes&amp;quot; ] ; then echo &amp;quot;&lt;br /&gt;
Not creating new User account&lt;br /&gt;
Modifying User \&amp;quot;$CHROOT_USERNAME\&amp;quot; &lt;br /&gt;
Copying files in $CHROOT_USERNAME's \$HOME to \&amp;quot;$HOMEDIR\&amp;quot;&lt;br /&gt;
&amp;quot;&lt;br /&gt;
usermod -d &amp;quot;$HOMEDIR&amp;quot; -m -s &amp;quot;$SHELL&amp;quot; $CHROOT_USERNAME &amp;amp;&amp;amp; chmod 700 &amp;quot;$HOMEDIR&amp;quot;&lt;br /&gt;
fi&lt;br /&gt;
&lt;br /&gt;
if [ &amp;quot;$CREATEUSER&amp;quot; = &amp;quot;yes&amp;quot; ] ; then {&lt;br /&gt;
echo &amp;quot;Adding User \&amp;quot;$CHROOT_USERNAME\&amp;quot; to system&amp;quot;&lt;br /&gt;
useradd -m -d &amp;quot;$HOMEDIR&amp;quot; -s &amp;quot;$SHELL&amp;quot; $CHROOT_USERNAME &amp;amp;&amp;amp; chmod 700 &amp;quot;$HOMEDIR&amp;quot;&lt;br /&gt;
&lt;br /&gt;
# Enter password for new account&lt;br /&gt;
if !(passwd $CHROOT_USERNAME);&lt;br /&gt;
  then echo &amp;quot;Passwords are probably not the same, try again.&amp;quot;&lt;br /&gt;
  exit 1;&lt;br /&gt;
fi&lt;br /&gt;
echo&lt;br /&gt;
}&lt;br /&gt;
fi&lt;br /&gt;
&lt;br /&gt;
# Create /usr/bin/groups in the jail&lt;br /&gt;
echo &amp;quot;#!/bin/bash&amp;quot; &amp;gt; usr/bin/groups&lt;br /&gt;
echo &amp;quot;id -Gn&amp;quot; &amp;gt;&amp;gt; usr/bin/groups&lt;br /&gt;
chmod 755 usr/bin/groups&lt;br /&gt;
&lt;br /&gt;
# Add users to etc/passwd&lt;br /&gt;
#&lt;br /&gt;
# check if file exists (ie we are not called for the first time)&lt;br /&gt;
# if yes skip root's entry and do not overwrite the file&lt;br /&gt;
if [ ! -f etc/passwd ] ; then&lt;br /&gt;
 grep /etc/passwd -e &amp;quot;^root&amp;quot; &amp;gt; ${JAILPATH}/etc/passwd&lt;br /&gt;
fi&lt;br /&gt;
if [ ! -f etc/group ] ; then&lt;br /&gt;
 grep /etc/group -e &amp;quot;^root&amp;quot; &amp;gt; ${JAILPATH}/etc/group&lt;br /&gt;
# add the group for all users to etc/group (otherwise there is a nasty error&lt;br /&gt;
# message and probably because of that changing directories doesn't work with&lt;br /&gt;
# winSCP)&lt;br /&gt;
 grep /etc/group -e &amp;quot;^users&amp;quot; &amp;gt;&amp;gt; ${JAILPATH}/etc/group&lt;br /&gt;
fi&lt;br /&gt;
&lt;br /&gt;
# grep the username which was given to us from /etc/passwd and add it&lt;br /&gt;
# to ./etc/passwd replacing the $HOME with the directory as it will then &lt;br /&gt;
# appear in the jail&lt;br /&gt;
echo &amp;quot;Adding User $CHROOT_USERNAME to jail&amp;quot;&lt;br /&gt;
grep -e &amp;quot;^$CHROOT_USERNAME:&amp;quot; /etc/passwd | \&lt;br /&gt;
 sed -e &amp;quot;s#$JAILPATH##&amp;quot;      \&lt;br /&gt;
     -e &amp;quot;s#$SHELL#/bin/bash#&amp;quot;  &amp;gt;&amp;gt; ${JAILPATH}/etc/passwd&lt;br /&gt;
&lt;br /&gt;
# if the system uses one account/one group we write the&lt;br /&gt;
# account's group to etc/group&lt;br /&gt;
grep -e &amp;quot;^$CHROOT_USERNAME:&amp;quot; /etc/group &amp;gt;&amp;gt; ${JAILPATH}/etc/group&lt;br /&gt;
&lt;br /&gt;
# write the user's line from /etc/shadow to /home/jail/etc/shadow&lt;br /&gt;
grep -e &amp;quot;^$CHROOT_USERNAME:&amp;quot; /etc/shadow &amp;gt;&amp;gt; ${JAILPATH}/etc/shadow&lt;br /&gt;
chmod 600 ${JAILPATH}/etc/shadow&lt;br /&gt;
&lt;br /&gt;
# endif for =! update&lt;br /&gt;
fi&lt;br /&gt;
&lt;br /&gt;
# Copy the apps and the related libs&lt;br /&gt;
echo &amp;quot;Copying necessary library-files to jail (may take some time)&amp;quot;&lt;br /&gt;
&lt;br /&gt;
# The original code worked fine on RedHat 7.3, but did not on FC3.&lt;br /&gt;
# On FC3, when the 'ldd' is done, there is a 'linux-gate.so.1' that &lt;br /&gt;
# points to nothing (or a 90xb.....), and it also does not pick up&lt;br /&gt;
# some files that start with a '/'. To fix this, I am doing the ldd&lt;br /&gt;
# to a file called ldlist, then going back into the file and pulling&lt;br /&gt;
# out the libs that start with '/'&lt;br /&gt;
# &lt;br /&gt;
# Randy K.&lt;br /&gt;
#&lt;br /&gt;
# The original code worked fine on 2.4 kernel systems. Kernel 2.6&lt;br /&gt;
# introduced an internal library called 'linux-gate.so.1'. This &lt;br /&gt;
# 'phantom' library caused non-critical errors to display during the &lt;br /&gt;
# copy since the file does not actually exist on the file system. &lt;br /&gt;
# To fix re-direct output of ldd to a file, parse the file and get &lt;br /&gt;
# library files that start with /&lt;br /&gt;
#&lt;br /&gt;
&lt;br /&gt;
# create temporary files with mktemp, if that doesn't work for some reason use&lt;br /&gt;
# the old method with $HOME/ldlist[2] (so I don't have to check the existence&lt;br /&gt;
# of the mktemp package / binary at the beginning&lt;br /&gt;
#&lt;br /&gt;
#TMPFILE1=`mktemp` &amp;amp;&amp;gt; /dev/null ||  TMPFILE1=&amp;quot;${HOME}/ldlist&amp;quot;; if [ -x ${TMPFILE1} ]; then mv ${TMPFILE1} ${TMPFILE1}.bak;fi&lt;br /&gt;
#TMPFILE2=`mktemp` &amp;amp;&amp;gt; /dev/null ||  TMPFILE2=&amp;quot;${HOME}/ldlist2&amp;quot;; if [ -x ${TMPFILE2} ]; then mv ${TMPFILE2} ${TMPFILE2}.bak;fi&lt;br /&gt;
TMPFILE1=`mktemp`&lt;br /&gt;
TMPFILE2=`mktemp`&lt;br /&gt;
for app in $APPS;  do&lt;br /&gt;
    # First of all, check that this application exists&lt;br /&gt;
    if [ -x $app ]; then&lt;br /&gt;
        # Check that the directory exists; create it if not.&lt;br /&gt;
#        app_path=`echo $app | sed -e 's#\(.\+\)/[^/]\+#\1#'`&lt;br /&gt;
        app_path=`dirname $app`&lt;br /&gt;
        if ! [ -d .$app_path ]; then&lt;br /&gt;
            mkdir -p .$app_path&lt;br /&gt;
        fi&lt;br /&gt;
&lt;br /&gt;
		# If the files in the chroot are on the same file system as the&lt;br /&gt;
		# original files you should be able to use hard links instead of&lt;br /&gt;
		# copying the files, too. Symbolic links cannot be used, because the&lt;br /&gt;
		# original files are outside the chroot.&lt;br /&gt;
		cp -p $app .$app&lt;br /&gt;
&lt;br /&gt;
        # get list of necessary libraries&lt;br /&gt;
        ldd $app &amp;gt;&amp;gt; ${TMPFILE1}&lt;br /&gt;
    fi&lt;br /&gt;
done&lt;br /&gt;
&lt;br /&gt;
# Clear out any old temporary file before we start&lt;br /&gt;
for libs in `cat ${TMPFILE1}`; do&lt;br /&gt;
   frst_char=&amp;quot;`echo $libs | cut -c1`&amp;quot;&lt;br /&gt;
   if [ &amp;quot;$frst_char&amp;quot; = &amp;quot;/&amp;quot; ]; then&lt;br /&gt;
     echo &amp;quot;$libs&amp;quot; &amp;gt;&amp;gt; ${TMPFILE2}&lt;br /&gt;
   fi&lt;br /&gt;
done&lt;br /&gt;
for lib in `cat ${TMPFILE2}`; do&lt;br /&gt;
    mkdir -p .`dirname $lib` &amp;gt; /dev/null 2&amp;gt;&amp;amp;1&lt;br /&gt;
&lt;br /&gt;
	# If the files in the chroot are on the same file system as the original&lt;br /&gt;
	# files you should be able to use hard links instead of copying the files,&lt;br /&gt;
	# too. Symbolic links cannot be used, because the original files are&lt;br /&gt;
	# outside the chroot.&lt;br /&gt;
    cp $lib .$lib&lt;br /&gt;
done&lt;br /&gt;
&lt;br /&gt;
#&lt;br /&gt;
# Now, cleanup the 2 files we created for the library list&lt;br /&gt;
#&lt;br /&gt;
#/bin/rm -f ${HOME}/ldlist&lt;br /&gt;
#/bin/rm -f ${HOME}/ldlist2&lt;br /&gt;
/bin/rm -f ${TMPFILE1}&lt;br /&gt;
/bin/rm -f ${TMPFILE2}&lt;br /&gt;
&lt;br /&gt;
# Necessary files that are not listed by ldd.&lt;br /&gt;
#&lt;br /&gt;
# There might be errors because of files that do not exist but in the end it&lt;br /&gt;
# may work nevertheless (I added new file names at the end without deleting old&lt;br /&gt;
# ones for reasons of backward compatibility).&lt;br /&gt;
# So please test ssh/scp before reporting a bug.&lt;br /&gt;
if [ &amp;quot;$DISTRO&amp;quot; = SUSE ]; then&lt;br /&gt;
  cp /lib/libnss_compat.so.2 /lib/libnss_files.so.2 /lib/libnss_dns.so.2 /lib/libxcrypt.so.1 ${JAILPATH}/lib/&lt;br /&gt;
elif [ &amp;quot;$DISTRO&amp;quot; = FEDORA ]; then&lt;br /&gt;
  cp /lib/libnss_compat.so.2 /lib/libnsl.so.1 /lib/libnss_files.so.2 /lib/ld-linux.so.2 /lib/ld-ldb.so.3 /lib/ld-lsb.so.3 /lib/libnss_dns.so.2 /lib/libxcrypt.so.1 ${JAILPATH}/lib/&lt;br /&gt;
  cp /lib/*.* ${JAILPATH}/lib/&lt;br /&gt;
  cp /usr/lib/libcrack.so.2 ${JAILPATH}/usr/lib/&lt;br /&gt;
elif [ &amp;quot;$DISTRO&amp;quot; = REDHAT ]; then&lt;br /&gt;
  cp /lib/libnss_compat.so.2 /lib/libnsl.so.1 /lib/libnss_files.so.2 /lib/ld-linux.so.2 /lib/ld-lsb.so.1 /lib/libnss_dns.so.2 /lib/libxcrypt.so.1 ${JAILPATH}/lib/&lt;br /&gt;
  # needed for scp on RHEL&lt;br /&gt;
  echo &amp;quot;export LD_LIBRARY_PATH=/usr/kerberos/lib&amp;quot; &amp;gt;&amp;gt; ${JAILPATH}/etc/profile&lt;br /&gt;
elif [ &amp;quot;$DISTRO&amp;quot; = DEBIAN ]; then&lt;br /&gt;
  cp /lib/libnss_compat.so.2 /lib/libnsl.so.1 /lib/libnss_files.so.2 /lib/libcap.so.2 /lib/libnss_dns.so.2 ${JAILPATH}/lib/&lt;br /&gt;
else&lt;br /&gt;
  cp /lib/libnss_compat.so.2 /lib/libnsl.so.1 /lib/libnss_files.so.2 /lib/libcap.so.2 /lib/libnss_dns.so.2 ${JAILPATH}/lib/&lt;br /&gt;
fi&lt;br /&gt;
&lt;br /&gt;
# if you are using a 64 bit system and have strange problems with login comment&lt;br /&gt;
# the following lines in, perhaps it works then (motto: if you can't find the&lt;br /&gt;
# needed library just copy all of them)&lt;br /&gt;
#&lt;br /&gt;
#cp /lib/*.* ${JAILPATH}/lib/&lt;br /&gt;
#cp /lib/lib64/*.* ${JAILPATH}/lib/lib64/ &lt;br /&gt;
&lt;br /&gt;
# if you are using PAM you need stuff from /etc/pam.d/ in the jail,&lt;br /&gt;
echo &amp;quot;Copying files from /etc/pam.d/ to jail&amp;quot;&lt;br /&gt;
cp /etc/pam.d/* ${JAILPATH}/etc/pam.d/&lt;br /&gt;
&lt;br /&gt;
# ...and of course the PAM-modules...&lt;br /&gt;
echo &amp;quot;Copying PAM-Modules to jail&amp;quot;&lt;br /&gt;
cp -r /lib/security ${JAILPATH}/lib/&lt;br /&gt;
&lt;br /&gt;
# ...and something else useful for PAM&lt;br /&gt;
cp -r /etc/security ${JAILPATH}/etc/&lt;br /&gt;
cp /etc/login.defs ${JAILPATH}/etc/&lt;br /&gt;
&lt;br /&gt;
if [ -f /etc/DIR_COLORS ] ; then&lt;br /&gt;
  cp /etc/DIR_COLORS ${JAILPATH}/etc/&lt;br /&gt;
fi &lt;br /&gt;
&lt;br /&gt;
# Don't give more permissions than necessary&lt;br /&gt;
chown root.root ${JAILPATH}/bin/su&lt;br /&gt;
chmod 700 ${JAILPATH}/bin/su&lt;br /&gt;
&lt;br /&gt;
exit&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>Marti</name></author>
	</entry>
</feed>