To ensure that all new files created by any user or program (e.g., touch, python, etc.) on a Red Hat system have the permissions -rw------- (i.e., 600) by default, you need to set the umask system-wide.
022, which results in new files having permissions 644 (-rw-r--r--).600 (-rw-------), the umask must be 077.To apply this for all users and processes, you need to modify the PAM (Pluggable Authentication Modules) configuration and the systemd service manager (for system services).
Edit /etc/profile and /etc/bashrc (or /etc/bash.bashrc) to include:
bashumask 077
This ensures that all interactive shells (for all users) use umask 077.
Edit /etc/systemd/system.conf and uncomment/modify the following line:
iniDefaultUMask=0077
Then, reload systemd:
bashsudo systemctl daemon-reload
This ensures that all services started by systemd (e.g., cron jobs, web servers, etc.) use umask 077.
Edit /etc/login.defs and set:
iniUMASK 077
This affects all user sessions, including non-interactive ones.
If you want to enforce this for existing users, you can also add umask 077 to:
/etc/profile.d/custom_umask.sh (create this file if it doesn't exist)/etc/bashrc (for non-login shells)Example for /etc/profile.d/custom_umask.sh:
bash#!/bin/sh umask 077
Make it executable:
bashsudo chmod +x /etc/profile.d/custom_umask.sh
Log out and log back in (or restart the system).
Test file creation:
bashtouch testfile ls -l testfile
The output should show -rw-------.
For Python:
pythonopen("testfile.py", "w").close()
Check permissions:
bashls -l testfile.py
Should also be -rw-------.
/root/.bashrc).755 (drwxr-xr-x). With umask 077, new directories will be 700 (drwx------). If you want directories to remain 755, you need a more granular approach (e.g., using ACLs or custom scripts).If you need more fine-grained control (e.g., different umasks for different users/groups), consider using Access Control Lists (ACLs) with setfacl. However, this is more complex and not necessary for your use case.
/etc/profileumask 077/etc/bashrcumask 077/etc/login.defsUMASK 077/etc/systemd/system.confDefaultUMask=0077/etc/profile.d/custom_umask.shumask 077After applying these changes, all new files created by any user or process will have -rw------- permissions by default.
Vibe can make mistakes. Check answers. Learn more