How to setup two IP address on single NIC in Linux
Setting up two IP address on one NIC
This is a small how-to to set up two are more IP address on single LAN card. There are some times which require two IP address to set up so that we can make a Linux box as a router. This can be possible without even having two NIC cards. We can configure two different IP address on single Network Card as shown below.
Setting up 2 IP address on “One” NIC. This example is on ethernet.
STEP 1:Setting up first IP address. Edit /etc/sysconfig/network-scripts/ifcfg-eth0 on Redhat Linux box and give the following entries as shown.
vi /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
BOOTPROTO=static
IPADDR=192.168.1.10
NETMASK=255.255.255.0
NETWORK=192.168.1.0
ONBOOT=yes
STEP 2: Setting up second IP address. Create one file as /etc/sysconfig/network-scripts/ifcfg-eth0:1 and give the entries as below in to this file.
vi /etc/sysconfig/network-scripts/ifcfg-eth0:1
DEVICE=eth0:1
BOOTPROTO=static
IPADDR=192.168.1.11
NETMASK=255.255.255.0
NETWORK=192.168.1.0
ONBOOT=yes
STEP 3: Once you configure above files and save them. Now reload the network service on your machine.
service network reload
STEP 4: Check if you get the IP address assigned to the eth0 and eth0:1 interfaces respectively.
ifconfig
Note1: We can assign virtual IP to the same interface with ifconfig but that one is not permanent so not giving info on that.
Note2: We can assign up to 16 virtual IP address to a single NIC card.
This is asked in some of the interviews. We can get default gateway information in linux in many ways. Such as viewing network card file or executing a command.
To see default gateway by view network interface card file content.
#cat /etc/sysconfig/networking/devices/interface-no
Example :
[root@example ~]# cat /etc/sysconfig/networking/devices/ifcfg-eth0
# Intel Corporation 82546EB Gigabit Ethernet Controller (Copper)
DEVICE=eth0
BOOTPROTO=none
HWADDR=00:02:a5:4c:af:99
ONBOOT=yes
TYPE=Ethernet
NETMASK=255.255.255.0
IPADDR=10.200.0.21
GATEWAY=10.200.0.1
Through route command
route
Example:
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.0.0 * 255.255.255.0 U 0 0 0 eth1
10.78.0.0 * 255.255.255.0 U 0 0 0 eth0
169.254.0.0 * 255.255.0.0 U 0 0 0 eth1
default 10.200.0.1 0.0.0.0 UG 0 0 0 eth0
Through ip route command
ip route
Example :
[root@example ~]# ip route
10.78.0.0/24 dev eth0 proto kernel scope link src 10.78.0.21
default via 10.200.0.1 dev eth0
Through netstat command
netstat -r
Example:
[root@example ~]# netstat -r
Kernel IP routing table
Destination Gateway Genmask Flags MSS Window irtt Iface
192.168.0.0 * 255.255.255.0 U 0 0 0 eth1
10.78.0.0 * 255.255.255.0 U 0 0 0 eth0
169.254.0.0 * 255.255.0.0 U 0 0 0 eth1
default 10.200.0.1 0.0.0.0 UG 0 0 0 eth0
Note:For any system/server there will be only one default gateway, if suppose if you assign two default gateways to your machine, your system will not come to know where to send the packets.
I have a system with two Lan cards
Eth0 is assigned with 10.22.33.21 IP address and default gateway is 10.22.33.1
Eth1 is assigned with 192.168.0.1 IP address and default gateway is 192.168.0.100
Now the system communicate with any system in these networks(10.22.33.0 and 192.168.0.0). The answer is no.
Never try to assign multiple gateways to a system if it have more than one LAN card.
Just assign a single gateway for effective communication.
So in this example we can have two scenarios:
Either you can assign Eth0 10.22.33.21 IP address with default gateway as 10.22.33.1 and Eth1 should have just an IP 192.168.0.1 no default gate for this Eth1 interface
Or
Eth0 10.22.33.21 IP address without default gateway and Eth1 IP address 192.168.0.1 with default gateway as 192.168.0.100.
You may get doubt if I assign multiple default gateways what will happen?
Ans : You cannot communicate/ping properly to other networks. If you want to try this out try on a test network and feel the difference.
How to implement ip forwarding in Linux
IP forwarding is a concept to make Linux machine to send data from one network to another, this is same as a router(A router is a device to send packets from one point to other point depending on the packet destination/rules etc).
Why we need IP forwarding on a Linux machine?
Ans : We need IP forwarding on a Linux machine because to make it as a router or proxy server to share one internet connection to many client machines.
Let me explain how this will work with small example.
You have 2 machines which are in different network(PC1 in 10.0.0.0/255.0.0.0 network and PC2 in 192.168.0.0/255.255.255.0 network) and connected with a Linux machine(which is having two network interfaces). The IP address is as follows..
PC1: 192.168.0.1/255.255.255.0 default gateway:192.168.0.2
PC2: 10.0.0.1/255.0.0.0 default gateway:10.0.0.2
Linuxbox eth0 : 192.168.0.2/255.255.255.0
eth1 : 10.0.0.2/255.0.0.0
and Linuxbox is having two LAN cards which are connected to both the
machines as shown below
So do you think PC1 is capable of communicating with PC2?
Ans : The answer to this question is No.
How to make PC1 to communicate with PC2?
Ans : The answer is enable ip forwarding on Linux machine. Some times this is known as bridging two networks.
To make IP forwarding we have to edit /etc/sysctl.conf as shown below. Open sysctl.conf and change the value of “net.ipv4.ip-forard” from 0 to 1 and save the file
#vi /etc/sysctl.conf
net.ipv4.ip-forard = 0
to
net.ipv4.ip-forard = 1
Once its done still you are not able to ping from PC1 to PC2. We have to restart the linuxbox to take this update to kernel.
Why to restart if it’s a production machine try below command to make your linuxbox aware of IP forwarding without a restart.
echo 1 > /proc/sys/net/ipv4/ip-forward
Now try to ping from PC1 to PC2 which will ping successfully.
How to implement RAID10?
RAID10 can be implemented by first implement RAID1(ie mirring) then implementing RAID0(stripe set on different disks) on it.
Configuring RAID10
Step1:Get the info who many devices are participating, for example here we taken 4 disks(/dev/sda1,/dev/sdb1,/dev/sdc1,/dev/sdd1).
Step2:Implement RAID1 on four drives(taking 2 each)
#mdadm –create /dev/md0 –level=1 –raid-devices=2 /dev/sd[ab]1
#mdadm –create /dev/md1 –level=1 –raid-devices=2 /dev/sd[cd]1
Step3:Now implement RAID0 on two of RAID1 devices(/dev/md0,/dev/md1)
#mdadm –create /dev/md2 –chunk=64 –level=0 –raid-devices=2 /dev/md[01]
Step4:Format the RAID10 device with ext3 and mount the device
#mke2fs -j /dev/md2
#mkdir /store
#mount /dev/md2 /store
Unconfiguring RAID10
Step1:Unmount the RAID device /dev/md2
#umount /dev/md2 or #umount /store
Step2:Stop the RAID device
#mdadm –manage /dev/md2 –stop
#mdadm –manage /dev/md1 –stop
#mdadm –manage /dev/md0 –stop
Step3:Remove the Disks(/dev/sda1,/dev/sdb1,/dev/sdc1,/dev/sdd1) by using fdisk utility
Please visit http://www.linuxnix.com for more linux admin stuff
RAID01 Vs RAID10
So what is the difference between RAID01 and RAID10?
This is bit tricky question, Recently I came to know about this one in an interview. so do both are same?
No both are not same.
When we are dealing with RAID01 we are actually implementing RAID0 first then RAID1 on it. Ok little bit confused?
Let me put it in this way RAID0 is nothing but stripeset writing of data and RAID1 is Mirring of data on to disks.For example lets take 8 disks, so first we are writing whole data on 4 disks then we are mirring it on to remaining disks.
Where as in RAID10 we are first mirring disk and then striping data on mirrered disks
In general RAID01 is “a mirrior of 2 strips” and RIAD10 is “a single strip on mirrered disks”
So here one more question arises… which one is good?
RAID10 is good, the difference is that the chance of system failure with two drive failures in a RAID 0+1 system with two sets of drives is (n/2)/(n – 1) where “n” is the total number of drives in the system. The chance of system failure in a RAID 1+0 system with two drives per mirror is 1/(n – 1). So, using the 8 drive systems shown in the diagrams, the chance that loosing a second drive would bring down the RAID system is 4/7 with a RAID 0+1 system and 1/7 with a RAID 1+0 system.
Please visit http://www.linuxnix.com for more linux admin stuff
What is disk quota?
Ans : Disk quota is nothing but restricting the disk-space usage to the users. We have to remember one thing when we are dealing with disk quota i.e Disk Quota can be applied only on disks/partitions not on files and folders.
So how we can implement disk quota?
Disk quota can be implemented in two ways
a. On INODE
b. On BLOCK
What is an INODE?
Ans : In Linux every object is consider as file, every file will be having an inode number associated and this is very much easy for computer to recognise where the file is located.
Inode stands for Index Node, and is the focus of all file activities in the UNIX file-system.
Each file has one inode that defines the file’s type (regular, directory, device etc),The location on disk, The size of the file, Access permissions, Access times.
Note that the file’s name is not stored in the inode.
So how to know what is your file Inode number?
Ans : Its just simple execute ls -i on your file.
ls -i xmls.txt
13662 xmls.txt
I think now you got what is INODE? Lets move on to BLOCK.
A block usually represents one least size on a disk, usually one block equal to 1kb. Some terms in Disk quota.
Soft limit : This is the disk limit where the user gets just a warning message saying that your disk quota is going to expire. This is just a warning, no restriction on data creation will occur at this point.
Hard limit : This is the disk limit where user gets error message, I repeat user gets error message stating that unable to create data.
Implementing QUOTA :
Step1 : Select/prepare the partition for quota, most of the time disk quota is implemented for restricting users not to create unwanted data on servers, so we will implement disk quota on /home mount point.
#vi /etc/fstab
Edit the /home mount point as follows
Before editing
/dev/hda2 /home ext3 defaults 0 0
after editing
/dev/hda2 /home ext3 defaults,usrquota 0 0
Step2 : Remounting the partition(this is done because the mount table should be updated to kernel). Other wise you can reboot the system too for updating of mount table, which is not preferred for live servers.
#mount -o remount,rw /home
Here -o specifies options, with remounting /home partition with read and write options.
Step3 : Creating quota database
#quotacheck -cu /home
The option -c for creating disk quota DB and u for user
Check for user database is created or not when you give ls /home you have to see auota.user file in /home directory,which contains user database.
Step4 : Switching on quota
#quotaon /home
Now get the report for default quota values for user surendra
#repquoata -a | grep surendra
surendra_anne -- 4 0 0 1 0 0
surendra_a -- 4 0 0 1 0 0
surendra_test -- 16 0 0 4 0 0
Step5 : Now implementing disk quota for user phani on /home mount point(/dev/hda2)
#setquota -u surendra_anne 100 110 0 0 /dev/hda2
Step6 : Checking quota is implemented or not login to user surendra_anne and execute this command
#repquota -a
or
#quota
Step7 : Keep creating data, once 100MB is reached user will get an warning message saying, and when he reaches 110MB he can not create any more data.
Hint : To create a data file you can use seq command as below
#seq 1 10000 > test.txt
this command will create a file with 10000 lines with numbers in it.
Removing quota :
To do this one, all the users should log out from the system so better do it in runlevel one.
Step8 : Stop the disk quota
#quotaoff /home
Step9 : Removing quota database which is located /home
#rm /home/aquota.user
Step10 : Edit fstab file and remove usrdata from /home line
#vi /etc/fstab
Before editing
/dev/hda2 /home ext3 defaults,usrquota 0 0
After editing
/dev/hda2 /home ext3 defaults 0 0
Step11 : Remount the /home partition
#mount -o remount,rw /home
That’s it you are done with Disk Quota Implementation in Linux. Now test your self in creating Linux user disk quota on your own.
What is a sticky Bit and how to set it in Linux?
This is next to SGID in our ongoing File and Folder permissions in Linux. We already discussed about CHMOD, UMASK, CHOWN, CHGRP SGID and SUID File and folder permissions etc in our previous posts. In this post we will see
What is Sticky Bit?
Why we require Sticky Bit?
Where we are going to implement Sticky Bit?
How to implement Sticky Bit in Linux?
What is Sticky Bit?
Sticky Bit is used mainly on folders in order to avoid deletion of a folder and its content by other user though he is having write permissions. If Sticky bit is enabled on a folder, the folder is deleted by only owner of the folder and super user(root). This is a security measure to suppress deletion of critical folders where it is having full permissions by others.
Learn Sticky Bit with examples:
Example: Create a project(A folder) where people will try to dump files for sharing, but they should not delete the files created by other users.
How can I setup Sticky Bit for a Folder?
Sticky Bit can be set in two ways
1) Symbolic way (t,represents sticky bit)
2) Numerical/octal way (1, Sticky Bit bit as value 1)
Use chmod command to set Sticky Bit on Folder: /opt/dump/
Symbolic way:
chmod o+t /opt/dump/
or
chmod +t /opt/dump/
Let me explain above command we are setting Sticky Bit(+t) to folder /opt/dump by using chmod command.
Numerical way:
chmod 1757 /opt/dump/
Here in 1757, 1 indicates Sticky Bit set, 7 for full permissions for owner, 5 for read and execute permissions for group, and ful permissions for others.
Checking if a folder is set with Sticky Bit or not?
Use ls –l to check if the x in others permissions field is replaced by t or T
For example: /opt/dump/ listing before and after Sticky Bit set
Before Sticky Bit set:
ls -l
total 8
-rwxr-xrwx 1 xyz xyzgroup 148 Dec 22 03:46 /opt/dump/
After Sticky Bit set:
ls -l
total 8
-rwxr-xrwt 1 xyz xyzgroup 148 Dec 22 03:46 /opt/dump/
Some FAQ’s related to Sticky Bit:
Now sticky bit is set, lets check if user “temp” can delete this folder
$ rm -rf /opt/dump
rm: cannot remove `/opt/dump’: Operation not permitted
$ ls -l /opt
total 8
drwxrwxrwt 4 xyz xyzgroup 4096 2012-01-01 17:37 dump
$
if you observe he is unable to delete and even xyz user creted any files in dump folder, temp user can not delete it. But xyz user can crete and delte files in dump folder.
I am seeing “T” ie Capital s in the file permissions, what’s that?
After setting Sticky Bit to a file/folder if you see ‘T’ in the file permission area that indicates that the file/folder does not have executable permissions for others on that particular file/folder.
Sticky bit without Executable permissions:
so if you want executable permissions too, apply executable permissions to the file.
chmod o+x /opt/dump/
output: -rwxr-xrwt 1 xyz xyzgroup 0 Dec 5 11:24 /opt/dump/
Sticky bit with Executable permissions:
you should see a smaller ‘s’ in the executable permission position.
How can I find all the Sticky Bit set files in Linux/Unix.
find / -perm +1000
The above find command will check all the files which is set with Sticky Bit bit(1000).
Can I set Sticky Bit for files?
Yes, but most of the time its not required.
How can I remove Sticky Bit bit on a file/folder?
chmod o-t /opt/dump/
What is SGID and how to set SGID in Linux/Unix?
This is next to SUID in our ongoing File and Folder permissions in Linux. We already discussed about CHMOD, UMASK, CHOWN, CHGRP and SUID File and folder permissions etc in our previous posts. In this post we will see
What is SGID?
Why we require SGID?
Where we are going to implement SGID?
How to implement SGID in Linux?
What is SGID?
SGID (Set Group ID up on execution) is a special type of file permissions given to a file/folder. Normally in Linux/Unix when a program runs, it inherits access permissions from the logged in user. SGID is defined as giving temporary permissions to a user to run a program/file with the permissions of the file group permissions to become member of that group to execute the file. In simple words users will get file Group’s permissions when executing a Folder/file/program/command.
SGID is similar to SUID. The difference between both is that SUID assumes owner of the file permissions and SGID assumes group’s permissions when executing a file instead of logged in user inherit permissions.
Learn SGID with examples:
When implementing Linux Group quota for group of people SGID plays an important role in checking the quota timer. SGID bit set on folder is used to change their inherit permissions to group’s permissions to make it as single user who is dumping data. So that group members whoever dumps the data the data will be written with group permissions and inturn quota will be reduced centrally for all the users. For clear understanding of this you have to implement group quota from the above link. Without implementation of SGID the quota will not be effective.
How can I setup SGID for a file?
SGID can be set in two ways
1) Symbolic way (s)
2) Numerical/octal way (2, SGID bit as value 2)
Use chmod command to set SGID on file: file1.txt
Symbolic way:
chmod g+s file1.txt
Let me explain above command we are setting SGID(+s) to group who owns this file.
Numerical way:
chmod 2750 file1.txt
Here in 2750, 2 indicates SGID bitset, 7 for full permissions for owner, 5 for write and execute permissions for group, and no permissions for others.
How can I check if a file is set with SGID bit or not?
Use ls –l to check if the x in group permissions field is replaced by s or S
For example: file1.txt listing before and after SGID set
Before SGID set:
ls -l
total 8
-rwxr--r-- 1 xyz xyzgroup 148 Dec 22 03:46 file1.txt
After SGID set:
ls -l
total 8
-rwxr-sr-- 1 xyz xyzgroup 148 Dec 22 03:46 file1.txt
Some FAQ’s related to SGID:
Where is SUID used?
1) When implementing Linux group disk quota.
I am seeing “S” ie Capital s in the file permissions, what’s that?
After setting SUID or SGID to a file/folder if you see ‘S’ in the file permission area that indicates that the file/folder does not have executable permissions for that user or group on that particular file/folder.
chmod g+s file1.txt
output:
-rwxrwSr-x 1 surendra surendra 0 Dec 27 11:24 file1.txt
so if you want executable permissions too, apply executable permissions to the file.
chmod g+x file1.txt
output:
-rwxrwsr-x 1 surendra surendra 0 Dec 5 11:24 file1.txt
you should see a smaller ‘s’ in the executable permission position.
How can I find all the SGID set files in Linux/Unix.
find / -perm +2000
The above find command will check all the files which is set with SGID bit(2000).
Can I set SGID for folders?
Yes, you can if it’s required (you should remember one thing, that Linux treats everything as a file)
How can I remove SGID bit on a file/folder?
chmod g-s file1.t
There are some other special permission apart from the normal file permissions read, write and execute. They are SUID, SGID, Sticky Bit, ACL’s and SELinux etc for granular file/folder management by administrator. In this post we will see
1)What’s SUID?
2)How to set SUID?
3)Where to us SUID?
What is SUID and how to set it in Linux?
SUID (Set owner User ID up on execution) is a special type of file permissions given to a file. Normally in Linux/Unix when a program runs, it inherits access permissions from the logged in user. SUID is defined as giving temporary permissions to a user to run a program/file with the permissions of the file owner rather that the user who is running it. In simple words users will get file owner’s permissions as well as owner UID and GID when executing a file/program/command.
The above sentence is bit tricky and should be explained in depth with examples.
Learn SUID with examples:
Example1: passwd command
When we try to change our password we will use passwd command which is owned by root. This passwd command file will try to edit some system config files such as /etc/passwd, /etc/shadow etc when we try to change our password. Some of these files cannot be opened or viewed by normal user only root user will have permissions. So if we try to remove SUID and give full permissions to this passwd command file it cannot open other files such as /etc/shadow file to update the changes and we will get permission denied error or some other error when tried to execute passwd command. So passwd command is set with SUID to give root user permissions to normal user so that it can update /etc/shadow and other files.
Example2: ping command
Similarly if we take ping command, when we have to execute this command internally it should open socket files and open ports in order to send IP packets and receive IP packets to remote server. Normal users don’t have permissions to open socket files and open ports. So SUID bit is set on this file/command so that whoever executes this will get owner (Root user’s) permissions to them when executing this command. So when this command start executing it will inherits root user permissions to this normal user and opens require socket files and ports.
Example3: crontab and at command.
When scheduling the jobs by using crontab or at command it is obious to edit some of the crontab related configuration files located in /etc which are not writable for normal users. So crontab/at commands are set with SUID in-order to write some data.
How can I setup SUID for a file?
SUID can be set in two ways
1) Symbolic way(s, Stands for Set)
2) Numerical/octal way(4)
Use chmod command to set SUID on file: file1.txt
Symbolic way:
chmod u+s file1.txt
Here owner permission execute bit is set to SUID with +s
Numerical way:
chmod 4750 file1.txt
Here in 4750, 4 indicates SUID bitset, 7 for full permissions for owner, 5 for write and execute permissions for group, and no permissions for others.
How can I check if a file is set with SUID bit or not?
Use ls –l to check if the x in owner permissions field is replaced by s or S
For example: file1.txt listing before and after SUID set
Before SUID set:
ls -l
total 8
-rwxr--r-- 1 xyz xyzgroup 148 Dec 22 03:46 file1.txt
After SUID set:
ls -l
total 8
-rwsr--r-- 1 xyz xyzgroup 148 Dec 22 03:46 file1.txt
Some FAQ’s related to SUID:
A) Where is SUID used?
1) Where root login is required to execute some commands/programs/scripts.
2) Where you dont want to give credentials of a perticular user and but want to run some programs as the owner.
3) Where you dont want to use sudo command but want to give execute permission for a file/script etc.
B) I am seeing “S” I.e. Capital “s” in the file permissions, what’s that?
After setting SUID to a file/folder if you see ‘S’ in the file permission area that indicates that the file/folder does not have executable permissions for that user on that particular file/folder.
For example see below example
chmod u+s file1.txt
ls -l
-rwSrwxr-x 1 surendra surendra 0 Dec 27 11:24 file1.txt
If you want to convert this S to s then add executable permissions to this file as show below
chmod u+x file1.txt
ls -l
-rwsrwxr-x 1 surendra surendra 0 Dec 5 11:24 file1.txt
you should see a smaller ‘s’ in the executable permission position now.
SUID with execute permissions:

SUID with out execute permissions:
C) How can I find all the SUID set files in Linux/Unix.
find / -perm +4000
The above find command will check all the files which is set with SUID bit(4000).
D) Can I set SUID for folders?
Yes, you can if its required(you should remember one thing, that Linux treats everything as a file)
E) What is SUID numerical value?
It has the value 4 for SUID.
Please comment your thoughts about SUID usage in your company