
How can I hide passwords in a shell script?Dave, we have to do sudo in a shell/perl scripts for certain commands. As you know sudo needs password to be supplied. sudo has -S option for stdin. For example: echo $pw | sudo -S command Assume, somehow, we figure out the password and pass it in for the above echo. But, the buggest concern is, if someone does a "ps", one will be able to see the password. The above example is part of a shell script and what we need to know is how do we hide the password? If you're specifically trying to accomplish this so you can work with "sudo", then here's some good news: sudo doesn't actually require password entry. If you use the command visudo (on FreeBSD?, or the appropriate equivalent on your OS) to edit your sudoers file (which controls who is allowed to use the sudo command), you can add a line like the following to permit sudo usage by user 'username' without password entry for any command. username ALL = NOPASSWD: ALL Or a line like the following will allow only '/usr/local/bin/script.sh' to be run with sudo by the user 'username' without a password. username ALL = NOPASSWD: /usr/local/bin/script.sh The above would be by far the most secure option, since it doesn't require keeping a password in plain text anywhere on the system. Alternatively, you can hide command line options like a password from ps output by inserting the value with another command using back ticks. For example, you can put the password in a file called password.txt in the user's home directory, and instead of putting the password in the command line put in `cat /home/username/password.txt` Just make sure you chmod 700 password.txt so only that user can read the file. Oh, and I'd probably name it something less obvious too, just for insurance. Another thought: Some operating systems have settings that only let users see their own processes in ps. FreeBSD 4.x can be set to behave in this fashion by setting sysctl kern.ps_showallprocs to 0. FreeBSD 5.x has sysctls security.bsd.see_other_uids and security.bsd.see_other_gids that can both be set to 0 for the same effect. Linux needs kernel patches to accomplish this, like grsecurity, as one example. Note that all of these controls do not affect users with root access. Hope you find these ideas useful! My thanks to Chris Buechler for his aid with this question.
Help others find this article at Del.icio.us, Digg, Netscape, Reddit, and Simpy.
Categorized:
Unix and Linux Help
(Article 3987)
Tagged: Previous: Microsoft Internet Explorer IE6 can't search from address bar? Next: Saving Email Addresses in Microsoft Outlook Express? Subscribe!
Never miss another useful Q&A article again! Subscribe to AskDaveTaylor with Google Reader. Another trick you can do is like the old cobalt Raq servers. Create a 'super-secret-hidden-file' whose entire contents are the password and then do cat super-secret-hidden-file | sudo -S command The sudo trick mentioned above is better than this though. Here are two alternate ideas that let you do what you want but also give you the flexibility of running the task on other computers: You can consider using SSH. If you are logged in as a non-root user and you want to run a command as root (or any other user) without entering a password you can set up public key encryption using SSH and then just do this: ssh root@computername command My personal favorite is to create a cron job that runs every minute and watches a particular folder for a file. Your script can run as an unpriveledged user and simply do touch watched-folder/file.txt Then, your cron job should check if this file exists and if so, carry out the predetermined task and delete the temporary file. This is great for running tasks as root on the same computer or for running privileged or un-privileged tasks on another computer. I use it to run batch tasks on different machines. All I need to do is copy (or scp) a log file to the waiting computer. It will detect the presence of this new file and then begin processing the log. Posted by: Matt Nuzum at May 17, 2005 3:53 PMAt www.linuxsecurity.com you may check out an article on the generic shell script compiler (www.datsi.fi.upm.es/~frosal/; hope this contains no typo; anyway the tool is also listed as shc at osx.hyperjeff.net/apps). Posted by: Mike at May 24, 2005 9:29 AMDave, I'm using HP-UX machine and UNIX shell script. I developed 1 script using sql SYBASE (korn shell) and put into crontab. how I want to hide the password in my script ? Posted by: Fazil at February 7, 2007 11:41 PMHi,
Thanks I have a lot to say, but ...
I do have a comment, now that you mention it!
|
Search
Find just the answers you seek from among our 1700+ free tech support articles by using our Lijit search engine.
Help!
Subscribe to
Ask Dave Taylor!
Free Updates!
Sign up and get free weekly updates and special offers on books, seminars, workshops and more.
Articles and Reviews
Auctions and Online Shopping Blogs and RSS Feeds Building Web site traffic Business and Management Cell Phones and Mobile Phones CGI Scripts and Web Site Programming Computer and Internet Basics d) None of the Above HTML and CSS Mac OS X Help MySpace, Facebook, Twitter and Social Network Help Pay Per Click (PPC) Search Engine Optimization Shell Script Programming Sony PSP, MP3 Players, Etc. The Writing Business Unix and Linux Help Video Game Tips and Help Windows Help
Recent Entries
Join the List!
Book Links
|