I can’t help but be amazed by these practical tips for daily operation and maintenance~

need

As we accumulate more experience in operation and maintenance work, I wonder if you have encountered the following scenarios:

  • How to quickly delete a large number of files in Linux?

  • How does Vsphere identify the newly added iscsi hard disk without restarting?

  • How to prevent accidental deletion using the rm command in Linux?

  • How to achieve fast mounting between different Linux servers?

  • How to quickly add users to affiliated groups in Linux?

  • Careful people often handle things quickly in their own unique way, which makes people applaud.

Now let me introduce in detail how I handle it.

1.How to quickly delete a large number of files in Linux?

In Linux, when deleting a large number of small files or large files, we usually delete them directly through the rm command. At this time, you are likely to encounter the following problems:

  • The rm deletion process takes a long time and is inefficient;

  • After the rm deletion is complete, the file does not exist but the disk space has not been released;

In fact, the above problems are all rmrelated to the command, because when rm deletes content, it deletes (unlink) each entry in the directory one by one, and needs to cycle through it many times; although the unlink is lifted, the process can still read the deleted problem files. , resulting in the disk not being released.

Here we can use rsyncthe command instead of rm. When rsync deletes content, it replaces the old directory by creating a new empty directory, without requiring a lot of traversal operations, so the efficiency will be much higher.

rsync provides some parameters related to deletion as follows:

rsync --help | grep delete      --del                   an alias for --delete-during      --delete                delete files that don't exist on the sending side      --delete-before         receiver deletes before transfer (default)      --delete-during         receiver deletes during transfer, not before      --delete-after          receiver deletes after transfer, not before      --delete-excluded       also delete excluded files on the receiving side      --ignore-errors         delete even if there are I/O errors      --max-delete=NUM        don't delete more than NUM files

Quickly delete directories

1. 建立一个空目录    mkdir -p /del_tmp2. 需要清空的目标目录    /del_dest3. 如果目标目录下有大量的小文件,清空目录下文件    rsync --delete-before -a -H -v --progress --stats ./del_tmp/  ./del_dest/
选项说明:–delete-before 接收者在传输之前进行删除操作–progress 在传输时显示传输过程-a 归档模式,表示以递归方式传输文件,并保持所有文件属性-H 保持硬连接的文件-v 详细输出模式--stats 给出某些文件的传输状态

Delete files quickly

"Note:" When the SRC and DEST file properties are inconsistent, an error will be reported; when the SRC and DEST properties are both file [f], it means clearing the file content rather than deleting the file; when the SRC and DEST properties are both directory [d] When , it means to delete all the files in the directory, making it an empty directory;

2. How does Vsphere recognize the newly added iscsi hard disk without rebooting?


The Vsphere virtual machine adds a new hard disk for LVM expansion. At this time, it needs to restart and refresh the file system, otherwise the newly added hard disk cannot be recognized. Restarting the server is not allowed for a production environment, so is there any way to refresh the file system online?

There are two methods here:

the first method

The SCSI device parameters before adding a new hard disk are as shown in the figure

picture

​​​​​​​
如果新添加的硬盘为“Direct-Access”类型,因此我们需要执行命令,将Id+1后将是新磁盘的Id号:echo  'scsi add-single-device 2 0 1 0' > /proc/scsi/scsi

in:

  • HOST, host adapter identification, the first adapter is 0, the adapter of the newly added hard disk is 2;

  • Channel, the SCSI channel on the host adapter, the first channel is 00, the SCSI channel of the newly added disk is still 00;

  • ID, the SCSI identification of the device, that is, the hard disk identification starts from 00, and the identification of a newly added disk is 01;

The SCSI device parameters after adding the hard disk are as shown in the figure

picture

Second method :

# 刷新SCSI,需要根据hosts数量确认刷新次数。# 如果系统有3个scsi host,则需要刷新3次echo "- - -" > /sys/class/scsi_host/host0/scanecho "- - -" > /sys/class/scsi_host/host1/scanecho "- - -" > /sys/class/scsi_host/host2/scan

Both of the above methods can identify the newly added hard disk without restarting the server. Please choose according to the actual situation.

3. How to prevent accidental deletion of the rm command in Linux?


In the Linux system, the colon (:) is a built-in command in bash, not simply a separator. Its main functions are empty commands, parameter expansion, redirection, comments, etc. We can use its parameter expansion feature to implement the accidental deletion prevention function of rm. ​​​​​​​

格式:${parameter:-test}   功能:如果parameter没有设置或者为空,替换为test;否则替换为parameter的值。命令:rm -rf ${dest:-test}用法:当变量dest为空时,删除test;当变量dest不为空时,删除test用例:rm -rf /$dest。当变量dest没有设置或为空时,则命令变成rm -rf /,这将误删系统根目录,导致系统崩溃。改进:rm -rf /${dest:-test},当变量dest没有设置或为空时,会使用test代替,则命令变成rm -rf /test,删除此目录不会产生任何影响。

4. How to achieve fast mounting between different Linux servers?


Sharing between different Linux servers is generally achieved through NFS. However, once when a colleague was migrating ELK, he used the sshfs command to quickly realize data sharing based on the ssh protocol.

SSHFS (Secure SHell FileSystem) is a client that allows us to mount remote file systems through SSH File Transfer Protocol (SFTP) and interact with remote directories and files on the local machine.

The specific usage is as follows:​​​​​​​

# 1.安装 SSHFSyum install sshfs
# 2.创建 SSHFS 挂载目录mkdir /mnt/data
# 3.使用 SSHFS 挂载远程的文件系统sshfs [email protected]:/home/test/ /mnt/data
如果SSH有密钥授权,我们也可以使用公钥实现挂载sshfs -o IdentityFile=~/.ssh/id_rsa [email protected]:/home/test/ /mnt/data

As you can see, we quickly achieved the mount through the sshfs command, which is very convenient compared to NFS.

5.How to quickly add users to affiliated groups in Linux?


For the Linux operating system, we generally use the usermod command to adjust user groups, but there are pain points in the process of using it. ​​​​​​​

# 1.新增3个用户# useradd test1uid=508(test1) gid=508(test1) groups=508(test1)# useradd test2uid=509(test2) gid=509(test2) groups=509(test2)# useradd test3uid=510(test3) gid=510(test3) groups=510(test3)
# 2.调整test1 附加组# usermod -G test2 test1uid=508(test1) gid=508(test1) groups=508(test1),509(test2)# usermod -G test3 test1uid=508(test1) gid=508(test1) groups=508(test1),510(test3)# usermod -G test2,test3 test1uid=508(test1) gid=508(test1) groups=508(test1),509(test2),510(test3)

Through the above command, we can see the following pain points:

  • Each usermod additional group adjustment will overwrite the original group

  • When usermod adjusts multiple groups, all groups must be added together, otherwise the original group will be overwritten

So is there a command that allows us to directly update the group without knowing the original group information in advance? gpasswd can solve our pain point problem. It is a management tool for workgroup files /etc/group and /etc/gshadow under Linux. It is used to add a user to the group or delete it from the group.

# 1.将test1添加到test2分组# gpasswd -a test1 test2uid=508(test1) gid=508(test1) groups=508(test1),509(test2)# 2.将test1添加到test3分组# gpasswd -a test1 test3uid=508(test1) gid=508(test1) groups=508(test1),509(test2),510(test3)

We generally do not encounter situations where users belong to multiple groups at the same time, but when doing file sharing, it is used due to the requirement of permission separation. At this time, it is much more convenient for us to use the gpasswd command.

Summarize

During the operation and maintenance process, we can solve most problems through routine operations, but there may be problems such as low efficiency and troublesome operations during the processing. Therefore, we need to explore more and summarize more in our daily work, which may bring us unexpected results.

Guess you like

Origin blog.csdn.net/LinkSLA/article/details/132759058