In the last post, we looked at basicÂ file permissions. The ideas covered in that post are probably enough to get you through a large portion of the real world scenarioâ€™s that you will encounter. There are some special cases, however.Â One of them being access control lists (ACL) which I will discuss in this post.
Access Control List â€“ ACL
As we saw inÂ part 1Â every file on a Linux system has an owner and a group associated with it, each of which has separate permissions. But what if a user or group needed read permissions to a file that they do not own? You could modify permissions to allow all users access to a file withÂ Â
chmod o+rÂ but this is not ideal on a system with multiple users where data needs to be kept confidential.Â Instead of going this route, which is insecure, we can add a list of users, or groups, or both that have permissions to a file that are separate from the standard owner/group in basic POSIX permissions.
So letâ€™s look at a simple example. Batman and the finance group need to be able to read and write a payroll document, SupermanÂ and Robin need to be able to write, but not read, the payroll document (we donâ€™t want them to know that Batman makes more money than they do).
First, letâ€™sÂ set up our environment:
sudo useradd batman sudo useradd superman sudo useradd robin sudo groupadd finance sudo usermod -aG finance batman
Set a password for each user. Iâ€™m just going to use the username for each as the password to make it easy for this example. It should go without saying that this wouldnâ€™t be an acceptableÂ password policy for real world use.
sudo passwd batman New password: BAD PASSWORD: it is based on a dictionary word BAD PASSWORD: is too simple sudo passwd superman New password: BAD PASSWORD: it is based on a dictionary word BAD PASSWORD: is too simple sudo passwd robin New password: BAD PASSWORD: it is based on a dictionary word BAD PASSWORD: is too simple
Switch to the user batman and create a file called jlpayroll.txt in the /tmp directory.
su - batman cd /tmp newgrp finance touch jlpayroll.txt chmod 660 jlpayroll.txt ls -l jlpayroll.txt -rw-rw---- 1 batman finance 0 Feb 5 09:32 jlpayroll.txt
At this point, batman can read and write to the jlpayroll.txt file. Now add batmanâ€™s pay to this file.
echo "Batman Week One: 2,000,000" >> jlpayroll.txt cat jlpayroll.txt Batman Week One: 2,000,000
Now we need to add ACLâ€™s to allow superman and robin to add their payroll information, while also ensuring that they canâ€™t read the file. Batman can do that with theÂ Â
setfaclÂ command that he keeps in his utility belt.
setfacl -m u:superman:w -m u:robin:w jlpayroll.txt
Take a look at this command for a minute. First, notice that you can add multiple users at the same time each separated by the -m option. In the setfacl command -m means modify.
Next, notice the format that is used to identify users. The Â
setfaclÂ command expects 3 valuesÂ separated by colons:
<type>:<name>:<permission> â€“ the type can be â€œuâ€ for user or â€œgâ€ for group, the name is the group or username, and the permissions are the standard read, write, and execute permissions that we looked at previously.
So how does this affect the way permissions are displayed?
ls -l jlpayroll.txt -rw-rw----+ 1 batman finance 27 Feb 5 09:43 jlpayroll.txt
The change is subtle but itâ€™s there. Notice the â€œ+â€ after â€œ-rw-rwâ€”-â€œ. That + sign indicates that permissions have been altered by access control lists.
How to display the ACLâ€™s on a file.
Access control listâ€™s can be displayed with theÂ Â
getfacl jlpayroll.txt # file: jlpayroll.txt # owner: batman # group: finance user::rw- user:superman:-w- user:robin:-w- group::rw- mask::rw- other::---
The output here is a detailed look at exactly who has what level of access to this file. It lists the file name, owner, and group. Then each user and group have individual access levelâ€™s displayed.
Testing the ACLâ€™s
Switch to the user superman and make sure he can write to the jlpayroll.txt file.
su - superman cd /tmp id uid=1007(superman) gid=100(users) groups=100(users) ls -l jlpayroll.txt -rw-rw----+ 1 batman finance 27 Feb 5 09:43 jlpayroll.txt
We can see that superman is not in the finance group and he is not the owner of this file. Normally he wouldnâ€™t have any access to this file at all. But with the ACL we set up earlier he should be able to write to this file but not read it.
cat jlpayroll.txt cat: jlpayroll.txt: Permission denied
As you can see superman cannot read the jlpayroll.txt file. Can he write to it?
echo "Superman Week One: 400" >> jlpayroll.txt
As long as that command doesnâ€™t return an error then it shouldâ€™ve worked. Letâ€™s do the same thing for robin
su - robin cd /tmp echo "Robin Week One: 4,000" >> jlpayroll.txt
Since payroll is done for the week lets make sure that batman can see each entry. If youâ€™ve been following along you can get back to batman using the â€œexitâ€ command twice.
cat jlpayroll.txt Batman Week One: 2,000,000 Superman Week One: 400 Robin Week One: 4,000
Access control lists allow an administrator to exercise fine-grained control over files and directories that would not be possible using only POSIX permissions. This was a pretty simple example but it should give you an idea about how ACLâ€™s work, how to set them, and how to discover what ACLâ€™s are set on a file.
Â Clean up
When you are finished with this example make sure you clean up your system to get rid of the users and group we made at the beginning of the tutorial. Switch back to your normal user account with sudo privileges.
sudo userdel batman sudo userdel superman sudo userdel robin sudo groupdel finance sudo rm /tmp/jlpayroll.txt
Luke has an RHCSA for Red Hat Enterpirse Linux 7 and currently works as a Linux Systems Adminstrator in Ohio.
This post, re-published here with permission, wasÂ originally published on Luke’s site here.
Leave a Reply