LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-17-2015, 05:03 PM   #1
epipko
LQ Newbie
 
Registered: Mar 2015
Posts: 7

Rep: Reputation: Disabled
How to run a "sudo" script without password


Hi all,
I have a script where every line needs to be prefixed with sudo.
I was advised to run it as $ sudo /path/to/file.sh

This script needs to be scheduled in crontab to run. If I run it as above, will I be prompted for a password?
Also, one of the lines in the script is;
scp -r root@rem_server:/source/dir /local/dir
Does that mean I will have to provide two passwords: one for sudo and one for root?

Thanks,
 
Old 03-17-2015, 05:26 PM   #2
joe_2000
Member
 
Registered: Jul 2012
Location: Aachen, Germany
Distribution: Void, Debian
Posts: 808

Rep: Reputation: 216Reputation: 216Reputation: 216
Quote:
Originally Posted by epipko View Post
Hi all,
I have a script where every line needs to be prefixed with sudo.
I was advised to run it as $ sudo /path/to/file.sh

This script needs to be scheduled in crontab to run. If I run it as above, will I be prompted for a password?
This something you can configure. Run
Code:
sudo visudo
That will open the sudo config file in a text editor. Add a line that says
Code:
ALL ALL = NOPASSWD: /path/to/file.sh
Then close the editor.
It should then be sudo-runnable without password. Make sure the script is only writable by root (or admin users) and that it does not accept input the lets it do anything it should not be doing with root rights.

Quote:
Originally Posted by epipko View Post
Also, one of the lines in the script is;
scp -r root@rem_server:/source/dir /local/dir
Does that mean I will have to provide two passwords: one for sudo and one for root?

Thanks,
That's a different story. For that command you would typically have to provide the root password on the remote server, if password ssh authentication is activated. You can configure rsa-key based ssh authentification, and you can choose to make the ssh key password-free.

For security reasons, consider alternatives where you don't need to enable password-free root access on the remote machine. Since you are downloading, not uploading, maybe a regular user is enough? (You only need read rights on the files to be downloaded).

All in all, this whole post looks to me as if you were trying to solve a problem in a way that creates a lot of security holes, where it could actually be solved in a "cleaner" way. Although my answer from above should allow you to do what you are trying to do, I suggest to describe what you actually want to achieve, and I am sure better solutions could be offered.
 
1 members found this post helpful.
Old 03-19-2015, 11:25 AM   #3
epipko
LQ Newbie
 
Registered: Mar 2015
Posts: 7

Original Poster
Rep: Reputation: Disabled
Thank you.
I ended up running this piece of code to overcome "sudo" issue:
Quote:
if [ $EUID != 0 ]; then
echo "This script requires super privileges, attempting enlightenment ..."
sudo "$0" "$@"
exit $?
fi
I also created a user on "source" server with privs to the directory I need to copy and ran script as that user to overcome "root" issue.
 
Old 03-19-2015, 03:44 PM   #4
joe_2000
Member
 
Registered: Jul 2012
Location: Aachen, Germany
Distribution: Void, Debian
Posts: 808

Rep: Reputation: 216Reputation: 216Reputation: 216
Quote:
Originally Posted by epipko View Post
I also created a user on "source" server with privs to the directory I need to copy and ran script as that user to overcome "root" issue.
Probably a good decision. As far as your piece of code is concerned note that it won't do anything if you are actually running it as root.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
failed to run "sudo apt-get install system-config-kickstart" monika1 Ubuntu 1 10-27-2012 03:59 AM
Given "sudo su" when asked for root password acschnabel Linux - Newbie 7 09-28-2011 11:11 PM
[SOLVED] "sudo: Can't mkdir /var/run/sudo/%user%: File exists" CNBarnes Linux - Server 7 01-10-2011 05:11 PM
Shell Script: Find "Word" Run "Command" granatica Linux - Software 5 07-25-2007 08:42 AM


All times are GMT -5. The time now is 07:00 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration