DEV Community

Zaki Arrozi Arsyad
Zaki Arrozi Arsyad

Posted on • Updated on

linux : user management and permission

This is one of the best part of linux, we can grant access for all services, directories, files, and even commands that can be executed for a specific user or group.


User management

  • Check who am i
$ whoami
Enter fullscreen mode Exit fullscreen mode
  • Check the user id
$ id USERNAME
Enter fullscreen mode Exit fullscreen mode
  • Check the group of user
$ groups USERNAME
Enter fullscreen mode Exit fullscreen mode
  • Check who is currently logged in
$ w
Enter fullscreen mode Exit fullscreen mode
  • Check the last logged in
$ last
Enter fullscreen mode Exit fullscreen mode
  • Change user
$ su
Enter fullscreen mode Exit fullscreen mode
  • Add a new user
$ useradd USERNAME
Enter fullscreen mode Exit fullscreen mode
  • Set password for a user
$ passwd PASSWORD
Enter fullscreen mode Exit fullscreen mode
  • Change user name
$ usermod -l USERNAME NEW_USERNAME
Enter fullscreen mode Exit fullscreen mode
  • Add a user to a group
$ usermod -a -G GROUPNAME USERNAME
Enter fullscreen mode Exit fullscreen mode
  • Change user primary group
$ usermod -g GROUPNAME USERNAME
Enter fullscreen mode Exit fullscreen mode
  • Remove user from a group
$ gpasswd -d USERNAME GROUPNAME
Enter fullscreen mode Exit fullscreen mode
  • Delete a user
$ userdel -r USERNAME
Enter fullscreen mode Exit fullscreen mode
  • Lock a user
$ passwd -l USERNAME
Enter fullscreen mode Exit fullscreen mode
  • Unlock a user
$ passwd -u USERNAME
Enter fullscreen mode Exit fullscreen mode

Group management

  • Add a new group
$ groupadd GROUPNAME
Enter fullscreen mode Exit fullscreen mode
  • Change group name
$ groupmod -n GROUPNAME NEW_GROUPNAME
Enter fullscreen mode Exit fullscreen mode
  • Delete a user
$ groupdel GROUPNAME
Enter fullscreen mode Exit fullscreen mode

Permission

After create a user or group, we can grant access for specific user or group.

Permission format :

rwxrwxrwx
Enter fullscreen mode Exit fullscreen mode

First 3 characters are the user permission, next 3 characters are the group permission, and the last 3 are other permission.

  • r is for read with value 4
  • w is for write with value 2
  • x is for execute with value 1
  • - is for no permission with value 0

We can also use the value of the characters to define the permission. Simply sum the values of the permissions for each role, and use only 3 numbers to define the permission.

# rwxrwxrwx
777

# rwxr-x-rx
755

# rwx------
700
Enter fullscreen mode Exit fullscreen mode

We can see the files or directories permission by run ls -l.

$ ls -la
drwxrwxrwx 1 root root  96 Jun 11 07:58 directory
-rwxr-xr-x 1 zaki zaki  96 Jun 11 07:58 file.txt
Enter fullscreen mode Exit fullscreen mode

The first column is the permission, the third column is the user, the fourth column is the group name.

Discussion (0)