HomeEnvironment SetupLinux (Users and Groups)

Linux (Users and Groups)

Contents

User and Groups

Adding user

Below command would add the user to linux servers. Here I am adding the user called “devopsdiggers”.

Command : sudo useradd <name of the user>

[ec2-user@ip-172-31-30-160 ~]$ sudo useradd devopsdiggers

Listing all the added users can be done in different ways. One is going to /etc/passwd where passwd file contains all the user details.Another way is using getent command. Bydefault there would be many users added to the list and we can see the added user (devopsdiggers) at very end.

Command : getent passwd

[ec2-user@ip-172-31-30-160 etc]$ getent passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:65534:65534:Kernel Overflow User:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
systemd-coredump:x:999:997:systemd Core Dumper:/:/sbin/nologin
systemd-resolve:x:193:193:systemd Resolver:/:/sbin/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
polkitd:x:998:996:User for polkitd:/:/sbin/nologin
unbound:x:997:995:Unbound DNS resolver:/etc/unbound:/sbin/nologin
sssd:x:996:993:User for sssd:/:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
chrony:x:995:992::/var/lib/chrony:/sbin/nologin
rngd:x:994:991:Random Number Generator Daemon:/var/lib/rngd:/sbin/nologin
ec2-user:x:1000:1000:Cloud User:/home/ec2-user:/bin/bash
devopsdiggers:x:1001:1001::/home/devopsdiggers:/bin/bash

Creating Password for User

To be able to login with the created user name, password must need to be created.

Command: sudo passwd <name of the user>

When it asks for the password , give the strong password and also retype it when it asks for retype.

[ec2-user@ip-172-31-30-160 ~]$ sudo passwd devopsdiggers
Changing password for user devopsdiggers.
New password: 
Retype new password: 
passwd: all authentication tokens updated successfully.

Deleting User

Below command will delete the user.

Command : sudo userdel <name of the user>

[ec2-user@ip-172-31-30-160 ~]$ sudo userdel devopsdiggers

Above command only deletes the user but it does not delete the user home directory. If you want to delete the entire user account please add “-r” flag to the userdel command.

Command : sudo userdel -r <name of the user>

Creating Groups

To create groups in linux, groupadd command would help.Here the adding group name is “devops“.

Command : sudo groupadd <name of the group>

[ec2-user@ip-172-31-30-160 ~]$ sudo groupadd devops

To list the all the groups use getent command or open the group file in etc directory.

Command : getent group

[ec2-user@ip-172-31-30-160 etc]$ getent group
root:x:0:
bin:x:1:
daemon:x:2:
sys:x:3:
adm:x:4:ec2-user
tty:x:5:
disk:x:6:
lp:x:7:
mem:x:8:
kmem:x:9:
wheel:x:10:
cdrom:x:11:
mail:x:12:
man:x:15:
dialout:x:18:
floppy:x:19:
games:x:20:
tape:x:33:
video:x:39:
ftp:x:50:
lock:x:54:
audio:x:63:
users:x:100:
nobody:x:65534:
dbus:x:81:
utmp:x:22:
utempter:x:35:
input:x:999:
kvm:x:36:
render:x:998:
systemd-journal:x:190:ec2-user
systemd-coredump:x:997:
systemd-resolve:x:193:
tss:x:59:
polkitd:x:996:
unbound:x:995:
ssh_keys:x:994:
sssd:x:993:
sshd:x:74:
chrony:x:992:
rngd:x:991:
ec2-user:x:1000:
devopsdiggers:x:1001:
devops:x:1002:

Deleting Group

Groups can be deleted by using “groupdel” command.

Command: sudo groupdel <name of the group>

[ec2-user@ip-172-31-30-160 etc]$ sudo groupdel devops

Adding User to Group

Adding an existing users to an existing groups can be done in different ways.

Command: sudo usermod -a -G <name of the group> <name of the user>

[ec2-user@ip-172-31-30-160 ~]$ sudo usermod -a -G devops devopsdiggers

To validate the groups which user belongs to.

Command : id <name of the user>

[ec2-user@ip-172-31-30-160 ~]$ id devopsdiggers
uid=1001(devopsdiggers) gid=1001(devopsdiggers) groups=1001(devopsdiggers),1002(devops)

Removing User From Group

Removing the user from the group can be done by using “gpasswd” command.

Command : sudo gpasswd -d <name of the user> <name of the group>

[ec2-user@ip-172-31-30-160 ~]$ sudo gpasswd -d devopsdiggers devops
Removing user devopsdiggers from group devops
RELATED ARTICLES

Latest Articles