I want to write a file in the Machine B under

/home/test whose permissions are root:root and rwxr-xr-x.

I have an account on the Machine B, named testUser. When logged to the Machine B with this user, I can use sudo to write a file under /home/test.

However now I want to use ssh command on the Machine A to order testUser@MachineB to write a file under /home/test via ssh, but I got the Permission denied message when using scp to copy a file to /home/test. I want to avoid typing password so that these action can be done using a script. Is there any way to solve it?

By the way, I've set ssh-key to login without password authentication.

Is using setuid on a executable file on Machine B to write a file under /home/test a good way?

  • You'll have to delete the file or change its ownership first. Or scp as root - baaaad idea ... – tink Feb 24 '16 at 2:59

You could configure sudo to allow the command cp foo /home/test/ without a password, and then use this command:

ssh testUser@MachineB 'cat > foo && sudo mv foo /home/test/' < my_file

Piping data through ssh to a command of your choice on the other end is the general trick here. You can probably find a dozen other ways to accomplish your goal using variations of this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.