df and du commands of linux disk management

 

df  View the total capacity, used capacity, remaining capacity, etc. of the mounted disk. You can add no parameters. The default is to display in units of k.

[image]

Common parameters of df are -i -h -k -m, etc.

-i use inodes to display results

[image]

-h display in appropriate units, e.g. G

[image]

-k -m respectively use K, M is the unit display

[image]

Briefly describe the relevant data you see. Filesystem means sectors, which is the area you divide the disk; 1K-blocks/1M-blocks means 1K/1M units; Used and Available are used and remaining respectively; Use% is the percentage that has been used, if If this value is greater than 90%, then you should pay attention, the disk is likely to become full soon; Mounted on means where the partition (sector) is mounted.

du  is used to view the space occupied by a directory

Syntax: du [-abckmsh] [file or directory name] Common parameters are:

-a: List all file and directory sizes. If no options and parameters are added, only the size of the directory (including subdirectories) is listed.

[image]

-b: The listed value is output in bytes, the default is Kbytes

[image]

-c: add up at the end

[image]

-k: output in KB

-m: output in MB

-s: only list sums

-h: The system automatically adjusts the unit. For example, if the file is too small, it may be a few K, so it is displayed in K. If it is a few G, it is displayed in G. The author is accustomed to using the form du –sh filename.

[image]

Partitioning and formatting of disks

What I often do is take a brand new disk and partition and format it. This also shows that as a Linux system administrator, you must be proficient in disk operations. So please study this part carefully.

Hard disk partition tool under fdisk linux

Syntax: fdisk [-l ] [device name]

-l : All disk devices and partition tables in the system will be listed directly without the device name, and the partition table of the device will be listed with the device name.

[image]

[image]

If -l is not added, another mode is entered, in which the disk can be partitioned.

[image]

When you just enter this mode, there will be a prompt Command (m for help): At this time, pressing m will print out the help list. If your English is good, I think it is not difficult for you to understand the functions of these letters. The author commonly used are p, n, d, w, q.

P: Print the partition status of the current disk.

[image]

n: Re-create a new partition.

w: save operation.

q: quit.

d: delete a partition

Because my linux system is installed on a virtual machine, I can add a new disk. Then I will divide the new disk into multiple partitions.

[image]

When looking at fdisk -l again, it is found that there is an additional /dev/hdb device, and it is prompted that there is no available partition table for this device. Then let's divide this /dev/hdb.

[image]

First check it with p, and there is no partition information.

[image]

Use n to create a new partition, you will be prompted to create e (extended partition) or p (primary partition), here the author chooses the primary partition, so after pressing p, enter the first cylinder again, or you can directly return Enter a number or enter a number, because this disk is new and does not have any partitions, so directly Entering is actually starting from 1. You can also customize the input, but it should not exceed 2080. I enter 1 and press Enter here. At this point, you will be prompted how much to divide, you can write a value (2-2080), or you can enter +sizeK or +sizeM, the latter is more intuitive and easy to understand, so I enter +100M here, that is, I divided a 100M main partition. When you use p to view it again, there really is an extra partition. Then the author continued to repeat the previous operation and established 4 primary partitions. When the author entered n again to create a partition, the result was wrong.

[image]

From this, you will find that you can only create up to 4 primary partitions in linux, so what if you want to create several more partitions? It's easy, after creating the third partition, choose extended partition when creating the fourth partition.

[image]First delete the fourth primary partition, and then create an extended partition

[image]

When creating an extended partition, you will be asked how much you want to allocate to this extended partition. The author directly presses Enter, that is, all the space is allocated to this extended partition. This extended partition /dev/hdb4 cannot write data into it, it is just an empty shell, we need to continue to create partitions in this empty shell.

[image]

When the extended partition is created, and then press n to create a new partition, you will find that you will no longer be prompted whether to create p or e, because we can no longer create p. What you need to understand here is that hdb5 is actually just a sub-partition in hdb4, and there are only 4 partitions available so far, so I will create a fifth partition.

[image]

Then press w to save, the mode will automatically exit, if you do not want to save the partition information, just press q to exit.

[image]

Next, we delete the newly divided partition and re-create the partition. How to delete you remember, by the way, just press d and select the appropriate number. After deleting all partitions, the disk is restored to its original state.

[image]

[image]

The first partition, we create an extended partition. And give it 200M.

[image]

When a new partition is created again, it changes, it is no longer p or e, but p or l (logical partition), why is this? As mentioned above, an extended partition is just an empty shell, and only small partitions can be divided under the extended partition. This small partition is actually a logical partition.

[image]

And this logical partition starts from the word number 5 by default, because the previous number is either reserved for the primary partition or the extended partition. From this, we can also get that up to 4 primary partitions can be created in linux, and once 4 primary partitions are created, no more partitions can be added. In addition, only one extended partition can be created at most. What is the maximum number of logical partitions that can be created under an extended partition? IDE hard disks (similar to hda, hdb, hdc, etc.) can create up to 10 (hdb5-hdb15), which is the result of the author's experiment. Some sources say that there is no limit to the logical partition under linux, and some say that the maximum can be 64. As for whether it is right or not, you need to investigate further. We do not need to study this issue in depth, and it is meaningless.

Through the above operations, I believe you have learned to use fdisk to partition it. It is worth mentioning that, don't play with partitions idle, the risk of this operation is very high, and all the data on your server will be lost if you are not careful. If there is a partition operation, then please keep 200% careful, remember to remember!

mkfs.ext2 / mkfs.ext3 /mke2fs  format linux hard disk partition

When you use man to query the help documents of these three commands, you will find that we see the same help document, which means that the three commands are the same. Common options are:

-b: Set the size of the space occupied by each data block when partitioning, currently supports 1024, 2048 and 4096 bytes per block.

-i: set inode size

-N: Set the number of inodes, sometimes using the default number of inodes is not enough, so you need to customize the number of inodes.

-c: Check the disk for problems before formatting, it will be very slow after adding this option

-L: Preset the label of the partition

-j: Create a partition in ext3 format, if you use mkfs.ext3, you don't need to add this option

[image]

Without any options, format /dev/hdb1 directly

[image]

In the above example, the block size is changed to 4096, the default is 1024, and the inode size is set to 4096.

The following example partitions the label (label) name of the custom partition.

[image]

e2label  is used to view or modify the label of the partition ( label )

This command is very simple. Follow the partition number directly to view the label of the partition. When you want to modify the label name, the partition number is followed by the desired label name.

[image]

fsck  check hard disk for bad sectors

Syntax: fsck [-Aar] [partition]

-A : When adding this parameter, there is no need to follow the partition name as a parameter. It will automatically check all partitions under the /etc/fstab file (this operation will be performed once during the boot process);

-a : Automatically repair the detected partitions with problems;

-r : Let the user decide whether to repair or not when a partition with bad sectors is detected.

[image]

When you use fsck to check the disk for bad sectors, it will prompt the user "running this task may cause some mounted file systems to be damaged", so this command should not be run lightly. Otherwise, there is really a problem, and the system can't even start.

[image]

Mount / Unmount Disk

The partitioning and formatting of the disk is mentioned in the above content, so how to use it after formatting? This involves mounting the disk. The formatted disk is actually a block device file with type b. Maybe you will think, since this block file is the partition, isn’t the data written directly in that file written to that partition? Of course not.

Before mounting a partition, you need to create a mount point, which appears in the form of a directory. Once a certain partition is mounted to this mount point (directory), then writing data to this directory will all be written to this partition. This requires you to pay attention to the fact that before mounting the partition, the mount point (directory) must be an empty directory. In fact, the fact that the directory is not empty does not affect the use of the mounted partition, but once it is mounted, the previous things in the directory cannot be seen. You can only see it after unmounting the partition.

mount  mount device

[image]

First create the /test1/test2 directory, and then create a 1.txt file in the /test1 directory.

[image]

Mount the /dev/hdb1 partition to the /test1 directory, and then view the /test1 directory and post it. 1.txt no longer exists. At this time, writing data to the /test1 directory will be written to the /dev/hdb1 partition. When talking about the -a option of mount, it is necessary for us to first understand the file /etc/fstab

[image]

This file is each partition that needs to be mounted when the system starts. The first column is the label of the partition; the second column is the mount point; the third column is the format of the partition; the fourth column is some mount parameters of mount, which will be introduced in detail later. You can directly write defaults; the number in the fifth column indicates whether it is backed up by dump. If yes, it is 1 here, otherwise it is 0; Both 1 and 2 indicate detection, and 0 indicates no detection. In Redhat, there is another argument for 1 and 2. The / partition must be set to 1, and only one 1 is allowed in the entire fstab. There is a priority argument here. 1 has a higher priority than 2, so check 1 first, and then check 2. If there are multiple partitions that need to be powered on, set them to 2. After 1 is checked, it will check 2 at the same time. Let's talk about the parameters commonly used in the fourth column.

async/sync: async means that it is not synchronized with the disk and memory. The system writes the memory data to the disk at regular intervals, while sync will synchronize the data in the memory and the disk from time to time;

auto/noauto : auto-mount/not auto-mount at startup;

default: set the mount definition according to the default value of most permanent file systems, it includes rw, suid, dev, exec, auto, nouser, async;

ro: mount with read-only permission;

rw: mount with readable and writable permissions;

exec/noexec : allow/disallow executable files to execute, but do not mount the root partition as noexec, then the system cannot be used, and even the mount command cannot be used. At this time, the only thing to do is to restart the system;

user/nouser: allow/disallow other users other than root to mount the partition, for security reasons, please use nouser;

suid/nosuid: allow/disallow partitions to have suid attributes, generally set nosuid;

usrquota : enable the user disk quota mode, the related content of disk quota will be introduced in the following chapters;

grquota : enable group disk quota mode;

After learning this /etc/fstab, we can modify this file by ourselves and add a line to mount the new partition. For example, I added a line like this

/dev/hdb1 /test1 ext3 defaults 0 0

Then the partition will be mounted when the system restarts.

After talking about /etc/fstab, we will continue to talk about this mount. If mout -a runs this command, it will mount all disk partitions that appear in /etc/fstab. So when you add a line to the /etc/fstab file, you can directly run mount -a to mount the line you added, so you don't need to restart.

You can use the mount -o option to remount a partition, specifying the options you want (ie those in the sixth column of fstab described above)

[image]

You see, if you use the ro option, you can't create a new file.

[image]

Remount it again and it will return to normal. If no other options are added, it is defaults.

The author has encountered such a situation in daily operation and maintenance work. A new bright disk is installed on a server, disk a (displayed as sdc on the server) and disk b (displayed as sdd on the server). The two disks were unplugged, then re-plugged, and the machine was restarted. As a result, the disk numbers were changed, a became sdd, and b became sdc (this is because the disk was inserted into the wrong slot), the problem is . Through the above study, you mount the disk through the partition name such as /dev/hdb1. If it was previously added to /etc/fstab, the wrong partition will be mounted after the system starts. So how to avoid this from happening?

The blkid  command is used to display the uuid of the disk partition. The uuid is actually a large string of characters. In the Linux system, each partition will have a unique uuid. Speaking of this, you are smart enough to think, yes, we will use this unique uuid to mount the disk partition.

[image]

This command is only used by the author to display the uuid, and there is no other purpose, so I will not introduce it in detail. Of course, you can also follow a certain partition after the command to display only the uuid of the partition.

[image]

You see, it's actually quite useful. So how to make it also boot up? It's very simple, just write the command to mount the disk just now to the /etc/rc.d/rc.local file. By the way, the author has not told you about the function of this rc.local file yet. Simply put, the commands in this file will be executed after the system starts. So as long as you want to run any commands after booting, write them down to this file.

[image]

In fact, this file is a shell script, and I will introduce it in a separate chapter in the future. The "#" at the beginning of the line means a comment, which means that the line does not take effect in this script. What command do you want the system to run after booting, write the command here. For example, the command I just mounted. You can do it like this:

[image]

Another commonly used option for mount is -t, which specifies the type of file system. For example, when mounting a floppy disk, you need to specify vfat, and when mounting a CD, you need to specify iso9660. However, in the author's experience over the years, the current system All of them intelligently identify the system format category of the partition to be mounted. In other words, you don't need to specify it, it will automatically judge.

[image]

umount  unmount device

Now that you have learned how to mount a device, then how to unmount a device, you need to use umount, this command is also very simple, it can be followed by the mount point or the partition name (/dev/hdb1 )

[image]

Sometimes you may encounter devices that are difficult to unmount, just like the U disk cannot be deleted under Windows. A particularly useful method to teach you is umount -l /dev/hdb1 , this -l option means forcibly unmounting, you Remember, it's very useful.

[ Create a swap file ]

I have been in touch with this swap since I installed the system. As mentioned earlier, it is similar to the virtual memory of Windows. When partitioning, the size is generally 2 times that of the memory. If your memory exceeds 4G, then it seems unnecessary for you to divide 8G. . 4G is enough for daily exchange. However, there will also be insufficient virtual memory. If you really encounter it, do you have to redistribute the disk? Of course not! Then we add a virtual disk out.

The basic idea is: create a swapfile  format it as swap format  enable the virtual disk

[image]

Use dd to create a 419M file /tmp/newdisk, where if represents which file to read from, /dev/zero is a unique 0 generator under linux, of represents which file to output to, bs is the block size, and count is the Define how many blocks there are.

[image]

The mkswap command is specially designed to format the partition in swap format. When using this command, you must read it clearly, otherwise you will cry if you format other partitions incorrectly.

[image]

free is used to view system memory and virtual memory usage, and the -m option is viewed in the form of M. You can see the current system. And swapon is to enable our new swap file. After enabling it, use free to check and find that there is an extra 400M.

[image]

We can also close enabled swap files with swapoff.

Disk Quota

Disk cooperation is actually to assign a certain amount of disk to each user, and only allow him to use the disk space within this amount. In the Linux system, it is a multi-user multi-tasking environment, so there will be many people sharing a disk. It is necessary to limit a certain amount of disk space for each user, so as to be fair.

In linux, the thing used to manage disk quotas is quota. If there is no quota on your linux, you need to install this package quota-3.13-5.el5.RPM (actually it doesn't matter what the version is, the key is this package). In practical applications, quota is restricted for the entire partition. If your /dev/hda3 is mounted in the /home directory, all directories in /home will be restricted.

This module is mainly divided into quota quotacheck quotaoff quotaon quotastats edquota setquota warnquota repquota these commands, the following will introduce these commands respectively.

quota is used to display the quota of a group or a user.

Syntax: quota [-guvs] [user,group]

-g : show the quota for a group

-u : Display a user's quota

-v : the meaning of the display

-s : select inod or hard disk space to display

 

quotacheck is used to scan the quota space of a disk.

Syntax: quotacheck [-auvg] /path

-a : scan all mounted disks with quota support

-u : scan a user's files and directories

-g : scan files and directories for a group

-v : show the scan process

-m : Force a scan

 

edquota is used to edit the quota value of a user or group.

Syntax: edquota [-u user] [-g group] [-t]

edquota -p user -u user

-u : edit a user's quota

-g : edit the quota of a group

-t : edit grace time

-p : copy a user or group's quta to another user or group

When running edquota -u user, the system will open a file, and you will see that there are 7 columns in this file, and their meanings are:

Filesystem: Disk partition, such as /dev/hda3

blocks : The disk capacity occupied by the current user in the current Filesystem, in Kb. Please do not modify this value.

soft/hard : The quota value of the current user in the Filesystem, soft refers to the minimum limit, which can be exceeded, but the disk capacity must be reduced below this value within the grace period. hard refers to the maximum limit, that is, it cannot exceed this value. When the user's disk usage is higher than the soft value, the system will warn the user, prompting him to reduce the usage space to below the soft value within the grace period.

inodes : The status of the currently used inodes, do not need to be modified.

 

quotaon starts quta. After editing the quota, it needs to be activated for quta to take effect.

Syntax: quotaon [-a] [-uvg directory]

-a : All set quotas are started

-u : start quota for a user

-g : start quota for a group

-s : display related information

 

quotaoff turn off quota

This command is commonly used in only one case. quotaoff -a closes all quotas

The above mentioned a lot of quota-related commands, then the author will teach you how to do this disk quota in practical applications. The entire execution process is as follows:

First make sure that your /home directory is mounted on a separate partition, just use df to view it. If not, you need to do it with me. Otherwise, this step can be omitted.

[image]

In the author's linux system, /home does not occupy a separate partition. So you need to mount the /home directory in a separate partition, because quota is for the partition to limit.

[image]

The author uses fdisk -l to check that there are currently 5 available partitions on the /dev/hdb disk, so the author intends to mount /dev/hdb1 in the /home directory

[image]

See, the author's /home directory is already a separate partition.

1) Create a test user

[image]

First create a test user, then create a test group at the same time. You can have a line starting with test in /etc/passwd, where both uid and gid are 500, and then create a test1 account to add it to the test group, and check the /etc/passwd file to find that the gids of test and test1 users are both is 500. (Maybe you are unfamiliar with the /etc/passwd file, adding a user, and concepts such as uid and gid. It doesn't matter. It will be introduced in later chapters. You only need to understand it here)

2) Turn on the quota function of the disk

By default, Linux does not support quota for any partition, so we need to manually turn on the quota function of the disk. Do you remember that this quota option (usrquota, grpquota) was mentioned when analyzing the fourth column of the /etc/fstab file in the previous content ). That's right, if you want to open quota support for this disk, you need to modify the fourth column. Edit /etc/fstab with vim and add a line, as shown below:

[image]

The vim command will be introduced in detail in the subsequent chapters. The previous section introduced how to enter the edit mode and how to save the file. If your linux system already has the /home line, then directly modify the fourth column, add usrquota, grpguota (no spaces in between). Next you need to remount /home.

[image]

Alternatively you can remount /home like this

[image]

How to check whether quota is enabled?

[image]

Just check whether there is usrguota or grpquota in the line where /home is located in the /etc/mtab file. The author's /dev/hdb1 now supports quota

3) Scan the user usage status of the disk, and generate important aquota.group and aquota.user

In this step, quotacheck is needed. aquota.group and aqouta.user are the configuration files required for group and user disk quota respectively. Without these two files, disk quotas will not take effect.

[image]

When the quotacheck command is used for the first time, it will prompt "cannot stat old user quota file..." Actually, this is to prompt you that there are no aquota.user and aquota.group files in the /home directory. It doesn't matter, because the disk quota has not been configured before, and of course these two files are no longer there. When the quotacheck command is executed, these two files will be generated in the /home directory.

4) Start quota quota

[image]

5) Edit user disk quota

First, set the quota of the test account, and then directly copy the quota of test to test1. Here you need to use edquota.

[image]

Modify the above to

[image]

The unit is Kb, so the soft value is about 20Mb, and the hard value is 30Mb. Save this file in the same way as a vim file.

[image]

Copy test's quota to test1. Continue to set the grace time below.

[image]

The default is 7days here we changed to 1days. Let's take a look at the test and test1 user quotas.

[image]

6) Edit group disk quota

[image]

Set the soft quota value of group test to 40M and the hard value to 50M. Check out the quota for group test below.

[image]

7) Set boot up

As mentioned earlier, the command to start disk quota is quotaon -aug, so if you want to start it, you only need to add this command to the /etc/rc.d/rc.local file.

[image]

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324601063&siteId=291194637