Skip to content

Instantly share code, notes, and snippets.

@latusikl
Last active August 8, 2019 19:32
Show Gist options
  • Save latusikl/869f863b0a52818eaefdfe01c96108cc to your computer and use it in GitHub Desktop.
Save latusikl/869f863b0a52818eaefdfe01c96108cc to your computer and use it in GitHub Desktop.
Where to store your bash scripts? How execute bash script without sudo?
  • Storing your bash scrpits
    One of better ways to store scripts created by yourself is to create bin folder in your home direcotry and add it to path where bash is looking for scripts.
  1. Create bin folder.
mkdir ~/bin
  1. Add it to PATH in .bashrc configuartion file.
nano ~/.bashrc

And at the end of file write:

export PATH="$HOME/bin:$PATH"

After doing that you can run scrpits from bin folder just by typing their name.

  • Execute script without sudo password Before doing that you must remember that this step can cause some danger to the system beacuse you allow to execute any command in particular script without sudo password. So keep that in mind while choosing files.

I recommend to pass file ownership to root and allow other users only to execute it. It will block possibility of changing a file if you are not a root and make adding some extra lines without your knowledge more difficult.

  1. Changing file owner to root and leaving only execute option to others users. (not reqiured)
chmod +x <filename>
chmod o-rw <filename>
sudo chown root:root <filename>
  1. Putting choosen file to run without sudo password.
sudo visudo

Add at the end of file:

<username> ALL = NOPASSWD: <file_path>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment