Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SSH SCP Local file to Remote in Terminal Mac Os X

I am attempting to copy a local file 'magento.tar.gz' from my local machine to a remote server using SSH through a VPN. This is connecting to the Virtual Machine's Internal IP which I've used as xx.x.x.xx here.

I have full 'sudo' access on the SSH account so there shouldn't be any problem copying across. I have tried the following:

I have tried the following (the magento.tar.gz file is already in the local root dir)

sudo scp magento.tar.gz [email protected]/var/www/ 

This asks me to type in my local password. Afterwards returns cp: [email protected]/var/www: Not a directory

sudo scp /Users/myname/magento.tar.gz [email protected]/var/www/ 

Returns the same.

Do I need to include a SSH in there anywhere?

Do I need to connect via SSH to the site first?

Side note: I've managed to connect via SSH to the server, browse to the directory and make a folder and delete it using sudo mkdir etc so I definitely have permissions.

like image 536
James Avatar asked Aug 06 '12 03:08

James


People also ask

How do I transfer files from local Mac to SSH?

Copy Files Remotely To create one quickly, simply type echo "Hello SSH" > hello. txt in Terminal.) This will copy the local file sample. txt from the current working directory to the remote host's ~/Documents/ directory.

How do I scp a directory from local to remote?

To copy a directory (and all the files it contains), use scp with the -r option. This tells scp to recursively copy the source directory and its contents. You'll be prompted for your password on the source system ( deathstar.com ). The command won't work unless you enter the correct password.

How do I scp on a Mac?

In the Terminal app on your Mac, use the cp command to make a copy of a file. The -R flag causes cp to copy the folder and its contents. Note that the folder name does not end with a slash, which would change how cp copies the folder.

How copy SSH file from local to remote?

Copy all files from local to remote using scp. Copy all files and folders recursively from local to remote using scp. remoteuser need to exist and have write permission to /remote/folder/ in the remote system. GUI programs such WinSCP can also be used to transfer files between local and remote host using scp methods.


2 Answers

At first, you need to add : after the IP address to indicate the path is following:

scp magento.tar.gz [email protected]:/var/www 

I don't think you need to sudo the scp. In this case it doesn't affect the remote machine, only the local command.

Then if your user@xx.x.x.xx doesn't have write access to /var/www then you need to do it in 2 times:

Copy to remote server in your home folder (: represents your remote home folder, use :subfolder/ if needed, or :/home/user/ for full path):

scp magento.tar.gz [email protected]: 

Then SSH and move the file:

ssh [email protected] sudo mv magento.tar.gz /var/www 
like image 179
JScoobyCed Avatar answered Sep 21 '22 19:09

JScoobyCed


Just to clarify the answer given by JScoobyCed, the scp command cannot copy files to directories that require administrative permission. However, you can use the scp command to copy to directories that belong to the remote user.

So, to copy to a directory that requires root privileges, you must first copy that file to a directory belonging to the remote user using the scp command. Next, you must login to the remote account using ssh. Once logged in, you can then move the file to the directory of your choosing by using the sudo mv command. In short, the commands to use are as follows:

Using scp, copy file to a directory in the remote user's account, for example the Documents directory:

scp /path/to/your/local/file remoteUser@some_address:/home/remoteUser/Documents 

Next, login to the remote user's account using ssh and then move the file to a restricted directory using sudo:

ssh remoteUser@some_address sudo mv /home/remoteUser/Documents/file /var/www 
like image 29
legend12 Avatar answered Sep 24 '22 19:09

legend12