Restrict Linux users to their home directories only

by Vivek Gite on August 17, 2006 · 9 comments

Q. How can I make sure that users can only access their own home directories?

A. You can use rbash i.e. restricted bash shell. A restricted shell is used to set up an environment more controlled than the standard shell. It behaves identically to bash with the exception that the following are disallowed or not performed:

  1. Changing directories with cd
  2. Setting or unsetting the values of SHELL, PATH, ENV, or BASH_ENV
  3. Specifying command names containing /
  4. Specifying a file name containing a / as an argument to the . builtin command
  5. Specifying a filename containing a slash as an argument to the -p option to the hash builtin command
  6. Importing function definitions from the shell environment at startup
  7. Parsing the value of SHELLOPTS from the shell environment at startup
  8. Redirecting output using the >, >|, , >&, &>, and >> redirection operators
  9. Using the exec builtin command to replace the shell with another command
  10. Adding or deleting builtin commands with the -f and -d options to the enable builtin command
  11. Using the enable builtin command to enable disabled shell builtins
  12. Specifying the -p option to the command builtin command
  13. Turning off restricted mode with set +r or set +o restricted.

These restrictions are enforced after any startup files are read. When a command that is found to be a shell script is executed, rbash turns off any restrictions in the shell
spawned to execute the script.

Open /etc/passwd file and setup shell to /bin/rbash
# vi /etc/passwd

For example here is a sample entry for user vivek:
vivek:x:100:101::/home/vivek:/bin/rbash

Save and close the file.

Featured Articles:

Share this with other sys admins!
Facebook it - Tweet it - Print it -

{ 9 comments… read them below or add one }

1 surekha November 1, 2007

From Where I can get rbash

In my /bin no file is there by name rbash so, my telnet is terminating after entering password
as it is not finding rbash command

Reply

2 Frank Daley March 27, 2008

cp /bin/bash /bin/rbash

will do the trick

Reply

3 Mike January 30, 2009

Hi!

While using a command line, this restriction works. But if I have mc (Midnight Commander) installed, then this ‘rbashed’ user just can use mc and browse the whole system.

Reply

4 Jasleen May 17, 2009

THANKS FRANK, IT WAS VERY USEFUL

Reply

5 actions February 18, 2010

how vnc other user restrict ?

Reply

6 Ahmad Issa May 17, 2010

thanks that is very usefull

how can i permit the users to use only ifconfig command to change the server IP

Reply

7 reijjo August 12, 2010

I can get out of ~ with everything but cd… I mean
pico /etc/passwd for example or ls /bin etc.

Reply

8 legolasthehansy December 30, 2010

If you have /bin in your PATH, you can easily bypass rbash’s restrictive properties. See below,

[root@host ~]# !su
su – blah2010
-rbash-3.2$ cd /
-rbash: cd: restricted
-rbash-3.2$ bash
bash-3.2$ cd /
bash-3.2$ ls | wc -l
29
bash-3.2$

The key is to fine tune until you have the right setting. Nice post.

Reply

9 Simon February 12, 2011

Hmmm….

it doesn’t work very well

Users can still execute command like
cat /home/another_user/public_html/includes/database_password.inc.php

More so SFTP and FTP doesn’t work once user’s bash has been changed to rbash

Reply

Leave a Comment

You can use these HTML tags and attributes for your code and commands: <strong> <em> <ol> <li> <u> <ul> <blockquote> <pre> <a href="" title="">
What is 12 + 14 ?
Please leave these two fields as-is:
Solve the simple math so we know that you are a human and not a bot.



Previous post:

Next post: