9

On my Fedora 20 system I use scp a lot, and this is the second time I encounter this, when I run this command:

scp -r -P PORT user@host:/home/user/something/{file1,folder1,folder2,folder3,folder4} folder/folder2/

it asks me for the password for each file/directory it transfers.

user@host's password: "password here"

Question:

What is happening here?

Is this normal, I would think this is very peculiar behavior?

13

Your local shell (probably bash) is expanding

user@host:/home/user/something/{file1,folder1,folder2,folder3,folder4}

into:

user@host:/home/user/something/file1 user@host:/home/user/something/folder1 user@host:/home/user/something/folder2 user@host:/home/user/something/folder3 user@host:/home/user/something/folder4

Instead, you can do:

scp -r -P PORT user@host:"/home/user/something/file1 /home/user/something/folder1 /home/user/something/folder2 /home/user/something/folder3 /home/user/something/folder4" folder/folder2/

or, if you know user's login shell on the remote end is bash, you can use brace expansion too:

scp -r -P PORT user@host:"/home/user/something/{file1,folder1,folder2,folder3,folder4}" folder/folder2/

to have the remote shell split the string into arguments instead of the local shell.

5

Yes, it's normal. What happens is that your shell expands the braces before running the command so what you're actually running is

scp -r -P PORT user@host:/home/user/something/file1 \ 
               user@host:/home/user/something/folder1 \ 
               user@host:/home/user/something/folder3 \ 
               user@host:/home/user/something/folder4 folder/folder2

Basically, scp sees multiple connection requests so opens a new connection for each of them. One way The simplest way(using quotes as suggested by @geirha is simpler for a one-off operation) to solve this is to set up passwordless ssh/scp:

$ ssh-keygen 
$ ssh-copy-id -p PORT user@host

After running those two commands once (and answering the prompts as needed), you will be able to ssh/scp without using a password for all future ssh connections. This is i) actually more secure and ii) much more convenient.

  • +1, nice way (of course the ssh-keygen & ssh-copy-id has to be made only once). I'd say that it is more secure ... only if you make sure the private part is "unreachable" by anyone except that user... Which is not always easy. – Olivier Dulac Nov 10 '14 at 16:12
  • @OlivierDulac what do you mean? The id.rsa will have 600 permissions by default and ssh won't even work if it doesn't. This means it is safe from anyone but root and nothing is really safe from root, certainly not passwords. – terdon Nov 10 '14 at 16:14
  • I just meant to add this comment, so that people setting up a passwordless access take into account (and grok) the fact that the private key has to be in a safe environment and unreadable/unshared/etc. (ie, it's a gentle way to point this out, as the ones asking may not know all the implications that sharing/letting the private key be accessed could have) – Olivier Dulac Nov 10 '14 at 16:17
  • @OlivierDulac ah, fair enough. I didn't think it worth mentioning because 1) it happens automatically when using ssh-keygen and 2) ssh/scp will complain and ask for a password if the file is readable by anyone else. At least on Linux anyway. – terdon Nov 10 '14 at 16:19
4

scp isn't very smart: when given multiple command line arguments that are files from the same remote host, it opens a new connection for each argument.

You can use rsync instead of scp, it's smarter this way (and in other ways).

rsync -r -e 'ssh -P PORT' user@host:/home/user/something/{file1,folder1,folder2,folder3,folder4} folder/folder2/

Another approach is to pass a single argument to scp that describes multiple files.

A different approach is to set up your system so that you don't have to authenticate all the time. Preferably, set up key authentication, which is in most scenarios both more convenient and more secure. Alternatively, or in addition, set up connection sharing, so that you only need to authenticate once per session. In any case, set up an alias so you don't have to specify the username and port every time. In your ~/.ssh/config:

ControlMaster auto
ControlPath ~/.ssh/control:%h:%p:%r

Host nick
HostName real-host-name.example.com
User bob
Port 1234

Run ssh -Nf nick to open a connection, and then all subsequent connections to nick will piggyback on the existing connection. Now you can just run

scp -r nick:/home/user/something/{file1,folder1,folder2,folder3,folder4} folder/folder2/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.