I have 4 identical boxes, I logged on to the first one and did sudo -s, followed by ssh-keygen -t rsa which generated the keys and placed them in /root/.ssh/

I then typed ssh-copy-id -i /root/.ssh/id_rsa.pub user@machine which then asked for my password and worked fine.

When I went to the next box, I got the following message:

mktemp: failed to create file via template ‘/home/user/.ssh/ssh-copy-id_id.XXXXXXXXXX’: No such file or directory
mktemp failed

and I seem to be pretty stuck, but, also very confused.

Can anyone help and explain what has happened/why this worked on one machine?

  • add a verbose mode to the ssh-copy-id and see what it's tell you. also check on google the error you show us from the next box
    – maniat1k
    Commented Jun 25, 2015 at 11:33
  • @maniat1k - after trying for hours, I didn't know that this wasn't compiled, ssh-copy-id is just a script... I opened it and I have found the problem... Will write answer shortly. ... Nothing on Google! Trust me... I searched the hell out of this!
    – wilhil
    Commented Jun 25, 2015 at 11:35
  • 1
    @wilhil - file /path/to/program is your friend :-)
    – Jenny D
    Commented Jun 25, 2015 at 15:19

4 Answers 4


I didn't realise ssh-copy-id is a script and I took a look at it.

I was using Ubuntu as root (via sudo -s) after logging in as a non root user, so, home was still set as /home/user

So, mktemp doesn't create subfolders, and is hard coded to create a temp file as ~/.ssh/tempfile- I just created .ssh in /home/user and it worked fine.

I had previously used SSH on the first machine, so, this folder already existed.

  • I am getting the same error, but it's trying to create the file direct in my home directory. Obviously they've tweaked the script, but not enough to allow me to use it via sudo
    – Adam
    Commented Oct 5, 2016 at 17:57
  • 1
    @Adam Check permissions on the file/folder, make sure it isn't owned by root/similar
    – wilhil
    Commented Oct 7, 2016 at 9:02

After WSL 2 Ubuntu 20.04 apt update upgrade:

ssh-copy-id user@hostname
mktemp failed to create file via template '~/.ssh/ssh-copy-id.XXXXXXXXXX': No such file or directory
ssh-copy-id: ERORR mktemp failed

I was able to solve the above problem by doing the following:

sudo vi /usr/bin/ssh-copy-id





ssh-copy-id now works as expected.

  • This seems like a solution, but you also appear to be asking a (follow-up) question (at the end), which is a little confusing, and not how Stack Exchange works (as this is a Q&A site and not a thread of messages). Please take the tour to better understand how SE works. Thanks. Commented Jul 26, 2022 at 23:14
  • This solution also works on debian based systems Commented Sep 12, 2022 at 22:10
  • Did not work for me (WSL Ubuntu 20.04)
    – Marc
    Commented Sep 15, 2022 at 6:22

Be sure .ssh folder's owner is your current user and not root.


Apparently ssh-copy-id when its mktemp sees ~/.ssh it thinks that means /root/.ssh, so you just need to create directory .ssh under /root, that solved it for me.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .