The Linux mailx command gives Cannot open mailbox /var/mail/user: Permission denied error when the user is not part of the mail group or /var/mail/user does not exist
Below is the exact error,
$ mailx Cannot open mailbox /var/mail/atechtown: Permission denied No mail for atechtown
This is the way you can resolve it,
First, you need to check the group of the current user. Use the groups command to display all groups for the current user as shown below.
$ groups atechtown adm cdrom sudo dip plugdev lpadmin lxd sambashare
You can see the user atechtown is not part of the mail group. So you have to add.
Now, add the user to the mail group using the adduser command.
$ sudo adduser atechtown mail [sudo] password for atechtown: Adding user `atechtown' to group `mail' ... Adding user atechtown to group mail Done.
Run groups command again to see if user is added to the group.
$ groups atechtown adm mail cdrom sudo dip plugdev lpadmin lxd sambashare
Next, check if the/var/mail/atechtown directory exists and it has proper permission. If the directory does not exist the run the following command.
$ mail [email protected] Cc: Subject: hi Null message body; hope that's ok $
Now run mailx command and now you can see the folder is created and there is email as well.
$ mailx "/var/mail/atechtown": 1 message 1 new >N 1 atechtown Wed Sep 2 23:51 12/417 hi ?
That’s it. Above steps should help you to resolve permission denied error while using mailx command.
I hope you found this small tip helpful. Please share and subscribe.
Reference – askubuntu