chmod

Set a file's permissions for its owner, its group and everyone else, as letters like u+x or as a number like 755.

chmod <mode> <file>
Save

Angle brackets are yours to fill in. Linux, macOS.

Examples for chmod

  • Make a script executable

    chmod +x <file>
    Save

    Adds run permission for everyone who can already read it. Then run it with ./<file>. Use u+x to add it for the owner only.

    chmodtool+xadd execute<file>path
  • Set 755: owner can change, everyone can run

    chmod 755 <file>
    Save

    Each digit adds read 4, write 2 and run 1, for the owner, then the group, then everyone else. 7 is 4+2+1, 5 is 4+1. The usual mode for scripts, programs and directories.

    chmodtool755rwx for you, r-x for the rest<file>path
  • Set 644: owner can change, everyone can read

    chmod 644 <file>
    Save

    6 is read and write for the owner, 4 is read only for the group and everyone else. The usual mode for ordinary files, such as web pages and config.

    chmodtool644rw- for you, r-- for the rest<file>path
  • Fix permissions on an SSH key

    chmod 600 ~/.ssh/id_ed25519
    Save

    ssh refuses a private key that anyone but you can read, with the error UNPROTECTED PRIVATE KEY FILE. 600 is read and write for you, nothing for anyone else. The ~/.ssh directory itself should be 700.

    chmodtool600only you~/.ssh/id_ed25519path
  • Set files and directories to different modes

    find <directory> -type d -exec chmod 755 {} + -o -type f -exec chmod 644 {} +
    Save

    chmod -R gives files and directories the same mode, but directories need x and plain files should not have it. find applies 755 to directories and 644 to files in one pass.

    findtool<directory>path-type ddirectories-execchmod 755tool{} + -o-type ffiles-execchmod 644tool{} +
  • Take away write permission from others

    chmod go-w <file>
    Save

    u is the owner, g the group, o everyone else, a all three. + adds, - removes, = sets exactly. This leaves the other permissions as they are.

    chmodtoolgo-wgroup and others lose write<file>path
  • Why not chmod 777

    chmod 777 <file>
    Save

    777 lets every user on the machine change or replace the file. It usually hides a wrong owner. Fix the owner with chown, or give the group write with 775, instead.

    Any user or process on the machine can then change the file, including to run their own code.

    chmodtool777everyone can do everything<file>path

Flags

-R --recursive
Change a directory and everything in it.Directories need x to be entered. A mode such as 644 applied with -R makes every directory under it unusable.
-v --verbose
Print each file as it is changed. GNU chmod only; on macOS, -v prints every file.
--reference <file>
Copy the permissions from another file. GNU chmod only.