- How do I know a specified user’s permissions on Linux with root access?
- 3 Answers 3
- How to Check the Permission of Current Logged In User in Linux
- How to Check the Permission of Current Logged In User in Linux
- 1. Using id command
- 2. Using sudo command
- 3. Using umask command
- 4. Using groups command
- 5. Using chmod command
- 6. Using chown command
- 1 thought on “How to Check the Permission of Current Logged In User in Linux”
- Check if different user has read/write permissions to a file on linux
- Test Read Permission
- Test Write Permission
- Test As Specific User
- How to check permissions of a specific directory?
- 10 Answers 10
- For Dir
- For file
How do I know a specified user’s permissions on Linux with root access?
I have root access to my local server. Some days ago, my colleague created a user on that server, giving me the username and password, but the user has minimized permissions. For instance, the user can’t even create a file under its own home directory. Is there any concept about «the permissions of a user»? If there is, how do I check/modify it?
3 Answers 3
It may be the case that your colleague, while creating the account, created the home directory «by hand» which resulted in it being owned by root . Try running the following as root :
chown -R username ~username chgrp -R $(id -gn username) ~username
Where username is the name of the problematic account.
If this turns out to be your problem, to avoid this happening in the future, you want to add the -m switch to the useradd command line used to create the user account. This ensures that the user’s selected home directory is created if it doesn’t exist. This creates the home directory with the «right» ownership and permissions so you don’t face this kind of issue.
The chgrp command added above will change group ownership of the entire home directory of username to username ‘s primary group. Depending on your environment, this may not be exactly what you want and you’ll possibly need to change group ownership of specific sub-directories inside the home-directory «manually», thereby setting different group ownership for different sub-directories. This is usually not the case for personal computers, but since you mentioned «a colleague», I’m assuming we’re talking about a networked office environment, in which case group ownership is important for shared directories.
How to Check the Permission of Current Logged In User in Linux
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
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 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”
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
Check if different user has read/write permissions to a file on linux
will return 0 if otheruser can read the file, or 1 if otheruser cannot read the file. You can run test -r /path/to/file; echo «$?» to view the return code of the test command.
Use test -w to test for write permission and test -x to test for execute permission.
Test Read Permission
Attempt to read the beginning of the file and discard the normal output. You can then look for an empty string (success) or a «Permission denied» message (you can also check for other error messages such as «No such file or directory»). For example:
head -1 /path/to/file 2>&1 > /dev/null | grep 'Permission denied'
Test Write Permission
Use the touch command with the -c (—no-create) option. Combine stdout and stderr and again search for an empty string (success) or an error:
touch -c /path/to/file 2>&1 | grep 'Permission denied'
If you’re explicitly testing write access of a directory, be sure to test the directory and not a file contained within, since with the -c option, there’s no error condition if the file doesn’t exist even in a directory you don’t have write access to:
-c, if the file does not exist, do not create it and do not report this condition
Test As Specific User
The final piece of the puzzle is how to check this as a different user. As root execute the test command as the desired user with «sudo -u [username] [command]» so using your suggested user:
sudo -u apache touch -c /path/to/file 2>&1
How to check permissions of a specific directory?
I know that using ls -l «directory/directory/filename» tells me the permissions of a file. How do I do the same on a directory? I could obviously use ls -l on the directory higher in the hierarchy and then just scroll till I find it but it’s such a pain. If I use ls -l on the actual directory, it gives the permissions/information of the files inside of it, and not of the actual directory. I tried this in the terminal of both Mac OS X 10.5 and Linux (Ubuntu Gutsy Gibbon), and it’s the same result. Is there some sort of flag I should be using?
10 Answers 10
-d, --directory list directory entries instead of contents, and do not dereference symbolic links
You might be interested in manpages. That’s where all people in here get their nice answers from.
I think the man page is poorly worded. I scoured it five times before I started googling. I don’t want directory ‘entries’ (thing ‘entered’ into directories? Like their files and sub-directories?) nor their ‘contents’ (they sound like the same concept to me), I want the directories themselves.
it’s completely standard terminology, the directories themselves are the directory entries, i.e. entries in the filesystem
It may be standard terminology, but to someone who would likely be asking such a question, it is probably confusing jargon.
You can also use the stat command if you want detailed information on a file/directory. (I precise this as you say you are learning ^^)
— indicates the beginning of the command options.
l asks for a long list which includes the permissions.
d indicates that the list should concern the named directory itself; not its contents. If no directory name is given, the list output will pertain to the current directory.
In GNU/Linux, try to use ls , namei , getfacl , stat .
For Dir
[flying@lempstacker ~]$ ls -ldh /tmp drwxrwxrwt. 23 root root 4.0K Nov 8 15:41 /tmp [flying@lempstacker ~]$ namei -l /tmp f: /tmp dr-xr-xr-x root root / drwxrwxrwt root root tmp [flying@lempstacker ~]$ getfacl /tmp getfacl: Removing leading '/' from absolute path names # file: tmp # owner: root # group: root # flags: --t user::rwx group::rwx other::rwx [flying@lempstacker ~]$
[flying@lempstacker ~]$ stat -c "%a" /tmp 1777 [flying@lempstacker ~]$ stat -c "%n %a" /tmp /tmp 1777 [flying@lempstacker ~]$ stat -c "%A" /tmp drwxrwxrwt [flying@lempstacker ~]$ stat -c "%n %A" /tmp /tmp drwxrwxrwt [flying@lempstacker ~]$
For file
[flying@lempstacker ~]$ ls -lh /tmp/anaconda.log -rw-r--r-- 1 root root 0 Nov 8 08:31 /tmp/anaconda.log [flying@lempstacker ~]$ namei -l /tmp/anaconda.log f: /tmp/anaconda.log dr-xr-xr-x root root / drwxrwxrwt root root tmp -rw-r--r-- root root anaconda.log [flying@lempstacker ~]$ getfacl /tmp/anaconda.log getfacl: Removing leading '/' from absolute path names # file: tmp/anaconda.log # owner: root # group: root user::rw- group::r-- other::r-- [flying@lempstacker ~]$
[flying@lempstacker ~]$ stat -c "%a" /tmp/anaconda.log 644 [flying@lempstacker ~]$ stat -c "%n %a" /tmp/anaconda.log /tmp/anaconda.log 644 [flying@lempstacker ~]$ stat -c "%A" /tmp/anaconda.log -rw-r--r-- [flying@lempstacker ~]$ stat -c "%n %A" /tmp/anaconda.log /tmp/anaconda.log -rw-r--r-- [flying@lempstacker ~]$