Some Linux file permission management methods you may not know

Some Linux file permission management methods you may not know

Why do we need permission management?

1. Computer resources are limited, and we need to allocate computer resources reasonably.

2. Linux is a multi-user system. For every user, the protection of personal privacy is very important.

rwx permissions for the directory

Current user: vagrant:vagrant

Create a testdir directory and enter the testdir directory. Create a file test.

$ mkdir testdir
$ cd testdir
$ touch test

Change the testdir permission to 000 and try to execute ls testdir

$ chmod 000 testdir
$ ls testdir/
ls: cannot open directory testdir/: Permission denied

Change the testdir permission to 400 and try to execute ls testdir

$ chmod 400 testdir
ls -l testdir/
ls: cannot access testdir/test: Permission denied
total 0
-????????? ? ? ? ? ? test

Result: The file list in the directory can be read, but the specific file information (permissions, size, user group, time, etc.) cannot be seen, although the current user is the owner of /testdir/test and has rwx permissions.

The r permission on a directory allows you to read the list of files in the directory.

Go ahead and try to change into the testdir directory.

$ cd testdir/
-bash: cd: testdir/: Permission denied

It seems that the r permission does not allow us to enter the directory.

Let's try adding an x ​​permission.

~$ chmod 500 testdir/
~$ cd testdir/
~/testdir$ ls -l
total 0
-rw-rw-r-- 1 vagrant vagrant 0 Nov 19 08:16 test

Entered successfully.

Having x permissions on a directory allows us to enter the directory. In this working directory, we can view the file list and file attribute information.

Try to delete the test file or create a new file test1.

~/testdir$ rm test
rm: cannot remove 'test': Permission denied
~/testdir$ touch test1
touch: cannot touch 'test1': Permission denied

Having rx permissions on a directory does not allow us to change the contents of the directory. The list of files in a directory can be considered the contents of the directory.

A user with the w permission for a directory can add or delete the contents of the directory.

~/testdir$ chmod 700 .
~/testdir$ rm test
~/testdir$ touch test1
~/testdir$ ls -l
total 0
-rw-rw-r-- 1 vagrant vagrant 0 Nov 19 08:30 test1

umask

In the above example, the permissions of the new file we created are 664 (-rw-rw-r--). Why is the default permission 664? What if I want to change the default permissions of the new file?

Console input umask:

$ umask
0002

umask is the two's complement of the permissions. The default permissions for files are 666 - umask.

If we do not want other users to have r permissions for the files we create, we can change the complement code to 0006.

~/testdir$ umask 0006
~/testdir$ touch test2
~/testdir$ ls -l | grep test2
-rw-rw---- 1 vagrant vagrant 0 Nov 19 08:38 test2

Why aren't the default file permissions 777 - umask? Because newly created files do not have executable permissions by default, if we only consider rw permissions, this operation will naturally be 666.

By default, directories have x permissions. When the umask is 0002, the default permissions of the created directories should be 777 - 0002 = 775:

~/testdir$ mkdir dir1
~/testdir$ ls -l | grep dir1
drwxrwxr-x 2 vagrant vagrant 4096 Nov 19 08:39 dir1

Special permissions

SUID

Generally speaking, file permissions are rwx. Let's check the permissions of passwd (change password command):

~/testdir$ ls -l /usr/bin/passwd
-rwsr-xr-x 1 root root 47032 May 16 2017 /usr/bin/passwd

If you look carefully, you will find that the x bit of its user permission is actually s. This permission is called SUID and is only valid for binary programs.

When a user has the execute permission for a file, executing the file will temporarily obtain the support of the file owner's permissions.

For example: All users' passwords are stored in the file /etc/shadow, and the default permission of the file is -r-------- root root. Only the root user has mandatory write permission. Then why can ordinary users still modify their passwords? Because the passwd command has SUID permissions, when a user executes the command, he or she will obtain the permission support of the file owner root and thus modify his or her own password.

SGID

When the x position of group becomes s, it means that the file has SGID permission.

SGID permissions are valid for binary programs. Similar to SUID, when a user has the x permission for a file, when executing the file, the user group to which the file belongs will obtain the permission support.

In addition to binary programs, SGIDs can also be set on directories.

If the user has SGID permissions for the directory:

The user's effective user group in this directory will become the user group of this directory.

If the user has the w permission for the directory, the user group of the files created by the user in the directory is the same as the user group of the directory.

This permission is important for project development.

SBIT

This permission is currently only valid for directories:

When a user has w,x permissions for this directory, after the user creates a folder or directory under this directory, only the user and root have permission to delete the file.

If the x permission bit of Others is t, it means that the folder has SBIT permission.

For example, the /tmp directory:

$ ls -l / | grep tmp
drwxrwxrwt 4 root root 4096 Nov 19 09:09 tmp
$ sudo -s
# touch test
root@vagrant-ubuntu-trusty-64:/tmp# exit
exit
vagrant@vagrant-ubuntu-trusty-64:/tmp$ rm test
rm: remove write-protected regular empty file 'test'? y
rm: cannot remove 'test': Operation not permitted

How to set the above three permissions

If you add another number before the three numbers in the normal permission settings, the number in front will represent these permissions:

  • 4 is SUID
  • 2 is SGID
  • 1 for SBIT

for example:

# chmod 777 /tmp
# ls -l / | grep tmp
drwxrwxrwx 4 root root 4096 Nov 19 09:17 tmp
# chmod 1777 /tmp
# ls -l / | grep tmp
drwxrwxrwt 4 root root 4096 Nov 19 09:17 tmp
End.

Summarize

The above is the full content of this article. I hope that the content of this article will have certain reference learning value for your study or work. If you have any questions, you can leave a message to communicate. Thank you for your support for 123WORDPRESS.COM.

You may also be interested in:
  • A brief analysis of common Linux file management commands
  • Detailed explanation of Linux file management
  • Sharing of Linux operating system file manager
  • Linux file and user management practice
  • Linux file/directory permissions and ownership management
  • Summary of Linux file directory management commands
  • Detailed steps for Linux account file control management
  • Detailed explanation of Linux file permissions and directory management
  • Linux du command to view folder sizes and sort in descending order
  • How to retrieve file contents using grep command in Linux
  • Detailed application of command get to download files and put to upload files in Linux ftp command line
  • Linux commands to delete folders and files (forced deletion including non-empty files)
  • Linux file management command example analysis [permissions, create, delete, copy, move, search, etc.]

<<:  Installation and use tutorial of Elasticsearch tool cerebro

>>:  MySQL 5.7.18 free installation version configuration tutorial

Recommend

A brief discussion on the magical slash in nginx reverse proxy

When configuring nginx reverse proxy, the slashes...

Quickly master how to get started with Vuex state management in Vue3.0

Vuex is a state management pattern developed spec...

Several ways to store images in MySQL database

Usually the pictures uploaded by users need to be...

Example code of javascript select all/unselect all operation in html

Copy code The code is as follows: <html> &l...

37 Tips for a Good User Interface Design (with Pictures)

1. Try to use single column instead of multi-colum...

JavaScript implements div mouse drag effect

This article shares the specific code for JavaScr...

Implementation of Nginx load balancing/SSL configuration

What is load balancing? When a domain name points...

MySQL database monitoring software lepus usage problems and solutions

When using lepus3.7 to monitor the MySQL database...

Tutorial on building file sharing service Samba under CentOS6.5

Samba Services: This content is for reference of ...

HTML code text box limit input text box becomes gray limit text box input

Method 1: Set the readonly attribute to true. INPU...

Some thoughts and experience sharing on web page (website) design and production

First, before posting! Thanks again to I Want to S...

Django online deployment method of Apache

environment: 1. Windows Server 2016 Datacenter 64...

Docker file storage path, modify port mapping operation mode

How to get the container startup command The cont...