Block all default commands while a bash script are running?

Paolo Di. Janeiro

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
sarlacii

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"

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Variable notation when running python commands with arguments in a bash script

分類Dev

Commands Working In Terminal, But Not In Bash Script

分類Dev

Send data and user commands to running Puppeteer script

分類Dev

Running bash script in a kubernetes pod

分類Dev

Running a bash script to append .csv to files in a subdirectory moved/delted all files from computer

分類Dev

How can I make this multi-select Bash script default to all items selected?

分類Dev

Why combine commands on a single line in a Bash script?

分類Dev

Combine sed commands into one command in bash script

分類Dev

Optimizing a shell script with long running while loop

分類Dev

UnicodeEncode Error while running a python script

分類Dev

Error while running a .sh script via QProcess

分類Dev

Running a command in a new terminal instance in a bash script

分類Dev

Running a shell script with and without "bash" command

分類Dev

How to log all Bash commands by all users on a server?

分類Dev

phpseclib hangs webserver until all commands are done running

分類Dev

Issue unable to execute all bash script within bash script

分類Dev

How to change bash script output when the script is running?

分類Dev

How to run PowerShell core commands in bash script directly?

分類Dev

Why does this shell script fail in bash, but the commands work in SSH?

分類Dev

How to parse commands from a text file to a bash script in the CLI

分類Dev

Multiple find -exec commands in one bash script doesn't work?

分類Dev

How to execute shell commands in a loop from within a bash script?

分類Dev

Bash script does not execute commands until after delay

分類Dev

Ksh/Bash script to run commands based on kernel name

分類Dev

Propagate all arguments in a bash shell script

分類Dev

PHP Page Stuck at Loading while Another PHP script is running

分類Dev

Is it possible to continue working in Revit while running a python script in pyrevit?

分類Dev

Command line input while a running script displays strange symbols

分類Dev

Irrelevant warning while running git command from bash

Related 関連記事

  1. 1

    Variable notation when running python commands with arguments in a bash script

  2. 2

    Commands Working In Terminal, But Not In Bash Script

  3. 3

    Send data and user commands to running Puppeteer script

  4. 4

    Running bash script in a kubernetes pod

  5. 5

    Running a bash script to append .csv to files in a subdirectory moved/delted all files from computer

  6. 6

    How can I make this multi-select Bash script default to all items selected?

  7. 7

    Why combine commands on a single line in a Bash script?

  8. 8

    Combine sed commands into one command in bash script

  9. 9

    Optimizing a shell script with long running while loop

  10. 10

    UnicodeEncode Error while running a python script

  11. 11

    Error while running a .sh script via QProcess

  12. 12

    Running a command in a new terminal instance in a bash script

  13. 13

    Running a shell script with and without "bash" command

  14. 14

    How to log all Bash commands by all users on a server?

  15. 15

    phpseclib hangs webserver until all commands are done running

  16. 16

    Issue unable to execute all bash script within bash script

  17. 17

    How to change bash script output when the script is running?

  18. 18

    How to run PowerShell core commands in bash script directly?

  19. 19

    Why does this shell script fail in bash, but the commands work in SSH?

  20. 20

    How to parse commands from a text file to a bash script in the CLI

  21. 21

    Multiple find -exec commands in one bash script doesn't work?

  22. 22

    How to execute shell commands in a loop from within a bash script?

  23. 23

    Bash script does not execute commands until after delay

  24. 24

    Ksh/Bash script to run commands based on kernel name

  25. 25

    Propagate all arguments in a bash shell script

  26. 26

    PHP Page Stuck at Loading while Another PHP script is running

  27. 27

    Is it possible to continue working in Revit while running a python script in pyrevit?

  28. 28

    Command line input while a running script displays strange symbols

  29. 29

    Irrelevant warning while running git command from bash

ホットタグ

アーカイブ