Tuesday, July 11, 2017

How to create a new user in the linux machine (centos)

Steps to Create a New Sudo User
  1. Log in to your server as the root user. ssh root@server_ip_address.
  2. Use the adduser command to add a new user to your system. Be sure to replace username with the user that you want to create. ...
  3. Use the usermod command to add the user to the wheel group.
  4. Test sudo access on new user account.

if you are signed in as the root user, you can create a new user at any time by typing:

adduser username

Note

After creating the user, for example you create ec2-user.
when you switch the user from root to the ec2-user,we will be using the sudo before any command to execute,Inorder to avoid the password asking part, you need to make some changes in the visudo file 

go to the root user   >  visudo    >   

## Read drop-in files from /etc/sudoers.d (the # here does not mean a comment)
#includedir /etc/sudoers.d

kaushik   ALL=(ALL)       NOPASSWD: ALL
ec2-user    ALL=(ALL)       NOPASSWD: ALL

so,whenever you execute any command  in the above specified two users, it won’t ask you for the password.


If you are signed in as a non-root user who has been given sudo privileges, as demonstrated in the next section of this tutorial, you can add a new user by typing:

sudo adduser username

Next, you'll need to give your user a password so that they can log in. To do so, use the passwd command:


passwd username


We can do this by adding the user to the wheel group (which gives sudo access to all of its members by default) through the gpasswd command. This is the safest and easiest way to manage sudo user rights.

If you are currently signed in as the root user, type:

gpasswd -a username wheel


If you are signed in using a non-root user with sudo privileges, type this instead:

sudo gpasswd -a username wheel



Note

sudo lid -g wheel

The output will show you the usernames and UIDs that are associated with the group. This is a good way of confirming that your previous commands were successful, and that the user has the privileges that they need.


Deleting Users


If you want to delete the user without deleting any of their files, type this command as root:

userdel username

If you want to delete the user's home directory along with the user account itself, type this command as root:


userdel -r username

Example:

How to add a user to the existing group?

add a docker user to the docker group

sudo groupadd docker
sudo usermod -a -G docker  centos


List All Users In the System

cat /etc/passwd

to list all the users in the machine.


linux List users command

To list only usernames type the following awk command:

$ awk -F':' '{ print $1}' /etc/passwd

root
daemon
bin
sys
sync
games
man





References : https://www.cyberciti.biz/faq/linux-list-users-command/



No comments:

Post a Comment