VSFTP chroot or jail users – limit users to only their home directory howto

Patrick asks:
How do I limit users of vsftp to only their home directory? Therefore, that user cannot go outside other directories to browser something.

Yesterdays VSFTPD troubleshooting note (read as post) brought me back this question.

If you do not wish FTP users to be able to access any files outside of their own home directory, set up chroot jail.

For consider following example:

  • Ftp username : user1
  • FTP home directory: /home/user1

$ ftp ftp.domain.com

Output:

Connected to ftp.domain.com.
220 (vsFTPd 2.0.5)
Name (ftp.domain.com:user1): user1
331 Please specify the password.
Password:
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> pwd
257 "/home/user1"
ftp> cd /etc
250 Directory successfully changed.
ftp> ls
200 PORT command successful. Consider using PASV.
150 Here comes the directory listing.
150 Here comes the directory listing.
-rw-r--r--    1 0        0            7959 Mar 02 22:20 Muttrc
drwxr-xr-x    3 0        0            4096 Jul 24 12:20 Wireless
drwxr-xr-x   16 0        0            4096 Jul 30 22:58 X11
drwxr-xr-x    4 0        0            4096 Sep 05  2005 Xprint
-rw-r--r--    1 0        0            2188 Sep 05  2005 adduser.conf
-rw-r--r--    1 0        0              47 Aug 16 14:52 adjtime
-rw-------    1 0        0            4330 Aug 18  2005 afick.conf
-rw-r--r--    1 0        0             194 Sep 05  2005 aliases
-rw-r--r--    1 0        0           12288 Jul 19 21:27 aliases.db
drwxr-xr-x    2 0        0            8192 Aug 15 09:33 alternatives
...
.....
..

Now normal user can go to /etc directory (may be to all other directories) and if there is read only permission to sensitive files user can download the file via ftp.

To avoid this security problem you can lock ftp user in a jail.

Open vsftpd configuration file - /etc/vsftpd/vsftpd.conf
# vi /etc/vsftpd/vsftpd.conf

Make sure following line exists (and uncommented):
chroot_local_user=YES

Save and close the file. Restart vsftpd.
# /etc/init.d/vsftpd restart

Now all users of VSFTPD/FTP will be limited to accessing only files in their own home directory. They will not able to see /, /etc, /root and /tmp and all other directories. This is an essential security feature.

Want to read Linux tips and tricks, but don't have time to check our blog everyday? Subscribe to our daily email newsletter to make sure you don't miss a single tip/tricks. Subscribe to our weekly newsletter here!

{ 21 comments… read them below or add one }

1 Tyler 03.16.07 at 2:33 am

What if I need a ‘root’ type FTP account; I want to chroot all but one user?

2 Bassist 03.20.07 at 6:43 pm

“What if I need a ‘root’ type FTP account; I want to chroot all but one user?”

Create a user account with the root directory as the server root directory i.e. ‘/’. Or for better security, use the main directory of all sub-directories you would like to access, as the home directory. e.g. if you have ftp accounts for /var/www/html2 and /var/www/html3, then use /var/www as the home directory for the main ftp account.

3 joel 03.23.07 at 8:34 am

hi,
i have just installed cerberus ftp server inside my network with an ip address of 192.168.x.xx…problem is i cannot connect with it outside because i really don’t knw(noobs) how to forward port 21 to it from my internet gateway.btw, my gateway runs on mandrake 10.can u tell me what to do specifically the commands to make it work? many thanks…

4 nixcraft 03.23.07 at 3:18 pm

joel,

You can use port forwarding from your router itself. If you want software based port forwarding use –
Internet TCP redirection server called rinetd. You can also use iptabled DNAT rules for same.

5 joel 03.24.07 at 1:19 am

hi nixcraft,

thanks for the reply.i was just wondering can i just add this rule to your fw_proxy script that you made..

iptables -t nat -I PREROUTING -p tcp -i INTERNET –dport 21 -j DNAT –to 192.168.x.xx:21

will that work or is there anything else that i should add?

thanks again :)

6 joel 03.26.07 at 2:41 am

nixcraft,

sorry, fw.proxy was posted by vivek,my apologies to him and to you also.anyway im still on my problem.i have installed fw.proxy script on my gateway machine running mandrake 10 and run all the commands there.even tried adding the iptable rule that i have just mentioned but i still cannot connect outside my network.im totally out of my mind figuring it out how to work.pls bail me out on this one…

joel

7 nixcraft 03.27.07 at 5:21 am

Lan is at eth0 and internet is eth1 (to isp router)

Gateway ip is 192.168.0.1
FTP server is at 192.168.0.5

You want to redirect to 192.168.0.5 at port 21, use following
iptables -t nat -A PREROUTING -i eth1 -p tcp --dport 21 -j DNAT --to-destination 192.168.0.5

HTH

8 Robert O'Rourke 12.19.07 at 3:02 pm

Hi,

I’ve been trying this on a redhat system but its not restricting the user to their home directory. It’s all they have permission on but they can still list all the files under /,/dev,/bin etc…

It might be because I have a symlink in their home folder to another part of the file system they have permission on. Would it be better to make the symlink the other way around? I’ve done everyhitng I can work out that I need to do so I don’t get why they can sill list any other directories…

Cheers

9 Paul 12.24.07 at 4:14 pm

Ref: chroot ftp users. This is taken from the vsftpd.conf file:

# You may specify an explicit list of local users to chroot() to their home
# directory. If chroot_local_user is YES, then this list becomes a list of
# users to NOT chroot().
chroot_list_enable=YES
chroot_list_file=/etc/vsftpd/vsftpd.chroot_list

This works very well for my systems.

10 Mentor 01.14.08 at 3:57 pm

Hi:

I want to be able to add another folder to the list of folders that the users are permitted to use.

Viz.:
/home/user
/var/www/html

Is this possible?

Rgds. Mentor

11 Happy 01.25.08 at 8:33 am

Thank you very much…Works like a charm…

12 Chris Hunt 02.03.08 at 3:43 pm

You can use the command “mount –bind olddir newdir”. The call ‘–bind’ mounts part of the file system to another area. You may have try to use links but these are prohibited for security reasons.

13 br0ken rlz 05.12.08 at 3:17 pm

hi guys
i face this problem and i do the solution up
but the same thing nothing change the user can
able to see / & /etc
and i have question
what i put in chroot_user_list file
only the username ?
need your advice

14 Ajay 05.22.08 at 10:56 am

Suppose if I have 5 users, I want to limit only 2 users to their home directory and remaining users to have full access. How to do that?

15 modjo-jojo 10.15.08 at 1:40 pm

Ajay, you have 2 options:

.) chroot_local_user=YES
chroot_list_enable=YES
chroot_list_file=/etc/vsftpd/nonchroot.list
Where file /etc/vsftpd/nonchroot.list should contail the users you don’t want to chroot.

.) chroot_list_enable=YES
chroot_list_file=/etc/vsftpd/chroot.list
Where the file etc/vsftpd/chroot.list should contain the users you want to be chrooted to their home. By default all other users should have access to the / (root).

BR,
modjo-jojo

16 Stan 01.14.09 at 12:35 pm

Thanks Chris! I’ve been looking for ways to mount my FTP pub directory in home dir of some users… This solution have finally solved my problems. Keep the good work. Thanks.

17 Bjarte Aune Olsen 02.01.09 at 5:31 pm

I have the same problem as some others have mentioned. When “chroot_local_user” is set to NO, the user starts up in his home folder, and can navigate everywhere on the system. When “chroot_local_user” is set to YES, the user can only WRITE to his home directory, but he can navigate anywhere and see the whole folder structure of the machine, which I don’t like.

Even worse, when “chroot_local_user” is set to YES, the user starts up in the root folder, “/” and not in his home directory. Which means that if he isn’t that familiar with Linux system, he will have a hard time finding his home directory where he’s able to upload files.

- Bjarte

18 Mizan 02.03.09 at 1:07 pm

I have a Apache webserver. I want to access the thml directory to my web developer so that he can upload file through ftp. I use vsftp server. I careate a user for him and assing /var/www/html as home directgory. the problem is when he want to upload file it shows “File could not be opended by server. 553 could not create file” . I think it is permission problem. What permision can I provide the user to mention directory without sacrifice the security. thank you.

19 Martin 03.15.09 at 10:36 pm

To Bjarte Aune Olsen:
search in the configuration file the directive local_root=… and comment it out (thus local_root will be unset). If this directive was set then vsftpd always respect him, so after loging in, he put the user in the directory that was set by local_root=…
I hope that will help you.

-Martin-

20 srinivas 06.09.09 at 6:10 am

my vsftpd setup has following

->useradd -d /var/www/html/site ftpupload
->chown ftp:ftp /var/www/html/site
->chmod g+w /var/www/html/site
->vi /etc/vsftpd/vsftpd.conf
chroot_list_enable=YES
chroot_list_file=/etc/vsftpd/chroot_list
-> vi /etc/vsftpd/chroot_list
ftpupload

this works fine when i tried from command mode from another linux
box using both ftp and lftp commands and my user restricting to his home
directory.

this is the same with the case if i use WinSCP with ftp as protocol.
but if i use sftp as the protocol i am able to chroot to another directories too

anybody has this problem ??
i need help..

Thanks in advance

21 Bineesh 07.03.09 at 9:34 am

How i can configure different chroot directory for different users.

Leave a Comment

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Previous post: QD#1: Yahoo security, SEO 101 facts, MySQL playground,webpage UI dialog

Next post: Perl 5.9.4 Released and available for download