Showing posts with label ubuntu. Show all posts
Showing posts with label ubuntu. Show all posts

Most useful Tar command examples

In this post we will see how to use the tar command to compress and uncompress your files and directories. Many softwares are distributed by compressing them using tar so the examples below will guide you how you can uncompress or open such files. This command is also very useful when you have to archive or share huge directories or files. 

Create a tar 

In the below example we have a directory called cloudvedas which has multiple files. Let's tar this directory. With -v option you can see what all files it is adding in the tar ball.

-c: Create an archive.
-v: Verbose output shows you all the files being archived.
-f: Allows you to specify the filename of the archive.

cvterm#tar -cvf cloudvedas.tar cloudvedas
cloudvedas/
cloudvedas/cv1.sh
cloudvedas/cv2.sh
cloudvedas/cv3
cloudvedas/cv4
cloudvedas/cv5.py
So now we can see we have our tar file.
cvterm#ls -lrth
total 12K
drwxr-xr-x 1 sagu sagu 4.0K Jun 16 08:19 cloudvedas
-rw-r--r-- 1 sagu sagu  10K Jun 16 08:20 cloudvedas.tar
cvterm#


List or View contents of a tar

Now if you want to check all the files which are in the tar ball without doing untar/opening/uncompressing you can check it as below

 cvterm#tar --list -f cloudvedas.tar
cloudvedas/
cloudvedas/cv1.sh
cloudvedas/cv2.sh
cloudvedas/cv3
cloudvedas/cv4
cloudvedas/cv5.py
cvterm#

 Another way to see the contents of a tar ball without untar is using -tvf option like below. This will show the permissions also.

cvterm#tar -tvf cloudvedas.tar
drwxr-xr-x sagu/sagu         0 2021-06-16 08:19 cloudvedas/
-rw-r--r-- sagu/sagu         0 2021-06-16 08:19 cloudvedas/cv1.sh
-rw-r--r-- sagu/sagu         0 2021-06-16 08:19 cloudvedas/cv2.sh
-rw-r--r-- sagu/sagu         0 2021-06-16 08:19 cloudvedas/cv3
-rw-r--r-- sagu/sagu         0 2021-06-16 08:19 cloudvedas/cv4
-rw-r--r-- sagu/sagu         0 2021-06-16 08:19 cloudvedas/cv5.py
cvterm#

Create tar.gz or .tgz which is tar and gzip in single command.

Now let's try to gzip the directory along with tar in a single command. We will do this with option -z .

-z: Compress the archive with gzip.

cvterm#tar -cvzf cloudvedas.tar.gz cloudvedas

We can see below a .tar.gz is created cloudvedas.tar.gz 


cvterm#ls -lrth
total 8.0K
drwxr-xr-x 1 sagu sagu 4.0K Jun 17 19:34 cloudvedas
-rw-r--r-- 1 sagu sagu 5.5K Jun 17 19:34 cloudvedas.tar.gz
cvterm#

We can also create .tgz which is same as .tar.gz 

cvterm#tar -cvzf cloudvedas.tgz cloudvedas

cvterm#ls -lrth
total 16K
drwxr-xr-x 1 sagu sagu 4.0K Jun 17 19:34 cloudvedas
-rw-r--r-- 1 sagu sagu 5.5K Jun 17 19:34 cloudvedas.tar.gz
-rw-r--r-- 1 sagu sagu 5.5K Jun 17 19:36 cloudvedas.tgz
cvterm#

Create a  tar.bz2 or tbz or tb2

The tar.bz2 or tbz or tb2 creates a file which is more compressed as compared to gz but these 3 extensions takes more time to compress and uncompress. If you see the file size listed below you can see clear difference.


cvterm#tar -cvjf cloudvedas.tar.tb2 cloudvedas

or

cvterm#tar -cvjf cloudvedas.tar.tbz cloudvedas

or

cvterm#tar -cvjf cloudvedas.tar.bz2 cloudvedas

cvterm#ls -lrth
total 5.1M
drwxr-xr-x 1 sagu sagu 4.0K Jun 17 19:34 cloudvedas
-rw-r--r-- 1 sagu sagu 5.5K Jun 17 19:34 cloudvedas.tar.gz
-rw-r--r-- 1 sagu sagu 5.5K Jun 17 19:36 cloudvedas.tgz
-rw-r--r-- 1 sagu sagu  332 Jun 17 19:37 cloudvedas.tar.tb2
-rw-r--r-- 1 sagu sagu  332 Jun 17 19:38 cloudvedas.tar.tbz
-rw-r--r-- 1 sagu sagu  332 Jun 17 19:38 cloudvedas.tar.bz2
-rw-r--r-- 1 sagu sagu 5.1M Jun 17 19:44 cloudvedas.tar
cvterm#


List / View the contents of .tar.gz .tgz .tar.bz2 .tar.tbz  .tar.tb2

You can simply view the contents of all these file types by same option of -tvf

cvterm#tar -tvf cloudvedas.tar.gz

or 

cvterm#tar -tvf cloudvedas.tgz

or

cvterm#tar -tvf cloudvedas.tar.tbz

or

cvterm#tar -tvf cloudvedas.tar.bz2

or

cvterm#tar -tvf cloudvedas.tar.tb2


Extract or Untar a file

To untar a file in the current directory do below

-f: It tells tar the name and path of the compressed file.
-x: Extract the files.
-v: Verbose output shows you all the files being extracted.

cvterm#tar -xvf cloudvedas.tar 

To untar a file in a different directory use option -C

cvterm#tar -xvf cloudvedas.tar -C /var/tmp/newdir 
cvterm#pwd
/var/tmp/newdir
cvterm#ls
cloudvedas
cvterm#

Now let's extract single file from tar

cvterm#tar -xvf cloudvedas.tar cloudvedas/file5.txt

Uncompress and untar .tar.gz or .tgz

cvterm#tar -xvf cloudvedas.tar.gz
or
cvterm#tar -xvf cloudvedas.tgz 

Uncompress and untar single file .tar.gz or .tgz


cvterm#tar -zxvf cloudvedas.tar.gz cloudvedas/file4.txt
cloudvedas/file4.txt
cvterm#tar -zxvf cloudvedas.tgz cloudvedas/file5.txt
cloudvedas/file5.txt

Uncompress and untar .tar.gz or .tgz in a different directory

cvterm#tar -xvf cloudvedas.tar.gz -C /home/sagu/newdir/
or
cvterm#tar -xvf cloudvedas.tgz -C /home/sagu/newdir/

Uncompress and untar tar.bz2, tar.tbz, tar.tb2 

cvterm#tar -xvf cloudvedas.tar.bz2
or
cvterm#tar -xvf cloudvedas.tar.tbz
or
cvterm#tar -xvf cloudvedas.tar.tb2

Extract multiple files from .tar

cvterm#tar -xvf cloudvedas.tar "cloudvedas/file.txt" "cloudvedas/file2.txt"
cloudvedas/file.txt
cloudvedas/file2.txt

Extract multiple files from tar.gz .tgz using -z option

-z: Decompress the archive using gzip

cvterm#tar -zxvf cloudvedas.tar.gz "cloudvedas/file3.txt" "cloudvedas/file4.txt"
or
cvterm#tar -zxvf cloudvedas.tgz "cloudvedas/file3.txt" "cloudvedas/file4.txt"

Extract multiple files from tar.bz2, tar.tbz, tar.tb2 using -j option

cvterm#tar -jxvf cloudvedas.tar.bz2 "cloudvedas/file5.txt" "cloudvedas/cv1.sh"
or
cvterm#tar -jxvf cloudvedas.tbz "cloudvedas/file5.txt" "cloudvedas/cv1.sh"
or
cvterm#tar -jxvf cloudvedas.tar.tb2 "cloudvedas/file5.txt" "cloudvedas/cv1.sh"

Extract group of files using --wildcard option

.tar

cvterm#tar -xvf cloudvedas.tar --wildcards '*.sh'

.tar.gz .tgz using -z option

cvterm#tar -zxvf cloudvedas.tar.gz --wildcards '*.sh'
cvterm#tar -zxvf cloudvedas.tgz --wildcards '*.sh'

tar.bz2, tar.tbz, tar.tb2 using -j option

cvterm#tar -jxvf cloudvedas.tar.bz2 --wildcards '*.txt'
cvterm#tar -jxvf cloudvedas.tar.tbz --wildcards '*.txt'
cvterm#tar -jxvf cloudvedas.tar.tb2 --wildcards '*.txt'

Add a new file or directory in .tar archive using -r option

cvterm#tar -rvf cloudvedas.tar cvnew1.sh   ##Adding a file
cvterm#tar -rvf cloudvedas.tar newdir1
  ## Adding dir

Check below that now we have new file and dir added in our .tar

cvterm#tar -tvf cloudvedas.tar
drwxr-xr-x sagu/sagu         0 2021-06-17 19:34 cloudvedas/
-rw-r--r-- sagu/sagu         0 2021-06-16 08:19 cloudvedas/cv1.sh
-rw-r--r-- sagu/sagu         0 2021-06-16 08:19 cloudvedas/cv2.sh
-rw-r--r-- sagu/sagu         0 2021-06-16 08:19 cloudvedas/cv3
-rw-r--r-- sagu/sagu         0 2021-06-16 08:19 cloudvedas/cv4
-rw-r--r-- sagu/sagu         0 2021-06-16 08:19 cloudvedas/cv5.py
drwxr-xr-x sagu/sagu         0 2021-06-17 19:17 cloudvedas/cvdir1/
drwxr-xr-x sagu/sagu         0 2021-06-17 19:17 cloudvedas/cvdir2/
-rw-r--r-- sagu/sagu   1048576 2021-06-17 19:33 cloudvedas/file.txt
-rw-r--r-- sagu/sagu   1048576 2021-06-17 19:34 cloudvedas/file2.txt
-rw-r--r-- sagu/sagu   1048576 2021-06-17 19:34 cloudvedas/file3.txt
-rw-r--r-- sagu/sagu   1048576 2021-06-17 19:34 cloudvedas/file4.txt
-rw-r--r-- sagu/sagu   1048576 2021-06-17 19:34 cloudvedas/file5.txt
-rw-r--r-- sagu/sagu         0 2021-06-18 19:27 cvnew1.sh
drwxr-xr-x sagu/sagu         0 2021-06-18 19:26 newdir1/

cvterm#

Note: You cannot add files or dir  to tar.gz, .tgz, tar.bz2, tar.tbz, tar.tb2 archives

Exclude specific file or directories while creating a tar

cvterm#tar -cf cloudvedasexclude.tar --exclude='cloudvedas/cvdir1' --exclude='cloudvedas/file2.txt' cloudvedas

As we can see below the file and directory we excluded are not in the tar.

cvterm#tar -tvf cloudvedasexclude.tar
drwxr-xr-x sagu/sagu         0 2021-06-18 19:25 cloudvedas/
-rw-r--r-- sagu/sagu         0 2021-06-16 08:19 cloudvedas/cv1.sh
-rw-r--r-- sagu/sagu         0 2021-06-16 08:19 cloudvedas/cv2.sh
drwxr-xr-x sagu/sagu         0 2021-06-17 19:17 cloudvedas/cvdir2/
-rw-r--r-- sagu/sagu   1048576 2021-06-17 19:33 cloudvedas/file.txt
-rw-r--r-- sagu/sagu   1048576 2021-06-17 19:34 cloudvedas/file3.txt
-rw-r--r-- sagu/sagu   1048576 2021-06-17 19:34 cloudvedas/file4.txt
-rw-r--r-- sagu/sagu   1048576 2021-06-17 19:34 cloudvedas/file5.txt
cvterm#


Above we have seen some examples of frequently used tar command options. You can also use tar command to convert your vmware ova files to ovf . Check this post on how to convert ova to ovf  using tar.

There are lot more options in tar that you can see using tar --help in your terminalDo let us know in the comment section if you want us to add any other examples.

Steps to create docker containers in your laptop

In this post we will see how to create a docker container of ubuntu on your windows laptop.

Pre-requisites:-

I am using “Docker for Windows” software to run dockers on my Windows 10 laptop. You can get “Docker for Windows” by clicking on this link .
If you have Windows 7 download Docker Toolbox for Windows with Virtualbox.

Ubuntu docker creation
Once you are done with docker installation let’s move ahead.
  • In the windows command prompt or in “Docker Quickstart Terminal” execute below command. By default it will pull the latest image of ubuntu container available in repository.
C:\CloudVedas>docker run ubuntu
  • If you need  specific version of Ubuntu you can mention the version name in command. Like below we are pulling Ubuntu 14.04 version. You can check all the available versions here .
C:\CloudVedas> docker run ubuntu:14.04
Unable to find image 'ubuntu:14.04' locally
14.04: Pulling from library/ubuntu
2e6e20c8e2e6: Pulling fs layer
30bb187ac3fc: Pulling fs layer


  • Now let’s see the image we have downloaded .
C:\CloudVedas> docker image ls
REPOSITORY      TAG                 IMAGE ID            CREATED             SIZE
ubuntu                   14.04               6e4f1fe62ff1        4 months ago        197MB


  • Let’s create a container with that image using the image id. Here we are using -t and -d option so that the container keeps on running in detached mode and we can login to it.
C:\CloudVedas>docker run -t -d 6e4f1fe62ff1
a8dee68d78026adb830edb04391af00ec8b7e1033e711fc640a1489ca54adc0a



  • List the running containers using “docker container ls”
C:\CloudVedas>docker container ls
CONTAINER ID       IMAGE        COMMAND      CREATED         STATUS     PORTS       NAMES
a8dee68d7802        6e4f1fe62ff1   "/bin/bash"      About a minute ago   Up About a minute eager_johnson



We can see our container is created 5 minutes ago and is up. You can also identify the container using the container id. Note that the container ID is same as the first 12 digits of the string we got when we executed docker run in last step.
  • Let’s get inside our container and check it.
C:\CloudVedas>docker exec -it  a8dee68d7802 /bin/bash


Once you are inside the Ubuntu container you can explore it. Let’s check the OS version.
root@a8dee68d7802:/# more /etc/os-release NAME="Ubuntu" VERSION="14.04.6 LTS, Trusty Tahr" ID=ubuntu ID_LIKE=debian PRETTY_NAME="Ubuntu 14.04.6 LTS" VERSION_ID="14.04" HOME_URL="http://www.ubuntu.com/" SUPPORT_URL="http://help.ubuntu.com/" BUG_REPORT_URL="http://bugs.launchpad.net/ubuntu/" root@a8dee68d7802:/#
  • If you don’t see your container up, it may have stopped automatically. To check all the containers which are/were running, execute command
C:\CloudVedas>docker ps -a

You can also create your own customized container image which you can use to deploy more containers in your environment. Refer this post on how to create a container image.  If instead of Docker hub you are using AWS ECR, check the post on how to push/pull image to/from AWS ECR

If you want to try next level on docker you can user dockerfile or docker-compose files to install your application. Like in our other post we cover how you can install wordpress in docker.

Hope this post helped you. Do let us know if you have any query or you get stuck in any installation step. 

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

Linux can generally have two types of Disks. IDE and SCSI.
IDE
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
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 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 .

Install vagrant and create VM on your windows laptop or desktop

In this post we will show you how to install vagrant in your laptop and start a Ubuntu Linux VM.
You will have to download these softwares and install them on your laptop.
  • Once they are downloaded.
First Install Virtual Box.  Just click on exe and follow installation instructions.
Next Install Vagrant. Just click on exe and follow installation instructions.
Putty and PuttyGen don’t need any installation, you can use them directly.
Once the pre-requisites are installed let’s move ahead.
  • Open the windows command prompt. CMD
  • Go to the directory where you will need the vagrant machines to be installed. e.g.
cd C:\users
  • Once you are in desired directory create a new directory .
mkdir ProjectVagrantcd ProjectVagrant
  • Create a new Vagrant File
vagrant init ubuntu/trusty64
  • Below command will download the Ubuntu Trusty 64 OS image and create VM. Ensure you are connected to network.
vagrant up
  • Setup ssh
vagrant ssh
Above command will give you IP, Port and Private key for the machine.
  • Using putty you can connect to the machine. Add entries like below.
Hostname:- vagrant@127.0.0.1
Port :- 2222
Default password :- vagrant


If you want to login using the ssh key you will have to first convert the .pem key to .ppk. Follow this post on How to convert .pem key to .ppk and login to VM ?
That’s all in this post! Hope it’s useful for you. If you have any query please write in comments section.

Solved: Install ifconfig and ssh in Ubuntu

In this post we will see how to install ifconfig and ssh in Ubuntu 16.04 Xenial Xerus. You can install both or either of them independently .
ifconfig
By default you can check IP details in Ubuntu using
ip addr
But if you are more comfortable with “ifconfig”  then follow on.
Ensure that your Ubuntu instance is connected to internet or to your local package repository server from which it can pull packages.
If you need ifconfig in your ubuntu server use following commands.
sudo apt-get updatesudo apt-get install net-tools
“ifconfig” command should now work
ifconfig -a
ssh
If you are trying to get ssh. Use the below command.
sudo apt-get install openssh-server
Once the ssh installation is done check the status of ssh service
sudo service ssh status
If the service is not running you will have to start it with following command.
sudo service ssh start
Once this is done you should get a message of service started ok. You can check service status again with
sudo service ssh status
The default configuration file for ssh is /etc/ssh/sshd_config . If you make any changes to this file you will have to restart the ssh service to make the changes effective.
sudo service ssh restart
If you are using docker you can create a golden image after doing this installation. So that you don’t have to do this installation in all new containers. To check how to create a golden image for a docker container check this post .