Showing posts with label redhat. Show all posts
Showing posts with label redhat. Show all posts

Solved : How to zip directories in Linux

In this post we will see how you can use zip command in Linux to compress directories and files and unzip them. zip is also very useful if you have to further uncompress the files on windows machine as by default windows allows you to open zip files without installing any extra program.

First let's install zip. Below we will show installation instruction for both Ubuntu and Redhat linux/CentOS.

Ubuntu :

Update the package repo:

root@CloudVedas:~# sudo apt-get update

Now let's install zip and unzip packages:

sudo apt-get install zip unzip -y


Update the package repo:

root@CloudVedas:~# sudo yum makecache

Install the zip and unzip packages

root@CloudVedas:~# sudo yum install zip unzip

Once the zip and unzip packages are installed we can now see their usage.

The basic syntax of the zip command is below:

zip -r <output_file>.zip  <dir1> <dir2> ... <dirn>

Let's see some examples :

We will zip three directories

root@CloudVedas:~# zip -r dir1 dir2 dir3

  adding: dir1/ (stored 0%)

  adding: dir1/test11 (stored 0%)

  adding: dir1/test13 (stored 0%)

  adding: dir1/tet12 (stored 0%)

  adding: dir2/ (stored 0%)

  adding: dir2/test21 (stored 0%)

  adding: dir2/test22 (stored 0%)

  adding: dir2/test23 (stored 0%)

  adding: dir3/ (stored 0%)

  adding: dir3/test31 (stored 0%)

  adding: dir3/test32 (stored 0%)

  adding: dir3/test33 (stored 0%)


When we list the contents we can see our zipped file .

root@CloudVedas:~# ls -lrth

total 4.0K

drwxr-xr-x 1 root root 4.0K Aug 29 13:54 dir1

drwxr-xr-x 1 root root 4.0K Aug 29 13:54 dir2

drwxr-xr-x 1 root root 4.0K Aug 29 13:54 dir3

-rw-r--r-- 1 root root 1.8K Aug 29 13:55


You can also zip all the directories in the present working directory using this simple bash script.

for dir in $(ls -d */); do zip $dir; done 

View contents of a zip file 

If you want to see the contents of a zip file without unzipping it you can use either vi or vim .

root@CloudVedas:~# vim


root@CloudVedas:~# vi

With the above commands you can easily see the list of files and directories inside the zip.


Now let's see how you can unzip a file. Be careful when you are unzipping a file with out any extra options as it will overwrite the existing contents if same name exist. Best is to unzip the file in a different directory and then copy the required files and directories as needed in the destination folder.

root@CloudVedas:~/unziptest# unzip


   creating: dir1/

 extracting: dir1/test11

 extracting: dir1/test13

 extracting: dir1/tet12

   creating: dir2/

 extracting: dir2/test21

 extracting: dir2/test22

 extracting: dir2/test23

   creating: dir3/

 extracting: dir3/test31

 extracting: dir3/test32

 extracting: dir3/test33

root@CloudVedas:~/unziptest# ls

dir1  dir2  dir3


If you want to see other useful options on unzip you can use below command

unzip -hh

So above we have seen how you can use zip and unzip in Linux. zip is a good utility for smaller files and directories but if you need better compression you should use tar.gz. Check out our post on most useful tar command examples if you want to know more about tar.gz and tar.bz2 or other compression methods in Linux.

If you want to transfer this file out of the Linux server check our post on how to transfer files to and from linux .

Do let us know in comments section if you have any query or suggestion about this post.

Solved: How to use nmtui and nmcli tool to configure network in RedhatLinux

In this post we will see how to use nmtui for network management. This can be a very useful tool for starters in Redhat Linux 7.
  • Login to your server using putty and execute command “nmtui” to invoke the interface. If your machine is not in network you can login via console and follow the same steps mentioned below.
  • If you don’t have the nmtui tool installed, you can install it using yum (Note: If you don’t want to install a new package in your server you can use nmcli for network configuration. Refer the post for how to configure network with nmcli )
[root@cloudvedas ~]# yum install NetworkManager-tui
[root@cloudvedas ~]# nmtui
  • Once you get the interface you can navigate using Arrow keys , Tab key and make selection using “enter” key. So, using arrow key select “Edit a connection” and press enter.

  • We have two network interfaces enp0s8 and enp0s3 . Today we will be configuring enp0s3. As enps0s8 is already configured and in use. So once highlighted enp0s3 go to “Add” and press enter.

  • Select the type of connection. For this tutorial we are selecting “Ethernet”.

  • Once in “Edit connection” section enter “Profile name” , “Device”.  If you want static IP select “Manual” for IPv4 and select “Show”.

  • Once you select  “Show” enter your IP detail with subnet id. Here subnet id is /24 or . Also enter gateway. If you want to use DNS enter DNS server details else leave it blank and select OK.

  • Once the IP configuration is done select “OK” again. Now you can see the new connection that you have created. Select “Quit” to come out.

  • Now let’s activate the connection. Execute “nmtui” in putty or console again and select “Activate a connection”  and press Enter.

  • Navigate with arrow key select the interface and then move right and Press enter on “Activate”. This will activate your interface as in image below.

  • Check if the new IP is configured.

  • Also let’s check in “nmcli” about the new connection.

If you want to change hostname refer this post on how to change hostname with nmtui or nmcli .
Hope this post is helpful. Do let me know if you have any query.

Solved: How to mount ISO in a linux VM

In this post we will discuss how to mount ISO in Linux VM running inside Virtualbox or VMware.
You can mount ISO in Linux VM running on Virtualbox by following these steps
  • Select the running machine window.
  • Click on Devices > Optical Devices
  • Choose disk image, browse and select ISO .
  • Now go to the redhat linux server and execute the mount command
 mount /dev/cdrom /mnt
  • If the linux VM is in VMware. You can select the iso image in vmware console similar to what we did for virtual box.
  • Right click on the machine > “Removable Devices” > “CD/DVD” > Settings. Browse and select the ISO. Check mark on “Connected”.
  • Finally execute the following command in Linux VM.
    mount /dev/sr0 /mnt
  • If you do “df -h” new ISO should be mounted and visible to you as /mnt .
Tip: If you are getting error /mnt busy ensure that /mnt is not already mounted. If /mnt is already mounted either unmount /mnt first and try again or create a new directory and mount the ISO on the new directory.

What are the maximum number of usable partitions in a disk in Linux

Linux can generally have two types of Disks. IDE and SCSI.
By convention, IDE drives will be given device names /dev/hda to /dev/hdd. Hard Drive A (/dev/hda) is the first drive and Hard Drive C (/dev/hdc) is the third.
A typical PC has two IDE controllers, each of which can have two drives connected to it. For example, /dev/hda is the first drive (master) on the first IDE controller and /dev/hdd is the second (slave) drive on the second controller (the fourth IDE drive in the computer).
Maximum usable partitions 63 for IDE disks.
SCSI drives follow a similar pattern; They are represented by ‘sd’ instead of ‘hd’. The first partition of the second SCSI drive would therefore be /dev/sdb1.
Maximum usable partitions 15 for SCSI disks.
A partition is labeled to host a certain kind of file system (not to be confused with a volume label). Such a file system could be the linux standard ext2 file system or linux swap space, or even foreign file systems like (Microsoft) NTFS or (Sun) UFS. There is a numerical code associated with each partition type. For example, the code for ext2 is 0x83 and linux swap is 0x82.
To see a list of partition types and their codes, execute /sbin/sfdisk -T

Solved: How to scan new LUNs in Redhat Linux

In this post  we will discuss how to scan new LUNs allocated by storage team to a Redhat Linux system.
There are two ways of scanning the LUNs
Method 1:-
Find how many SCSI bus controllers you have

  • Go to directory /sys/class/scsi_host/  and list it’s contents.

cd /sys/class/scsi_host/ 
[root@scsi_host]# ls
host0 host1 host2
  • Here we can see we have three SCSI bus controllers. So in below command replace hostX with these directory names.
Run the Command ,
echo "- - -" > /sys/class/scsi_host/hostX/scan 
[root@cloudvedas]# echo "- - -" > /sys/class/scsi_host/host0/scan
[root@cloudvedas]# echo "- - -" > /sys/class/scsi_host/host1/scan
[root@cloudvedas]# echo "- - -" > /sys/class/scsi_host/host2/scan
[root@cloudvedas]# echo "- - -" > /sys/class/scsi_host/host3/scan
TIP:- Here the “- – -” denotes CxTxDx i.e. Channel(controller) , Target ID and Disk or LUN number. This is asked in Linux Admin Interviews also.
  • Repeat the above step for all three directories.
If you have FC HBA in the system you can follow the steps as below:-
  • First check number of FC controllers in your system
#ls /sys/class/fc_hosthost0 host1 host2
  • To scan FC LUNs execute commands as
echo "1" > /sys/class/fc_host/host0/issue_lip
echo "1" > /sys/class/fc_host/host1/issue_lip
echo "1" > /sys/class/fc_host/host2/issue_lip

Tip :- Here echo “1” operation performs a Loop Initialization Protocol (LIP) and then scans the interconnect and causes the SCSI layer to be updated to reflect the devices currently on the bus. A LIP is, essentially, a bus reset,  and will cause device addition and removal. This procedure is necessary to configure a new SCSI target on a Fibre Channel interconnect. Bear in mind that issue_lip is an asynchronous operation.
  • Verify if the new disk is visible now
fdisk -l |egrep '^Disk' |egrep -v 'dm-'
Method 2 :-
  • Next method is to scan using SG3 utility. You can install it using
yum install sg3_utils
  • Once installed  run the command

Solved: How to add swap space in Redhat or Ubuntu Linux

In this post  we will see how we can add a file as swap space in Linux. Same steps are to be followed for Redhat and Ubuntu Linux.
Type the following command to create 100MB swap file (1024 * 100MB = 102400 block size):
dd if=/dev/zero of=/swap1 bs=1024 count=102400
Secure swap file
Setup correct file permission for security reasons, enter:
# sudo chown root:root /swap1# sudo chmod 0600 /swap1
Set up a Linux swap area
Type the following command to set up a Linux swap area in a file:
# sudo mkswap /swap1
Activate /swap1 swap space :
# sudo swapon /swap1
Update /etc/fstab file to make it persistent across reboot.
vi /etc/fstab
Add the following line in file:
/swap1 swap swap defaults 0 0
To check if the swap file is added or not
Type the following swapon command:
#sudo swapon -sFilename Type Size Used Priority/dev/dm-0 partition 839676 0 -1/swap1 file 102396 0 -2
It should show you the new file.
If you want add a logical volume for swap please refer how to add LV for swap .

How to add logical volume for swap in Redhat Linux

In our last post we have seen how to add a file for swap space.
In this post we will see how to add a LVM2 Logical Volume as swap.
Here we have a VG name VG1 in which we will create a volume LV1 of 1GB.
# lvcreate VG1 -n LV1 -L 1G
Format the new swap space using mkswap:
# mkswap /dev/VG1/LV1
Update /etc/fstab file with below entry:
# /dev/VG1/LV1 swap swap defaults 0 0
Enable the extended logical volume:
# swapon -v /dev/VG1/LV1

Solved: How to change hostname in AWS EC2 instance of RHEL 7

In our last post we have seen how to change hostname of an RHEL server.
But if you are using the RHEL 7 AMI provided on AWS marketplace the steps will be slightly different.
First login to your EC2 instance. (Check this post to know How to login to AWS EC2 Linux instance.)
Once you login to your EC2 instance execute below command.
 sudo hostnamectl set-hostname --static cloudvedas
(Here “cloudvedas” is the new hostname.)
If you want to make it persistent across reboot follow further.
Now using vi or vim editor edit the file /etc/cloud/cloud.cfg
sudo vi /etc/cloud/cloud.cfg
At the end of file add the following line and save the file
preserve_hostname: true
Finally reboot the server
sudo reboot
Once the server is up, check the hostname.
ec2-user# hostname
It should now show you the new hostname.