linux tricks – ExpLinux https://www.explinux.com Explore Linux How to, Tutorials, Unix, Updates, Technology. Tue, 01 Aug 2023 19:01:34 +0000 en-US hourly 1 https://wordpress.org/?v=6.4.4 https://www.explinux.com/wp-content/uploads/2023/04/cropped-favicon-32x32-1-32x32.png linux tricks – ExpLinux https://www.explinux.com 32 32 100 Best Use Cases of Find Command in Linux -ExpLinux https://www.explinux.com/2023/02/100-use-of-find-command-in-linux.html https://www.explinux.com/2023/02/100-use-of-find-command-in-linux.html#respond Mon, 20 Feb 2023 20:05:50 +0000 https://explinux.online/?p=794 100 Best Use Cases of Find Command in Linux -ExpLinux Read More »

]]>
Find Command In Linux
Find Command in Linux
Find Command in linux

The very popular Linux admins friend find command in Linux was created as a part of the Unix operating system by Ken Thompson and Dennis Ritchie at Bell Labs in the golden era of new computer development in the 1970s. Thompson and Ritchie were two of the pioneer developers of the Unix operating system and also developed the mother of most programming language C programming language, C is mostly used for developing software for Unix-based systems. The find command in Linux and Unix operating systems is still widely used today for searching for files and directories on a system and performing operations on them.

Some Alternatives of Find Command

Related Post: MV Command

22 Most Useful Linux Commands

15 Linux Commands to Boost Productivity

Master Guide to Vi Editor

There are only locate is main alternatives to the find command in Linux, which gives it some competition but we have added 4 more alternatives that can be useful in specific tasks.

locate: The locate command is the fastest way to search for files and directories on a Linux system. Unlike the find command, locate command creates its database of file paths that are generated periodically, so it provides faster results than find but it may not always be up to date. It will not come default in the system we have to install it and before first use has to update the Database of locate command. Update duration will depend upon your system file number.

grep: The grep command is an awesome tool for searching for patterns in text files. It can also be used to search for files and directories by name, although it is not as versatile as the find command in Linux because it searches only in text files.

whereis: The whereis command in Linux is used to locate the binary, source, and manual page files for a specified command. It is a simple but useful tool that provides basic information about a command, including its all locations on the system. for example command may be in /bin/ /sbin/ or /usr/bin/ it will show all.

which: The which command is in Linux is also used to locate the binary for a specified command. Unlike whereis, it only provides the location (bin /usr/bin, etc) of the executable file for the command for your bash profile and does not provide information about its source code or manual pages.

findmnt: The findmnt command in Linux is used to find and display information about mounted file systems on a Linux and Unix system. It provides information about the mounted file system type, source, and target mounted on for each mounted file system.

These are some alternatives but the find command is still on top because it gives you real-time all details of the file and provides the ability to perform operations even in the script you can use the find command to get currently accurate results and perform operations on the right file.

Top 100 Use Cases of Find Command

Please find below examples

SNUseCommand
1Find files in a specific directory:find /path/to/directory -type f
2Find directories in a specific directory:find /path/to/directory -type d
3Find files with a specific name:find /path/to/directory -name “file_name”
4Find files with a specific extension:find /path/to/directory -name “*.extension”
5Find files based on their size:find /path/to/directory -size +10M
6Find files based on their modification time:find /path/to/directory -mtime +7
7Find files based on their access time:find /path/to/directory -atime +7
8Find files based on their inode number:find /path/to/directory -inum 123456
9Find files that are readable:find /path/to/directory -perm -444
10Find files that are writable:find /path/to/directory -perm -222
11Find files that are executable:find /path/to/directory -perm -111
12Find files that are owned by a specific user:find /path/to/directory -user user_name
13Find files that are owned by a specific group:find /path/to/directory -group group_name
14Find files with a specific user ID:find /path/to/directory -uid 123456
15Find files with a specific group ID:find /path/to/directory -gid 123456
16Find files that match a specific pattern:find /path/to/directory -regex “.*pattern.*”
17Find files that contain a specific word or phrase:find /path/to/directory -type f -exec grep -q “word or phrase” {} \; -print
18Find files that are symbolic links:find /path/to/directory -type l
19Find files that are hard links:find /path/to/directory -type f -links +1
20Find files that are empty:find /path/to/directory -empty
21Find files that are not empty:find /path/to/directory ! -empty
22Find files and execute a specific command on each file:find /path/to/directory -type f -exec command {} \;
23Find files and move them to a new location:find /path/to/directory -type f -exec mv {} /path/to/new/location \;
24Find files and delete themfind /path/to/directory -type f -exec rm -i {} \;
25Find files and copy them to a new location:find /path/to/directory -type f -exec cp {} /path/to/new/location \;
26Find files and change the permissions on each file:find /path/to/directory -type f -exec chmod 755 {} \;
27Find files and change the owner of each file:find /path/to/directory -type f -exec chown user:group {} \;
28Find files and change the group of each file:find /path/to/directory -type f -exec chgrp group_name {} \;
29Find files and print the full path of each file:find /path/to/directory -type f -printf “%p\n”
30Find files and print the size of each file:find /path/to/directory -type f -printf “%s\n”
31Find files and print the inode number of each file:find /path/to/directory -type f -printf “%i\n”
32Find files and print the modification time of each file:find /path/to/directory -type f -printf “%T@\n”
33Find files and print the user name of the owner of each file:find /path/to/directory -type f -printf “%u\n”
34Find files and print the group name of the owner of each file:find /path/to/directory -type f -printf “%g\n”
35Find files and print the permissions of each file:find /path/to/directory -type f -printf “%M\n”
36Find files and execute a command with a specific time limit:find /path/to/directory -type f -mmin +5 -exec command {} \;
37Find files and execute a command only if the file has been modified in the last 5 minutes:find /path/to/directory -type f -mmin -5 -exec command {} \;
38Find files and execute a command only if the file has not been modified in the last 5 minutes:find /path/to/directory -type f ! -mmin -5 -exec command {} \;
39Find files and search for a specific string in each file:find /path/to/directory -type f -exec grep “string” {} \;
40Find files and list only the first 10 files that match a specific criteria:find /path/to/directory -type f -print | head -10
41Find files and list only the last 10 files that match a specific criteria: find /path/to/directory | head 10
42Find files and exclude a specific directory from the search:find /path/to/directory -type f -not -path “/path/to/directory/to/exclude/*” -print
43Find files and exclude multiple directories from the search:find /path/to/directory -type f \( -not -path “/path/to/directory/to/exclude/*” -and -not -path “/path/to/directory/to/exclude2/*” \) -print
44Find files and search for files with a specific name pattern:find /path/to/directory -type f -name “*.txt”
45Find files and search for files with a specific size:find /path/to/directory -type f -size +100M
46Find files and search for files smaller than a specific size:find /path/to/directory -type f -size -100M
47Find files and search for files larger than a specific size:find /path/to/directory -type f -size +100M
48Find files and search for files of an exact size:find /path/to/directory -type f -size 100M
49Find files and search for files with a specific user owner:find /path/to/directory -type f -user user_name
50Find files and search for files with a specific group owner:find /path/to/directory -type f -group group_name
51Find files and search for files with a specific type of permissions:find /path/to/directory -type f -perm 644
52Find files and search for files that are writable:find /path/to/directory -type f -perm -222
53Find files and search for files that are readable:find /path/to/directory -type f -perm -444
54Find files and search for files that are executable:find /path/to/directory -type f -perm -111
55Find files and search for files with specific type of executability:find /path/to/directory -type f -perm /u=x,g=x
56Find files and search for files that have changed in the last 7 days:find /path/to/directory -type f -ctime -7
57Find files and search for files that have changed more than 7 days ago:find /path/to/directory -type f -ctime +7
58Find files and search for files that have been modified in the last 7 days:find /path/to/directory -type f -mtime -7
59Find files and search for files that have been modified more than 7 days ago:find /path/to/directory -type f -mtime +7
60Find files and search for files that have been accessed in the last 7 days:find /path/to/directory -type f -atime -7
61Find files and search for files that have not been accessed in the last 7 days:find /path/to/directory -type f -atime +7
62Find files and search for files that match multiple criteria:find /path/to/directory -type f \( -name “*.txt” -or -name “*.pdf” \) -and -size +100M
63Find files and delete the files that match certain criteria:find /path/to/directory -type f -name “*.bak” -delete
64Find files and print the output in a specific format:find /path/to/directory -type f -printf “%p %u %g %s\n”
65Find files and execute a specific command on the matching files:find /path/to/directory -type f -exec chmod 644 {} \;
66Find files and limit the number of results to display:find /path/to/directory -type f -print | head -10
67Find files and sort the results by file size:find /path/to/directory -type f -printf “%s %p\n” | sort -nr
68Find files and exclude specific files or directories from the results:find /path/to/directory -type f \( ! -name “*.bak” ! -path “/path/to/directory/to/exclude/*” \) -print
69Find files and search for files based on their inode number:find /path/to/directory -type f -inum 123456
70Find files and search for files based on their access time and modification time:find /path/to/directory -type f -anewer file.txt -or -mnewer file.txt
71Find files and search for files with a specific type of type:find /path/to/directory -type l -ls
72Find files and search for files with a specific type of type and perform an action on them:find /path/to/directory -type l -exec ls -l {} \;
73Find files and search for files based on the number of links:find /path/to/directory -type f -links +5
74Find files and search for files based on the number of links and perform an action on them:find /path/to/directory -type f -links +5 -exec ls -l {} \;
75Find files and search for files based on the type of file system they are on:find /mnt/c/ -type f -fstype xfs
76Find files and search for files with a specific owner:find /path/to/directory -type f -user username
77Find files and search for files with a specific group:find /path/to/directory -type f -group groupname
78Find files and search for files with a specific permissions:find /path/to/directory -type f -perm 0644
79Find files and search for files with a specific name and perform a certain action on them:find /path/to/directory -type f -name “*.log” -exec gzip {} \;
80Find files and search for files that match a certain pattern and perform a certain action on them:find /path/to/directory -type f -regex “.*\.\(jpg\|jpeg\|png\)” -exec cp {} /path/to/destination/ \;
81Find files and search for files that have been modified in the last 7 days and perform a certain action on them:find /path/to/directory -type f -mtime -7 -exec rm {} \;
82Find files and search for files that have a specific size and perform a certain action on them:find /path/to/directory -type f -size +100M -exec mv {} /path/to/destination/ \;
83Find files and search for files that have a specific size range and perform a certain action on them:find /path/to/directory -type f -size +10M -size -100M -exec cp {} /path/to/destination/ \;
84Find files and search for files that have a specific size range and print the results in a specific format:find /path/to/directory -type f -size +10M -size -100M -printf “%p %u %g %s\n”
85Find files and search for files that have a specific name and move them to a specific location:find /path/to/directory -type f -name “*.log” -exec mv {} /path/to/destination/ \;
86Find files and search for files that have a specific name and copy them to a specific location:find /path/to/directory -type f -name “*.log” -exec cp {} /path/to/destination/ \;
87Find files and search for files that have a specific name and count the number of matches:find /path/to/directory -type f -name “*.log” | wc -l
88Find files and search for files that have a specific name and delete the matches:find /path/to/directory -type f -name “*.log” -delete
89Find files and search for files that have a specific name and rename the matches:find /path/to/directory -type f -name “*.log” -exec mv {} {}.bak \;
90Find files that have been modified in the last day and delete them:find /path/to/directory -type f -mtime -1 -delete
91Find files that have been modified in the last 7 days and compress them:find /path/to/directory -type f -mtime -7 -exec gzip {} \;
92Find files that have a specific name and show the details in a human-readable format:find /path/to/directory -type f -name “*.log” -exec stat -c “%n %s %y” {} \;
93Find all symbolic links and delete them:find /path/to/directory -type l -delete
94Find all symbolic links and replace them with their targets:find /path/to/directory -type l -exec sh -c ‘ln -sf “$(readlink -f “$1″)” “$1″‘ _ {} \;
95Find all symbolic links that point to a specific location and delete them:find /path/to/directory -type l -lname “/path/to/link/target” -delete
96Find all files that are larger than a specific size and delete them:find /path/to/directory -type f -size +100M -delete
97Find mp3 files in ext4 filesystemfind /mnt/usb/ -type f -fstype ext4 | grep -i ‘\.mp3$’
98Find files that have been modified in the last hour and move them to a different location:find /path/to/directory -type f -mmin -60 -exec mv {} /path/to/destination/ \;
99Find commnad in linux with sed command . Find file and replace text mylinux to explinuxfind my_files -type f -name ‘*.txt’ -exec sed -i ‘s/mylinux/explinux/g’ {} \;
100Find process pid status and print from proc find /proc/1234 -maxdepth 0 -name status -o -name stat -exec cat {} \;
Find Command in Linux – explinux

Feel free to provide you advice and a new or effective way to use the find command in Linux.

Conclusion:

Here we have seen 100 use cases of the find command in Linux and this can be used in a script or in daily use of system admin and troubleshooting tasks using the find command.

]]>
https://www.explinux.com/2023/02/100-use-of-find-command-in-linux.html/feed 0
How To Enable X11 Forwarding in RHEL 8 / Centos 8 https://www.explinux.com/2020/07/how-to-enable-x11-forwarding-in-rhel-8-centos-8.html https://www.explinux.com/2020/07/how-to-enable-x11-forwarding-in-rhel-8-centos-8.html#respond Tue, 07 Jul 2020 03:00:00 +0000 https://explinux.online/2020/07/07/how-to-enable-x11-forwarding-in-rhel-8-centos-8/ How To Enable X11 Forwarding in RHEL 8 / Centos 8 Read More »

]]>
How To Enable X11 Forwarding in RHEL 8 / Centos 8

How To Enable X11 Forwarding in RHEL 8 / Centos 8

X11 is very important for a sysadmin because some applications only install or run on GUI. This always happen in case of third party applications. So in this article, we are talking about how to use x11 forwarding.
This is an awesome tool for every sysadmin who is working remotely via putty and need to work or install GUI applications.
Mostly cases we use it on java and KVM applications with the help of x11. 
If you are using it on the remote machine do not set any environment variable. Just enable x11 forwarding in ssh setting in putty and follow the below steps.
Putty Setting for x11

What is X11?

X11 is the final or 11th version of x windows system .Mostly places we see x window and in Linux we see x11. 
In this system, a program or we can say, x client display graphics on our localhost or windows computer and run on the remote machine. And receives its input from the screen for the remote machine. 
In Linux when we are working on the remote machine via putty and we required to work on any graphical application we use x11 forwarding on the Linux machine.

X11 Forwarding is Secure?

 The secure shell has the feature of x11 forwarding. So our all data travel from ssh to localhost. Then we can say x11 forwarding is secure as ssh connection is secure.

How To Enable X11 Forwarding in Linux?

Step-1 Install requires packages 

# yum install xterm xorg-x11-xauth xorg-x11-fonts-* xorg-x11-utils -y

Step-2 Change the ssh configuration file for X11

We need to do changes to access the GUI application over ssh. Search Address family and unhash and change it as below:
#Port 22
AddressFamily inet
#ListenAddress 0.0.0.0
#ListenAddress ::
Search X11Forwarding  and unhash it. Do not change any other setting or environment variable.
X11Forwarding yes

Step-3 Restart ssh 

Restart ssh daemon for apply changes 
# systemctl restart sshd

Step-4 You need an Application 

You need to install xming or any application where putty forward x11 requests and display graphics.
You can check by type xterm application.
# xterm 
This will open the application in GUI mode in your x display application 
xterm
Now at this point, you have successfully configured x11 to use the graphical application on putty.
Share the article if found helpful.
]]>
https://www.explinux.com/2020/07/how-to-enable-x11-forwarding-in-rhel-8-centos-8.html/feed 0
How to Check and Disable Selinux in Centos7/8, RHEL 7/8 https://www.explinux.com/2020/06/how-to-check-and-disable-selinux-in-centos7-8-rhel-7-8.html https://www.explinux.com/2020/06/how-to-check-and-disable-selinux-in-centos7-8-rhel-7-8.html#comments Tue, 30 Jun 2020 03:00:00 +0000 https://explinux.online/2020/06/30/how-to-check-and-disable-selinux-in-centos7-8-rhel-7-8/ How to Check and Disable Selinux in Centos7/8, RHEL 7/8 Read More »

]]>
How to Check and Disable Selinux  in Centos7/8, RHEL 7/8

How to check and disable Selinux  in Centos7/8, RHEL 7/8

SELinux – Security-Enhanced Linux

It is a security architecture that gives you more control over the system that others can access. It was integrated into Linux in 2003. SELinux is enabled by default and you can check SELinux status with the command sestaus. SELinux usage on AVC – access vector cache. You can configure SELinux for any subject or object.
Here in this article, we will talk about how to disable SELinux. As recommended do not disable it in the public domain if you do not have a firewall at the network level.
There are three modes of SELinux :
#     enforcing stand for – SELinux security policy is enforced.
#     permissive stands for –  SELinux prints warnings instead of enforcing.
#     disabled stand for – No SELinux policy is loaded.
Related Article:

How To Disable SELinux

Step 1- Check the Status with the below command

#  sestatus
sestatus
Now you can see in the above pic SELinux is in enforcing status. This means SELinux is active.

Step 2- Disable SELinux from the below command and check the status

# setenforce 0
setenforce 0
This will disable SELinux temporarily. For permanent you need to do an entry in the configuration file.

Step 3-  Make changes in the configuration file for permanent SELinux disable

There are two configuration files that we have to change to disable or permissive as per our requirement.

File -1 – Change by the below command

# vi /etc/sysconfig/selinux
/etc/selinux/config
Change permissive to disabled on SELINUX= *. This will disable SELinux permanently.

File -2 – Change by the below command

# vi /etc/selinux/config
/etc/selinux/config
Do the same as done in file -1. Save both files by (pressing the esc key):wq! Press enter.

4- Check now it will show disables in the config file.

# sestatus
SELinux disabled
Now at this point, you have successfully disabled SELinux and now you can check your services that are impacted by SELinux policy. Thanks, you can share this with your colleagues and request new articles in the comment section.
]]>
https://www.explinux.com/2020/06/how-to-check-and-disable-selinux-in-centos7-8-rhel-7-8.html/feed 1
How to Use and Enable/Disable Firewall in Centos7/8, RHEL 7/8 https://www.explinux.com/2020/06/how-to-check-and-disable-firewall-in-centos7-8-rhel-7-8.html https://www.explinux.com/2020/06/how-to-check-and-disable-firewall-in-centos7-8-rhel-7-8.html#respond Sun, 28 Jun 2020 03:00:00 +0000 https://explinux.online/2020/06/28/how-to-check-and-disable-firewall-in-centos7-8-rhel-7-8/ How to Use and Enable/Disable Firewall in Centos7/8, RHEL 7/8 Read More »

]]>
How to Disable firewall in Linux

How to Check and Disable Firewall  in Centos7/8, RHEL 7/8

As we know the firewall is very important for our system or server security. In some cases where you have an external firewall in your network or you are using a different firewall, you need to disable the firewall. Few sysadmins disable the firewall for any application too but we recommend that for a case of application configure the firewall, do not disable it.
In this article, we will see how to check and disable the firewall in Linux.

Disable Firewall :

Step -1 Check firewall status with the below command

 

#  systemctl status firewalld
systemctl status firewalld
This will show the current status of the firewall. Now it is on active showing.

Step -2 Disable the firewall with below command

#  systemctl stop firewalld
This command stops the firewall temporarily but if you reboot or reload it will be active again.

Step – 3 Permanent disable it with the below command

#  systemctl disable firewalld
systemctl disable firewalld
This will disable the firewall permanently. After this, you can manually start firewall.

Step -4 Check firewall status with the below command

#  systemctl status firewalld
systemctl status firewalld dead
Now you have successfully disabled the firewall of your Linux machine.
In case you want to Enable Firewall again. Follow the below procedure :

Enable Firewall

Step -1 Start firewall with below command

#  systemctl start firewalld
This will start firewall temporarily if it is disabled using disable command

Step -2 Enable the firewall permanently with the below command

 #  systemctl enable  firewalld
This will enable the firewall permanently.

Add a Port or Service  in the Firewall

 

# firewall-cmd --zone=public --add-port=8080/tcp --permanent
# firewall-cmd --zone=public --add-service=tomcat --permanent
# firewall-cmd --reload
This will add ports and services to firewall
At this point, you know how to disable and enable the firewall on RHEL 7/8  and Centos 7/8. Share this if it is helpful.
]]>
https://www.explinux.com/2020/06/how-to-check-and-disable-firewall-in-centos7-8-rhel-7-8.html/feed 0
How To Open/Extract ISO File in Linux https://www.explinux.com/2020/06/how-to-open-extract-iso-file-in-linux.html https://www.explinux.com/2020/06/how-to-open-extract-iso-file-in-linux.html#respond Fri, 26 Jun 2020 03:00:00 +0000 https://explinux.online/2020/06/26/how-to-open-extract-iso-file-in-linux/ How To Open/Extract ISO File in Linux Read More »

]]>
Extract ISO File

How To Open/Extract ISO File in Linux

In this article, we will see how to open an ISO file and extract or copy data from it.
Here we take an example of the RHEL ISO image in your case it can be different.
iso
make sure the correct permission of iso file because if your user does not have permission to mount then you will get an error message. So as recommended do this as a sudo or root user.

Steps to open/extract ISO File:

Step -1: Create a temporary mount point or you can use /mnt also for this.

#mkdir /isomount
This will create a directory where you can mount ISO 

Step – 2: Mount ISO on the created directory with the below command.

# mount -o loop rhel-server-7.8-x86_64-dvd.iso /isomount
mount -o loop

Step -3: Check directory correctly mounted on Linux OS by below command 

# df -h 
df -h
If you see your mount point here means successfully mounted the iso. 

Step -4: Go to the directory and now you can see data of directory or copy.

# cd /isomount
# ll 
cd and ll command
Here you can see all file which in ISO. 
At this point, you have successfully extracted ISO file and now you can copy and edit files. 
Note: Because this is an iso file so you can not edit anything here you have to copy on another location to edit.
There are many other ways also but the easiest and fast method is this one. You can use tools too and if you know a more simple method comment below.
]]>
https://www.explinux.com/2020/06/how-to-open-extract-iso-file-in-linux.html/feed 0
DU vs DF – Why du and df show different results in Linux https://www.explinux.com/2020/06/du-vs-df-why-du-and-df-show-different-results-in-linux.html https://www.explinux.com/2020/06/du-vs-df-why-du-and-df-show-different-results-in-linux.html#respond Wed, 24 Jun 2020 03:00:00 +0000 https://explinux.online/2020/06/24/du-vs-df-why-du-and-df-show-different-results-in-linux/ DU vs DF – Why du and df show different results in Linux Read More »

]]>
df vs du

DU vs DF – Why du and df show different results in Linux?

This is a common problem and question of every sysadmin that why they got different results in du and df. Mostly sysadmin relies on du over df for accurate results. In this article, we learn some basic facts about df and du to help you choose which one better and where to use.

Basic Definition:

df(Disk Free):

df reads disk blocks directly in the filesystem metadata and its trust on the superblock information . df reads the deleted file information also which is open in the running process. This always happens in case of big directory deletion which is used by large audiences. This command reads how much disk is totally free and trust filesystem metadata. This is the reason it gives you faster results.

df -h

du(Disk Usage): 

du reads every file in the directory and read their size. If any file deleted and open in the running process, it will not take any reference of the deleted file. du relies on which file currently in the directory and what is actual size. This is the reason it is slower than the df.

du -h

Conclusion: 

We can say df work on the filesystem and on the other hand du work on files. 

When to use df or du?

DF-

1- Use df in the new system both will show the same results because there is no delay or more running processes.
2- For daily operations use df because it is much faster and we required an overview of space
3- scripts where you create small or fewer files for a faster solution.

DU –

1- When you are running very low space in the directory use du to get all files accurate size.
2- Scripts where you create files on a large scale.
3- Disk size troubleshooting the best option is du.
At this point, you have understood the basic difference between du and df.
]]>
https://www.explinux.com/2020/06/du-vs-df-why-du-and-df-show-different-results-in-linux.html/feed 0
How To Use fdisk To Partition Disk latest updated https://www.explinux.com/2020/06/how-to-use-fdisk-to-partition-disk-latest-updated.html https://www.explinux.com/2020/06/how-to-use-fdisk-to-partition-disk-latest-updated.html#respond Mon, 22 Jun 2020 03:00:00 +0000 https://explinux.online/2020/06/22/how-to-use-fdisk-to-partition-disk-latest-updated/ How To Use fdisk To Partition Disk latest updated Read More »

]]>
How to use fdisk in linux for partition disk

How to use fdisk to Partition Disk in Linux

Fdisk is a disk partition table manipulation program. By this command, you can manipulate the disk partition table. fdisk is can be used for less than 2TB disk more than 2TB you have to use the parted command. 

In this article, we will see how to delete and create a new partition using fdisk in Linux.

Step 1 – List the disk 

Use the below command to list the disk and their partition table.
#fdisk -l 
check disk using fdisk
This will show you the disk information. In our case, we have added new disk in our system so we will check sdb (select which disk you have added) by below command:
#fdisk -l /dev/sdb
check disk by fdisk

Step 2 – Using the command to delete 

Now we have one disk sdb with label gpt we have to delete it. Use below command to enter :
#fdisk /dev/sdb
fdisk delete
Type “m” for help, this will show you all command available for this disk. This is a tip also because in a small Linux environment people share the same disk to another system and sysadmin tries to create a partition but he fails because everyone skips step one and then we get an error ” Value out of range. ” in the disk. check size available too.
fdisk help
We are going to delete so we press “d” for delete partition :
Command (m for help): d
delete disk partition
Command (m for help): w
fdisk save configuration
press w to save the configuration.
If you have raw disk then you can skip step 2 but it is always safe to check first then proceed.

Step 3 – Create partition 

We will do the same steps to enter the disk and press m for listing available options for the disk.
use n for creating a new partition,1 for the number of partition one press enter to leave default settings to use the full disk.
# fdisk /dev/sdb
Command (m for help): m  
Command (m for help): n
Partition number (1-128, default 1): 1
First sector (34-33554398, default 2048):    press enter key if you need default 
Last sector, +sectors or +size{K,M,G,T,P} (2048-33554398, default 33554398):  press enter key if you need default
Command (m for help): w
create partition using fdisk

Now you have successfully created partition. Now you can create PV,VG , LVM, and formate disk in any filesystem to mount on the mount point.

]]>
https://www.explinux.com/2020/06/how-to-use-fdisk-to-partition-disk-latest-updated.html/feed 0
SSH Without Password in Linux/RHEL/Centos/Ubuntu https://www.explinux.com/2020/06/ssh-without-password-in-linux-rhel-centos-ubuntu.html https://www.explinux.com/2020/06/ssh-without-password-in-linux-rhel-centos-ubuntu.html#respond Sat, 20 Jun 2020 03:00:00 +0000 https://explinux.online/2020/06/20/ssh-without-password-in-linux-rhel-centos-ubuntu/ SSH Without Password in Linux/RHEL/Centos/Ubuntu Read More »

]]>
SSH Without Password in Linux

SSH Without Password in Linux/RHEL/Centos/Ubuntu Easy Steps

Here in our article, we will show you how to log in to another Linux machine using ssh secure key without a password. This ssh passwordless authentication will work on almost every Linux distribution RHEL, Centos, Fedora, Ubuntu.

Our Setup

In this setup, we will take the example of two machines and user explinux. you can do with root user too as per your requirement.
machine 1- 192.168.11.63 
machine 2- 192.168.11.65

Step – 1 Generate ssh-keygen key on – 192.168.11.63 

For this, you have to log in server with the user you want to do passwordless authentication and use below command to generate ssh-keygen:
[root@explinux ~]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):  press enter key
Enter passphrase (empty for no passphrase):  press again enter key
Enter same passphrase again:  press enter key 
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:hules3L8D52ayd2J248QqPGXZAtvbchWnyTUTHH51J4 root@explinux
The key's randomart image is:
+---[RSA 2048]----+
|               o=|
|              +.+|
|             . =o|
|       o   ..  Eo|
|      o S o +... |
|     . . + B Oo..|
|      ..+ o & o..|
|     ...o+ X * o |
|      .o..*.=.=..|
+----[SHA256]-----+

Step – 2 Create a directory for ssh on another machine 

Create a directory on the remote host where you will upload the generated authentication key.
[root@explinux ~]# ssh explinux@192.168.11.65 mkdir -p .ssh
explinux@192.168.11.65's password: Enter password

Step – 3 Copy generated keys to the second machine 

Copy the generated authentication key to another remote host 192.168.11.65 by below command
[root@explinux ~]# cat .ssh/id_rsa.pub | ssh explinux@192.168.11.65 'cat >> .ssh/authorized_keys'
explinux@192.168.11.65's password: Enter password 

Step – 4 Set Correct Permission on the Second Machine

Set the correct permission for keys. make sure you will do as shown below any incorrect permission will cause ssh error. 
[root@explinux ~]# ssh explinux@192.168.11.65 "chmod 700 .ssh; chmod 640 .ssh/authorized_keys"
If you have followed the upper steps correctly then this step will not prompt for the password.

Step -5 Now you can log in from machine 1(192.168.11.63 ) to machine 2(192.168.11.63 ) without any password 

[root@explinux ~]# ssh explinux@192.168.11.65
[explinux@localhost ~]#
Now you have successfully created passwordless authentication on the remote host. Now you can ssh, create a script without any issue or extra tool.
If you want to use our script for passwordless authentication: ssh passwordless script
]]>
https://www.explinux.com/2020/06/ssh-without-password-in-linux-rhel-centos-ubuntu.html/feed 0
How to Use Parted for Partition Disk https://www.explinux.com/2020/06/how-to-use-parted-for-partition-disk.html https://www.explinux.com/2020/06/how-to-use-parted-for-partition-disk.html#respond Thu, 18 Jun 2020 01:26:00 +0000 https://explinux.online/2020/06/18/how-to-use-parted-for-partition-disk/ How to Use Parted for Partition Disk Read More »

]]>
How to Use Parted for Partition Disk
Sometimes we face issues in fdisk for more than 2TB volume format. In the end, it will ask for gpt error.
Parted is commonly used by sysadmin for volume more than 2 TB but you can use less volume too. Please find the below method how to use parted command :

Step 1- Check disk from lsblk which disk you want to format

#lsblk
explinux-lsblk-output
Here you can see sdb is new disk and not mounted on any mount point or directory. In case of multipath volume, you need to take extra care because sometimes mount point does not show. So compare before and after command output.

Step 2- Run below the command if you want only one partition from that disk 

# parted /dev/sdb
GNU Parted 2.1
Using /dev/sdb
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) mklabel gpt
(parted) unit GB
(parted) mkpart
Partition name?  []? 1
File system type?  [ext2]? ext4
Start? 0%
End? 100%
(parted) set 1 lvm on
(parted) quit

Information: You may need to update /etc/fstab.

how-to-use-parted-command
Now at this point, you have formated disk using parted. Now you have learned how to use the parted command on Linux systems or servers s For more updates subscribe to our blog.
]]>
https://www.explinux.com/2020/06/how-to-use-parted-for-partition-disk.html/feed 0
How To Reduce vdi Size in VirtualBox and Free Disk Unused Space | Linux Servers https://www.explinux.com/2020/06/how-to-reduce-vdi-size-in-virtualbox-and-free-disk-unused-space-linux-servers.html https://www.explinux.com/2020/06/how-to-reduce-vdi-size-in-virtualbox-and-free-disk-unused-space-linux-servers.html#respond Tue, 02 Jun 2020 11:46:00 +0000 https://explinux.online/2020/06/02/how-to-reduce-vdi-size-in-virtualbox-and-free-disk-unused-space-linux-servers/ How To Reduce vdi Size in VirtualBox and Free Disk Unused Space | Linux Servers Read More »

]]>
How To Reduce vdi Size in Virtual Box and Free Disk Unused Space | Linux Servers

How To Reduce vdi Size in VirtualBox and Free Disk Unused Space

VirtualBox is an oracle hypervisor that we install in our base machine or laptop to use another guest OS in this. Virtual box and we always select dynamically allocated disk which will increase as we add data in guest OS. But it does not automatically reduce even we delete data in guest OS. We have to reduce the size by using some commands with a process. This process I will show you here.
Make sure you are shrinking the disk if you seen the difference between guest OS disk size is lower than vdi disk size.
1 size how to reduce vdi size in virtualbox
2 %2Bsize%2Bof%2Bvdi how to reduce vdi size in virtualbox

Step 1 – Check you are using dynamically allocated disk

Step 2 – Need to Tell OS unallocated space has nothing(zero) or defrag to consolidate space.

Step 3 – Run Virtual Box Command to Shrink Disk

Step 1 – Check you are using dynamically allocated disk 

Check the virtual box that it has dynamically allocated disk if it is fixed then you have to change it.
Power off the virtual machine and change it to dynamically allocated to shrink disk.
3 size%2Bof%2B.vdi%2Bfile how to reduce vdi size in virtualbox

Step 2 – Need to Tell OS unallocated space has nothing(zero) or defrag to consolidate space

For windows, you need to defrag disk but here we will show you the Linux procedure.

1. Install zerofree

Go to guest OS or VM and install zerofree
# apt-get install zerofree
Install Zerofree

2.Reboot Server

Reboot server because this command will not work properly on normal mode so we have to boot in recovery mode.
reboot server

3. Go to Advanced Option

Press the escape button to stop normal boot and the select Advance option for ubuntu.
Advance option for Ubuntu

4. Go to Recovery Mode

Select recovery mode
recovery mode

5.Drop to root shell Prompt

Select Drop to root shell Prompt
Drop to root Shell prompt

6. Check Root Partition

check partition name with df command
# df 
9 %2Bcheck%2Broot%2Bpartion%2Bby%2Bdf how to reduce vdi size in virtualbox

7.Run zerofree command

Now run the command on the disk you want to reduce. In our case, it is the root.
# zerofree -v /dev/dm-0
zerofree
After successfully completing this command run below command
# halt
system halt

8- Power off VM

Power off Virtual Box

Step 3 – Run Virtual Box Command to Shrink Disk 

Now open the terminal in base OS and go to the location where vdi file is located. Otherwise, you have to mention the full path of vdi file.
Run below command :
 VBoxManage modifyhd name.vdi –compact
#VBoxManage modifyhd webserver.vdi --compact
 
vboxmanage
 
Now at this point, we have successfully decreased the size of vdi file.
15 %2Bsize%2Breduced how to reduce vdi size in virtualbox
 
16 how to reduce vdi size in virtualbox
This is very useful in the case of the Database server. Where you rotate data from one server to another.
]]>
https://www.explinux.com/2020/06/how-to-reduce-vdi-size-in-virtualbox-and-free-disk-unused-space-linux-servers.html/feed 0