LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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 06-12-2012, 01:42 PM   #1
jibtga
LQ Newbie
 
Registered: Jun 2012
Posts: 16

Rep: Reputation: Disabled
run script as root without password


I want to run a script without prompt for password.
I edited /etc/sudoers file and I wrote several lines behind last one. This is my file:

#
Defaults env_reset

# Host alias specification

# User alias specification

# Cmnd alias specification

# User privilege specification
root ALL=(ALL:ALL) ALL

# Members of the admin group may gain root privileges
%admin ALL=(ALL) ALL

# Allow members of group sudo to execute any command
%sudo ALL=(ALL:ALL) ALL

#includedir /etc/sudoers.d

jose ALL= NOPASSWD: /sbin/shutdown -h now
jose ALL= NOPASSWD: /sbin/shutdown -r now
jose ALL= NOPASSWD: /home/jose/sync.sh



I can shutdown and reboot without type password but when I try to run the last line, the script prompt for password.

The script code (that work fine with password) is:

#!/bin/bash
#Filename: sync.sh

echo "Empezando sincronizar....."

ping -c 1 -t 1 172.18.2.100 > /dev/null 2> /dev/null # ping and discard output

if [ $? -eq 0 ]; then # check the exit code
echo "${ip} is up" # display the output
sudo mount -t smbfs //172.18.2.100/Downloads /home/jose/pc1
rsync -r -u -verbose /home/jose/pc1/movies/ /home/jose/Movies/
rsync -r -u -verbose /home/jose/pc1/series/ /home/jose/TV?Shows/
sudo umount /home/jose/pc1

# you could send this to a log file by using the >>pinglog.txt redirect
else
echo "${ip} is down"
fi

echo "Fin sincronizacion"


I've read a lot of forums but I can't solve the question.

Any help? Thanks.

Last edited by jibtga; 06-12-2012 at 01:54 PM.
 
Old 06-12-2012, 02:22 PM   #2
fogpipe
Member
 
Registered: Mar 2011
Distribution: Slackware 64 -current,
Posts: 550

Rep: Reputation: 196Reputation: 196
What i did to solve a similar problem, was first making sure my user belongs to all the appropriate groups in /etc/group, putting the following in /etc/sudoers
Code:
Defaults:myuser   !requiretty
Substitute your actual username for myuser
and running my script in an xterm with
Code:
xterm -e scriptname
Hope this points you in the right direction.

Also make sure that /home/jose/pc1 appears in /etc/fstab with the "user" flag set.
 
Old 06-12-2012, 03:18 PM   #3
jibtga
LQ Newbie
 
Registered: Jun 2012
Posts: 16

Original Poster
Rep: Reputation: Disabled
User belongs....

jose : jose adm dialout cdrom floppy audio video plugdev users lpadmin sambashare admin

I put "Defaults:jose !requiretty" in sudoers and it doesn't work.

Thanks, any idea?
 
Old 06-12-2012, 05:33 PM   #4
VDP76
Member
 
Registered: Apr 2010
Location: Bayreuth, Germany
Distribution: CrunchBang Linux (#!)
Posts: 111

Rep: Reputation: 19
I guess the point is just that your script includes the sudo command to mount and unmount your remote drive.
I would try to add your samba drive in /etc/fstab with the option users so that non-root user can mount it, this might help.

good luck

p.s.: please, when posting code and/or terminal outputs use the [CODE] tags, it make the thread more readable..
 
1 members found this post helpful.
Old 06-14-2012, 04:04 AM   #5
jibtga
LQ Newbie
 
Registered: Jun 2012
Posts: 16

Original Poster
Rep: Reputation: Disabled
Great, it works, but I am wondering why mount command doestn't work when I add at the end of sudures file:

Quote:
jose ALL= NOPASSWD: ALL
And others commands work without password.
 
Old 06-14-2012, 06:24 AM   #6
VDP76
Member
 
Registered: Apr 2010
Location: Bayreuth, Germany
Distribution: CrunchBang Linux (#!)
Posts: 111

Rep: Reputation: 19
Glad it worked! So you might want to use the thread tools and mark it as solved.

I guess to have mount working without sudo you need to add specific lines to your sudoers file, check this out.

Anyway, I guess you already know that what you are doing is dangerous for the security of your system, so I won't tell you...

Last edited by VDP76; 06-14-2012 at 06:25 AM.
 
Old 06-14-2012, 06:26 AM   #7
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
there appears to be NO justification for using sudo here at all. If you want to do that mount as a normal user, just put the entry into /etc/fstab.
 
Old 06-14-2012, 06:46 AM   #8
Roken
Member
 
Registered: Oct 2011
Location: Warrington, UK
Distribution: Arch local, Debian on VPS, several RPIs.
Posts: 300
Blog Entries: 1

Rep: Reputation: 55
The way I've always done it, rather than giving the whole script root priveliges, is to use a simple echo statement piped to sudo:

Code:
echo "password" | sudo -S rootcommand
 
Old 06-14-2012, 07:23 AM   #9
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
Quote:
Originally Posted by Roken View Post
The way I've always done it, rather than giving the whole script root priveliges, is to use a simple echo statement piped to sudo:

Code:
echo "password" | sudo -S rootcommand
Wow, that's... horrible! why would you want to hardcode a password???
 
Old 06-14-2012, 08:26 AM   #10
Roken
Member
 
Registered: Oct 2011
Location: Warrington, UK
Distribution: Arch local, Debian on VPS, several RPIs.
Posts: 300
Blog Entries: 1

Rep: Reputation: 55
Well, for me it's safe. I'm the only user on this system and login is protected anyway. If anyone could get into the system then they already have the password.

I admit it's not a good solution on a multi-user/networked system.
 
Old 06-14-2012, 08:31 AM   #11
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
Quote:
Originally Posted by Roken View Post
Well, for me it's safe. I'm the only user on this system and login is protected anyway. If anyone could get into the system then they already have the password.

I admit it's not a good solution on a multi-user/networked system.
just update /etc/sudoers in future, it's no more work at all.
 
  


Reply



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
Creating a script that will log into root and enter password and then run commands. tananthulus Linux - Newbie 8 11-02-2009 05:37 PM
How to run a shell script as root (when user is not root) taylorkh Linux - Newbie 10 09-12-2008 06:05 PM
How can I have a script owned as root and run as root by a user: setuid? stickey bit? abefroman Linux - Newbie 9 04-19-2008 05:15 PM
Is a script, run at boot time from init.d, run with root authority? tmbrwolf53 Linux - Server 2 03-31-2007 08:15 PM
My Knoppix 3.7 need root password when I run program AskMe Linux - Software 5 04-15-2005 05:35 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:53 AM.

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
Open Source Consulting | Domain Registration