2

I am trying to edit the file /etc/postfix/main.cf but this applies to any file.

Currently its permission bits are set to -rw-r--r--

I want to open, edit and save this file when operating as a regular user in emacs. However, try as I might, I just can not do it. Here are the options I have tried.

First I tried this method: C-x C -q This allows me to edit the buffer well enough, but when I try to save the buffer I am presented with: File main.cf is write-protected; try to save anyway? (yes or no) and when I type yes I get:

Doing chmod: operation not permitted, /etc/postfix/main.cf

Similarly, if I try M-! chmod u+w main.cf at any point I get the message:

chmod: changing permissions of 'main.cf': Operation not permitted

Lastly if I try to edit the file in wdired with wdired-allow-to-change-permissions I get

chmod 664 '/etc/postfix/main.cf' failed

Emacs is so powerful it seems like there must be a way to do this without using the /sudo::/filename trick mentioned here

Additionally, I think there should be a "permissions" tag here in emacs stack overflow.

  • 5
    Well, if you're not actually running Emacs as root (which you shouldn't!), then the "sudo filename trick" is the correct solution - you need to elevate privilege somehow to edit the file, and Tramp's sudo transport is the way to do so. It sounds like you might really benefit from a package that'll automatically reopen files requiring elevation via the sudo transport, instead of making you do it by hand; this Stack Overflow thread has a wide variety of options, on none of which I'm qualified to comment. – Aaron Miller Aug 16 '16 at 19:42
  • 3
    If I understand right, you don't own that file, so you can't change the permissions on it with C-x C-q, nor can you change the permissions with wdired. You have to switch to root using sudo to edit files you don't own. I use a variant of Burton's answer in the link Aaron gave. – amitp Aug 16 '16 at 21:13
  • The way I do this (well, not for the main.cf file, but for other files in /etc/postfix) is using group permissions. Since I'm in the root group, which is the normal group for those files, you can just set the files you want to edit to g+w and then edit at your leisure. But only change the permissions on the files you really need lest you accidentally break things. – MAP Aug 19 '16 at 7:45
6

-rw-r--r-- means that only the file owner may write to the file (file ownership would be meaningless if any user could write to any other user's files).

The operating system enforces the file permissions, so unless Emacs was run by the file's owner, it cannot write to this file unless the operating system allows it to.

The facilities available to you are using sudo or su or otherwise logging in as the file owner or as root. Neither Emacs nor any other application should ever have the ability to do what you are asking without going through such OS-approved channels.

The sudo tramp method (e.g. visit /sudo::/etc/postfix/main.cf to edit the file as the root user) is the typical approach to take in Emacs.

1

https://github.com/nflath/sudo-edit

There are also other more "convenient" but dangerous solutions(for example, we can advice find-file)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.