2

I want to make a custom commands directory, that only works when the shell script are running and when the user stops the script all will fall back to the default without altering the default commands like alias in bash.

For example when you open a terminal bash automatically allows access to all commands in /sbin or /usr/bin or /bin. I need to block them all because my custom commands will have the same names like cd, cp, mv.

Here a fake example of what i need to do in code:

#!/bin/bash
disable_path_commands "/sbin"
disable_path_commands "/usr/local/bin"
disable_path_commands "/usr/bin"

global_commands_dir="/my_custom_commands_dir"
bash --block_default_commands "*" --allow_only_commands_from_default "sh,cd,sudo,su" --only-access-commands-from-dir "$global_commands_dir"
cd ~/

After execute the script, the user will only have access to the custom commands that are stored in /my_custom_commands_dir, but the current dir of the user will be ~/ or the current work dir. When the user typed exit, automatically will closed bash that have access to the /my_custom_commands_dir, and now will all back to the normality. The user will have access to all commands, something similar to chroot but without a complete environment for the OS, only for commands or similar to

export DEFAULT_COMMANDS_DIR="/my_custom_commands_dir" 
unset /SBIN
unset /USR/BIN
  • 3
    It looks like you may want to just modify your PATH variable for the special bash session, alternatively, set up a chroot that the script can run in. Could you clarify what you mean by "command"? One of the ones that you mention in the code, cd, is a shell built-in command. Are you proposing to disable other built-in commands (like echo etc.)? – Kusalananda Oct 10 '19 at 7:13
  • delete all default (unset PATH) variables to remove temporary the default system commands TEMPORARY only while script are running, and export a variable PATH with the /my_custom_commands_dir to use my own commands from it dir, and when the script is closed the new path of "/my_custom_commands_dir" will be destroyed, and the thefault paths of the system will be again working – Paolo Di. Janeiro Oct 10 '19 at 7:18
  • In other words, disable temporarily all paths of commands(clean all commands), for use my own path of commands, without affecting all terminals, only affect the terminal in where the script are running, and destroy the change after the user exit the script, to comming back in the default values. – Paolo Di. Janeiro Oct 10 '19 at 7:23
  • Basically, export and unset paths only affecting the terminal in where the script are running not others, and only while script is running, not permanent... – Paolo Di. Janeiro Oct 10 '19 at 7:24
  • Exactly, bash special session!!, only temporaly!!, and only in the terminal where the script are running :) – Paolo Di. Janeiro Oct 10 '19 at 7:27
1

Consider using "source", if you want the script to change the environment in your currently running shell. Other processes and scripts can then access the exported variables in the shell.

source myscript

This will source myscript. The file need not be executable but it must be a valid shell script. The file can be in current directory or in a directory in $PATH.

. myscript

This will also source myscript. This "spelling" is the official one as defined by POSIX. Bash defined source as an alias to the dot.

You can then copy and modify, for example, an existing .bashrc/profile file, to override the PATH with your chosen options. This will export the new settings to the currently running shell. When you are finished, source the original .bashrc/profile etc. to return to normal.

You can also research the use of the "exec" command to execute the script: The "exec" command will kill or terminate the current shell before executing "myscript". So, you will need to create the environment (PATH etc. from scratch):

#!/bin/bash
#myscript to check exec
exec /path/myRestrictedShellscript.sh
echo "This text will not be printed"
  • Ok, im testing your code, give me few seconds... – Paolo Di. Janeiro Oct 10 '19 at 7:56
  • Before to test the "source" command, in other words the "source" command will save all exports temporarily in myscript, without affecting the original .bashrc/profile for testing purposes?, and after it will able to decide if i use it for permanent or not? – Paolo Di. Janeiro Oct 10 '19 at 8:01
  • The source command makes any variables (and environment changes) available to the currently running shell. It does not change the defaults permanently (i.e. does not affect .bashrc). This is different to the usual method of "executing" a script (eg. /path/myscript or ./myscript in the current dir) where all variables created are sandboxed, and not available to any other running process, except ones created via the script, in the new environment. I will also add detail to my answer about the "exec" command, if you want a completely new env, that persists after you exit. – sarlacii Oct 10 '19 at 8:17
  • Thank you so much, Now im very interested in your method "source", strangely i use it now for test your code, and works at the first time, but now not works again, by a moment all default commands was disabled and only work my custom commands, but after exit and run again source myscript, not work again, i will research about it, and if it works now i will comment to you the results. – Paolo Di. Janeiro Oct 10 '19 at 8:24
  • Love you!, works perfect <3 <3 <3 <3 <3 100% approved... – Paolo Di. Janeiro Oct 10 '19 at 8:31
0

Have a look at the --restricted option, it disables a lot of functionality, including overriding the PATH variable. You can also invoke bash as rbash through a symbolic link to get the restricted mode.

  • Ok, im checking now it, give me a few seconds. – Paolo Di. Janeiro Oct 10 '19 at 7:41
  • bash --restricted does not work for me, after read your answer i try it, and always, i have access to all default commands, but now i will try with rbash – Paolo Di. Janeiro Oct 10 '19 at 7:43
  • Ok, with rbash the changes are permanent or temporaly(virtual)?, for example if i entry these command: export "/sbin:/my_custom_commands_dir" and export "/usr/local/bin:/my_custom_commands_dir" and export "/usr/bin:/my_custom_commands_dir" to override the defaults and use my own command, it changes will be permanent in rbash or will be temporaly? – Paolo Di. Janeiro Oct 10 '19 at 7:49
  • Thanky by answer anyway... – Paolo Di. Janeiro Oct 10 '19 at 7:55
  • Sorry, not works for me, rbash sets readonly the export PATH="/XX/XX/XX" as read only, it not allow me override the default commands, and keeps with access to de defaults, it not works for me, i need override temporally the default commands paths while rbash are running and when i exit of rbash lost the changes maded to will back to defaults. – Paolo Di. Janeiro Oct 10 '19 at 8:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.