Is there any way to set a password on a script (or any file for that matters), without having to enter a specific command line to decrypt it each time I want to edit it? Setting this password to a directory, making every file (present and future) password-protected would be fine, and actually even better.

Situation example :

nano path/to/script
Enter passphrase : *****
[Modification allowed, then file closed]
[Is executed without asking for password]

sudo is in itself a solution, but the problem is that unless I leave terminal, it doesn't ask for password when it's been entered once in a single session.

The purpose of this would be to connect to a distant SSH without having to type the password every time (mostly to do scp commands actually), hence putting it in the script. But if everyone can access it, it becomes pretty much meaningless. Any idea/solution?

Thanks for your answers.

  • 1
    Use key-based authentication for SSH. The keys can be protected with a password. Run ssh-keygen, for example.
    – muru
    Nov 18, 2015 at 23:53
  • If your only concern with sudo is the password caching, you can disable that.
    – Jeff Schaller
    Nov 19, 2015 at 0:40
  • You could also use su -c instead of sudo. Nov 19, 2015 at 3:06
  • My concern is not that it's only asking for sudo password once, I'm fine with it, more than once would be boring for every task. What I would want though is that it ALWAYS asks me for sudo password every time I want to open these specific files whatsoever, but not for anything else.
    – Razakhel
    Nov 19, 2015 at 20:07

1 Answer 1


As Jeff Schaller mentions, you can disable the password caching of sudo by adding this to your /etc/sudoers file:

Defaults timestamp_timeout=0

Then you can simply chown root FILE; chmod 755 FILE so that only root can edit the file but everyone can read/execute it.

Alternatively, you could do su root -c nano FILE, since su does not cache passwords.

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.