10 October 2026 · One command a day
How do I make a .env file readable only by its owner on Linux?
The command chmod 600 .env changes a file's permissions so that only the user who owns it can read or write it. Everyone else on the server is locked out. Use it on any file that holds secrets, such as database passwords, API keys or session secrets.
I learn my automation tips in the AI Profit Boardroom. Have a look here. Affiliate link.
The command
chmod 600 .envchmod changes a file's mode, which is its set of permissions. The three digits apply in order to the owner, the group and everyone else. Each digit adds up read (4), write (2) and execute (1). So 6 means read and write for the owner. The first 0 means no access for the group, and the second 0 means no access for anyone else. .env is the file to change, relative to your current directory. If you own the file, you do not need sudo. If root or another user owns it, run sudo chmod 600 .env.
What you will see
user@host:~/appname$ ls -l .env
-rw-r--r-- 1 appname appname 412 Oct 10 09:14 .env
user@host:~/appname$ chmod 600 .env
user@host:~/appname$ ls -l .env
-rw------- 1 appname appname 412 Oct 10 09:14 .envchmod prints nothing when it works, so run ls -l before and after to check. Ignore the first character, which shows the file type. The next nine characters are three sets of three: owner, group and others. Before the change, rw-r--r-- meant any user on the server could read the file. After it, rw------- means only the owner, here appname, can read or write it. The two names after the link count are the owning user and group.
When to use it
- Straight after you create a
.envfile for a new app, before you put any real secrets in it. - After a deploy or
git pullthat recreates the file with default permissions, which are usually644and readable by everyone. - On other secret files, such as private keys, database dump credentials or a
.pgpassfile. Some tools refuse to use these files unless they are already locked down.
Watch out for
With 600, only the owner can read the file, so the owner has to be the user your app runs as. If you created .env as root and the app runs under PM2 as appname, the app will no longer be able to read its own config. It will usually fail to start or connect with missing values. Check the owner with ls -l. If it is wrong, fix it with sudo chown appname:appname .env first, as covered in how to fix file ownership on Linux after running something as root. Then restart the app.
Questions people ask
Does chmod 600 stop root from reading the file?
No. Root can read any file whatever its permissions. 600 protects the file from other ordinary users and from processes running as them.
Should I use 400 instead of 600?
400 makes the file read-only, even for the owner. That is slightly stricter, but you then have to change it back each time you edit the file. For most app secrets, 600 is the practical choice.
Setting permissions on your own secret files only works on a server where you have a shell and control who else has an account. You can get the same VPS with 20% off at https://asksteves.co.uk/vps. Affiliate link.
Want more automation tips? The AI Profit Boardroom on Skool is where I get mine. Have a look here. Affiliate link.
Everything on this blog runs on one Hostinger KVM 8. This link takes 20% off the same box.
Affiliate link.