Cyberithub

How to Check the Permission of Current Logged In User in Linux

Advertisements

In this article, we will see how to check the permission of current logged In user in Linux. If you are a programmer or developer working on a production machine which runs on Linux Operating system then chances are that you will be having some non-root account to login and do your work. Once logged in probably the first question you might ask is what permission do you have in that account ? Whether you are allowed to create or delete some files or not ? Do you have sudo access to perform certain administrative tasks ?  All permission sorts of question immediately goes through your mind.

So to answers these kind of questions I am going to give you some very basic but important commands that anyone can run and quickly get an idea about the kind of permission they have. Just to let you know here I am using my Ubuntu 20.04 LTS version to show the demonstration but it does not matter which flavor you are using as all the below command will work in almost all kind of Linux system just fine. Also I am using a non-root user account called cyberithub to login to my System. There is one more user account called john I am using here to explain the usage of chmod and chown command.

How to Check the Permission of Current Logged In User in Linux

How to Check the Permission of Current Logged In User in Linux

Also Read: How to Define Audit Control Rules in Rocky Linux 8

1. Using id command

You can check the current logged In user id and group id using below id command. It will shows all the other group id which you are part of. For example, you can see sudo group here, it means you have sudo access to run privileged command. Similarly you can check what other groups you are in. More about id command.

cyberithub@localhost:~$ id
uid=1000(cyberithub) gid=1000(cyberithub) groups=1000(cyberithub),4(adm),24(cdrom),27(sudo),30(dip),46(plugdev),120(lpadmin),131(lxd),132(sambashare)

 

2. Using sudo command

sudo is probably the most important privilege that you need to check. You can use sudo -l command to check all the commands currently logged In user can run. If you see output like below i.e (ALL:ALL) ALL then it means you have unlimited access and you are almost capable of running any command. To check more about sudo command, visit 10 Popular Examples of sudo command in Linux(Redhat/CentOS 7/8).

cyberithub@localhost:~$ sudo -l
[sudo] password for cyberithub:
Matching Defaults entries for cyberithub on cyberithub:
env_reset, mail_badpass, secure_path=/usr/local/sbin\:/usr/local/bin\:/usr/sbin\:/usr/bin\:/sbin\:/bin\:/snap/bin

User cyberithub may run the following commands on cyberithub:
(ALL : ALL) ALL

 

3. Using umask command

umask is a very important to determine the kind of default permission you have to create files and directories. For example, here when I run umask command, I see 0002 on the output. So this is the default umask value set for my user account. If I want I can change this value using umask command. To known more about how to change umask value, you can check How to change default umask values permanently in Linux. 

cyberithub@localhost:~$ umask
0002

As you might be aware to calculate the default directory creation permission you need to subtract this number from 0777. Once you do that you get 0775 so it means when you create a directory, by default it will have 0772 permission. This can be further understood by creating a simple directory and then checking the permission on that by using below given command. To understand more about permissions, check  Concepts of Regular and Special Permissions(SUID and SGID) in Linux

cyberithub@localhost:~$ mkdir hello
cyberithub@localhost:~$ ls -ltrd hello
drwxrwxr-x 2 cyberithub cyberithub 4096 Jan 22 19:13 hello

Similarly, to calculate the default file creation permission you need to subtract the umask value from 0666. So it will be 0666-0002 = 0664. It means whenever you will create a file, by default it will have 0664 permission. To prove this, we can create sample file called hello.txt and check its permission by using ls -ltr hello.txt command. You can see that by default it has 0664 permission.

cyberithub@localhost:~$ touch hello.txt
cyberithub@localhost:~$ ls -lrt hello.txt
-rw-rw-r-- 1 cyberithub cyberithub 0 Jan 22 19:12 hello.txt

 

4. Using groups command

You can also use groups command to check all the groups you are in. The output you will get here is more or less same like what you saw in id command. But still an important command to remember and check.

cyberithub@localhost:~$ groups
cyberithub adm cdrom sudo dip plugdev lpadmin lxd sambashare

 

5. Using chmod command

Next command that you can use is chmod command to modify the permissions of files and directories. If you want to modify the permission of a file or a directory then you need to use chmod <option> <permission> <file/directory> syntax. For example, here we modifying the permission of hello.txt file to 777 using chmod 777 hello.txt command and then confirming it by using ls -ltr hello.txt command as shown below.

cyberithub@localhost:~$ chmod 777 hello.txt
cyberithub@localhost:~$ ls -ltr hello.txt
-rwxrwxrwx 1 cyberithub cyberithub 0 Jan 22 19:12 hello.txt

But if you try to change the permission of a file which you don't have access to then you will end up in getting below "Operation not permitted" error. To know more about chmod command, check 11 Popular Unix/Linux chmod command examples to change File Permissions.

john@localhost:/home/ubuntu$ chmod 775 hello.txt
chmod: changing permissions of 'hello.txt': Operation not permitted

 

6. Using chown command

One more command that you can use is chown command to modify the ownership of a file or a directory to another user or group. If you try to modify the ownership of some file which you haven't created or you don't have access to then you will end up in getting "Operation not permitted" error. Here I am trying to change the ownership of file hello.txt to user ubuntu and group ubuntu using chown ubuntu:ubuntu hello.txt command. This file was created by user john. To know more about chown command, check 17 Useful Linux chown command examples to change owner and group

john@localhost:/home/ubuntu$ chown ubuntu:ubuntu hello.txt
chown: changing ownership of 'hello.txt': Operation not permitted

1 thought on “How to Check the Permission of Current Logged In User in Linux”

  1. shah@ubuntu:~$ sudo -i
    [sudo] password for shah:
    shah is not in the sudoers file. This incident will be reported.
    shah@ubuntu:~$

    please guide how to go root user
    before was working

    Reply

Leave a Comment