Linux Server development: chmod () function, chmod command, and file screen umask command, program modification Umask, Detailed introduction + case Demo

Source: Internet
Author: User
Tags function definition

1. Dependent header Files

#include <sys/stat.h>

2. Function definition:

Change the file-making permissions by passing in the name given in path

int chmod (const char *path,mode_t mode);

To reset permissions for a file by passing in a file descriptor

int fchmod (int fd,mode_t mode);

Note: If you use the Linux chmod command, you have to have root privileges

3. Definition of mode_t;

The definition of a:mode_t is actually the unsigned int form.

B: But the function chmod (const char *path,mode_t mode) interprets the mode as 8 in the interpretation mode_t.

4. The parameter mode has several combinations:

       s_isuid  (04000)   set-user-id  (set  process  effective  user   id   on
                          Execve (2))                                                                                                 // The (Set User-id on execution) bit of the file

S_isgid (02000) Set-group-id (Set process effective group ID on
Execve (2); Mandatory locking, as described in
Fcntl (2); Take a new file ' s group from parent direc‐
                          Tory, as described in Chown (2) and mkdir (2))                                       //(set Group-id on execution) bits of the file

S_ISVTX (01000) sticky bit (restricted deletion flag, as described in
Unlink (2))//sticky bit of file

       s_irusr  (00400)   read by owner                                                                         //file owner readable permissions                                                                         

       s_iwusr  (00200)   write by owner                                                                       //File owner has writable permissions

       s_ixusr  (00100)   execute/search  by Owner ("Search" applies For direc‐
                          Tories, and means that entries within  the  directory
& nbsp;                         can be accessed)                                                                                  //file owner has executable permissions

S_IRGRP (00040) read by group//user group has Read permissions

S_IWGRP (00020) write by group//user group has executable permissions

S_IXGRP (00010) Execute/search by group//user group with executable permissions

       s_iroth  (00004)   read by others                                                                       //other users have Read permissions                 

S_iwoth (00002) write by others//other users have writable permissions

S_ixoth (00001) Execute/search by others//other users have executable permissions

5. For example, to modify the permissions of the file test to 644, you can use the following methods:

chmod ("File name", s_irusr| s_iwusr| s_irgrp| S_iroth);

chmod ("filename", 0644);

chmod ("filename", 420);


One, the first method is to 00400 and 00200 and 00040 and 00004 to carry out or operation, the result is 0644 (octal), and octal 0644 is equal to the decimal 420, so the above several ways equivalent.

Second, when we pass the parameter to the chmod function, he converts the corresponding decimal mode parameter to the corresponding octal operation. So, when we want to change the permissions of the incoming file in the function to 644, the arguments passed to the function chmod cannot be directly 644. And it should be 420. This is because the decimal 420 equals eight binary 644.

Third, but we can enter 644 directly when we use the chmod command

chmod () function case:

6. About the use of CHMOD commands in Linux:

Numerical notation:


Read permission (symbol: R)--4

Write permission (symbol: W)--2

Execute permission (symbol: x)--1

No permissions granted--0

Case Description:

chmod 644 Test

chmod 666/mydoc/*//Set permissions for all files in directory MyDoc

Chmod-r 666/mydoc/*//Include directory


Use text to change permissions

chmod u = rwx file1 file1 file owner permission is rwx, this sentence can also be changed to: chmod u+rwx file1

chmod g = rwx file1 file1 file group has permission for rwx, this sentence can also be changed to: chmod g+rwx file1

chmod o = rwx file1 file1 file Other user rights are rwx, this sentence can also be changed to: chmod o+rwx file1

chmod u-x Filex Delete file1 file owner's x permission

chmod UG=RW File1 also set the File1 file owner and group permissions to RWX

chmod ug+x file also increases the X-rights of file1 files owners and groups


File Permission Masking

Umask [-S] [rights masking]

The so-called permission mask is composed of 4 octal digits, and these 4 octal numbers are used to determine the access rights of the newly generated file. The Umask command is set to not want the permissions of the new file to be opened.

In general, the system default new file access is 0666, the default value of the new directory is 0777. And this access minus the Umask setting is the real access to this file.

For example: If the Umask value is set to 0222, the default permissions for each new file are:

0666-0022 = 0644 (equals rw-r--r--)

The default permissions for the directory are:

0777-0022 = 0755 (equals Rwxr-xr-x)

Case Description:

Operation Result:

Linux Server development: chmod () function, chmod command, and file screen umask command, program modification Umask, Detailed introduction + case Demo

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.