Report this

What is the reason for this report?

Restrict the new user

Posted on September 21, 2017

Hi;

I want to restrict my new created user. Only the /home/user section can be accessed. How can I do that?



This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Use permission 700 and make sure the folder owner is correct.

Usually users don’t have access to run stuff outside of the files they have their user and group ownership. Having said that, you can use suduoers for that.

If you want to fully restrict that they can’t leave this folder. You can try Using Restricted Shell (rbash)

You can also use a restricted shell (such as rbash) to limit the user’s access to other parts of the system. This is a simpler method that doesn’t require setting up a full chroot jail.

Steps to set up a restricted shell:

  1. Set the User’s Shell to rbash: To restrict the user, change their default shell to rbash (restricted bash). You can do this by editing the user’s entry in /etc/passwd:
sudo usermod -s /bin/rbash user
  1. Restrict Access to Commands Outside of /home/user: Create a bin directory in the user’s home and make symbolic links for the allowed commands.
mkdir /home/user/bin
ln -s /bin/ls /home/user/bin/ls
ln -s /bin/cd /home/user/bin/cd
ln -s /bin/mkdir /home/user/bin/mkdir
  1. Restrict the PATH Variable: Add the following line to the user’s .bash_profile or .bashrc to ensure they can only execute commands from the bin directory in their home folder:
PATH=$HOME/bin
export PATH
  1. Set Permissions: Ensure the user doesn’t have write access to the main shell configuration files, which would allow them to bypass restrictions:
sudo chown root:root /home/user/.bash_profile
sudo chmod 755 /home/user/.bash_profile

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.