关于sed -i 修改selinux 的软链接文件的问题

关于sed -i 修改selinux 的软链接文件的问题

 

http://blog.csdn.net/kumu_linux/article/details/8598005

 

因为sed -i /etc/sysconfig/selinux(selinux文件是/etc/selinux/config的软链接)配置文件重启SELINUX没有关闭,才发现原来sed -i是不能直接修改软链接文件的,如下我修改之后的后果:

 

  1. [root@node1 ~]# ll /etc/sysconfig/selinux   
  2. lrwxrwxrwx. 1 root root 19 2月  20 20:34 /etc/sysconfig/selinux -> /etc/selinux/config  
  3. [root@node1 ~]# sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/sysconfig/selinux  
  4. [root@node1 ~]# ll /etc/sysconfig/selinux   
  5. -rw-r--r-- 1 root root 457 2月  20 22:50 /etc/sysconfig/selinux  
  6. [root@node1 ~]#   


我们发现链接文件不再是链接文件了,后来查看sed man选项时发现如下选项说明

 

--follow-symlinks               follow symlinks when processing in place; hard links will still be broken.

-i[SUFFIX], --in-place[=SUFFIX]               edit  files  in  place (makes backup if extension supplied).  The default operation mode is to               break symbolic and hard links.  This can be changed with --follow-symlinks and --copy.

-c, --copy               use copy instead of rename when shuffling files in -i mode.  While this  will  avoid  breaking               links  (symbolic  or hard), the resulting editing operation is not atomic.  This is rarely the               desired mode; --follow-symlinks is usually enough, and it is both faster and more secure.

以上说明就不作过多解释了,说的很明显,看下面实例

 

  1. [root@node1 ~]# echo "test" >>test  
  2. [root@node1 ~]# ln -s ~/test ~/test_soft  
  3. [root@node1 ~]# ln ~/test ~/test_hard  
  4. [root@node1 ~]# ll -i test*  
  5. 271653 -rw-r--r-- 2 root root  5 2月  20 23:04 test  
  6. 271653 -rw-r--r-- 2 root root  5 2月  20 23:04 test_hard  
  7. 271655 lrwxrwxrwx 1 root root 10 2月  20 23:04 test_soft -> /root/test  
  8. [root@node1 ~]# sed -i "s/test/hard/g" test_hard   
  9. [root@node1 ~]# sed -i "s/test/soft/g" test_soft   
  10. [root@node1 ~]# ll -i test*  
  11. 271653 -rw-r--r-- 1 root root 5 2月  20 23:04 test  
  12. 271656 -rw-r--r-- 1 root root 5 2月  20 23:05 test_hard  
  13. 271657 -rw-r--r-- 1 root root 5 2月  20 23:06 test_soft  
  14. [root@node1 ~]#   

很明显如man中所说-i选项对软链接和硬链接都会使受到破坏

 

 

  1. [root@node1 ~]# rm -rf test*  
  2. [root@node1 ~]# echo "test" >>test  
  3. [root@node1 ~]# ln -s ~/test ~/test_soft  
  4. [root@node1 ~]# ln ~/test ~/test_hard  
  5. [root@node1 ~]# ll -i test*  
  6. 271653 -rw-r--r-- 2 root root  5 2月  20 23:08 test  
  7. 271653 -rw-r--r-- 2 root root  5 2月  20 23:08 test_hard  
  8. 271655 lrwxrwxrwx 1 root root 10 2月  20 23:08 test_soft -> /root/test  
  9. [root@node1 ~]# sed -i -c  "s/test/soft/g" test_soft   
  10. [root@node1 ~]# sed -i -c  "s/test/soft/g" test_hard   
  11. [root@node1 ~]# ll -i test*  
  12. 271653 -rw-r--r-- 2 root root  5 2月  20 23:11 test  
  13. 271653 -rw-r--r-- 2 root root  5 2月  20 23:11 test_hard  
  14. 271655 lrwxrwxrwx 1 root root 10 2月  20 23:08 test_soft -> /root/test  

--follow-symlinks选项只对软链接有效,硬链接还是会被破坏,建议使用-c选项,这里就不举例了

 

后来朋友在RHEL5上运行相同的操作居然没有出现类似的现象,运行结果如下:

 

  1. $ echo "test" >> test  
  2. $ ln -s ~/test ~/test1  
  3. $ ll ~/test1  
  4. lrwxrwxrwx 1 sxkj sxkj 15 02-21 13:26 /home/sxkj/test1 -> /home/sxkj/test  
  5. $ sed -i "s/test/test1/g" ~/test1  
  6. $ ll ~/test1  
  7. lrwxrwxrwx 1 sxkj sxkj 15 02-21 13:26 /home/sxkj/test1 -> /home/sxkj/test  

经查是sed的版本不同造成的影响,RHEL5系列的还是使用老版本的sed,没有--follow-symlinks类似的选项,笔者之前实验的版本是RHEL6.3,所以出现之前的一系列问题了

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值