How to setup and configure SSH keys for root and additional users for terminal access to the server

I am having some trouble trying to set up SSH keys, i have been following various articles but i keep facing obstalces.
I would be grateful if someone could help me with some step by step instructions. in creating ssh keys for all users
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

On your workstation, or the system you want to SSH from generate the keys:

ssh-keygen -t rsa

(Enter information)

Copy the newly generated key to the server:

scp ~/.ssh/ server:.ssh/authorized_keys2

Make sure you have the correct permissions on your .ssh directory:

chmod -R 700 ~/.ssh

Thats it.
BTW, it is a bad idea to allow root remote access.  It is a major security violation as well as creates auditing issues.  Your better off making people log in as themselves as using sudo.  But that is up to you.
X-quisiteAuthor Commented:

Thank you for your response. Root user has been disabled, instead its for sudoers. i have a user amansi which i want to make public keys for ssh login.

Are you saying that a sudoer cannot use SSH public keys?
Introducing the "443 Security Simplified" Podcast

This new podcast puts you inside the minds of leading white-hat hackers and security researchers. Hosts Marc Laliberte and Corey Nachreiner turn complex security concepts into easily understood and actionable insights on the latest cyber security headlines and trends.

X-quisiteAuthor Commented:

as per your instructions i ma doing the following:

1. on my mac in terminal : ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/nazmulislam/.ssh/id_rsa):

pressed enter

Your public key has been saved in /Users/nazmulislam/.ssh/
The key fingerprint is: some values

2. then in terminal again scp ~/.ssh/ nislam@server:.ssh/authorized_keys2

i get the following:

stdin: is not a tty
scp: .ssh/authorized_keys2: No such file or directory

not sure what to do from here?
Gerwin Jansen, EE MVETopic Advisor Commented:
You need to copy that file to /home/<userid>/.ssh on the remote server, so if your remote home path is /home/nislam then the command would be:

scp ~/.ssh/ nislam@server:/home/nislam/.ssh/authorized_keys2

Alternatively, you can logon to the remote server and edit the file like this:

vi ~/.ssh/authorized_keys2

and then copy/paste the text with that is in the file from the other machine (one line). Set the access to the .ssh folder to 700 and the authorized_keys file to 600
That error typically means that the .ssh folder has not yet been created.

The simplest way would be to install ssh-copy-id, but macs do not have that by default.  You could install it with homebrew, but sometimes it's easier to use existing commands.

Connect to the remote server and create the .ssh directory in the user's folder if it doesn't exist.  Be sure to include the spaces between the brackets.
ssh nazmulislam@server [ ! -d .ssh ] && mkdir .ssh

Append the key to the file.  It's better to append, in case the file already exists and has data.  It will still create the file if it hasn't been created.
cat ~.ssh/ |ssh nazmulisam@server  'sh -c "cat - >>~/.ssh/authorized_keys2"'

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
X-quisiteAuthor Commented:

This did made it work
cat ~.ssh/ |ssh nazmulisam@server  'sh -c "cat - >>~/.ssh/authorized_keys2"'

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Linux Security

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.