How to use "chmod" under linux

    技术2022-06-26  47

    This "chmod" command is be used quite a lot. and I cry for understanding and being familiar with this command. While I am looking its helpful, it cannot satisfy me. And then I make a decision to collect it belows:

     

    reference:

    1, "man chmod" under liunx

    2, https://help.ubuntu.com/community/FilePermissions

     

    In linux and Unix, everything is a file. The super user "root" has the ability to access any file on the sytem, while it is unsafe for most users by using "root". Each file has access restriction with permissions, user restrictions with owner/group association. and permissions are referred to as bits.

     

    This "chmod" command is used to change file mode bits, so this command is able to change the access to any file.

    There are three types of access restrictions:

    Permission

    Action

    chmod option

    read

    (view)

    r or 4

    write

    (edit)

    w or 2

    execute

    (execute)

    x or 1

    There are also three types of user restrictions:

    User

     ls output

    owner

    -rwx------

    group

    ----rwx---

    other

    -------rwx

     

    here givt the functions use "chmod" command:

     

    (1) permissions in action  

    ----how to get the file's permissions and "/etc/hosts" as a example

    $ ls -l /etc/hosts

    and u will get "-rw-r--r-- 1 root root 259 2011-01-01 08:37 /etc/hosts", this results means

       owner = Read & Write (rw-)   group = Read (r--)   other = Read (r--)

     

    (2) changing permissions

    Here give the usage about "chmod" command: chmod {options} filename

     

    Options

    Definition

    u

    owner

    g

    group

    o

    other

    x

    execute

    w

    write

    r

    read

    +

    add permission

    -

    remove permission

    =

    set permission

     

    and then give some examples below:

    chmod with Letters

    First create some empty files:

    $ touch file1 file2 file3 file4 $ ls -l   and get the results: -rw-r--r-- 1 user user 0 Nov 19 20:13 file1 -rw-r--r-- 1 user user 0 Nov 19 20:13 file2 -rw-r--r-- 1 user user 0 Nov 19 20:13 file3 -rw-r--r-- 1 user user 0 Nov 19 20:13 file4

    Add owner execute bit:

    $ chmod u+x file1 $ ls -l file1   and get the results: -rwxr--r-- 1 user user 0 Nov 19 20:13 file1

    Add other write & execute bit:

    $ chmod o+wx file2 $ ls -l file2 and get the results: -rw-r--rwx 1 user user 0 Nov 19 20:13 file2

    Remove group read bit:

    $ chmod g-r file3 $ ls -l file3 and get the results -rw----r-- 1 user user 0 Nov 19 20:13 file3

    Add read, write and execute to everyone:

    $ chmod ugo+rwx file4 $ ls -l file4   and get the results: -rwxrwxrwx 1 user user 0 Nov 19 20:13 file4

    chmod with Numbers

    Usage: chmod {options} filename

    Options

    Definition

    #--

    owner

    -#-

    group

    --#

    other

    1

    execute

    2

    write

    4

    read

    Owner, Group and Other is represented by three numbers. To get the value for the options determine the type of access needed for the file then add. For example if you want a file that has -rw-rw-rwx permissions you will use the following:

    Owner

    Group

    Other

    read & write

    read & write

    read, write & execute

    4+2=6

    4+2=6

    4+2+1=7

    $ chmod 667 filename Another example if you want a file that has --w-r-x--x permissions you will use the following:

    Owner

    Group

    Other

    write

    read & execute

    execute

    2

    4+1=5

    1

      (3) recursive permission changes  ----to change the permissions of multiple files and directions with one command Recursive chmod with -R and sudo

    To change all the permissions of each file and folder under a specified directory at once, use sudo chmod with -R

    $ sudo chmod 777 -R /path/to/someDirectory $ ls -l and gets the results: -rwxrwxrwx 1 user user 0 Nov 19 20:13 file1 drwxrwxrwx 2 user user 4096 Nov 19 20:13 folder -rwxrwxrwx 1 user user 0 Nov 19 20:13 file2  

    Recursive chmod using find, pipemill, and sudo

    $ sudo find /path/to/someDirectory -type f -print0 | xargs -0 sudo chmod 644 $ ls -l and gets the results: -rw-r--r-- 1 user user 0 Nov 19 20:13 file1 drwxrwxrwx 2 user user 4096 Nov 19 20:13 folder -rw-r--r-- 1 user user 0 Nov 19 20:13 file2   To change permission of only directories under a specified directory (including that directory): $ sudo find /path/to/someDirectory -type d -print0 | xargs -0 sudo chmod 755 $ ls -l and gets the results: -rw-r--r-- 1 user user 0 Nov 19 20:13 file1 drwxr--r-- 2 user user 4096 Nov 19 20:13 folder -rw-r--r-- 1 user user 0 Nov 19 20:13 file2   (4) changing the file owner and group-----to change file's owner using the chown command. For example, to change the foobar file's owner to tux: $ sudo chown tux foobar To change the foobar file's group to penguins, you could use either chgrp or chown with special syntax: $ sudo chgrp penguins foobar $ sudo chown :penguins foobar Finally, to change the foobar file's owner to tux and the group to penguins with a single command, the syntax would be: $ sudo chown tux:penguins foobar

    最新回复(0)