1

I have 2 Ubuntu servers: 12.04.5 LTS and 16.04.1 LTS, in a local network, where I'm a administrator of the both servers and can be the super users on them. Let's say each server is A and B, and I log in to the server A now.

When I want to copy a file from A to a directory in B, where root authority of B is required to put the file, how can I do that?

My trial was as follows but it didn't work due to no authority for the server B:

sudo scp /foobar/foo/bar.txt user@serverB:/bar.txt
scp: /bar.txt: Permission denied

The sudo power affects only the permission of the source and doesn't affect the permission of the destination directory.

Of course, If I change the permission of the destination directory appropriately, I can copy the file without no permission error. But changing the permission every time when I copy files is a little annoying.

And root login is not allowed to both servers as the default configuration of Ubuntu is so.

If any of you know some good way, please teach me. I use bash shell.

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
Taiki Bessho
  • 1,105

3 Answers3

2

Umm... why don't you create a directory on server B where you don't need elevated rights, copy the file to that location, and then simply move it wherever you want it when logged in at server B. KISS.

  • KISS principle. I see. I asked this question because I thought most linux users use some cool way to accomplish my wish at first. But it has turned out there is no such cool way. Thank you. – Taiki Bessho Jan 25 '17 at 11:50
  • You're welcome. The KISS principle is pretty cool imo. –  Jan 25 '17 at 12:10
2

If you can read the file on the source machine as a regular user (rather than root), consider a pull scp rather than a push one:

serverB:~$ sudo scp user@serverA:/foobar/foo/bar.txt /bar.txt

If you cannot read the file on the source machine, you'll need to do the two steps you described. There is no way to have sudo work on more than one machine.

  • Pulling the file from B by scp is also impossible since the file in A is not permitted to read by a regular user... "There is no way to have sudo work on more than one machine." < I see and thank you for your advice. – Taiki Bessho Jan 25 '17 at 11:37
  • You could use SSH keys though, and allow root on one machine to access root on another machine using that SSH key. The fact that Ubuntu doesn't allow that by default shouldn't be a blocker... it's your machine, after all, not Ubuntu's – Wouter Verhelst Jan 25 '17 at 11:39
  • permitting root login with a SSH key on one machine may be the easiest way to accomplish my wish. I see. – Taiki Bessho Jan 25 '17 at 11:47
1

I recommend you to use rsync so you can use sudo in the distention machine, follow this instruction:

  1. Find out the path to rsync: which rsync
  2. Edit the /etc/sudoers file: sudo visudo
  3. Add the line <username> ALL=NOPASSWD:<path to rsync>, where username is the login name of the user that rsync will use to log on. That user must be able to use sudo

Then, on the source machine, specify that sudo rsync shall be used:

rsync ... --rsync-path="sudo rsync" ...

Using it without the NOPASSWD on the destination machine will result in the message

sudo: no tty present and no askpass program specified