I often mess up when I manually set up the .ssh -dir, easy to forget some flag. I often need to create a new user and only allowing access with SSH, how can I do that? Basically, I want to stop doing this "# mkdir .ssh; cd .ssh; ssh-keygen; cat myKey > authorized_keys; cd ..; chown go-r; scp .ssh/myKey friend@IP' etc", I am sure I even now did some mistake somewhere -- I am pretty sure some command such as ssh-copy-id may be a solution here, at least it fixes the perms with .ssh when used rightly.

Want something like this, perhaps with ssh-copy-id?

   # adduser tester
   # ssh-key-gen docs
   # ssh-copy-id docs /home/tester/  # not working, how to do this easily?

and then on client side

   $ eval `ssh-agent`
   $ ssh-add ~/.ssh/docs
   $ ssh tester@IP

that should give the access to the repo with the priv -key.

Perhaps related

  1. How to add a ssh user who only has permissions to access specific folder?

  2. How would you create a user with the HOME_DIR field in /etc/passwd completely blank?

1 Answer 1


There is /etc/skel directory which is used as template for new user's home. You can place there .ssh directory with correct permissions and small shell script with commands you need to generate and copy ssh keys.

So all you need after user creation is to go to its home and run this script. Pretty simple.

Also you can end script with string

rm $0

so it will delete itself after run.

You must log in to answer this question.