The Linux utility scp is great for copying files to remote hosts. But very often you need to copy files into folders that only have root permissions, like /etc , /var , /bin . And very often root login via ssh in not allowed. And scp itself does have an option for sudo on the remote host. What a pain!
Ahhh... Here is a solution : run the scp command-- via ssh-- on the remote host instead of source host that has the file you want to copy.
From the remote host you can use scp to pull the file from the source host , as long as your user account on the source host has read access to it.
But you need to run "sudo scp" on the remote host to copy the file into a folder with root-only permissons
Hooray !!
Here are the steps:
-Copy a file "script.sh" from sourcehost:/var/prtg/scripts to /var/prtg/scripts on a remote host-
#The /var/prtg/scripts
directory does not exist on the remote host so we have to create
it. And we have to use “sudo” because
my account afrancis does not have write permissions for /var
[afrancis@linuxhost/]$
ssh –t afrancis@remotehost “sudo mkdir –p /var/prtg/scripts “
#Now we need to use ssh to run scp on the remote host and copy the file from
the source host to /var/prtg/scripts.
# In this example my user account has read access to the file "script.sh" on the source so it can copy
it ok, but we need to use "sudo scp" on the remote host because
my user account does not have write perms for /var/prtg/scripts on the remote
host.
[afrancis@linuxhost/]$ssh -t afrancis@remotehost "sudo scp afrancis@linuxhost:/var/prtg/scripts/script.sh
/var/prtg/scripts/"
#Type in my password to ssh into the remote host
afrancis@remotehost’s password:
#Type in again to run
sudo on the remote host
[sudo] password for afrancis:
# Type my password a third time to copy the file from the source
afrancis@linuxhost's password:
# And it copied !
script.sh
100% 1058 1.0KB/s 00:00
Connection to remotehost closed.
[afrancis@linuxhost/]$
#All Done !
--------------------------------------------------------------------------------------------------------------
Notes :
- The /var/prtg/scripts directory does not exist on the remote host so we have to create it.
- We have to use “sudo” because my account does not have write permissions for /var
- The –t override the “no tty error” when ssh’ing to the remote host
- Mkdir –p creates both prtg and scripts folders at the same time.
No comments:
Post a Comment