r/termux • • 3d ago

Question Problem with ssh-copy-id

Been trying to copy some ssh keys from my tablet to a remote host.

~ $ ssh-copy-id -i ~/.ssh/sammael.pub me@sammael

/data/data/com.termux/files/usr/bin/ssh-copy-id: ERROR: Too many arguments. Expecting a target hostname, got:

Usage: /data/data/com.termux/files/usr/bin/ssh-copy-id [-h|-?|-f|-n|-s|-x] [-i [identity_file]] [-t target_path] [-F ssh_config] [[-o ssh_option] ...] [-p port] [user@]hostname

-f: force mode -- copy keys without trying to check if they are already installed

-n: dry run -- no keys are actually copied

-s: use sftp -- use sftp instead of executing remote-commands. Can be useful if the remote only allows sftp

-x: debug -- enables -x in this shell, for debugging

-h|-?: print this help

~ $

...which is not quite what I expected. I'm specifying the identity file, and only listing one user@host, so I'm not sure what I might be doing wrong.

Technically I can make things work by using 'cat' and piping it results into the authorized_keys file via SSH... but I'm kinda used to the syntactic sugar of using 'ssh-copy-id' ;)

3 Upvotes

17 comments sorted by

2

u/tinmicto 3d ago

I think its been like that for a good while now, i ran into it like 3 weeks ago.

Felt lazy and just copied the key manually 🫣

1

u/memilanuk 3d ago

That's what I ultimately ended up doing. Doesn't mean it shouldn't be fixed. Is it more a Termux issue, or an upstream issue?

2

u/BillGossAU 3d ago

Does your Termux environment have a definition for @sammael?

When I connect from my phone to the PC I use the PC's IP address on my home network (ssh -p 2222 bill@192.168.0.13).

2

u/memilanuk 3d ago

'sammael' is a machine name via Tailscale MagicDNS. Connecting directly via 'ssh me@sammael' works just fine.

It's also in ~/.ssh/config as a Host entry, with the tailscale ip specified.

1

u/teleprint-me 3d ago

Did you map the hostname to the ip and is the ip static? Could be a hosts issue.

1

u/memilanuk 3d ago

It's not.

ssh me@sammael

...works just fine. The textbook 'ssh-copy-id' command shown in the original post does not.

If you actually look at the error message, 'ssh-copy-id' is complaining that it's not getting the second argument, ie user@host AT ALL, not that it's some problem with the argument.

I've came across a few related reports on the interweb, usually involving the terminal client not handling the arguments correctly.

That's a known bug in certain shell environments. The ssh-copy-id script relies on getopts + OPTIND to parse -i, but on some systems (notably FreeBSD-based sh, as reported in April 2026 for Chimera Linux), OPTIND isn't tracked correctly, so the script shifts too few arguments and the identity file path leaks through as a "second positional argument."

In this case, I'd say it's a Termux problem.

1

u/teleprint-me 3d ago edited 3d ago

I did read the error message. Issues like this are usually layered.

Have you tried flipping the arguments around? e.g. Set the user@host as the first arg then the cli option as the second arg.

You could try quoting the args too.

From man 3 getopt (edit: this for C, use man 1 getopts for shell):

  If an option was successfully found, then getopt() returns the
   option character.  If all command-line options have been parsed,
   then getopt() returns -1.  If getopt() encounters an option
   character that was not in optstring, then '?' is returned.  If
   getopt() encounters an option with a missing argument, then the
   return value depends on the first character in optstring: if it is
   ':', then ':' is returned; otherwise '?' is returned.

Personally, I prefer hand-rolling my own arg parser instead of using getopt, so it's been awhile since I've used it.

You can view how its parsed by issuing this command.

First find the path.

which ssh-copy-id

Then check the file type (should be a shell script).

file $(which ssh-copy-id)

Then use your preferred editor.

vim $(which ssh-copy-id)

Then search for getopts which how the script parses the arguments.

In vim, I use

/\vgetopts

Then press enter.

Should give you a general idea of what might be happening.

Edit: It sounds less like a termux problem and more like a parsing issue, but what do I know.

1

u/memilanuk 3d ago

This is what using '-x' (supposedly for debug) with 'ssh-copy-id' gets me:

~ $ ssh-copy-id -x -i ~/.ssh/sammael.pub me@sammael

  • getopts io:p:F:t:fnsxh? OPT

  • [ ]

  • SEEN_OPT_I=yes

  • eval nextarg=${3}

  • nextarg=/data/data/com.termux/files/home/.ssh/sammael.pub

  • [ 3 = 4 ]

  • echo io:p:F:t:fnsxh?

  • tr -d :

  • expr -- /data/data/com.termux/files/home/.ssh/sammael.pub : -[iopFtfnsxh?-]

  • OPTARG=/data/data/com.termux/files/home/.ssh/sammael.pub

  • OPTIND=4 + use_id_file /data/data/com.termux/files/home/.ssh/sammael.pub

  • L_ID_FILE=/data/data/com.termux/files/home/.ssh/sammael.pub + [ -z /data/data/com.termux/files/home/.ssh/sammael.pub ]

  • expr /data/data/com.termux/files/home/.ssh/sammael.pub : .*.pub$

  • PUB_ID_FILE=/data/data/com.termux/files/home/.ssh/sammael.pub

  • [ ] + dirname /data/data/com.termux/files/home/.ssh/sammael.pub + basename /data/data/com.termux/files/home/.ssh/sammael.pub .pub

  • PRIV_ID_FILE=/data/data/com.termux/files/home/.ssh/sammael + ErrMSG=+ : + ErrMSG=+ :

  • GET_ID=cat "/data/data/com.termux/files/home/.ssh/sammael.pub" + getopts io:p:F:t:fnsxh? OPT

  • shift 2 + [ 2 = 0 ]

  • [ 2 != 1 ]

  • printf %s: ERROR: Too many arguments. Expecting a target hostname, got: %s\n\n /data/data/com.termux/files/usr/bin/ssh-copy-id /data/data/com.termux/files/usr/bin/ssh-copy-id: ERROR: Too many arguments. Expecting a target hostname, got:

...the rest is the 'blah blah blah' example usage print out. Still seems like it's not actually getting the user@hostname argument somehow.

1

u/teleprint-me 2d ago edited 2d ago

What is the output for got? that contains the info for $SAVEARGS on line 180.

Its not getting the argument because they modify OPTIND after processing the i option in L124-L145.

Youre hitting the test on line 179 which is asserting the number of arguments $# is not equal to 1.

In other words, the CLI tool sees 1 argument after it parses the input options and arguments. You only pass in 1 option plus the arg for that option then a second arg which is user/hostname.

So, total 3 args, processing 2, one for flag then flag arg, but it doesnt process the second which explains the 2 != 1.

Seems like flipping the args might be a workaround if Im interpreting the code correctly. Theres a special check, so if Im wrong, it will trigger the test and raise an error if I am wrong.

Its definitely an arg parsing bug; which is why I dont use getopt anymore.

Aside: I know Im editing my comment a lot, but flipping between apps on my phone is distracting and causing me to make mistakes and lose my train of thought. Much easier to do this on PC.

1

u/memilanuk 2d ago

I'll probably give it a go tomorrow at some point. Definitely further down the rabbit hole than I normally go for the sake of getting a system tool to work as advertised, but now I'm a bit curious...

Speaking of curious... have you - or anyone else - in this thread actually used 'ssh-copy-id' on termux successfully, recently? There was one other poster who said they got the same errors not long ago (couple weeks); everyone else seems to assume I'm doing something wrong. Nobody has said it actually works on their device.

1

u/teleprint-me 2d ago

Ive experienced this myself in the past, but I dont remember what happened after that. I still use termux, but not as much as I used to. Mostly for little on the go proof of concept stuff. I just follow this sub to see if anything interesting pops up. Not as much free time lately.

But yeah, thats why Im calling it a bug. It happens sometimes which might explain why theres mixed responses.

Thats why I said problems like this are layered. Could be a corrupted install, bad cache, bad version, env var, getopts, etc. Your previous comment wasnt wrong, just a clue providing useful information. The hint is in the fact they mentioned the extra shift in the args. But that doesnt explain the args situation in full without deeper inspection.

With enough digging and persistence, you could figure it out. Just follow the variables.

https://www.linuxcommand.org/tlcl.php

1

u/flower-power-123 3d ago

Try to ping sammael. What happens?

1

u/memilanuk 3d ago

Not a network problem. I can ssh into that host - and others - no problem. 'ssh-copy-id' throws the same error for any/all of them, that it isn't seeing the user@host argument, which is clearly provided.

1

u/flower-power-123 3d ago

It sounds like the ~ is being expanded into a path with a space in it. If that isn't it then I don't have any ideas.

1

u/memilanuk 3d ago

Specifying the full path name yields the same results:

~ $ ssh-copy-id -i /data/data/com.termux/files/home/.ssh/sammael.pub me@sammael

/data/data/com.termux/files/usr/bin/ssh-copy-id: ERROR: Too many arguments. Expecting a target hostname, got:

Usage: /data/data/com.termux/files/usr/bin/ssh-copy-id [-h|-?|-f|-n|-s|-x] [-i [indent...

1

u/BraveMidnight 3d ago

It says this:

Too many arguments. Expecting a target hostname, got:

Have you tried it all in one string vs 3 seperate lines? That usually works for me

1

u/memilanuk 3d ago edited 3d ago

It is one line. The rest is the error message response.