How to add a new user into Ubuntu and setup ssh key?
Create a new user
sudo adduser <newusername>
i.e: create a new username: snowan
Now you've already created a new user, username: snowan
Next you want to add new user to sudo
group, so new user can have sudo
privileges.
Add user to the sudo
group
sudo
groupsudo usermod -aG sudo <username>
i.e. sudo usermod -aG sudo snowan
Now you've added new user into sudo
group, and you want to verify it's added successfully.
Verify sudo
user group
sudo
user groupLogin with newly added user
su - <username>
i.e. su - snowan
and enter password you added for this user
Verify
sudo
group user privilegessudo ls -la /root
orsudo -l -U <username>
for example: snowan
is sudo user and snowan.test
is not user,
Now you want to be able to ssh
into remote server without password. you need to create private and public key.
Add public key to allow remote SSH
login for new user
SSH
login for new userGo to client server and create ssh key
ssh-keygen -t rsa
and add filesnowan-rsa
under folder~/.ssh/
under ~/.ssh/
you will see 2 files
snowan-rsa
-- this is private keysnowan-rsa.pub
-- this is public key
and you need to add snowan-rsa.pub
public key into server ~/.ssh/
Copy public key over to server (snowan)
ssh-copy-id ~/.ssh/snowan-rsa.pub <remote server>
i.e ssh-copy-id ~/.ssh/snowan-rsa.pub snowwan@localhost
NOTE: If you encounter permission denied (publickey) issue, it is possible that your server do not allow passwordAuthentication. how to fix?
go to server (snowan) --
su - snowan
go to
/etc/ssh
--cd /etc/ssh
check
PasswordAuthentication
insshd_config
file, by default this value is set tono
, and to allow copy public key over from client server, temporary set toyes
vim sshd_config
and then setPasswordAuthentication yes
go to client server, and copy public key over to server (snowan), it should copy over.
Now you've copied public key over to server, you need to verify ssh is working
Verify ssh key working
Add ssh private key into host in config under
~/.ssh
now go to terminal,
ssh snowan
, and you should ssh into remote server
Congrates, you've already added a new user and ssh into remote server! :)
Last updated