Create files with safe permissions
Set umask 077 before creating files so only the owner can read them. The default 022 leaves new files readable by every local user.
A script writes a token or a backup, and the file must not be readable by other users on the machine.
umask
touch default.txt
(umask 077; touch private.txt; mkdir private-dir)
stat -c '%a %n' default.txt private.txt private-dir0022
644 default.txt
600 private.txt
700 private-dirThe umask is a mask of permission bits removed from the mode a program asks for. With 022 a new file gets 644, and with 077 it gets 600, while a directory gets 700 instead of 755. The behavior is specified for the umask utility. Running the change in a subshell, as the second command does, confines it so the rest of the script keeps its normal mask.
The umask only affects files created after it is set, and not a file that already exists. A file that gets its mode from another program, such as one copied with cp -p, keeps the source mode. For one file, install -m 600 /dev/null file creates it with the right mode from the start, while chmod after creation leaves a short window where the mode is wrong.