[原创]用chflags实现文件只能添加不能修改和删除_VMware, Unix及操作系统讨论区_Weblogic技术|Tuxedo技术|中间件技术|Oracle论坛|JAVA论坛|Linux/Unix技术|hadoop论坛_联动北方技术论坛  
网站首页 | 关于我们 | 服务中心 | 经验交流 | 公司荣誉 | 成功案例 | 合作伙伴 | 联系我们 |
联动北方-国内领先的云技术服务提供商
»  游客             当前位置:  论坛首页 »  自由讨论区 »  VMware, Unix及操作系统讨论区 »
总帖数
1
每页帖数
101/1页1
返回列表
0
发起投票  发起投票 发新帖子
查看: 2944 | 回复: 0   主题: [原创]用chflags实现文件只能添加不能修改和删除        下一篇 
wulcan
版主
等级:中校
经验:1795
发帖:124
精华:0
注册:2014-3-19
状态:离线
发送短消息息给wulcan 加好友    发送短消息息给wulcan 发消息
发表于: IP:您无权察看 2014-10-27 11:10:18 | [全部帖] [楼主帖] 楼主

效果:只能添加,不能修改或删除:

典型应用:日志

操作:chflags uappnd命令,该命令使用文件只能添加不能更改,也不能删除

user@test:~/test % chflags uappnd 1.txt


查看

user@test:~/test % ls -lo 1.txt
-rw-r--r-- 1 test wheel uappnd 211 Oct 27 10:20 1.txt


验证1,使用重定向命令,可以添加,不可以重写:

user@test:~/test % echo "ssdddff" > 1.txt
1.txt: Operation not permitted.
user@test:~/test % echo "ssdddff" >> 1.txt


验证2,删除,会提示无权限

user@test:~/test % rm 1.txt
override rw-r--r-- test/wheel uappnd for 1.txt? y
rm: 1.txt: Operation not permitted


验证3,php的file_put_contents函数,注意第三个参数,FILE_APPEND代表添加而不是覆写,先看添加模式,最后用tail命令输出最后一行来验证:

user@test:~/test % cat tt.php
<?php
file_put_contents('1.txt',"sdkfjsldkfsldf\n",FILE_APPEND );
?>
user@test:~/test % php tt.php
user@test:~/test % tail -n 1 1.txt
sdkfjsldkfsldf


覆写模式,提示权限不足:

user@test:~/test % cat t.php
<?php
file_put_contents('1.txt',"sdkfjsldkfsldf\n" );
?>
user@test:~/test % php t.php
PHP Warning: file_put_contents(1.txt): failed to open stream: Operation not permitted in /home/test/test/t.php on line 2


验证4,php的fopen函数:

w模式,提示权限不足:

user@test:~/test % cat tf.php
<?php
$fp = fopen('1.txt',"w" );
fclose( $fp );
?>
user@test:~/test % php tf.php
PHP Warning: fopen(1.txt): failed to open stream: Operation not permitted in /home/test/test/tf.php on line 2
PHP Warning: fclose() expects parameter 1 to be resource, boolean given in /home/test/test/tf.php on line 3


a模式:

user@test:~/test % cat ta.php
<?php
$fp = fopen('1.txt',"a" );
fwrite( $fp , "the fourth test!\n");
fclose( $fp );
?>
user@test:~/test % php ta.php
user@test:~/test % tail -n 1 1.txt
the fourth test!


c模式:

user@test:~/test % cat tc.php
<?php
$fp = fopen('1.txt',"c+" );
fwrite( $fp , "the fourth test!\n");
fclose( $fp );
?>
user@test:~/test % php tc.php
PHP Warning: fopen(1.txt): failed to open stream: Operation not permitted in /home/test/test/tc.php on line 2
PHP Warning: fwrite() expects parameter 1 to be resource, boolean given in /home/test/test/tc.php on line 3
PHP Warning: fclose() expects parameter 1 to be resource, boolean given in /home/test/test/tc.php on line 4


系统级append Only,普通用户也会提示权限不足

user@test:~/test % chflags sappnd 2.txt
chflags: 2.txt: Operation not permitted
user@test:~/test % su
Password:
root@mm:/home/test/test # chflags sappnd 2.txt
root@mm:/home/test/test # exit
user@test:~/test % ls -lo 2.txt
-rw-r--r-- 1 test wheel sappnd 289 Oct 27 10:50 2.txt


验证:

user@test:~/test % cat t.php
<?php
file_put_contents('2.txt',"sdkfjsldkfsldf\n" );
?>
user@test:~/test % php t.php
PHP Warning: file_put_contents(2.txt): failed to open stream: Operation not permitted in /home/test/test/t.php on line 2


该贴由hui.chen转至本版2014-11-5 17:04:52



赞(0)    操作        顶端 
总帖数
1
每页帖数
101/1页1
返回列表
发新帖子
请输入验证码: 点击刷新验证码
您需要登录后才可以回帖 登录 | 注册
技术讨论