Where do 644 and 755 come from?
New files and directories get their permissions from the umask, a mask of the permissions to take away. Files start from 666 (no one gets x by default) and directories from 777; the umask is removed from that:
umask
touch new.txt
mkdir new-dir
ls -ld new.txt new-dirIn the terminal
student@vulntech:~$ umask 0022 student@vulntech:~$ touch new.txt student@vulntech:~$ mkdir new-dir student@vulntech:~$ ls -ld new.txt new-dir drwxr-xr-x 2 student student 0 Oct 17 06:31 new-dir -rw-r--r-- 1 student student 0 Oct 17 06:31 new.txt
| Starts at | minus umask 022 | Result | |
|---|---|---|---|
| New file | 666 | 022 | 644 rw-r--r-- |
| New directory | 777 | 022 | 755 rwxr-xr-x |
A private umask
umask 077 takes everything away from the group and others, so all your new files are private:
umask 077
touch secret.txt
mkdir secret-dir
ls -ld secret.txt secret-dirIn the terminal
student@vulntech:~$ umask 077 student@vulntech:~$ touch secret.txt student@vulntech:~$ mkdir secret-dir student@vulntech:~$ ls -ld secret.txt secret-dir drwx------ 2 student student 0 Oct 17 06:31 secret-dir -rw------- 1 student student 0 Oct 17 06:31 secret.txt
The setting only lasts until you close the shell. To keep it, add the line umask 077 to your ~/.bashrc.
Remember:
umasklists what's taken away from new files (666) and directories (777). 022 gives 644/755, 077 gives 600/700.