Update Jan-2022
Why not to use chmod 777: https://pimylifeup.com/chmod-777/
Using chmod to Set File Permissions
You can specify permissions for chmod using the letters u, g, and o, as symbolic code for the owner (“user”), group, and others (the class). This “symbolic mode” is easy to remember, since the symbols r, w, and x (the mode) are used directly as arguments in the command.
The chmod syntax uses the +, -, and = signs. The syntax is:
chmod class[�=]mode,[ … ] filename
For example, you can use the symbolic mode to create rw-r–r– permissions by specifying the symbols rw, r, and r directly in the chmod command. “User” is represented by u, “group” by g, and “other” by o. To assign the permissions absolutely, use the = sign in the argument. Unspaced commas separate class permissions:
chmod u=rw,g=r,o=r myfile
When permissions are being set the same, you can also combine the arguments as:
chmod ugo=r myfile
With only read permission on myfile, no one can write to it. Also, if you now try to remove myfile, the rm command asks you whether you really want to remove the file:
rm myfilemyfile: 444 mode? (y/n) nIf you do not want to remove it, enter n. If you do want to remove it, enter y.
To create rw——- permissions and set “no permissions” for the classes g and o, use = with no symbol following:
chmod u=rw,g=,o= filename
Permissions are added with the + sign. Again, separate each class-permission with a comma and no space:
chmod u+rw,g+r,o+r filename
You can also subtract permissions from u, g, or o, using -, to restrict the level of permission from a previous “higher” level. For example, if you had set rwxrw-rw- and you wanted to change this to rwx——:
$ chmod g-rw,o-rw filename
However, unless you began with no permissions you may find that using + or – has added to, or subtracted from, some previously existing permissions for that file. Run the ll command to check this. If in doubt, set the permissions absolutely by using =.
Later, if you want to permit yourself and members of your group to read from and write to myfile, use chmod as follows:
$ chmod ug=rw,o=r myfile
The ll command now should show:
-rw-rw-r– 1 leslie users 154 Nov 4 10:18 myfile
Here is a summary of the various chmod commands you can use to protect myfile.
To Set Permissions so that… Type This…
Only you can read from myfile, and no one (including you) can write to it. Set permissions to -r——–.
chmod u=r,g=,o= myfile
Everyone can read from myfile, but no one can write to it. Set permissions to -r–r–r.
chmod ugo=r myfile
Only you can write to myfile, but everyone can read it. Set permissions to -rw-r–r–.
chmod u=rw,go=r myfile
Only you and members of your group can write to myfile, but everyone can read it. Set permissions to -rw-rw-r–
chmod ug=rw,o=r myfile
Everyone can read from or write to myfile. Set permissions to -rw-rw-rw-.
chmod ugo=rw myfile
Only you can read from or write to myfile, but no one else can. Set permissions to -rw——-
chmod u=rw,go= myfile
In addition to changing permissions on files, the chmod command can also change permissions on directories. For example, you can protect a directory so that no one can alter its files.
The following examples assume that the directory projects exists under your current working directory.
To Set Permissions to… Type This…
Allow other users to list and access the files in projects, but not to create or remove files from it. Set permissions to drwxr-xr-x.
chmod u=rwx,go=rx projects
Allow all users to list, create, remove, and access files in projects. Set permissions to drwxrwxrwx.
chmod ugo=rwx projects
Allow only yourself to list, create, remove, and access files in projects. Set permissions to drwx——.
chmod u=rwx,go=- projects
When determining who should be allowed to use your directories, be aware that anyone who can write to a directory also can remove or rename a file in that directory � even if that person cannot write to the file.