User permission linux 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 1

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

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

Источник

Читайте также:  Настроить интернет сервер linux

Linux File Permission Tutorial: How to Check and Change Permissions

Linux, like other Unix-like operating systems, allows multiple users to work on the same server simultaneously without disrupting each other.

Individuals sharing access to files pose a risk exposing classified information or even data loss if other users access their files or directories. To address this, Unix added the file permission feature to specify how much power each user has over a given file or directory.

In this tutorial, you will learn how to view and change file permissions in Linux.

Tutorial on how to change file permission on Linux.

How to View Check Permissions in Linux

To start with file permissions, you have to find the current Linux permission settings. There are two options to choose from, depending on your personal preference: checking through the graphical interface or using the command.

Check Permissions using GUI

Finding the file (directory) permission via the graphical user interface is simple.

1. Locate the file you want to examine, right-click on the icon, and select Properties.

2. This opens a new window initially showing Basic information about the file.
Navigate to the second tab in the window, labeled Permissions.

open text properties ubuntu

3. There, you’ll see that the permission for each file differs according to three categories:

  • Owner (the user who created the file/directory)
  • Group (to which the owner belongs to)
  • Others (all other users)

For each file, the owner can grant or restrict access to users according to the categories they fall in.

check file permission ubuntu

In our example, the owner of the file test.txt has access to “Read and write”, while other members of its group, as well as all other users, have “Read-only” access. Therefore, they can only open the file, but cannot make any modifications.

Читайте также:  Запуск линукс через grub

To alter the file configuration, the user can open the drop-down menu for each category and select the desired permission.

Additionally, you can make the file executable, allowing it to run as a program, by checking the Execute box.

Check Permissions in Command-Line with Ls Command

If you prefer using the command line, you can easily find a file’s permission settings with the ls command, used to list information about files/directories. You can also add the –l option to the command to see the information in the long list format.
To check the permission configuration of a file, use the command:

For instance, the command for the previously mentioned file would be:

using ls command to check permission with owner and group

As seen in the image above, the output provides the following information:

  • file permission
  • the owner (creator) of the file
  • the group to which that owner belongs to
  • the date of creation.

It shows the permission settings, grouped in a string of characters (-, r, w, x) classified into four sections:

  1. File type. There are three possibilities for the type. It can either be a regular file (), a directory (d) or a link (i).
  2. File permission of the user (owner)
  3. File permission of the owner’s group
  4. File permission of other users

file permission syntax explained

The characters r, w, and x stand for read, write, and execute.
The categories can have all three privileges, just specific ones, or none at all (represented by , for denied).

Users that have reading permission can see the content of a file (or files in a directory). However, they cannot modify it (nor add/remove files in a directory). On the other hand, those who have writing privileges can edit (add and remove) files. Finally, being able to execute means the user can run the file. This option is mainly used for running scripts.

Читайте также:  Linux открыть последовательный порт

In the previous example, the output showed that test.txt is a regular file with read and write permission assigned to the owner, but gives read-only access to the group and others.

permission settings for file output

Using Chmod Command to Change File Permissions

As all Linux users, you will at some point need to modify the permission settings of a file/directory. The command that executes such tasks is the chmod command.

chmod [permission] [file_name]

There are two ways to define permission:

  1. using symbols (alphanumerical characters)
  2. using the octal notation method

Define File Permission with Symbolic Mode

To specify permission settings using alphanumerical characters, you’ll need to define accessibility for the user/owner (u), group (g), and others (o).

Type the initial letter for each class, followed by the equal sign (=) and the first letter of the read (r), write (w) and/or execute (x) privileges.

To set a file, so it is public for reading, writing, and executing, the command is:

chmod u=rwx,g=rwx,o=rwx [file_name]

To set permission as in the previously mentioned test.txt to be:
• read and write for the user
• read for the members of the group
• read for other users

Use the following command:

Note: There is no space between the categories; we only use commas to separate them.

Another way to specify permission is by using the octal/numeric format. This option is faster, as it requires less typing, although it is not as straightforward as the previous method.

Instead of letters, the octal format represents privileges with numbers:

The privileges are summed up and depicted by one number. Therefore, the possibilities are:

  • 7 – for read, write, and execute permission
  • 6 – for read and write privileges
  • 5 – for read and execute privileges
  • 4 – for read privileges

As you have to define permission for each category (user, group, owner), the command will include three (3) numbers (each representing the summation of privileges).

For instance, let’s look at the test.txt file that we symbolically configured with the chmod u=rw,g=r,o=r test.txt command.

The same permission settings can be defined using the octal format with the command:

chmod octal numeric format explained

Define File Permission in Octal/Numeric Mode

Note: If you need a more in-depth guide on how to use Chmod In Linux to change file permissions recursively, read our Chmod Recursive guide.

Changing User File and Group Ownership

Aside from changing file permissions, you may come across a situation that requires changing the user file ownership or even group ownership.

Performing either of these tasks requires you first need to switch to superuser privileges. Use one of the options outlined in the previous passage.

To change the file ownership use the chown command:

chown [user_name] [file_name]

Instead of [user_name] type in the name of the user who will be the new owner of the file.

To change the group ownership type in the following command:

chgrp [group_name] [file_name]

Instead of [group_name] type in the name of the group that will be the new owner of the file.

Learning how to check and change permissions of Linux files and directories are basic commands all users should master. To change file’s group permissions, you might find helpful our article on how to use the chgrp command.

No matter whether you prefer using the GUI or command-line, this article should help you better understand how to use file permissions.

Источник

Оцените статью
Adblock
detector