Hello.
When i open a file in my linux localhost with the command "gedit file.php" and then put a part of code in it, then i can't save the file. the save icon above the gedit is deactivated.
What can i do?

Dani AI

Generated

Quick summary: this is almost always an ownership/permissions problem — you opened the file as a normal user but the file is owned by root, so the GUI editor can't overwrite it. was right to check ownership, and correctly explains why a root-owned file won't be writable by an ordinary account. Below are safe, practical ways to edit or fix the situation without creating new permission problems.

Safe ways to edit a root-owned file

  • Use a safe elevated edit that doesn't run a GUI as root:

    sudoedit /path/to/file.php

    This opens your preferred editor on a temporary copy and writes back as root when you save.

  • If you need a GUI, use gedit's admin backend (Polkit will prompt for authentication):

    gedit admin:///path/to/file.php

When a permanent change is appropriate (development files, not critical system files)

  • Make the project files editable by your user or group rather than making them world-writable. Example pattern:

    sudo chown $USER:$USER /path/to/file.php
    sudo chmod 664 /path/to/file.php
  • For web projects, add your user to the webserver group (usually www-data) and set group permissions/setgid on the project directory so new files inherit the group.

Quick troubleshooting checklist (if saving still fails)

  • Ensure the filesystem isn't full or mounted read-only: df -h and mount.
  • Check for an immutable attribute: lsattr /path/to/file.php.
  • If on a server with SELinux/AppArmor, verify policy isn't blocking writes.

Cautions and best practice

  • Avoid running a GUI editor with plain sudo (it can create root-owned config files in your home). Prefer sudoedit or the admin:// approach. Use version control and a local development workspace so you rarely need to edit files as root.

Recommended Answers

All 4 Replies

The file is probably not yours, or has wrong permissions. Open a terminal and type ls -l file.php to see the permissions. Post the result here.

Waht do you mean by the file is probably not mine?!
I typed that and the result:
-rw-r--r-- 1 root root

I found the solution myself. Thank you.

Hi Niloofar24,

Greetings !!!

As per the permissions to the file (i.e -rw-r--r-- 1 root root). This file is actually own by root user (see at last line "root root") which is a administrator user in linux who has permission to read and write on file (see first characters i.e."-rw") and other that "root" user all users have read permission only (i.e."-r--r--").

As you said you are unable to save he file, this is because you are not owner of that file and you cannot do any changes in file. For making changes into file you need to have same permission which root user has. Ask you system admin to assign permission of read and write to you.

Hope my answer was more clear to you and you will not take it as offending.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.