9

I needed to edit a .config file (which I know for a fact is not creating these problems, just to clear that up) and it wouldn't let me save it, so I took ownership of /etc and all contents with the command chown -hR username /etc and that let me edit the .config file but now whenever I try to install any packages or use any sudo commands, it doesn't work (I've checked for errors in the sudo file in sudoers.d and there was nothing wrong with that).

So how do I return it to the previous owner?

terdon
  • 100,812
Raf
  • 91
  • 1
  • 1
  • 3
  • Try running sudo chown -R root:root /etc. It works... now you know you shouldn't play like this with root own files! :) – edwin May 27 '13 at 13:20
  • i did that, now whenever i run a sudo command it says 'sudo: /etc/sudoers.d is world writable' – Raf May 27 '13 at 14:11
  • 1
    @Raf If it says /etc/sudoers.d is world writable after re-chowning everything back to root you must have done something else, like chmod 666. That is an entirely different kettle of fish. – l0b0 Feb 20 '17 at 18:34
  • 2
    unless you have some long winded configurations setup for this system i would reinstall it , it will be safer and more reliable than having unexpected permissions in /etc IMHO – Amias Feb 21 '17 at 11:14
  • This was years ago, long forgotten now! I ended up reinstalling the system I think. Cheers anyway lads. – Raf Feb 23 '17 at 00:58
  • 3
    Can anyone explain to me why this is a candidate for reopening? Thanks. – Elder Geek Aug 24 '17 at 23:59
  • 2
    @ElderGeek I see this question as a unique question involving /etc only which is arguably less complex than /. Additionally this question has an answer using --reference which the duplicate has no answers for. – WinEunuuchs2Unix Jun 28 '20 at 22:07
  • 1
    Reopen Voters: See above comment to @ElderGeek – WinEunuuchs2Unix Jun 28 '20 at 22:08
  • @WinEunuuchs2Unix Thank you for sharing that perspective. Your point is taken. On the flip side, do you think that reopening this question will result in answers better than what it already has? It's certainly not in danger of being deleted and the signpost to the more generic answer regarding system directories doesn't do any harm and may actually help. Am I missing a finer point here? – Elder Geek Jul 30 '20 at 17:09
  • 1
    @ElderGeek This was a long time ago and I can't remember all my arguments at that time. My system was fixed writing a script based on Terdon's answer so I'm all happy. I've moved on to bigger things :) – WinEunuuchs2Unix Jul 31 '20 at 01:27

2 Answers2

16

Since some of the files in /etc might not be owned by root, if you want to avoid reinstalling, you can do the following:

  1. Boot into a live system and mount the partition containing the /etc directory. For example, if your / partition is /dev/sda1, you would do this from the live system:

    sudo mount /dev/sda1 /mnt
    
  2. Set the ownership of all files to root:

    sudo chown -R root /mnt/etc
    
  3. At this point all files belong to root. This is probably what you need but some files in /etc/ might have different ownership. If those files are also not owned by root in the live system, you can use them as a reference to change the permissions on the installed system:

    shopt -s globstar
    liveSystem="/etc"
    cd /mnt/etc
    for file in **/*; do 
        [ -e "$liveSystem/$file" ] &&
            echo sudo chown --reference "$liveSystem/$file" "$file"
    done
    

    If you have also changed the permissions, do this instead:

    shopt -s globstar
    liveSystem="/etc"
    cd /mnt/etc
    for file in **/*; do 
        [ -e "$liveSystem/$file" ] &&
            echo sudo chown --reference "$liveSystem/$file" "$file" &&
            echo sudo chmod --reference "$liveSystem/$file" "$file"
    done
    

    That will just print what command would be run. Once you've seen what that does and are satisfied that it is correct, remove the echo to make it actually run the commands.

    Now that will iterate over all files, including those whose ownership is root in both directories, but that doesn't matter. You could write a more sophisticated approach but it's not worth it since this will take seconds.

  4. Reboot your machine: everything should be back to normal.

IMPORTANT: this will not help if you have installed something that isn't on the live system and whose files in /etc don't belong to root. If this is the case, you'll either need to find what you installed and reinstall it or you might need to reinstall the system.

muru
  • 197,895
  • 55
  • 485
  • 740
terdon
  • 100,812
  • @WinEunuuchs2Unix chown will also set the group. – terdon Jun 29 '20 at 08:13
  • Yes I read that myself later. I'll be trying it out myself later. I had a software development "mishap" yesterday where /tmp/$USER/scp.999999 directory files were supposed to be changed but it ended up being / somehow :( – WinEunuuchs2Unix Jun 29 '20 at 14:07
  • It was a wild and crazy 24 hours but I got my system working again (although screenfetch generates an error with xargs and echo). I referenced your answer and gave you credit to the duplicate candidate this Q&A is closed against: https://askubuntu.com/questions/43621/what-if-i-accidentally-run-command-chmod-r-on-system-directories-etc/1255242#1255242 – WinEunuuchs2Unix Jul 01 '20 at 01:50
2

Boot into recovery and issue the following commands:

mount -o remount,rw -n /
chown -R root: /etc
Zanna
  • 70,465
Frantique
  • 8,493