Chmod

From OnnoWiki
Jump to navigation Jump to search

Sumber: http://www.washington.edu/computing/unix/permissions.html


File dan directory di Unix mempunyai tiga (3) tipe ijin: read ("r"), write ("w"), dan execute ("x"). Setiap permission dapat di "on" atau "off" untuk masing-masing dari tiga (3) kategori dari pengguna:

  • owner dari file / directory
  • user lain dalam group yang sama dengan owner
  • semua yang lain (user se dunia)

File

Untuk mengetahui mode (atau permission) dari sebuah file dapat menggunakan

ls -lg filename

Perintah ini akan membuat message kira-kira,

-rwxr-x--x 1 owner group 2300 Jul 14 14:38 filename

Penjelasan / cara membacanya:

  • 10 karakter di kiri, menunjukan mode / permission.
  • "-" karakter di paling awal menunjukan tipe file. "-" menunjukan file biasa. "d" menunjukan sebuah directory.
  • karakter 2-4 menunjukan permission bagi owner, "r", "w" atau "x" menunjukan ijin yang bagi owner. "-" jika tidak diberi permission.
  • karakter 5-7 menunjukan permission bagi group.
  • karakter 8-10 menunjukan permission bagi yang lain (seluruh dunia).
  • string ke dua menunjukan banyaknya link ke file tersebut
  • string ke tiga menunjukan owner dari file
  • string ke empat menunjukan group dari file

Untuk mengubah mode dari sebuah file dapat menggunakan chmod, secara umum adalah,

chmod X@Y file1 file2 ...

dimana

  • X adalah huruf kombinasi dari "u" (untuk owner), "g" (untuk group), "o" (untuk others), "a" (untuk all; yaitu untuk "ugo")
  • @ bisa berisi "+" untuk menambahkan permission, "-" untuk membuang permission, atau "=" untuk mengalokasikan absolut permission
  • Y adalah kombinasi dari "r", "w", "x".

Contoh:

chmod u=rx file        (Beri owner rx permission, tapi tidak w)
chmod go-rwx file      (Buang rwx permission untuk group, dan others)
chmod g+w file         (Beri write permission untuk group)
chmod a+x file1 file2  (Beri execute permission ke everybody)
chmod g+rx,o+x file    (OK untuk combine menggunakan koma)

Directory

The permission scheme described above also applies to directories. For a directory, whoever has `read' permission can list files using the ls command (and thus discover what files are there); whoever has `write' permission can create and delete files in that directory; whoever has execute permission can access a file or subdirectory of known name. To find out the mode of a directory:

ls -dl dir ...  Show permissions for
                the named directory(ies)
ls -al dir ...  Long list of all files
                in named directory(ies)
                (including those with names
                     starting in `.')

If no directories are specified, the listing is for all files in the current directory. The output will look something like:

drwx------12 fred        592 Jul 11 13:46 .
drwxr-xr-x24 root       1424 Jul 10 13:07 ..

The initial `d' in the 10-character mode string indicates that the file is a directory. The file name `.' always refers to the current directory; the file name `..' always refers to the parent of the current directory. Thus, this output shows the permissions for the current directory and its parent.

Informasi Lanjut

Untuk membaca lebih lanjut bisa menggunakan man / user manual

man chmod
man ls

Variable "umask" digunakan sebagai permission mask untuk semua file dan directory yang baru dibuat. umask adalah 3 digit octal. Default mask adalah 022 = 000 010 010 binary. dua 1 bit tersebut akan menghalangi "group" dan "other" untuk write permission. Oleh karena itu, file yang baru akan mempunyai rwx permission untuk owner, dan rx permission untuk group dan others. umask 077 = 000 111 111 akan menyebabkan no permissions untuk group and others.

Untuk mengunakan umask selain default, kita harus memasukan kalimat

umask num (dimana num adalah octal number)

di .cshrc file. Untuk mengetahui lebih lanjut tentang umask, baca-baca

man umask

Contoh

Kadang kita perlu mengcopy sebuah file dari directory seseorang. Bagaimana caranya agar kita dapat mengakses directory tersebut?

Suppose that user `joe' wants to copy the file `prog.f' from user `fred.' At the Unix prompt, Fred should type

chmod go+x ~

This command changes the mode of Fred's home directory (represented by the ~), giving permission to all users to get to files in that directory. Therefore, Joe can access any file, of which he knows the name, in Fred's home directory. Fred has told Joe that the file he wants is called `prog.f,' so now Joe types

cp ~fred/prog.f prog.f

If Joe had an existing file with the name `prog.f,' which he did not want overwritten by Fred's file, he could instead type

 cp ~fred/prog.f prog2.f

If Joe receives a message from the system saying that he is denied permission to copy the file, Fred should make the file readable by others, changing its mode by entering

chmod go+r prog.f

If Joe wanted to copy several files from Fred's home directory, for example `prog.a,' `prog.b,' `prog.c,' and to give these files the same names in his own home directory, he would type

cp ~fred/prog.a ~fred/prog.b ~fred/prog.c .

The period (.) at the end of the command line specifies that the files are to be copied into Joe's current directory (which in this case is his home directory).

Once Joe has copied the files, Fred will probably want to change the mode of his home directory so that it is no longer accessible to the world at large. To do this, Fred should type

chmod go-rx ~

As you can see, a + sign used with `chmod' adds accessibility and a - sign takes it away. It is possible to use these features on directories of all levels and all files within those directories, individually or as a group. For detailed online information about the `chmod' command, enter

man chmod


Referensi