Logo Mengz's Space ...
  • Dark Theme
    Light Theme Dark Theme System Theme
Logo Inverted Logo
  • Tags
  • algorithm
  • ansible
  • aws
  • cicd
  • cloud
  • cluster
  • cncf
  • command
  • compose
  • contianer
  • cotainer
  • cpp
  • crontab
  • debug
  • docker
  • find
  • github
  • gitlab
  • gitops
  • hardware
  • hostname
  • k3s
  • kernel
  • kubectl
  • kubernetes
  • linux
  • mariadb
  • mqtt
  • network
  • nvidia
  • opensuse
  • rclone
  • sbom
  • security
  • shell
  • slack
  • systemd
  • terraform
  • terrafrom
  • ubuntu
  • virtualization
  • vpn
  • web
  • wechat
  • windows
  • wireguard
  • wsl
  • zypper
Hero Image
LINUX上创建不可删除文件

我们都知道在Linux上默认都会有一个名为root的超级用户,该用户可以修改系统上的任何文件和目录,那我们怎么创建一些不能被删除/修改的文件和目录呢? 那在Linux系统中有一个命令chattr可以用来修改文件和目录的属性,通过该命令就可以设置文件和目录不可删除,甚至包括root也不能操作。 创建不可删除的文件 例如,我们在系统上新建一个名为undeletable-file的文件,通过名了chattr设置其属性为不可修改的: ❯ echo "some contents" ❯ ~/undeletable-file ❯ sudo chattr +i -V ~/undeletable-file chattr 1.43.8 (1-Jan-2018) Flags of /home/mengz/undeletable-file set as ----i-------------- ❯ rm -f ~/undeletable-file rm: cannot remove 'undeletable-file': Operation not permitted ❯ sudo rm -f ~/undeletable-file rm: cannot remove 'undeletable-file': Operation not permitted ❯ echo "change" ❯❯ ~/undeletable-file bash: undeletable-file: Operation not permitted 注意 : 是用命令chattr修改属性的时候需要root权限,因此这里使用了sudo 。 我们也可以通过命令lsattr来查看当前文件的属性: ❯ lsattr ~/undeletable-file ----i-------------- /home/mengz/undeletable-file 设置目录不可修改 针对目录,同样是用命令chattr,是用-R选项可以递归地修改目录和其文件的属性:

August 24, 2019 Read
Navigation
  • 关于
  • 技能
  • 工作经历
  • 教育
  • 近期文章
  • 技能学习
  • LiveTerm
Contact me:
  • mengz.you@outlook.com
  • mengzyou
  • You Mengzhe

Toha Theme Logo Toha
© 2024 - Copyright.
Powered by Hugo Logo