There are lots of different ways that you could achieve this. I'm going to list one of several possible solutions.
I would propose using several different layers of protection to prevent users from running the commands that they shouldn't be allowed to access. All of the directions here assume that users have their own
/home/[username] directory, that their shell is
/bin/bash and you would like them to be use the bash shell when they log in to the system.
1) Change directory permissions so that only the user can edit the contents of their home directory
chmod 755 /home/[username]
2) Remove the user's
rm /home/[username]/.bashrc This site has more information as to why it might be a good idea to delete the
.bashrc in this situation.
3) Create a
.bash_profile and add "safe" aliases for all the commands that you would like to disable
./bash_profile file example
alias apt-get="printf ''"
alias aptitude="printf ''"
alias vi="vi -Z" #this is vi's safe mode and shell commands won't be run from within vi
alias alias="printf ''"
And please check the full list of bash commands for more information. You must make sure that the
alias alias="printf ''" command is the last command on the list otherwise you lose your ability to alias all of those commands.
4) Disable shell commands in vi by aliasing the vi command to restricted mode
The syntax is
alias vi="vi -Z", but please see this site for more information.
5) Change the ownership of the user's
.bash_profile to root
chown root:root /home/[username]/.bash_profile
6) Remove write permissions on the user's
chmod 755 /home/[username/.bash_profile]
7) Finally change the users bash to restricted bash mode so that they can't change directories (if you don't have a restricted bash mode on your system, this link will help and give you more information)
chsh -s /bin/rbash [username]
Now when the users log in they won't be able to change directories, all of the commands that you don't want them to use will output the same information as if the user pressed the ENTER key with no command specified, and your
/bin/bash functions stay intact.
Depending on what functions you choose to or not to alias this way, users may still be able to circumvent some of the controls that you implemented. However, since we implemented a few safety buffers, the user would really have to know about computer systems to do anything dangerous.
On a related note and something that you might want to consider, if you directly place these aliases into each and every users'
.bash_profile you would have difficulty maintaining which functions should and shouldn't be aliased, and if you need to change the alias on anything you would have to change all of them individually. Also, since users can use
vi to view files, they could see the contents of their
.bash_profile and understand what restrictions they have and don't have.
To get around this I would suggest.
1) Putting all of the aliases in a directory not accessible by the users (paste the contents of the
2) Sourcing the aliases into their
improved ./bash_profile file example
if [[ -f /[path_to_file]/startup_functions_for_beginners.sh ]]; then
This should put you on your way, but remember that there are almost always ways to circumvent restrictions.
Also, feel free to remix the information in this answer to suit your needs. These can most definitely be combined with a number of other restrictions as well.
Q: I need users to have access to
bg, but I don't want them to be able to access
alias apt-get="printf ''" #the user won't be able to run this
alias aptitude="printf ''" #the user won't be able to run this
alias bash="printf ''" #the user won't be able to run this
#alias fg="printf ''" #this will run as a bash built-in
#alias bg="printf ''" #you actually don't need to include these in your script