关于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,所以出现之前的一系列问题了

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

  1. Linux之SElinux安全上下文件(1)

    SELinux:Secure Enhanced Linux,是美国国家安全局(NSA=The National Security Agency)和SCC(Secure Computing Courpo ...

  2. sed 's/AA/BB/' file # 将文件中的AA替换成BB,只替换一行中第一次出现的AA,替换后的结果输出到屏幕 sed 's/AA/BB/g' file # 将文件中的所有AA都替换成BB,替换后的结果输出到屏幕

    生信人的自我修养:Linux命令速查手册 简佐义 ​ 四川大学 生物信息学硕士 科学求真 赢 10 万奖金 · 院士面对面 209 人赞同了该文章 许多人做生物信息学,要么不重视Linux,要么不知道 ...

  3. Tomcat编码问题及访问软链接文件设置

    Tomcat编码问题及访问软链接文件设置 一.编码问题:让其支持UTF-8格式 修改tomcat中server.xml Connector port=" protocol="org ...

  4. centos7 修改selinux 开机导致 faild to load SELinux policy freezing 错误

    centos7 修改selinux 开机导致 faild to load SELinux policy  freezing 错误 之前把selinux关闭了,这次想打开selinux,于是修改了 /e ...

  5. 特殊权限set_uid /特殊权限set_gid/特殊权限stick_bit/软链接文件/硬连接文件

    2.18 特殊权限set_uid 2.19 特殊权限set_gid 2.20 特殊权限stick_bit 2.21 软链接文件 2.22 硬连接文件 特殊权限set_uid(s权限用户user权限) ...

  6. Linux:自动获取静态IP地址,清空iptable,修改selinux脚本

    自动获取静态IP地址,清空iptable,修改selinux脚本 环境:VMware 平台:centos6.8全新 功能: 1)应用ifconfig -a,route -n,cat /etc/reso ...

  7. Linux CentOS7 VMware 特殊权限set_uid、特殊权限set_gid、特殊权限stick_bit、软链接文件、硬连接文件

    一.特殊权限set_uid root用户本身拥有对/etc/passwd的写权限,无可厚非:那普通用户呢,这里就用到了setuid,setuid的作用是“让执行该命令的用户以该命令拥有者的权限去执行” ...

  8. RHEL7 修改SSH默认端口及修改SELinux运行状态

    RHEL7安装后,默认开启SSH服务以便远程配置,但默认端口22并不安全,一般不建议使用默认端口,那就修改SSH默认端口.在sshd_config里面的修改RHEL7.0上修改和7.0以下类似,但要注 ...

  9. 修改目的端trail文件的最大大小--转载

    本文为原创,转载请注明出处: http://blog.csdn.net/msdnchina/article/details/38346435 修改目的端trail文件的最大大小. 本文产生的背景: 在 ...

随机推荐

  1. 关于NaN(Not a Number)的问题

    在游戏运行时,代码若写得不安全很容易出现NAN的异常.一旦NAN出现整个游戏不崩溃也坏死掉了,游戏上了则是要被直接打回来的节奏,更是一个开发及测试人员每人都要扣3000块的大BUG.   一般表现为: ...

  2. MathType初级教程:怎么安装MathType

    MathType 由美国Design Science公司开发,是一款功能强大的数学公式编辑器,它同时支持Windows和Macintosh 两种操作系统,有很好的兼容性,能够在各种文档中加入复杂的数学 ...

  3. ubuntu压缩

    .tar解包:tar xvf FileName.tar打包:tar cvf FileName.tar DirName(注:tar是打包,不是压缩!)-------------------------- ...

  4. 超全面的JavaWeb笔记day20<监听器&国际化>

    JavaWeb监听器 三大组件: l Servlet l Listener l Filter Listener:监听器 1. 初次相见:AWT 2. 二次相见:SAX 监听器: l 它是一个接口,内容 ...

  5. Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile)

    使用maven打包的时候出现如下错误: Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compil ...

  6. VS-Qt-OSG-CMake基本项目框架

    在VS-Qt-CMake基础上,打开mainwindow.ui,添加一个QWidget,然后在widget上右键-提升,选择SceneViewWidget CMakeLists.txt中添加了OSG相 ...

  7. IIC协议

    总线信号 :  SDA :串行数据线 SCL  :串行时钟 总线空闲状态 : SDA :高电平 SCL :高电平 起始位:SCL为高电平期间    SDA出现下降沿 终止位:SCL为高电平期间 SDA ...

  8. Path类和File类的应用

    今天是我学习C#基础的第13天,可以说马上就要结束这个基础课程,感觉学习的理论性的我不能说全部掌握了,我只想说在思路上面的语法以及用法我应该基本掌握了,感觉效果不错,不得不说,要想在一种语言上面有大的 ...

  9. HTML5怎么实现录音和播放功能

    小旋风柴进 html: [html] view plain copy <span style="white-space:pre"> </span><a ...

  10. PHP之语句

    前面的话 任何 PHP 脚本都是由一系列语句构成的.一条语句可以是一个赋值语句,一个函数调用,一个循环,一个条件语句或者甚至是一个什么也不做的语句(空语句).语句通常以分号结束.此外,还可以用花括号将 ...