LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   sudo and non sudo in one command (https://www.linuxquestions.org/questions/linux-newbie-8/sudo-and-non-sudo-in-one-command-4175549675/)

Garrett85 08-02-2015 09:35 PM

sudo and non sudo in one command
 
I need to be able to run the command
Code:

rsync -a --delete ~/Music /SDcard && shutdown
The problem is that shutdown is a root or sudo command and I don't want to run rsync as sudo
because that would make all the files that rsync copies owned by root. I need to be able
to run this command because syncing my music library takes a while and that means I also
won't be around to type in my sudo password anyway. Any ideas or suggestions? Thanks.

syg00 08-02-2015 10:07 PM

That's what sudo was invented for. Give a group you're in access to the shutdown command without password.
"man sudoers".

pan64 08-03-2015 12:32 AM

&& sudo shutdown

schedar 08-03-2015 04:54 PM

echo 'yourusername ALL=(root) NOPASSWD: /usr/sbin/shutdown' >> /etc/sudoers

and

rsync -a --delete ~/Music /SDcard && sudo shutdown

Garrett85 08-03-2015 05:49 PM

pan64 - that command would be waiting for a password and I would be around to enter it. That's actually what I tried.
schedar - I'll give that a try. Does it matter that there is no file called /usr/sbin/shutdown? When I ran `which shutdown` it returned /sbin/shutdown. Should I replace /usr/sbin/shutdown with /sbin/shutdown in your examples? Thanks all.

schedar 08-03-2015 06:03 PM

Yes replace path with the valid one.

Garrett85 08-03-2015 09:33 PM

garrett@mint-desktop /sbin $ sudo echo 'garrett ALL=(root) NOPASSWD: /sbin/shutdown' >> /etc/sudoers
bash: /etc/sudoers: Permission denied

pan64 08-04-2015 02:03 AM

you should try sudo visudo instead (and add the line: 'garrett ALL=(root) NOPASSWD: /sbin/shutdown). That will skip password checking

descendant_command 08-04-2015 02:07 AM

Use visudo command to edit the sudoers file rather than editing it directly or echoing stuff into it.

edit: SNAP! :)

Garrett85 08-16-2015 03:47 PM

#
# This file MUST be edited with the 'visudo' command as root.
#
# Please consider adding local content in /etc/sudoers.d/ instead of
# directly modifying this file.
#
# See the man page for details on how to write a sudoers file.
#
Defaults env_reset
Defaults mail_badpass
Defaults secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"

# 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

# See sudoers(5) for more information on "#include" directives:

#includedir /etc/sudoers.d
garrett ALL=(root) NOPASSWD: /sbin/shutdown


It's still not working, I'm still getting the error that I need to be root.

JaseP 08-16-2015 03:52 PM

Here's a nice page on how to shutdown as a regular user...
http://www.spencerstirling.com/compu.../shutdown.html

Garrett85 08-16-2015 04:40 PM

Thanks JaseP - That got it.


All times are GMT -5. The time now is 05:45 AM.