- How to check permissions of a specific directory?
- 10 Answers 10
- For Dir
- For file
- 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
- How do you view file permissions?
- 4 Answers 4
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 ~]$
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
How do you view file permissions?
I want to know how to see the permissions a particular file has. Which command should I type in the terminal? However, I don’t want to change it.
4 Answers 4
If you want to see the the permission of a file you can use ls -l /path/to/file command.
ls -l acroread -rwxr-xr-x 1 10490 floppy 17242 May 8 2013 acroread
What does this mean ?
First — represents a regular file. It gives you a hint of the type of object it is. It can have following values.
- d (directory)
- c (character device)
- l (symlink)
- p (named pipe)
- s (socket)
- b (block device)
- D (door)
- — (regular file)
r represents read permission.
w represents write permission and
x represents executable permission.
First combination of rwx represents permission for the owner .
Second combination of rwx represents permission for the group .
Third combination of rwx represents permission for the other of the file.
Octal notation
Permission of file can also be represented in octal notation.
In octal notation
Read or r is represented by 4,
Write or w is represented by 2
Execute x is represented by 1.
Sum of these three is use to represent the permission.
stat command can be used to view file permission in octal notation
stat -c "%a %n" acroread 755 acroread
For owner it is 4+2+1=7 (111 in binary)
For group it is 4+0+1=5 (101 in binary) and
For other it is 4+0+1=5 (101 in binary).