tl;dr
To refer to the "file from your local machine", use /tmp/file.zip
instead of root@my_ip:/tmp/file.zip
.
Analysis
I am trying to send file from my local machine (Ubuntu) to Oracle VM (Oracle Linux).
[…]
scp […] root@my_ip:/tmp/file.zip opc@server_ip:/tmp
To handle an address like root@my_ip:…
scp
runs ssh
to run scp
(see "what happens under the hood" in this answer; but note it does not analyze the case of two remote addresses).
I understand my_ip
refers to your local machine, yet root@my_ip:…
is formally not a local path. Your scp
tries to ssh
to root@my_ip
and run scp
there; this is the connection that fails. If it succeeded, the other scp
would read /tmp/file.zip
.
Solution
scp
is perfectly capable of reading a local file without ssh
ing from local to local; just give it a local path:
scp -i /full_path_to_the_private_key/priv-key.key /tmp/file.zip opc@server_ip:/tmp
Here /tmp/file.zip
is the local path. (Note you don't need -r
when transferring a regular file I assume file.zip
is.)
The above is similar to the command scp
in your try would run after logging in via SSH to root@my_ip
, if you managed to log in. Our solution is to run the command directly. The difference is our scp
runs as the user you currently are, while your try requests scp
running as root. I don't know if your current user is root; it may not be and the difference may or may not matter. Keep reading.
About root
Specifying root@my_ip:/tmp/file.zip
only makes sense as a (cumbersome) way of accessing /tmp/file.zip
as root, in case you cannot access the file as the user you currently are. There are better ways to access the file as root.
A straightforward way is with sudo
:
sudo scp … /tmp/file.zip opc@server_ip:/tmp
however it will run the entire scp
as root. This is more than accessing a file as root, because now everything scp
does will (or at least can) be done as root. Considering the fact the scp
command you tried (if everything worked) would run another scp
after logging in to (formally remote) root@my_ip
and it would do this as root, running (some) scp
as root is what you tried anyway. In other words sudo scp …
is (almost) equivalent to what would happen if you were able to connect to root@my_ip
.
Still, using sudo
(or another way) to access the file as root is required only if you cannot access the file as your regular user. If you (as your current user) can simply access the file then you should directly invoke scp
and tell it to read the file by its local path /tmp/file.zip
; this is exactly the solution given in the previous section of this answer.