Ubuntu: permission, access, ownership of the files/directories

By

Check what are the users/groups exist in current system:

cat /etc/passwd

A demo output might looks like this:

The output is separated by colons ‘:’

From left to right, they are: username, password (shown as ‘x’), user ID(UID), group ID (GID), user information, home directory, login shell

In this context, we only concern the first field, which the username.

Similarly, to check the existing groups, we can type:

cat /etc/group

Check what groups you are in:

If you want to know as a user, how many groups you have been added:

groups username

Then, you might want to add a new group

If so, you can do this by this command:

sudo groupadd groupname

replacing the ‘groupname’ to the group name you want to call for the new group. Then you can verify the changes by:

getent group groupname

or you can still use the above command to list all the groups exists, you should able to see the new group if it is successful.

Modify the ownership of files/directories

First check the current access of the target files/directories:

ls -l file/directory

then you might want to change the ownership of the file/directory to a new user or group or both:

sudo chown -R newuser:newgroup file/directory

‘-R’: means recursively, it will change the ownership of the target and all its contents recursively.

‘newuser:newgroup’: you can either just give username newuser, or just group :newgroup, or both as shown above, depending what you want to achieve.

Adding user to new group

You can add a user to a new group by:

sudo usermod -aG group1,group2 username

‘-aG’: means adding new group without changing current group ownership.

‘group1,group2’: you can just give one group name if you do not want to add more than one groups.

‘username’: this is the user you want to modify.