Home > System Tutorial > LINUX > How to make a file readable, writable, and read-only under Linux?

How to make a file readable, writable, and read-only under Linux?

WBOY
Release: 2024-02-14 17:00:03
forward
797 people have browsed it

In the process of Linux operation and maintenance, we often encounter that the storage directory of an application is full, without any planning in advance, and the directory or disk cannot be expanded. At this time, our common solution is to make a soft link, through ln, soft link the originally stored directory to a directory in another large disk to achieve the purpose.

The disadvantage of soft links is that after multiple layers of soft links, they will be stunned, which can easily cause misoperation. Today I will introduce a method

mount –bind

The mount command operation and maintenance friends should be familiar with it, so I won’t introduce it in too much

Let’s first look at the introduction in man

How to make a file readable, writable, and read-only under Linux?

You can remount the file directory structure through mount –bind and connect two directories. It mounts the former directory to the latter directory. All access to the latter directory is actually access to the previous directory. Access to a directory

example

Let’s look at the example, create two different directories, and create different files in the two different directories

How to make a file readable, writable, and read-only under Linux?

View the inode of the file

How to make a file readable, writable, and read-only under Linux?

Mount test1 to test2 through mount –bind, and recheck the inode and directory contents

How to make a file readable, writable, and read-only under Linux?

You can see that the inode number and file are both test1

Then access and modify the files in the test2 directory. In fact, what is changed is the test1 directory. Under our test, create the file in the test2 directory

How to make a file readable, writable, and read-only under Linux?

After contacting mount, we will check the contents of the next two directories

How to make a file readable, writable, and read-only under Linux?

You can see that the test1 directory remains in the modified state, and the test2 directory files still exist and remain unchanged

principle

Take mount –bind test1 test2 as an example. When the mount –bind command is executed, Linux will record the directory entry of the mounted directory ( is also the block of the directory file and records the information of the lower-level directory) Shielding, that is, the lower-level path of test2 is hidden (Note that it is just hiding but not deleting, the data has not changed, it is just inaccessible). At the same time, the kernel records the directory entry of the mounting directory (test1) in an s_root object in the memory. When the mount command is executed, VFS will create a vfsmount object. This object contains all the mount information of the entire file system, among which It will also include the information in this mount. This object is a HASH value correspondence table (the HASH value is calculated by calculating the path string). The table contains the HASH value correspondence between the two directories /test1 to /test2

After the command is executed, when accessing files under /test2, the system will inform that the directory entry of /test2 is blocked, and automatically go to the memory to find VFS, and learn the corresponding relationship between /test2 and /test1 through vfsmount. , thereby reading the inode of /test1, so that all files read under /test2 are files in the /test1 directory

Notice

The corresponding relationship between the two directories exists in the memory. Once restarted, the mounting relationship will be gone, so the mounting relationship needs to be written to /etc/fstab

Advanced usage

  • Temporary configuration file reading test

    In some application scenarios, it is necessary to modify the configuration file for testing, but it cannot affect the running business. If you are not sure and are unwilling to modify the original configuration file directly, we can modify it in /tmp and other directories. , write a test configuration file, mount it to the configuration file reading directory through mount –bind, and run the program to read the temporary configuration file. After the test is completed, you only need to umount, which does not affect the original configuration

  • Read-only mount

    In some scenarios, for example, if you need to give developers permission to view some configurations, but do not allow them to modify the configurations, you can mount a read-only directory through mount –bind,ro. The original directory is writable and can be mounted The loaded directory is read-only. You only need to give the developer the read-only directory permission

The above is the detailed content of How to make a file readable, writable, and read-only under Linux?. For more information, please follow other related articles on the PHP Chinese website!

source:lxlinux.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template