Cron job initiating ssh AND sudo (from user, not root)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Cron job initiating ssh AND sudo (from user, not root)
# 1  
Old 04-07-2011
Cron job initiating ssh AND sudo (from user, not root)

I've been bashing my head on the desk for 2 days trying to get this to work, but I've had no luck. I'll try to be as clear as possible in my explanation without dragging out the details. I'm trying to set up a cron job for user "john" which runs a script. This script initiates an ssh connection to another box (user : john), and then does a series of commands which require privilege escalation. For the purpose of using keys and avoiding passwordless root traversal across ssh, I cannot do this with the root cron.

Example script and cron job.
Code:
14 * * * * sh /home/john/localscript.sh

example code from localscript.sh
Code:
#!/bin/sh
ssh -t john@remoteip "sudo mv /home/john/file.txt /data/pcap/"



Now I have added the entire path to all of the commands, and that fixed the rest of my script (which is actually quite extensive), but I simply cannot get cron to work with sudo on the remote machine. Now for the same thing you always hear...If I run this script manually, everything works great. When I run it as a cron job, it does everything fine EXCEPT the line with the sudo. Any help is very much appreciated.

Last edited by Franklin52; 04-08-2011 at 03:24 AM.. Reason: Please use code tags
# 2  
Old 04-07-2011
Found this, don't know if it helps...
sudo and cron Bruno’s blog

I know you've said you added the absolute path... but in the command above, I don't see an absolute path to sudo...
# 3  
Old 04-07-2011
Quote:
Originally Posted by kitykity
I know you've said you added the absolute path... but in the command above, I don't see an absolute path to sudo...
Oh sorry, I edited out the paths on my post to more easily present the coding. I haven't tried the solution on that page yet because it requires modifying REQUIRESTTY, which I get around by using the -t option in ssh. I have security worries with commenting out REQUIRESTTY, but if no other options come up, I suppose that is what will have to happen.

---------- Post updated at 01:40 PM ---------- Previous update was at 10:48 AM ----------

I did try the individual version of what was described about, but now I have satisfied my test script, but I still have issues with my "real" script. Just in case, I'll post the temporary solution for now, which was just to add in a user based requiretty in /etc/sudoers

Default:john !requiretty

I'll post my supersolution, once I come across it.
# 4  
Old 04-07-2011
A different kind of solution might be to use a special group. That is,

- make a new group with groupadd
- set all the files that must be accessed to that group with chgrp
- add the user to that supplementary group with usermod -a -G
- set the group permissions on all those files as required with chmod.

Then don't use any privilege escalation at all.
# 5  
Old 04-08-2011
check john's mail in unix id, do you get error message? You should see some error messages, it will help you to troubleshooting this issue.

And do you try kitykity's suggestion?

ssh -t john@remoteip "/usr/bin/sudo mv /home/john/file.txt /data/pcap/"
# 6  
Old 04-08-2011
Everything is working fine and I appreciate all of the help. To run my script, I needed to change two things. I edited the /etc/sudoers file to include the following;
Code:
Defaults:john  !requiretty

The next step was to remove the ssh -t option to go with the first step. While this didn't cure everything, a small rewrite of some other additional scripts did fix the rest. This fixed the real problem though. Thanks for the help people! Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Appending crontab using ssh and sudo without root credentials

Hi, i have two servers say server A and server B. i have a sudo user say user1 with full privilges on server A and B. i am trying to append the crontab entry of root from server A of server B with the following command. But its appending on A. i need to append it on server B. please find the... (4 Replies)
Discussion started by: venkitesh
4 Replies

2. Red Hat

Sudo to user other than root but do not allow sudo to root

I have a set of RHEL 5 boxes running our ERP software on Oracle databases. I need to allow my DBA's to su to oracle and one other account (banner) without knowing the oracle or banner password. But I need to prevent them from su'ing to any other user especially root. I only want them to be able to... (1 Reply)
Discussion started by: westmoreland
1 Replies

3. Shell Programming and Scripting

CRON JOB not running - for tape backup from root user

I am trying a cron on root user to backup to tape using TAR command here is the cron entry 11 08 * * 6 /erdhot1cron 2>&1 >> /test3/scripts/dba/erdhot1cron.log here is script inside - edhot1cron #!/bin/bash vsysdt=`date +%d%m%y` date tar -cvf /dev/st0... (4 Replies)
Discussion started by: vijaymec50
4 Replies

4. UNIX for Dummies Questions & Answers

Create cron job without root

Hi there, I need to create a crone job to backup certain files on my disk without root permissions. Also if I will be able to edit cronetab then I need to know how to edit it with any editor other than vim :mad: (5 Replies)
Discussion started by: aelhosiny
5 Replies

5. UNIX for Dummies Questions & Answers

Sudo to delegate permission from non-root user to another non-root user

I've been through many threads before i decide to create a separate thread. I can't really find the solution to my (simple) problem. Here's what I'm trying to achieve: As "canar" user I want to run a command, let's say "/opt/ocaml/bin/ocaml" as "duck" user. The only to achieve this is to... (1 Reply)
Discussion started by: canar
1 Replies

6. UNIX for Dummies Questions & Answers

SSH Cron Job Help

Hi there, I have an ESXi server and I need to shut it down every night (say at 11 for simplicity). To shut down the ESXi box remotely, I can send an SSH command "poweroff". Basically my question is, is there any way that I can save the script on my DD-WRT router so that it will login to the... (3 Replies)
Discussion started by: georgeharris
3 Replies

7. Shell Programming and Scripting

Does running a cron job of a user require the user to be logged in?

Suppose user 'asdf' is not logged into server 'bbbb', but the server is up. User 'asdf' has cron job. Will it be executed? (1 Reply)
Discussion started by: thulasidharan2k
1 Replies

8. Shell Programming and Scripting

Adding a new root cron job

Hi there, I have over 2000 systems (mainly Solaris) and I want to write a script that inserts a new root cronjob on each of those servers. obviously just adding a line to /var/spool/cron/crontabs/root looks like the easiest way, but i really dont want to have to send a HUP to crond on all boxes ... (3 Replies)
Discussion started by: rethink
3 Replies

9. Shell Programming and Scripting

Sudo command not working with cron job.

Hello, I have written a script that has a sudo command to change file permissions within it. When I run the script manually, the sudo command inside of it works fine. When the script is run through crontab I get the error "cron: not found". It the same user profile that I am using... (6 Replies)
Discussion started by: WhotheWhat
6 Replies

10. UNIX for Dummies Questions & Answers

Cron Sends mail to root on completion of job.

All I am running a few scripts through a cron job. This is for checking some key services that are running on by box. The problem is, everytime the cron runs, it sends a mail to the root account. The root account mails need to cleared every now and then. Is there some way I can stop these... (1 Reply)
Discussion started by: skotapal
1 Replies
Login or Register to Ask a Question