1753546 Members
5623 Online
108795 Solutions
New Discussion юеВ

change mode

 
Cheung_2
Frequent Advisor

change mode

I would like to change the mode as all users can write something to this file , but only root can delete this file , what is the approiprate mode in this case? Thx.
Andy
8 REPLIES 8
U.SivaKumar_2
Honored Contributor

Re: change mode

Hi,

Once write bit is set on the file for all the users , anybody can also delete/overwrite that file. SO your requirement is not possible.

regards,
U.SivaKumar
Innovations are made when conventions are broken
sycncs
Advisor

Re: change mode

Hi there

I am not sure if I understand your question correctly.

If you DO NOT want others to write to this file, do "chmod 755 ".

If you WANT all users to write to this file, do "chmod 777 "

Then after that, login as root, do "chmod root:sys . This will ensure only root can delete this file.

Hope this helps.
S.K. Chan
Honored Contributor

Re: change mode

You can control this from the permission/ownership of the directory where the file is located. For example ..
# cd /tmp
# mkdir dirA
# chmod 750 dirA
# chown root:users dirA
# cd /tmp/dirA
# touch fileA
# chmod 777 fileA
Now as normal user (assuming group ownership is "users"), fileA can be modified by anyone but cannot be deleted by anyone except root. Ignore if this is not what you're looking for.
Cheung_2
Frequent Advisor

Re: change mode

I have test it - use root to create a file ( touch file ) , change it to 666 ( chmod 666 file ) , change the owner as root (chown root:sys file ) then everyone can modify and delete , how can I prevent users can delete it? Thx.
Andy
T G Manikandan
Honored Contributor

Re: change mode

The case you can do here is the sticky bit.

setting sticky bit on a file will provide a way where only the owner or the super user can delete the file.

Create a file /tmp/a as root user and set sticky bit for the file with 777 permissions.

That should be fine.


chmod 1777
Also make sure the owner is root
U.SivaKumar_2
Honored Contributor

Re: change mode

Hi Manikandan,

sticky bit wont work for regular files

regards,
U.SivaKumar
Innovations are made when conventions are broken
T G Manikandan
Honored Contributor

Re: change mode

Yes Siva Just did not think more...

It works only for dir.
Then you should think of ACL's.
Bill Hassell
Honored Contributor

Re: change mode

Very important Unix concept: the ability to write to a file (including deleting the contents) is controlled by the FILE's permission. But the ability to delete, move or rename a file has absolutely nothing to do with the FILE's permissions. Put it another way: the existence of a file completely depends on the permissions of the directory, not the file!

For example, create a directory owned by root with 755 permissions. Then touch a file inside the directory and give it 666 permissions. Now as an ordinary user, you can do anything to the file EXCEPT remove it! The directory permissions control the existence, while the file permissions control the contents. Set the directory ownership to the user that can actually remove the file and you've protected the file(s).

As mentioned the sticky bit may be set on a globally writable directory such as /tmp (normally 777 permission, set sticky with 1777 permissions) and this will disable the ability of everyone except the owner of the file from removing the file. Now the file can be 666 permissions and the contents changed by anyone but not removed (or renamed) except by the owner. This is commonly done for /tmp and /var/tmp


Bill Hassell, sysadmin