Many answers focus on how to make your solution work, while very few suggest that your solution is a very bad approach. If you really want to “practice to learn”, why not practice using good solutions? Hardcoding your password is learning the wrong approach!
If what you really want is a password-less mount
for that volume, maybe sudo
isn’t needed at all! So may I suggest other approaches?
-
Use
/etc/fstab
as mensi suggested. Use optionsuser
andnoauto
to let regular users mount that volume. -
Use
Polkit
for passwordless actions: Configure a.policy
file for your script with<allow_any>yes</allow_any>
and drop at/usr/share/polkit-1/actions
-
Edit
/etc/sudoers
to allow your user to usesudo
without typing your password. As @Anders suggested, you can restrict such usage to specific commands, thus avoiding unlimited passwordless root priviledges in your account. See this answer for more details on/etc/sudoers
.
All the above allow passwordless root privilege, none require you to hardcode your password. Choose any approach and I can explain it in more detail.
As for why it is a very bad idea to hardcode passwords, here are a few good links for further reading:
- Why You Shouldn’t Hard Code Your Passwords When Programming
- How to keep secrets secret
(Alternatives to Hardcoding Passwords) - What’s more secure? Hard coding credentials or storing them in a database?
- Use of hard-coded credentials, a dangerous programming error: CWE
- Hard-coded passwords remain a key security flaw