I know I could just put something like sudo mypassword in my .bash_profile, but I don't want to run every command as root.

I want password to autofill under following circumstances:

  • only the commands requiring root privileges
  • only commands that I explicitly state I plan to run su to root with sudo


sudo cd /var/root           #When I type this
Password:                   #I don't want to be prompted for my password
                            #I want to fill it from my `.bash_profile`


cd /var/root                              #When I type this
-bash: cd: /var/root: Permission denied   #I still want this, or the like, returned

I saw this post on increasing sudo timeout, but I don't think it's quite equivalent. For example, I want it to carry across different shell log-in sessions. I could be wrong.

Any suggestions regarding what to (or not to!) add to my .bash_profile, or which method (timeout vs profile) is preferable would be greatly appreciated! Thank you in advance.

  • 3
    What not to do? ^this^. Use /etc/sudoers – mikeserv Aug 21 '14 at 20:06
  • 1
    Take care to use visudo to edit the sudoers file. – glenn jackman Aug 21 '14 at 20:17
  • 2
    You don't have to use ALL. You can specify by command which commands require a password and which don't. Look at man sudo - or just read the comments in your /etc/sudoers. and by the way, if there is a directory on your disk into which cannot cd - then proably nobody should. do ls dir then cat ./dir/tgt.file stay out of there - root user or otherwise. – mikeserv Aug 21 '14 at 20:50
  • 3
    Another problem here is that sudo cd /var/root doesn't do anything useful, because a subprocess can't change the directory of its parent. – cjm Aug 21 '14 at 21:01
  • 1
    @njboot - for what its worth, I thought it was a good example. – mikeserv Aug 22 '14 at 3:20

If you don't want to be challenged every time for your password then I'd recommend setting it to NOPASSWD in your /etc/sudoers file rather than hardcode your password in your logins. At least this way your primary login's password will remain intact and not be completely exposed in your .bashrc.

To make this change run the command sudo visudo, and change your user accounts entry to something like this:

userX        ALL=(ALL)       NOPASSWD: ALL
  • 1
    @njboot - yes this addresses your 1st bullet, your 2nd bullet, I don't think is possible, at least, I've never heard of anything that can meet that requirement, beyond listing these commands out within your sudoers file. – slm Aug 21 '14 at 21:01
  • 1
    @slm - one way: Cmnd_Alias ::= NAME '=' Cmnd_List NAME ::= [A-Z]([A-Z][0-9]_)*. It is this kind of stuff, by the way, that makes sudo psychotic, or, at least it is in my opinion. You might also want to mention that visudo should understand the $EDITOR env variable - like EDITOR=/usr/bin/nano visudo. – mikeserv Aug 21 '14 at 21:05
  • 1
    @mikeserv - true you can put patterns in your sudoers file, but I'd generally discourage it, just b/c it's extremely dangerous. But since he's already going w/ NOPASSWD, I guess there isn't much more harm. – slm Aug 21 '14 at 21:07
  • 2
    @mikeserv i never would have thought of that thank you. so should i # my export $EDITOR var. from my .bash_profile when I proceed (i use Textmate)? Thank you all btw for all this useful information and help. – njboot Aug 21 '14 at 21:37
  • 1
    @njboot - if you want the shell's default editor to be nano, then, yes, exporting that from the shell's environment file is a good way to go about it. vi is POSIX specified - and the shell is even spec'd to understand its default keymaps - and so it is the baseline default editor in almost all cases. – mikeserv Aug 21 '14 at 21:41

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.