如何在 Linux 中创建隐藏文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18852292/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create hidden files in Linux?
提问by stack_A
In my program I have to create a hidden file in order to avoid remove or modification of file.
在我的程序中,我必须创建一个隐藏文件以避免删除或修改文件。
PATH=/etc/
NAME = file
Is there is function in c which allow to do that ?
c中有没有允许这样做的功能?
Thanks.
谢谢。
采纳答案by Grammin
You can just add a .
to the front of the file name. Having said that if your goal is to not allow modification of the file change the permissions to something that can't be modified. Something like:
您可以.
在文件名的前面添加一个。话虽如此,如果您的目标是不允许修改文件,请将权限更改为无法修改的内容。就像是:
chmod 444 fileName
回答by Gangadhar
In LINUX Hidden file are start with .(DOT)
在 LINUX 中隐藏文件以 .(DOT)
if you create files with starting .(DOT)
, those files are hidden.
如果您使用开始创建文件.(DOT)
,则这些文件将被隐藏。
You can use chmod
to set permissions to the file.
您可以使用chmod
来设置文件的权限。
if you set only read only then those cannot be modified in program
如果你设置只读,那么那些不能在程序中修改
chmod 444 filename
if you want to use this from C-language use system()
function to execute this command
如果你想从 C 语言使用system()
这个函数来执行这个命令
if You use simple ls -alF
you can see those files.
如果你使用 simple ls -alF
你可以看到这些文件。
the below files are hidden files In LINUX
以下文件是LINUX中的隐藏文件
-rw------- 1 root root 27671 Sep 17 11:40 .bash_history
-rw-r--r-- 1 root root 3512 Jul 23 16:30 .bashrc
回答by hek2mgl
First: others argue with security arguments here. For those: Hidden files have nothing to do with security nor will it prevent somebody from deleting a file if he has propper permission and wants to do that.
第一:其他人在这里争论安全论点。对于那些:隐藏文件与安全无关,也不会阻止某人删除文件,如果他有适当的权限并想要这样做。
Hiddenmeans only that tools like ls
, bash globs or a graphical file managers will not display the files with their default settings. This can be useful to prevent from accidents (see explanation below) or just to keep directory listings more clean. You may try the commands ls -l $HOME
and ls -al $HOME
in order to see the differences.
隐藏仅意味着像ls
、bash globs 或图形文件管理器这样的工具不会显示具有默认设置的文件。这对于防止意外(见下面的解释)或只是为了保持目录列表更干净很有用。您可以尝试使用命令ls -l $HOME
并ls -al $HOME
查看差异。
On GNU/Linux systems and UNIXs it is by convention that files which's name begins with a dot .
will not being displayed by default meaning they are hidden. Like $HOME/.bashrc
在 GNU/Linux 系统和 UNIX 上,按照惯例,名称以点开头的文件.
默认不会显示,这意味着它们是隐藏的。喜欢$HOME/.bashrc
Solution: Prefix the file name with a dot:
解决方案:在文件名前加一个点:
.file
About accidents. Hiding a file can prevent you from accidently removing it when you type something like:
关于事故。隐藏文件可以防止您在键入以下内容时意外删除它:
rm *
The glob above will not list hidden files so they won't get deleted.
上面的 glob 不会列出隐藏文件,因此它们不会被删除。
回答by MKAROL
Sure,you have to add '.' before filename and your file wouldn't be seen by user(except user will turn the hidden files show option on). You could change the attrybutes (chmod) to 755 and only user could rwx and others could rx.
当然,你必须添加'.' 在文件名之前,用户不会看到您的文件(除非用户将打开隐藏文件显示选项)。您可以将属性 (chmod) 更改为 755,并且只有用户可以 rwx,其他人可以 rx。
回答by Maxim Egorushkin
There are no hidden files on Linux. Some tools don't show files starting with .
as others already mentioned.
Linux 上没有隐藏文件。某些工具不会显示.
以其他人已经提到的开头的文件。
Anyway, you can experiment with putting control characters like new-line into the filename. See Control characters in filenames are a terrible idea:
无论如何,您可以尝试将换行符等控制字符放入文件名中。请参阅文件名中的控制字符是一个糟糕的主意:
Some control characters, particularly the escape (ESC) character, can cause all sorts of display problems, including security problems. Terminals (like xterm, gnome-terminal, the Linux console, etc.) implement control sequences. Most software developers don't understand that merely displaying filenames can cause security problems if they can contain control characters. The GNU ls program tries to protect users from this effect by default (see the -N option), but many people display filenames without getting filtered by ls — and the problem returns. H. D. Moore's “Terminal Emulator Security Issues” (2003) summarizes some of the security issues; modern terminal emulators try to disable the most dangerous ones, but they can still cause trouble. A filename with embedded control characters can (when displayed) cause function keys to be renamed, set X atoms, change displays in misleading ways, and so on. To counter this, some programs modify control characters (such as find and ls) — making it even harder to correctly handle files with such names.
某些控制字符,尤其是转义 (ESC) 字符,会导致各种显示问题,包括安全问题。终端(如 xterm、gnome-terminal、Linux 控制台等)实现控制序列。大多数软件开发人员不明白,如果文件名可以包含控制字符,仅仅显示文件名会导致安全问题。默认情况下,GNU ls 程序试图保护用户免受这种影响(请参阅 -N 选项),但许多人显示文件名而没有被 ls 过滤——并且问题又回来了。HD Moore 的“终端仿真器安全问题”(2003 年)总结了一些安全问题;现代终端模拟器试图禁用最危险的终端模拟器,但它们仍然会引起麻烦。带有嵌入控制字符的文件名可以(当显示时)导致功能键被重命名,设置 X 原子,以误导性的方式更改显示,等等。为了解决这个问题,一些程序修改了控制字符(例如 find 和 ls)——这使得正确处理具有此类名称的文件变得更加困难。
回答by podwysoc
hek2mgl- partially yes - it has. Try to remove via rm -rf *
manner all of directory content. That's why for example .htaccess is hidden.
hek2mgl- 部分是 - 它有。尝试通过rm -rf *
方式删除所有目录内容。这就是为什么例如 .htaccess 被隐藏的原因。
回答by sjnarv
Your requirements are a bit vague: the program creates a file, wants to prevent its removal or modification. Do you expect other users (of your program? in general?) to be able to read it, but not find it easily, or modify or delete it?
您的要求有点模糊:程序创建了一个文件,想要防止其被删除或修改。您是否希望其他用户(您的程序?一般?)能够阅读它,但不容易找到它,或者修改或删除它?
Keep in mind that Unix-like systems don't really do hidden when the resource involved needs to remain visible (readable, presumably), as others have noted. Prepending a '.' to a file name helps in some important contexts (default ls(1)
behavior and shell *
globbing in particular) but only goes so far. But a few techniques might help obscure what and where your app is saving things, if that matters.
请记住,正如其他人所指出的那样,当所涉及的资源需要保持可见(可能是可读的)时,类 Unix 系统并不会真正隐藏。前置一个“.” 到文件名在一些重要的上下文中(特别是默认ls(1)
行为和 shell *
globbing)有帮助,但仅到此为止。但是,如果这很重要,一些技术可能有助于模糊您的应用程序保存内容的内容和位置。
Consider two users doing some shell commands like the following in a directory with its sticky bit set (say /tmp). (Sorry to not write C, but I think the scenario is easier to demonstrate out in the shell.)
考虑两个用户在设置了粘滞位的目录(例如 /tmp)中执行一些如下所示的 shell 命令。(抱歉没有写 C,但我认为这个场景更容易在 shell 中演示。)
As Bob:
作为鲍勃:
$ umask 066
$ mkdir /tmp/.hidden
$ umask 022
$ echo xyzzy > /tmp/.hidden/mysecret.txt
$ ls -la /tmp/.hidden
total 28
drwx--x--x 2 bob users 4096 Sep 17 11:19 .
drwxrwxrwt 27 root root 20480 Sep 17 11:26 ..
-rw-r--r-- 1 bob users 6 Sep 17 11:19 mysecret.txt
As Alice. Notice that attempts to search in /tmp/.hidden fail, but if she knows the name of a file in a directory with only execute but not read permissions set, she can read the file. She can't do much to mess with /tmp/.hidden, once it's properly created. If she'd been forced to guess the name of the secret file, that could also be a challenge depending on how the name is created.
作为爱丽丝。请注意,尝试在 /tmp/.hidden 中搜索失败,但如果她知道目录中的文件名,且仅设置了执行权限但未设置读取权限,则她可以读取该文件。一旦正确创建了/tmp/.hidden,她就无能为力了。如果她被迫猜测秘密文件的名称,这也可能是一个挑战,具体取决于名称的创建方式。
$ ls /tmp | grep hidden
$ ls -a /tmp | grep hidden
.hidden
$ file /tmp/.hidden
/tmp/.hidden: directory
$ ls /tmp/.hidden
ls: cannot open directory /tmp/.hidden: Permission denied
$ echo /tmp/.hidden/*
/tmp/.hidden/*
$ file /tmp/.hidden/mysecret.txt
/tmp/.hidden/mysecret.txt: ASCII text
$ cat /tmp/.hidden/mysecret.txt
xyzzy
$ rm -f /tmp/.hidden/mysecret.txt
rm: cannot remove '/tmp/.hidden/mysecret.txt': Permission denied
$ mv /tmp/.hidden /tmp/Hidden_No_More
mv: cannot move '/tmp/.hidden' to '/tmp/Hidden_No_More': Operation not permitted
$ rm -rf /tmp/.hidden
rm: cannot remove '/tmp/.hidden': Permission denied
In this scenario, the presence of the hidden directory can be obscured, but ls -a
reveals its name. Carefully chosen directory permissions prevent non-root and non-Bob users from listing or altering its contents. The use of a sticky-bit directory like /tmp prevents non-Bobs from renaming or removing the "hidden" directory. Anyone who knows the name of the "secret" file within the hidden directory can read it. But only Bob and root can change these "secret" files or the "hidden" directory.
在这种情况下,隐藏目录的存在可以被掩盖,但会ls -a
显示其名称。精心选择的目录权限可防止非 root 和非 Bob 用户列出或更改其内容。使用像 /tmp 这样的粘性位目录可以防止非 Bobs 重命名或删除“隐藏”目录。任何知道隐藏目录中“秘密”文件名称的人都可以阅读它。但是只有 Bob 和 root 可以更改这些“秘密”文件或“隐藏”目录。
You can do all the above in a C program; equivalents exist as library and system calls - see things like chmod(2)
, mkdtemp(3)
, umask(2)
, the mode argument to open(2)
, etc.
您可以在 C 程序中完成上述所有操作;等价物以库和系统调用的形式存在 - 请参阅诸如chmod(2)
、mkdtemp(3)
、umask(2)
、 的模式参数open(2)
等。
回答by Peter Schneider
If you use a kernel >= 3.11, you might want to try the O_TMPFILE
-flag. This kernel have been released on the 14.09.2013. Debian Jessie uses Kernel 3.16. so this feature should be available on all recent popular distributions.
如果您使用 >= 3.11 的内核,您可能想尝试使用O_TMPFILE
-flag。该内核已于 14.09.2013 发布。Debian Jessie 使用内核 3.16。所以这个功能应该适用于所有最近流行的发行版。
The news about this sounds promising. The file will be unreachable from the outside. No other process or may access this file .. neither read nor write. But the file will be lost as soon as the handle gets closed. Or link it to a regular file. But then, it will be accessible as any other file.
这方面的消息听起来很有希望。该文件将无法从外部访问。没有其他进程或可以访问此文件.. 既不读也不写。但是一旦句柄关闭,文件就会丢失。或将其链接到常规文件。但是,它将可以像任何其他文件一样访问。
If this is not an option for you (e.g. your file needs to be persistent): bad luck. There is no real "hidden" file in linux. You can hide your persistent files as secure as files on windows with the hidden attribute: prepend the name with a dot. As stated by others: ls -a
will show them nevertheless.
如果这不是您的选择(例如您的文件需要是持久的):运气不好。linux 中没有真正的“隐藏”文件。您可以使用 hidden 属性隐藏您的持久文件,就像 Windows 上的文件一样安全:在名称前加一个点。正如其他人所说:ls -a
尽管如此,还是会向他们展示。
Also, you can create a user specifically for your use and make the file read- and writable only for this user or put it in a folder, where only your user have rw-access. Other users may see this file but wont be able to access it. But if root
comes along and want to look into it, you have lost.
此外,您可以创建一个专门供您使用的用户,并使该文件仅对该用户可读和可写,或者将其放在一个文件夹中,其中只有您的用户具有 rw 访问权限。其他用户可能会看到此文件,但无法访问它。但是,如果root
出现并想要调查它,您就输了。