This question started off asking why my history file wasn’t working properly. Then I noticed it was
-rw------- 1 root root and hadn’t been updated since 2012-09-11. I changed the ownership, problem fixed.
But now I see some other files are owned by root:
.gitconfig .pearrc .viminfo
Can I safely change them to be owned by my normal user, not root? I’m scratching my head trying to work out if there is a downside, or a security consequence.
Losing seven weeks history is actually quite painful, because I lean on it a lot (e.g. to remind how I last did an archive). Would it be reasonable to set up a cron job to email me if it finds any files in my home directory owned by anyone else but me? Rephrased: is there ever a good reason for root to own a file in my home directory?
UPDATE: Thanks for the responses so far. I see how the root-owned files might get put there from doing something in a sudo session. The core of my question is: Can I reasonably always treat that as a mistake that needs fixing? Or is there ever a valid reason to leave a root-owned file in my home directory once I notice it? Thanks!
Root should usually not need to own files in your home directory. That is a generalization of course. As has been mentioned in other answers and comments, files may be owned by root if they were created or modified by programs running under sudo.
To list which files are owned by root, or belong to the root group, try running listing all files with their owner information in a terminal and grab the lines containing “root”:
ls -al | grep root.
On my machine, under my regular user account, I’ve got a few files and even a directory (.compiz-1/) that show up as owned by root, so it is quite common.
Looking at and inside those files – you may need to do this as root – I can make a qualified guess at how they got there. Some of them are log files from programs I’ve executed using
sudo program. Remember, using sudo runs the program as root but keeps
$HOME pointing to your home directory. Using
kdesu for GUI programs does the same, but also makes sure the graphical environment uses your settings (which is part of why you shoud not use sudo for them).
The .compiz-l/ directory appears to be a backup of .compiz/, which is owned by my user. My guess is that at some point I ran an upgrade or did something else to trigger a backup of the Compiz settings as root and that I can now delete those files. If in doubt, just move or rename the file/directory and see what happens afer a logout/login or running the program(s) you think uses them.
Let’s look at the files you listed and compare them to the same ones on my machine.
-rw-r--r-- 1 henrik henrik 211 apr 9 2011 .gitconfig
This file should most likely be owned by the user, without write access to anyone else, as it’s obviously a per user configuration file. If root owns this file and you don’t have write access, you’ll have to run git as root to be able to update your global git configuration, which isn’t very convenient. I don’t mind others having read access to this file since it doesn’t contain anything secret, only my name, email and a few preferences.
-rw-r--r-- 1 root root 143 dec 17 2011 .pearrc
This file is also a config file, but not something I normally use directly from my user account since it belongs to the PHP PEAR project. As I have to install PHP extensions using
sudo anyway, this file might as well be owned by root so I don’t accidentally mess it up.
-rw-rw-r-- 1 henrik henrik 30709 nov 7 00:29 .viminfo
This is Vim’s history file, containing a log of pretty much everything you’ve done in Vim so it can restore states easily. If it’s not owned or at least writable by you, I doubt Vim will be able to update it.
Generally, yes, root owned files in your home directory is a mistake, if they are used by programs you normally run without being root. It may even become difficult to do a full backup of the configuration files in your home directory if files are neither owned or readable by you.
Did you use sudo to copy those files over before? That might explain why those files are owned by root.
Usually, files/folders under /home/ should belong to the user.