LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-28-2017, 12:31 PM   #1
denka
LQ Newbie
 
Registered: Dec 2017
Posts: 3

Rep: Reputation: Disabled
chown of file doesn't changed the ownership permanently when file is in home directory


Hi,

I'm doing the following in denka's home directory:

denka@tom:~$ ls -l test.txt
-rw-r--r-- 1 denka denka 5 Dec 28 20:01 test.txt

denka@tom:~$ sudo chown user:user test.txt

denka@tom:~$ ls -l test.txt
-rw-r--r-- 1 user user 5 Dec 28 20:01 test.txt

denka@tom:~$ vim test.txt -> make some changes and save the file

denka@tom:~$ ls -l test.txt
-rw-r--r-- 1 denka denka 10 Dec 28 20:19 test.txt

If I do the same in /etc, it works as expected. Why the ownership is not changed permanently when the file is in home directory?
 
Old 12-28-2017, 12:38 PM   #2
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 9,078
Blog Entries: 4

Rep: Reputation: 3170Reputation: 3170Reputation: 3170Reputation: 3170Reputation: 3170Reputation: 3170Reputation: 3170Reputation: 3170Reputation: 3170Reputation: 3170Reputation: 3170
Did the editor replace the file with a new one? It certainly looks like it did ... eighteen seconds later.
 
Old 12-28-2017, 12:43 PM   #3
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 4,025

Rep: Reputation: 1776Reputation: 1776Reputation: 1776Reputation: 1776Reputation: 1776Reputation: 1776Reputation: 1776Reputation: 1776Reputation: 1776Reputation: 1776Reputation: 1776
When you edit a file with vim (or most other editors) it creates a new file, then once that new file has been written successfully renames it to the old name. It does that for safety, to avoid losing everything if something goes wrong when writing the updated file. That newly created file will of course be owned by your UID.

One way to avoid that behavior is to create a second hard link to that file. In order to avoid breaking the hard link, the editor is forced to throw caution to the winds and write directly to the original file. You of course have to retain write premission to that chown-ed file in order to be able to do that. In your example, you do not have that write permission.
 
Old 12-28-2017, 01:04 PM   #4
denka
LQ Newbie
 
Registered: Dec 2017
Posts: 3

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by rknichols View Post
When you edit a file with vim (or most other editors) it creates a new file, then once that new file has been written successfully renames it to the old name. It does that for safety, to avoid losing everything if something goes wrong when writing the updated file. That newly created file will of course be owned by your UID.

One way to avoid that behavior is to create a second hard link to that file. In order to avoid breaking the hard link, the editor is forced to throw caution to the winds and write directly to the original file. You of course have to retain write premission to that chown-ed file in order to be able to do that. In your example, you do not have that write permission.
You are right, if I create hard-link with : ln test.txt test1.txt and change the ownership then test or test1 can not be edited anymore and the ownership stays.
Thanks!
 
Old 12-28-2017, 01:17 PM   #5
scasey
Senior Member
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.5
Posts: 1,805

Rep: Reputation: 564Reputation: 564Reputation: 564Reputation: 564Reputation: 564Reputation: 564
Quote:
Originally Posted by rknichols View Post
When you edit a file with vim (or most other editors) it creates a new file, then once that new file has been written successfully renames it to the old name. It does that for safety, to avoid losing everything if something goes wrong when writing the updated file. That newly created file will of course be owned by your UID.

One way to avoid that behavior is to create a second hard link to that file. In order to avoid breaking the hard link, the editor is forced to throw caution to the winds and write directly to the original file. You of course have to retain write premission to that chown-ed file in order to be able to do that. In your example, you do not have that write permission.
Question: Why is the user denka able to overwrite a file it doesn't own and doesn't have write access to using vim? It seems to me that once the owner and group are changed to user:user, that user denka should be able to open the file in vim (as it's world-readable), but shouldn't be able to save changes to it.
When I duplicate the action on my server, I'm not allowed to save changes to the file.
 
Old 12-28-2017, 05:50 PM   #6
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 4,025

Rep: Reputation: 1776Reputation: 1776Reputation: 1776Reputation: 1776Reputation: 1776Reputation: 1776Reputation: 1776Reputation: 1776Reputation: 1776Reputation: 1776Reputation: 1776
Quote:
Originally Posted by scasey View Post
Question: Why is the user denka able to overwrite a file it doesn't own and doesn't have write access to using vim? It seems to me that once the owner and group are changed to user:user, that user denka should be able to open the file in vim (as it's world-readable), but shouldn't be able to save changes to it.
When I duplicate the action on my server, I'm not allowed to save changes to the file.
The ability to delete a file and replace it depends on the directory permissions, not the file permissions. Yes, the rm command will warn you if you try to delete a file for which you do not have write permission, but that is just a courtesy warning. All you have to do is respond with "y" to remove the file. Other programs might or might not follow that convention.

If a directory has the "sticky" bit set in its permissions, then you have to own the file (or be root) to delete it. That feature is generally used in publically writeable directories like /tmp.
 
Old 12-29-2017, 07:24 AM   #7
denka
LQ Newbie
 
Registered: Dec 2017
Posts: 3

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by rknichols View Post
The ability to delete a file and replace it depends on the directory permissions, not the file permissions. Yes, the rm command will warn you if you try to delete a file for which you do not have write permission, but that is just a courtesy warning. All you have to do is respond with "y" to remove the file. Other programs might or might not follow that convention.

If a directory has the "sticky" bit set in its permissions, then you have to own the file (or be root) to delete it. That feature is generally used in publically writeable directories like /tmp.
OK I've made the following test:
denka@tom:~/test$ ls -l
total 8
-rw-r--r-- 1 denka denka 6 Dec 29 13:49 test1.txt
-rw-r--r-- 1 denka denka 0 Dec 28 22:09 test2.txt
-rw-r--r-- 1 denka denka 0 Dec 28 22:09 test3.txt
-rw-r--r-- 1 denka denka 0 Dec 28 22:09 test4.txt
-rw-r--r-- 1 denka denka 14 Dec 29 13:50 test.txt
denka@tom:~/test$ vim test1.txt -> make some changes and save it
denka@tom:~/test$ rm test1.txt
rm: cannot remove 'test1.txt': Permission denied
denka@tom:~/test$ cd ..
denka@tom:~$ ls -ld test
drwxr-xr-x 2 user user 4096 Dec 28 22:12 test

As you said, even if denka is the file owner, denka can't remove the file, because user is the owner of the directory. But denka is able to edit it, so may be in some cases vim don't perform rm?
 
Old 12-29-2017, 09:15 AM   #8
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 4,025

Rep: Reputation: 1776Reputation: 1776Reputation: 1776Reputation: 1776Reputation: 1776Reputation: 1776Reputation: 1776Reputation: 1776Reputation: 1776Reputation: 1776Reputation: 1776
Quote:
Originally Posted by denka View Post
As you said, even if denka is the file owner, denka can't remove the file, because user is the owner of the directory. But denka is able to edit it, so may be in some cases vim don't perform rm?
Yes, vim is smart enough to overwrite the file directly if it can't create a new file in the directory.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] chown: changing ownership of `<file>': Operation not permitted schuurs Linux - General 14 06-26-2013 05:15 PM
How to inherit group ownership from directory when creating a file wastingtime Linux - Software 3 11-24-2009 08:06 PM
bash_profile file doesn't run in home directory spaceman spiff Slackware - Installation 3 01-13-2009 11:59 PM
Why doesn't "cp -p <file>" copy <file> and preserve ownership? davidas Linux - Newbie 5 04-12-2004 05:44 AM
Directory changed to a file cparton Linux - Newbie 0 09-25-2003 12:58 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:26 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration