Pages

Thursday, May 4, 2017

What is Soft Link and Hard Link

Here is Simple difference between soft link and hard link.



Softlink
Hardlink
File
Same file, but 2 inode number
2 diff files, Same Inode number
Filesystem Limit
Can cross file system
Cannot cross file system
Between Dir’s
Possible
Not Possible
Link Count
Link count value is 1
Link count value is 2
Command
ln –s <source> <link file>
ln <source> <link file>
To unlink
# unlink <link file name>
Source removal
Will affect the link file
Won't affect the link file


About IP Addressing & Calculating first and Last IP address of a subnet

IP Default Subnet Masks For Address Classes A, B and C

IP Address Classes

Class -A - 0 -126
Class  B - 128- 191
Class C  - 192 - 223
Class D - 224  -249 
Clase E  - 250 - 255

Class D for Multicast
Class E reserved for Testing

Class A - N.H.H.H
Class B - N.N.H.H
Class C - N.N.N.H

Default Subnet Mask

Subnet mask for a default, unsubnetted class A, B or C network has ones for each bit that is used for network ID or subnet ID, and zeroes for the host ID bits. Of course, we just said we aren't subnetting, so there are no subnet ID bits! Thus, the subnet mask for this default case has 1s for the network ID portion and 0s for the host ID portion. This is called the default subnet mask for each of the IP address classes.

Class A - 255.0.0.0
Class B - 255.255.0.0
Class C - 255.255.255.0

What is Subnetting

Subnetting is the process of dividing a Class A, B or C network into subnets, as we've seen in the preceding topics. In order to better understand how this “division of the whole” is accomplished, it's worth starting with a look at how the “whole” class A, B and C networks are represented in a subnetted environment. This is also of value because there are situations where you may need to define an unsubnetted network using subnetting notation.

This might seem like a strange concept—if you aren't going to bother creating subnets, why do you need to consider how the old-fashioned classes are used under subnetting? The answer is that after subnetting became popular, most operating systems and networking hardware and software were designed under the assumption that subnetting would be used. Even if you decide not to subnet, you may need to express your unsubnetted network using a subnet mask.


Custom subnetting 

Will be done by adding extra bits from host bit.

If we want two subnet then we need to add 1 bit from host bit. If we want 20 subnet then we need to borrow 5 bits as  4 bits will give only 16 subnets

Each bit taken from the host ID for the subnet ID doubles the number of subnets that are possible in the network.

Each bit taken from the host ID for the subnet ID (approximately) halves the number 

so /16 means 255.255.0.0 this will be the subnet of either class B or Class A. So to determine Class A or Class B, Class C only subnet is not enough we need IP address of Host as well.

eg 1) If 10.0.0.0/8 is the IP address then subnet will be 255.0.0.0 then the number of hosts will be calculated by the left over host bits. In this case out of 24 bits 8 are used by network so remaining 24 will be used for host, so 2^24 = 16777216, is the total number of hosts in this network.

eg 2) If the IP address is 10.0.0.0/17 then subnet mask will be  255.255.128.0. As the 9 bits are borrowed from host bit. And the number of hosts will be calculated by  ( Subnet mask - 1111 1111.1111 1111.1000 0000. 0000 0000) 32 -17 =15  2^15 = 32768 and total number of usuable ip's are 32768 -2 = 32766.

Normally first usable IP is the default gateway and last IP is used for brodcating. 


It's important to understand what default subnet masks are and how they work. A default subnet mask doesn't really represent subnetting, however, since it is the case where we are assigning zero bits to the subnet ID. To do “real” subnetting we must dedicate at least one of the bits of the pre-subnetted host ID to the subnet ID, as shown in the example in the topic that introduced subnet masks.

In this post will see how to calculate the first and last IP address of a subnet from a given IP address and also how to create subnet.

Part 1 : Calculate the first and last IP of an IP address range

Question : What is the first and last IP addresses of this network? 192.168.176.0/23. 

To identify the first and last IP of the given address we need to find out below details

1) Interesting Octet
2) Magic Number
3) Subnet Number
4) Broadcast ID

Interesting Octet - First you need to find the interesting octet of the mask. Take a look at each octet in the mask and see if it's a 255, 0, or some other number. If the octet is neither a 255 or 0, that's your interesting octet. 

In Our case - 255.255.254.0 (Highlighted 254 is the interesting octet)
So, Interesting octet from the IP - 254

Now that we have the interesting octet, compare the mask to the IP address. Anything on the left of the interesting octet, signals that you should copy the IP address octet to your subnet number. (X means it's currently unknown)

In our case - 192.168.X.X

Magic Number - Next we need to calculate the magic number. To find the magic number, simply subtract the number 256 from the interesting octet in the mask

In our case : 256 - 254 = 2

Subnet Number -  Step 3 is to identify the subnet number from the magic number. To identify the subnet number find the multiples of magic number that is lesser than or equal to the interesting octet in the IP address. 

Magic number is 2. Multiples of 2 is 2, 4, 6, 8 and ... 172, 174, 176...

You can see, 176 is the closest number to 176 and which is actually not going above 176. Yes, 176 is our subnet number. Simply place 176 in the interesting octet in the IP address.

So IP address will be like - 192.168.176.0

Brodcast - Finding the broadcast address is very easy.  Take the interesting octet from IP address field which we found now (176) and add the magic number (2) then subtract 1. This gives you your interesting octet for the broadcast address.

Broadcast = ( Interesting Octet of subnet + Magic Number ) -1 

From our eg, 176 + 2 -1 = 177. So, Brodcast IP will be - 192.168.177.255

Host Range - To find the host range, simply add 1 to the final octet of the subnet number and subtract 1 from the final octet of the broadcast address.

Host Range - 192.168.176.1 - 192.168.177.254

Total number of available Hosts – 2^ 9 = 512 (As remaining bits are used by subnet mask)

Subnet mask - 255.255.254.0
Wildcard mask - 0.0.1.255
Number od hosts - 2^9 - 512 hosts
So the starting will be 192.168.176.1 and ending will be 192.168.177.254

Practice Questions. 

1) Find the addresses in subnet 10.1.4.0/23.  

Subnet mask for the given address -  255.255.254.0
Magic number - 256 - 254 = 2 
Multiples of magic number - 2 - 0,2,4,6,8 -  In our case 4 is lesser r equal to the Interesting octet.
So subnet - 10.1.4.0 
Brodcast =  Magic number + Interseting octet = 4+2 -1 = 5. So broadcast IP is  10.1.5.255 

As you know first IP address is network address that is 10.1.4.0 and last address ( broadcast IP address 10.1.5.255) cant be assigned. So the usable IP address starting with 10.1.4.1 and ending with 10.1.5.254

2) A fellow engineer tells you to configure the DHCP server to lease the first 20 usable IP addresses in subnet 192.168.9.96/27.  Which of the following IP addresses could be leased as a result of your new configuration?

This is class C IP address only last part is host

Number of hosts - 2^5 =32
Subnet mask - 255.255.255.224
Magic number - 256-224 =32
Multiples of 32 = 32,64,96
96 is nearby to our IP so
First IP is 192.168.9.96  and
Brodcast = Magic number + Subnet Interesting number = 32+96 =128 = 128 -1 =127 
Brodcast IP address = 192.168.9.127

Usuable Host Range = 192.168.9.97 - 192.168.9.126

Subnet 192.168.9.96/27 has subnet mask as 255.255.255.224 so the IP address started from 192.168.9.96 and ending with 192.168..9.127 as you know first IP address is network address that is 192.168.9.96 and last address is broadcast IP address 192.168.9.127 and we cant assigned network and broadcast IP address so the usable IP address starting with 192.168.9.97 and ending with 192.168.9.126

Part 2 : How to create subnet's from the given IP addrss

Question : Create a VPC with CIDR 10.0.0.0/24 and we have to create 6 subnets. Let see how can we do that

The given IP 10.0.0.0 is Class A IP address so the default subnet mask is 255.0.0.0. Mask as per the CIDR block is 255.255.255.0

Total number of available Hosts – 2^ 8 = 512 (As remaining bits are used by subnet mask)

As per requirement we need 6 subnets for which we need 3 bits from hosts.

So the Subnet mask will be 10.0.0.0/27  (255.255.255.224)

Then to calculate Host range

Interesting octet in Mask - 224
Magic Number - 256 - 224 = 32
Multiples of 32, is 0, 32, 64
So Subnet number is 10.0.0.0
Brodcast = Interesting octet of subnet + Magic Number - 1 =31
Host Range = 10.0.0.1 - 10.0.0.30

Similaryly 2nd Subnet IP address wil be 10.0.0.32/27

So the 1st ip address of subnet will be 10.0.0.32 to 10.0.0.63


Similar Sample Questions

10.180.10.18, mask 255.192.0.0
10.200.10.18, mask 255.224.0.0
10.100.18.18, mask 255.240.0.0
10.100.18.18, mask 255.248.0.0
10.150.200.200, mask 255.252.0.0
10.150.200.200, mask 255.254.0.0
10.220.100.18, mask 255.255.0.0
10.220.100.18, mask 255.255.128.0
172.31.100.100, mask 255.255.192.0
172.31.100.100, mask 255.255.224.0
172.31.200.10, mask 255.255.240.0
172.31.200.10, mask 255.255.248.0
172.31.50.50, mask 255.255.252.0
172.31.50.50, mask 255.255.254.0
172.31.140.14, mask 255.255.255.0
172.31.140.14, mask 255.255.255.128
192.168.15.150, mask 255.255.255.192
192.168.15.150, mask 255.255.255.224
192.168.100.100, mask 255.255.255.240
192.168.100.100, mask 255.255.255.248
192.168.15.230, mask 255.255.255.252
10.1.1.1, mask 255.248.0.0
172.16.1.200, mask 255.255.240.0
172.16.0.200, mask 255.255.255.192
10.1.1.1, mask 255.0.0.0

Deciding How Many Subnet Bits to Use

The key decision in customized subnetting is how many bits to take from the host ID portion of the IP address to put into the subnet ID. Recall that the number of subnets possible on our network is two to the power of the number of bits we use to express the subnet ID, and the number of hosts possible per subnet is two to the power of the number of bits left in the host ID (less two, which I will explain later in this topic).

Thus, the decision of how many bits to use for each of the subnet ID and host ID represents a fundamental trade-off in subnet addressing:
of hosts that are possible within each subnet on the network.

How do we decide how to divide the “classful” host ID into subnet ID and host ID bits? This is the key design decision in subnetting. We must make this choice based on our requirements for the number of subnets that exist in the network, and also on the maximum number of hosts that need to be assigned to each subnet in the network. For example, suppose we have 10 total subnets for our Class B network. We need 4 bits to represent this, because 24 is 16 while 23 is only 8. This leaves 12 bits for the host ID, for a maximum of 4,094 hosts per subnet.

However, suppose instead that we have 20 subnets. If so, 4 bits for subnet ID won't suffice: we need 5 bits (25=32). This means in turn that we now have only 11 bits for the host ID, for a maximum of 2,046 hosts per subnet. Step #2 of the practical subnetting example discusses these decisions in more detail.

Now, what happens if we have 20 subnets and also need a maximum of 3,000 hosts per subnet? Well, we have a problem. We need 5 bits to express 20 different subnets. However, we need 12 bits to express the number 3,000 for the host ID. That's 17 bits—too many. The solution? We might be able to shuffle our physical networks so that we only have 16. If not, 
we need a second Class B network.

It's also important to realize that in regular subnetting, the choice of how many bits to use for the subnet ID is fixed for the entire network. You can't have subnets of different sizes—they must all be the same. Thus, the number of hosts in the largest subnet will dictate how many bits you need for the host ID. This means that in the case above, if you had a strange configuration where 19 subnets had only 100 hosts each but the 20th had 3,000, you'd have a problem. If this were the case, you could solve the problem easily by dividing that one oversized subnet into two or more smaller ones. An enhancement to subnetting called Variable Length Subnet Masking (VLSM) was created in large part to remove this restriction.
IP Custom Subnet Masks


Key Concept: Once the choice of how to subnet has been made, the custom subnet mask is determined simply, by starting with the default subnet mask for the network and changing each subnet ID bit from a 0 to a 1.
Subtracting Two From the Number of Hosts Per Subnet and (Possibly) Subnets Per Network


There's one more issue that needs to be explained regarding the split into subnet ID and host ID. We've already seen how in regular “classful” addressing, we must subtract 2 from the number of hosts allowed in each network. This is necessary because two host IDs in each subnet have “special meanings”: the all-zeroes host ID meaning “this network”, and the all-ones host ID which is a broadcast to “all hosts on the network”. These restrictions apply also to each subnet under subnetting too, which is why we must continue to subtract 2 from the number of hosts per subnet. (This is also why dividing the 8 host ID bits of a Class C network into 7 bits for subnet ID and 1 bit for host ID is not just silly, but in fact meaningless: it leaves 21-2=0 hosts per subnet. Not particularly useful.)


http://www.tcpipguide.com/free/t_IPDefaultSubnetMasksForAddressClassesABandC.htm

Sample Answers
1. 10.180.10.18, mask 255.192.0.0
---- Subnet Number - 10.128.0.0
---- Broadcast Address - 10.191.255.255
---- Host Range - 10.128.0.1 - 10.191.255.2542. 10.200.10.18, mask 255.224.0.0
---- Subnet Number - 10.192.0.0
---- Broadcast Address - 10.223.255.255
---- Host Range - 10.192.0.1 - 10.223.255.254
Which two statements describe the IP address 10.16.3.65/23?
Using above example let us find the starting and ending ip of above CIDR block
IP - 10.16.3.65
Subnet - 255.255.254.0
Wildcard Mask - 0.0.1.255
Innocent octet - 254
To find subnet number - 256-254 = 2 (this 2 is called as magic number)
So multiples of 2 is 0,2, 4,6 ,8,10
We need to take the number which is lesser and closer to our IP address. Our IP address is 3 and closer number is 2
so the subnet will be 10.16.2.0/23
to find the brodcast IP address add the interesting octet in our case it is 2
if the ip is like 172.21.80.0 then intersting octet is 80.

so Intersting octet + magic number = 2+2 =4 =3
so 10.16.3.255 is the brodcast ip address

Wednesday, May 3, 2017

What is Thread, Core and Socket

The central processing unit (CPU) in your computer does the computational work — running programs. But one single-core CPU can only perform one task at a time, which is   where multiple CPUs, hyper-threading, and multi-core CPUs come into play.

Having more than a single CPU allows multiple programs to run at the same time, ensuring your computer stays responsive — especially while multitasking or performing computationally-demanding tasks like video encoding.

Thread - Logical CPU
Core - Physical CPU core unit

Multiple CPUs

Before hyper-threading and multi-core CPUs came around, people attempted to add additional processing power to computers by adding additional CPUs. This requires a motherboard with more than one CPU socket — multiple CPUs are inserted into different sockets. The motherboard also needs additional hardware to connect those CPU sockets to the RAM and other resources. There’s a lot of overhead here — there’s additional latency if the CPUs need to communicate with each other, systems with multiple CPU will consume more power,  and the motherboard needs more sockets and hardware.

This isn’t very common among home-user PCs today. Even a high-powered gaming desktop with multiple graphics cards will generally only have a single CPU. You’ll find multi-CPU systems among supercomputers, servers, and similar high-end systems that need as much number-crunching power as they can get.
cpu-socket-on-motherboard

Hyper-Threading Technology

Hyper-threading was Intel’s first attempt to bring parallel computation to consumer PCs. It debuted on desktop CPUs with the Pentium 4 HT back in 2002. The original Pentium 4 had just a single CPU core, so it could only do one thing at a time — but hyper-threading attempted to make up for that.

A single physical CPU core with hyper-threading appears as two logical CPUs to an operating system. The CPU is still a single CPU, so it’s “cheating” a bit — while the operating system sees two CPUs for each core, the actual CPU hardware only has a single set of execution resources for each core. The CPU pretends it has more cores than it does, and it uses its own logic to speed up program execution. Hyper-threading allows the two logical CPU cores to share physical execution resources. This can speed things up somewhat — if one virtual CPU is stalled and waiting, the other virtual CPU can borrow its execution resources. Hyper-threading can help speed your system up, but it’s nowhere near as good as having additional cores.

Thankfully, hyper-threading is now a “bonus.” While the original consumer processors with hyper-threading only had a single core that masqueraded as multiple cores, modern Intel CPUs now have both multiple cores and hyper-threading technology. Your dual-core CPU with hyper-threading appears as four cores to your operating system, while your quad-core CPU with hyper-threading appears as eight cores. Hyper-threading is no substitute for additional cores, but a dual-core CPU with hyper-threading should perform better than a dual-core CPU without hyper-threading.

Multi-Core CPUs

Originally, CPUs had a single core. That meant the physical CPU had a single central processing unit on it. To increase performance, manufacturers add additional “cores,” or central processing units. A dual-core CPU has two central processing units, so it appears to the operating system as two CPUs. A different process can be using each core at the same time. This speeds up your system, because your computer can do multiple things at once.

Unlike hyper-threading, there are no tricks here — a dual-core CPU literally has two central processing units on the CPU chip. A quad-core CPU has four central processing units, an octa-core CPU has eight central processing units, and so on.

This helps dramatically improve performance while keeping the physical CPU unit small so it fits in a single socket. There only needs to be a single CPU socket with a single CPU unit inserted into it — not four different CPU sockets with four different CPUs, each needing their own power, cooling, and other hardware. There’s less latency because the cores can communicate more quickly, as they’re all on the same chip.

Most computers now have multi-core CPUs, which is the most efficient option here. However, Intel CPUs also have hyper-threading as a bonus. Some computers that need a large amount of CPU power may have multiple CPUs, but it’s much less efficient than it sounds. Multi-core CPUs are also found on smartphones and tablets, and they’re useful for the same reason.

The more CPUs or cores a computer has, the more things it can do at once. This helps improve performance across everything you do — even if you’re just doing one thing at a time, the computer needs to perform background tasks. With a multi-core CPU, it can use other cores for such background tasks to avoid slowing your programs down. The advent of dual-core CPUs offered a dramatically better desktop experience to PC users.


To find the number of socket's:
$ cat /proc/cpuinfo | grep "^physical id" | sort | uniq | wc -l
2

To find the number of cores per CPU:
$ cat /proc/cpuinfo | grep "^cpu cores" | uniq
cpu cores       : 4

The total number of processors available is the number of physical CPUs multiplied by the number of cores per CPU.

To find the total number of processors:
$ cat /proc/cpuinfo | grep "^processor" | wc -l
16

To find the threads per socket
$ lscpu | grep -i thread

What is Shebang

Starting a Script With #!. It is called a shebang or a "bang" line.

It is nothing but the absolute path to the Bash interpreter.

It consists of a number sign and an exclamation point character (#!), followed by the full path to the interpreter such as /bin/bash.

All scripts under Linux execute using the interpreter specified on a first line. Almost all bash scripts often begin with #!/bin/bash (assuming that Bash has been installed in /bin)

This ensures that Bash will be used to interpret the script, even if it is executed under another.

The shebang was introduced by Dennis Ritchie between Version 7 Unix and 8 at Bell Laboratories. It was then also added to the BSD line at Berkeley [4].


In computer science, an interpreter is a computer program that directly executes, i.e. performs, instructions written in a programming or scripting language, without previously compiling them into a machine language program.

Friday, April 28, 2017

Use of shopt command

1) SHOPT

Shopt(SHell OPTions) is a built-in command to change the properties of a shell such as..
Its history behavior
Spell check

Enable special characters for echo command by default, and many more.

This is an excellent command which give more control on Shell for you when you are working on bash and sh shells. This command is available after bash v2 and not available in other shells such as ksh, csh etc. Lets get familiarize with shopt command with some examples.
shopt is a shell builtin command to set and unset (remove) various Bash shell options. To see current settings, type:

# shopt

Sample outputs:

autocd           off
sourcepath               on
xpg_echo                  off

To enable (set) option use the following command:
# shopt -s histappend

To disable (unset) option use the following command:

# shopt -u histappend

Example5: Append history but not over write the history. Many of us see that some of our commands vanish or deleted in our history when we logout and login. We will be shocked to see that they are missing command in history. This is a default behavior of your shell to over write the the previous sessions with the last closed session. For example you initiated two sessions, say it as session1 and session2. When you close session2 first and then session1 next, you will lost session2 history in history command, in other words we can say your session2 is over written by session1 history. If you don’t want lose your all sessions history use histappend option with shopt command as shown below. Keep this command in ~/.bashrc file.

shopt -s histappend

From now on words you can see all your history without any issue(with merged with your the previous sessions). Do you want to know what time you executed your commands in history

PROBLEM WITH SPELL CHECKS?

Example 6: shopt is an excellent command used to check spellings too. With this command we can make cd command to correct any spelling mistakes in the directory structure when changing directories. Enable cdspell option with shopt command as shown below

#shopt -s cdspell

After this if you try to change directory /var/ftp but typed wrong as /var/fdp this error is taken care once the above option is set.
cd /var/fdp
pwd
/var/ftp

PROBLEM WITH ECHO COMMAND?

Example 7: By default echo command will not understand special characters such as /n( for new line), /r( for return) etc. see below example.

echo “This is first line\n now on second line”
This is first line\n now on second line

If you observe \n is displayed literally. To avoid this we can enable xpg_echo option as show below.

# shopt -s xpg_echo
Now if you run above echo command see the output.
This is first line
now on second line

Note: This can be achieved with -e option along echo command

GLOB OPTIONS

Globing is a concept to expand a pattern. By default bash can understand *(For generating multiple alphanumerics ), ?(For generating single alpha-numeric) and [-](For range of chars) globing options. In shopt we have Golb(al) options which are very much useful for wildcards and other stuff. some of the as follows.
dotglob –IF set echo * command will show even hidden files too.
extglob –If set This will exclude the matches of search pattern
failglob –If set, and no matches are found, an error message is printed and the command is not executed.
nocaseglob –If set this will ignore case when listing of files.
nullglob –If set, pattern match to no files to expand to a null string, rather than themselves

There are many another things your shopt command will do. A Good Reference for all the options: