# scp from one host to another [Solved; Workaround]

## ppurka

Edit: Solution obtained via workaround mentioned by @kurly: Add -3 argument to scp to do the transfer via local machine.

Original Post/Problem:

Recently I thought of this crazy method of transferring a file from one host to another via scp. I have publickey access (without password) to both hosts, and scp works in transferring files from local machine to either of the remote hosts and vice versa. But the following fails:

```
~» scp host1:filename host2:.

ssh: Could not resolve hostname host2: Name or service not known

lost connection
```

Whoops! So, maybe I need to give the full hostname along with domain name? Both are in the same LAN and have the same domain

```
~ [1] » scp host1.domain:filename host2.domain:.

Permission denied, please try again.

Permission denied, please try again.

Permission denied (publickey,password).

lost connection
```

So, even though I have passwordless publickey logins to both places, it seems scp can not transfer a file from one host to another! I am just curious about this behavior.

----------

## sabayonino

basically

```
$ scp /<local>/<file>  user@host_domain:/<remote>/<directory>/<filename>
```

to copy file1 from your local folder to remote host

From local user to LAN host

```
$ scp file1 <user>@192.168.1.40:/home/<user>/filename
```

From local user to Remote Host by domain name

```
$ scp file1 <user>@domain_name.org:/home/<user>/filename
```

see 

```
 man scp
```

 *Quote:*   

> [...]SYNOPSIS
> 
>      scp [-12346BCpqrv] [-c cipher] [-F ssh_config] [-i identity_file] [-l limit] [-o ssh_option] [-P port] [-S program]
> 
> [[user@]host1:]file1 ... [[user@]host2:]file2
> ...

 

----------

## 666threesixes666

i use sftp and file managers....  im pretty sure there is an sshfs to where you can mount remote directories as if they are local in portage.

----------

## NeddySeagoon

ppurka,

When you scp between hosts A and B while sitting at host C you need the same password/key on both hosts A and B.

At least, thats the only way I have made is work

----------

## ppurka

 *NeddySeagoon wrote:*   

> ppurka,
> 
> When you scp between hosts A and B while sitting at host C you need the same password/key on both hosts A and B.
> 
> At least, thats the only way I have made is work

 Actually, I have the same login, and publickey on both hosts A and B! I think it is a limitation of scp.

Using sshfs is of course a possibility. I was just curious why this didn't work.

Edit According to the manpage this shouldn't have failed:

```
     File names may contain a user and host specification to indicate that the file is to be copied to/from

     that host.  Local file names can be made explicit using absolute or relative pathnames to avoid scp

     treating file names containing ‘:’ as host specifiers.  Copies between two remote hosts are also per‐

     mitted.

```

----------

## gotyaoi

A couple thoughts, in no particular order:

Do you have an agent running on host 1 with the required key?

If you're ssh'd in to host 1 to run the scp, are you using agent forwarding?

Can you ssh from host 1 to host 2?

----------

## krinn

 *ppurka wrote:*   

> 
> 
> [code]~» scp host1:filename host2:.
> 
> ssh: Could not resolve hostname host2: Name or service not known
> ...

 

Well it just tell you host2 is not known, to jump from host1 to host2 you need host2 in host1 resolv.conf, but as soon as you have jump in host2, to jump to host3 : host2 resolve.conf must known host3, and it won't work even if host1 knows host3

I'm not sure what you are trying to do, copying to . is the same as doing [code]scp that host2[/code], no need to tell host2:.

----------

## ppurka

 *gotyaoi wrote:*   

> A couple thoughts, in no particular order:
> 
> Do you have an agent running on host 1 with the required key?
> 
> If you're ssh'd in to host 1 to run the scp, are you using agent forwarding?
> ...

 No ssh-agent is running anywhere.

I can not ssh from host 1 to host 2 with pubkey. I can do the ssh with password. *krinn wrote:*   

>  *ppurka wrote:*   
> 
> [code]~» scp host1:filename host2:.
> 
> ssh: Could not resolve hostname host2: Name or service not known
> ...

 I don't understand your point. Everything is under the same domain/LAN.

 *Quote:*   

> 
> 
> I'm not sure what you are trying to do, copying to . is the same as doing [code]scp that host2[/code], no need to tell host2:.

 It won't make a difference if I use :. or not.

----------

## krinn

My point is that you should just read what it tells you, and it tells you the host is unknown.

So your host resolution isn't working like you think.

The fact they are on the same domain just grant you ability to not specify the domain : ie my domain is "locdom" so i don't need to says "thathost.locdom" and just use "thathost" and it will assume looking at this one first.

but if it cannot resolve "thathost" or "thathost.locdom" you'll end with the same error : unknown host.

try scp -v it should tell you the resolved host ip, that's what that fail for you.

----------

## ppurka

 *krinn wrote:*   

> My point is that you should just read what it tells you, and it tells you the host is unknown.
> 
> So your host resolution isn't working like you think.
> 
> The fact they are on the same domain just grant you ability to not specify the domain : ie my domain is "locdom" so i don't need to says "thathost.locdom" and just use "thathost" and it will assume looking at this one first.
> ...

 thanks. I can understand that. But the second command also fails, where I give the full host.domain format. I guess, the two issues are unrelated?

----------

## kurly

Does the -3 flag for scp help?  This will redirect the transfer through the local machine.  It should not be necessary though if host1 and host2 can see each other.

----------

## gotyaoi

 *ppurka wrote:*   

> No ssh-agent is running anywhere.
> 
> I can not ssh from host 1 to host 2 with pubkey. I can do the ssh with password.

 

Hmm. Since you're not using -i, is your private key in the default location (~/.ssh/id_{rsa,dsa} or something, check the -i option in the ssh man page) on the initiating host?.

----------

## krinn

 *ppurka wrote:*   

> I can understand that. But the second command also fails, where I give the full host.domain format. I guess, the two issues are unrelated?

 

Well, not the same error, so chance they are related are low, but still they could share same root cause : if you badly set hosts resolution, you could even have host1.domain and host1 resolving to two different hosts...

Public key is also store without domain, and as you should have copy public key from an host to others host, the key is without domain.

So thatusers@thathost.thatdomain is not record in your key, but only thatuser@thathost.

- Something host1 connecting to the host2 should complain as it knows who is "user@host2" but not who is "user@host2.domain" yet, but offering you to connect to it still.

- Something that never bug host2 as when ssh or scp to it you cannot send your domain (well if not using -i), so host2 see thatuser@host1

- But that's something that change from copying from 2 hosts as now you tell host2 the one connecting is thatuser@host3.domain : and this can be denied if you have only thatuser@host3 set inside host2:authorized_keys file.

----------

## ppurka

 *kurly wrote:*   

> Does the -3 flag for scp help?  This will redirect the transfer through the local machine.  It should not be necessary though if host1 and host2 can see each other.

 Excellent! This works!

 *gotyaoi wrote:*   

> Hmm. Since you're not using -i, is your private key in the default location (~/.ssh/id_{rsa,dsa} or something, check the -i option in the ssh man page) on the initiating host?.

 Hmm.. -i ~/.ssh/<private key> does not work. I have both the hosts in my .ssh/config and the keyfile, login names, etc are all mentioned there.

```
Host host1* host2*

    ControlPath          ~/.ssh/master-%r@%h:%p

    ControlMaster        auto

    Compression          yes

    ForwardX11           yes

    ForwardX11Trusted    yes

    IdentityFile         ~/.ssh/private_key

    #GSSAPIAuthentication no

    User                 username

    PreferredAuthentications publickey,password
```

----------

