Sunteți pe pagina 1din 25

How To Find IP Conflicts In Linux

This tool sends ARP (Address Resolution Protocol) packets on local network to collect the
addresses. If there is multiple MAC addresses claiming the same IP address, then there is
conflict.

To install arp-scan on Ubuntu or Debian type:

$ sudo apt-get install arp-scan


For Fedora, CentOS or Redhat:

$ sudo yum install arp-scan


To detect IP conflicts, run the following:

$ sudo arp-scan –I eth0 -l


An output example:

192.168.1.10 00:1b:a9:63:a2:4c BROTHER INDUSTRIES, LTD.

192.168.1.30 00:1e:8f:58:ec:49 CANON INC.

192.168.1.33 00:25:4b:1b:10:20 Apple, Inc

192.168.1.37 10:9a:dd:55:d7:95 Apple Inc

192.168.1.38 20:c9:d0:27:8d:56 (Unknown)

192.168.1.39 d4:85:64:4d:35:be Hewlett Packard

192.168.1.39 00:0b:46:e4:8e:6d Cisco (DUP: 2)

192.168.1.40 90:2b:34:18:59:c0 (Unknown)


In this instance, the 19.168.1.39 IP is in conflict since it appears twice.

Q. I run quite large network with different subnets. I’d like to know how can I find out duplicate IP address under
Linux / UNIX?

A. You can use arpping command. The arping utility performs an action similar to ping command, but at the Ethernet
layer. You can send ARP REQUEST to a neighbor host / computers.

Task: Send ARP request


Let us find out reachability of an IP on the local Ethernet with arping i.e send ARP request 192.168.1.1:
$ sudo arping -I eth0 -c 3 192.168.1.1
Output:
ARPING 192.168.1.1 from 192.168.1.106 ra0

Unicast reply from 192.168.1.1 [00:18:39:6A:C6:8B] 2.232ms

Unicast reply from 192.168.1.1 [00:18:39:6A:C6:8B] 1.952ms

Sent 3 probes (1 broadcast(s))

Received 3 response(s)

Where,

 -I eth0 : Specify network interface i.e. name of network device where to send ARP REQUEST packets. This
option is required.
 -c 3 : Stop after sending 3 ARP REQUEST packets

Task: Find duplicate IP


The -D option specifies duplicate address detection mode (DAD). It returns exit status 0, if DAD succeeded i.e. no
replies are received.
$ sudo arping -D -I eth0 -c 2 192.168.1.1
If 192.168.1.1 duplicated you should see zero exit status:
$ echo $?
Always use following syntax for duplicate address detection with arping:
$ sudo arping -D -I <interface-name> -c 2 <IP-ADDRESS-TO-TEST>
$ echo $?

How to Set Static IP Address and Configure


Network in Linux
IP address: 192.168.0.100 Netmask: 255.255.255.0 Hostname: node01.tecmint.com Domain
name: tecmint.com Gateway: 192.168.0.1 DNS Server 1: 8.8.8.8 DNS Server 2: 4.4.4.4

Configure Static IP Address in RHEL/CentOS/Fedora:


To configure static IP address in RHEL / CentOS / Fedora, you will need to edit:

/etc/sysconfig/network

/etc/sysconfig/network-scripts/ifcfg-eth0
Where in the above "ifcfg-eth0" answers to your network interface eth0 . If your
interface is named “ eth1" then the file that you will need to edit is "ifcfg-eth1" .
Let’s start with the first file:

# vi /etc/sysconfig/network

Open that file and set:

NETWORKING=yes

HOSTNAME=node01.tecmint.com

GATEWAY=192.168.0.1

NETWORKING_IPV6=no

IPV6INIT=no

Next open:

# vi /etc/sysconfig/network-scripts/ifcfg-eth0

Note: Make sure to open the file corresponding to your network interface. You can find
your network interface name with ifconfig -a command.
In that file make the following changes:

DEVICE="eth0"

BOOTPROTO="static"

DNS1="8.8.8.8"

DNS2="4.4.4.4"

GATEWAY="192.168.0.1"

HOSTNAME="node01.tecmint.com"
HWADDR="00:19:99:A4:46:AB"

IPADDR="192.68.0.100"

NETMASK="255.255.255.0"

NM_CONTROLLED="yes"

ONBOOT="yes"

TYPE="Ethernet"

UUID="8105c095-799b-4f5a-a445-c6d7c3681f07"

You will only need to edit the settings for:

1. DNS1 and DNS2


2. GATEWAY
3. HOSTNAME
4. NETMASK
5. IPADDR
Other settings should have already been predefined.

Next edit resolve.conf file by opening it with a text editor such as nano or vi:

# vi /etc/resolv.conf

nameserver 8.8.8.8 # Replace with your nameserver ip

nameserver 4.4.4.4 # Replace with your nameserver ip

Once you have made your changes restart the networking with:

# /etc/init.d/network restart [On SysVinit]

# systemctl restart network [On SystemD]


Set Static IP Address in Debian / Ubuntu
To setup static IP address in Debian/ Ubuntu, open the following file:

# nano /etc/network/interfaces

You may see a line looking like this:

auto eth0

iface eth0 inet dhcp

Change it so it looks like this:

auto eth0

iface eth0 inet static

address 192.168.0.100

netmask 255.255.255.0

gateway 192.168.0.1

dns-nameservers 4.4.4.4

dns-nameservers 8.8.8.8

Save the file and then edit /etc/resolv.conf like this:

# nano /etc/resolv.conf

nameserver 8.8.8.8 # Replace with your nameserver ip

nameserver 4.4.4.4 # Replace with your nameserver ip

Restart the networking on your system with:


# /etc/init.d/network restart [On SysVinit]

# systemctl restart network [On SystemD]

Your static IP address has been configured.

How to configure IP address in Linux


This tutorial explains how to view and configure IP address in Linux step by step. Learn network
configuration commands (nmcli, nmtui, nm-connection-editor, ifdown, ifup and ip addr show) and
directives (BOOTPROTO, DEFROUTE, IPADDR, PREFIX, GATEWAY, DNS and ONBOOT) in detail with
practical examples.

The most essential part of networking is assigning IP address, default gateway and DNS server IP
address. An IP address is the unique identity of a computer in network. Without it computer will not
be able to communicate with other computers in network. Before we learn how to assign an IP
address, let’s learn how to view the current IP configuration.

How to view the IP address in Linux

To view the current IP configuration we can use ip addr show command. This command supports
abbreviate form which means we can use ip a s command instead of ip addr show command. Even
more we can use just ip a command. By default this command will list current IP configuration of all
interfaces.

Output includes following information :-

Current State :- First line shows the current state of interface. UP keyword indicates that interface is
currently UP and functioning properly.
MAC Address :- Next line shows the hardware (MAC) address with broadcast address of interface.

IPv4 Address :- This line shows the current IPv4 configuration (IP Address, Subnet Mask, Broadcast
address) on interface.

IPv6 Address :- This line shows the current IPv6 configuration on interface.

Interface Name :- First keyword shows the name of interface. In earlier version NICs were named as
eth (Ethernet), em (embedded) and wlan (wireless lan). NICs were numbered based on the order of
detection during the system boot. For example first discovered Ethernet (eth) would be named and
numbered as eth0. This naming scheme has scalability issue. For example a system which has multiple
NICs and NICs are dynamically removed or added, in this situation this naming scheme will work not
effectively. From RHEL7 default naming convention for NICs is based on firmware, device, topology
and device types. With new naming convention network card names always remain consist. In new
naming convention :-

 First two letters represents the type of NIC. For Ethernet interface it always begins with en, for WLAN interfaces
it begins with wl, for loopback adapters it starts with lo and for WWAN it begins with ww.
 Next letter represents the location of NIC. For onboard letter o is used, for hot plug slot letter a is used and for
PCI slot letter p is used.
 Remaining numeric numbers represents the index, port or ID number.

For example above figure illustrates the name used by NIC is eno16777734. In this name en stands for
Ethernet device, o stands for onboard and 16777734 stands for unique index number. Combinedly
eno16777736 stands for an onboard Ethernet device (with its unique index number).

To view the IP configuration of a specific interface we can use its name as argument with ip addr
show command.

To view only link state, use ip link show command for all interfaces and ip link show
[interface] command for a particular interface.
To view the default gateway IP use ip route show command.

To add new (or additional) temporary IP configuration on NIC we can use ip addr add dev
[inteface] command.

IP configuration added by this command will not be available at next login.

Okay now we know how to view the IP configuration. But have you wonder from where this IP
configuration comes? Let’s figure out the answer.

Every interface has an associated configuration file which stores its relative parameters such IP
address, protocol and configuration values. These configuration settings are automatically applied on
interface while it get activated. RHEL uses a central location for all (NICs) configuration files which
is /etc/sysconfig/network-scripts directory.
Configuration file’s name start with ifcfg and followed by the name of network card. For example
configuration file for eno16777736 Ethernet card will be ifcfg-eno16777736.

Let’s have a look in this file and understand some important configuration settings

Key directives (configuration settings)

BOOTPROTO :- This directive defines how this interface should receive IP configuration. There are
four valid options; dhcp, bootp, none and static.

 dhcp :- to obtain IP address from DHCP Server


 bootp :- to boot off of a network boot server and get IP
 none :- no booting protocol will be used
 static :- to use a static IP address

DEFROUTE :- This directive defines whether to use this interface as default route or not.

IPADDR :- This directive is used to assign the IP address on this interface.

PREFIX :- This directive is used to set the subnet mask.

GATEWAY :- This directive is used to set the default gateway IP address.

DNS1 :- This directive is used to specify the first DNS sever IP address.

ONBOOT :- This directive defines whether to activate this interface at system boot or not.
How to configure IP Address

To configure IP address we have three options; nmtui text tool, nm-connection-editor graphic utility
and nmcli command. We will understand all three options one by one. Let’s start with nmtui text tool.

Execute nmtui command from root account.

Select “Edit a connection” and press enter key.

Select NIC from left pane on which you want to set the IP address and select Edit option from right
pane and press Enter key.

Select Automatic from IPv4 configuration and press enter key


To obtain IP from DHCP Sever keep Automatic. To set static IP use Manual option.

Navigate to Show option and press Enter key

Set the IP address / subnet mask, Gateway IP address and DNS Server IP address.
Select OK and press Enter key to accept the change

Select Quit and press Enter key

When we exit from nmtui utility, it automatically updates the associated configuration files.
For instance in above example we have assigned IP configuration to NIC eno16777736. In this case
nmtui will update the /etc/sysconfig/network-scripts/ifcfg-eno16777736 file. An interface
restart is required to apply the new configuration. We can use ifdown-[NIC] and ifup-
[NIC] commands to restart the interface.

We have successfully assigned the IP address from nmtui text tool.

Now let’s do the same task from nm-connection-editor graphical utility. Login from root account and
open terminal. In terminal execute nm-connection-editor command.

From opened window select appropriate NIC and click Edit option
Switch to IPv4 Settings and select Manual option from Method drop down menu. Click Add button
and configure IP addresses in respective fields and click Save button and Close button at main
screen.
Restart the interface and verify the new IP configuration.

So far we have used two methods to assign the IP addresses.


Finally we will use nmcli command to assign the IP addresses. Login from root account and assign IP
addresses as illustrate in following figure.

nmcli con show:- This command will list the available connections with their associated devices.

A device is the network interface card and connection is the configuration used by device. A device
can have multiple connections.

As above figure shows connection eno16777736 is associated with Ethernet device eno16777736. We
will modify this connection to update the IP configuration.

nmcli con mod eno16777736 ipv4.addresses 192.168.1.100/24 :- This command will set the
new IP address and subnet mask.

nmcli con mod eno16777736 ipv4.gateway 192.168.1.1 :- This command will set the new
default gateway IP address.

nmcli con mod eno16777736 ipv4.dns 192.168.1.1 :- This command will set the new DNS Server
IP address.

As we know new IP configuration applies on start-up. Next two commands will restart the interface.

ifdown eno16777736 :- This command will shut down the interface.

ifup eno16777736 :- This command will bring up the device back.

Key points

 nm-connection-editor is available only in X-Window system. It provides less but sufficient features for end
users to manage the network connections. Network administrators rarely use this tool to manage the network
connections.
 Usually nmcli command and nmtui tool are used to manage the network connection.
 Among these options, nmcli command is the most powerful.
 nmcli command is little bit hard to learn but works in every condition.
 In exam you can use nmtui text tool instead of nmcli command.
 For real life system administration its worth to learn nmcli command as it is available in most situations such as
rescue mode, remote connection, terminal etc.

Linux Shell script to add a user with a


password to the system
A. You can easily write a shell script that reads username, password from keyboard and add to /etc/passwd and
/etc/shadow file using useradd command (create a new user command).

General syntax is as follows:


useradd -m -p encryptedPassword username

Where,

 -m : The user’s home directory will be created if it does not exist.


 useradd -p encryptedPassword : The encrypted password, as returned by crypt().
 username : Add this user to system

Task: Create an encrypted password

You need to create encrypted password using perl crypt():


$ perl -e 'print crypt("password", "salt"),"\n"'
Output:

sa3tHJ3/KuYvI

Above will display the crypted password (sa3tHJ3/KuYvI) on screen. The Perl crypt() function is a one way
encryption method meaning, once a password has been encrypted, it cannot be decrypted. The password string is
taken from the user and encrypted with the salt and displayed back on screen.

You can store an encrypted password using following syntax:


$ password="1YelloDog@"
$ pass=$(perl -e 'print crypt($ARGV[0], "password")' $password)
$ echo $pass
Output

paU5t8Al/qf6M
Sample shell script to add a user

Based upon above discussion here is a sample shell script (Download link):

#!/bin/bash

# Script to add a user to Linux system

if [ $(id -u) -eq 0 ]; then

read -p "Enter username : " username

read -s -p "Enter password : " password

egrep "^$username" /etc/passwd >/dev/null

if [ $? -eq 0 ]; then

echo "$username exists!"

exit 1

else

pass=$(perl -e 'print crypt($ARGV[0], "password")' $password)

useradd -m -p $pass $username

[ $? -eq 0 ] && echo "User has been added to system!" || echo "Failed to
add a user!"

fi

else

echo "Only root may add a user to the system"

exit 2

fi
Close and save the script:
$ ./adduser.sh
Only root may add a user to the system

Run as root:
# ./adduser
Output:

Enter username : roja

Enter password : HIDDEN

User has been added to system!

Now user roja can login with a password called HIDDEN.

As described by the others, writing to /dev/null eliminates the output of a program. Usually cron sends an
email for every output from the process started with a cronjob. So by writing the output to /dev/null you
prevent being spammed if you have specified your adress in cron.

I use >> /dev/null 2>&1 for silent cronjob, cronjob will do the job but not send report to my email.
As far as I know, don't remove /dev/null, it's useful especially when you running cpanel, can be used for
throw away cronjob report.

/dev/null - standard file that discards all you write to it, but reports that the write operation
succeeded. 1 is stdout and 2 is stderr. 2>&1 redirects stderr to stdout. &1 indicates file descriptor(stdout),
otherwise(if you use just 1) you will redirect stderr to file named 1. [any command] >>/dev/null
2>&1 redirects all stderr to stdout, and writes all of that to /dev/null.

> is for redirect


/dev/null is a black hole where any data sent, will be discarded
2 is the file descriptor for Standard Error
> is for redirect
& is the symbol for file descriptor (without it, the following 1 would be considered a filename)
1 is the file descriptor for Standard Out
Therefore >/dev/null 2>&1 is redirect the output of your program to /dev/null. Include both the Standard
Error and Standard Out.

Interactive Linux Shell Script To Create New Linux


User
Script helps to remember all main points during creating user and quickly create new user. If admin
need to create 100 users at once this script could be helpful. Script use command useradd with keys
and groupadd.
Shell Script
#!/bin/bash

while [ x$username = "x" ]; do

read -p "Please enter the username you wish to create : " username

if id -u $username >/dev/null 2>&1; then

echo "User already exists"

username=""

fi

done

while [ x$group = "x" ]; do

read -p "Please enter the primary group. If group not exist, it will be created : " group

if id -g $group >/dev/null 2>&1; then

echo "Group exist"

else

groupadd $group

fi

done

read -p "Please enter bash [/bin/bash] : " bash

if [ x"$bash" = "x" ]; then

bash="/bin/bash"

fi

read -p "Please enter homedir [/home/$username] : " homedir

if [ x"$homedir" = "x" ]; then

homedir="/home/$username"
fi

read -p "Please confirm [y/n]" confirm

if [ "$confirm" = "y" ]; then

useradd -g $group -s $bash -d $homedir -m $username

fi

Sample Result
sudo ./linux_user.sh

Please enter the username you wish to create : test

Please enter the primary group. If group not exist, it will be created : test

Please enter bash [/bin/bash] :

Please enter homedir [/home/test] :

Please confirm [y/n]y

22:12:58 [test@Desktop] :~ id test

uid=1003(test) gid=1003(test) groups=1003(test)

Learn Above Script Line by Line


#Write to console ask to enter group and save input to group variable

read -p "Please enter the primary group. If group not exist, it will be created : " group

#check if group already exist

if id -g $group >/dev/null 2>&1; then

#just warn that group already exist

echo "Group exist"

else

#if group not exist – create one more

groupadd $group
fi

#end of while loop

done

#ask to enter preferred bash

read -p "Please enter bash [/bin/bash] : " bash

#check if no input

if [ x"$bash" = "x" ]; then

#if no input, use default bash

bash="/bin/bash"

fi

#ask to enter preferred homedir

read -p "Please enter homedir [/home/$username] : " homedir

#check if no input

if [ x"$homedir" = "x" ]; then

#if no input , use default homedir

homedir="/home/$username"

fi

#ask to confirm all inputs

read -p "Please confirm [y/n]" confirm

#if input y

if [ "$confirm" = "y" ]; then

#command to add user with all entered info

useradd -g $group -s $bash -d $homedir -m $username

fi
Storage Management on Linux are classified into three types:

1. DAS(Direct?attached storage)
2. NAS (Network?attached storage)
3. SAN(Storage?Area?Network)
1. DAS(Direct?attached storage)

 Direct?attached storage (DAS) refers to a digital storage system directly connected to a server or
digital computer, while not a storage network in between.
 It’s a retronym, principally wont to differentiate non?networked storage from SAN and NAS
 A typical DAS system is created of a knowledge device connected on to a laptop through a host bus
adapter (HBA).
 Between those 2 points there’s no network device (like hub, switch, or router), and this is often the
most characteristic of DAS.
 the most protocols used for DAS connections are ATA, SATA, eSATA SCSI, SAS, and Fibre
Channel.
2. NAS (Network?attached storage)

 Network?attached storage (NAS), in distinction to SAN, uses file?based protocols like NFS or
SMB/CIFS wherever it’s clear that the storage is remote, associate degreed computers request some
of an abstract file instead of a disk block.
3. SAN(Storage?Area?Network)

 A Storage Area Network (SAN) could be a dedicated network that has access to consolidated, block
level information storage.
 SANs are primarily wont to build storage devices, like disk arrays, tape libraries, and optical
jukeboxes, accessible to servers in order that the devices appear as if regionally connected devices
to the package.
 A SAN generally has its own network of storage devices that square measure typically not
accessible through the native space network by different devices. the price and quality of SANs
born within the early 2000s to levels permitting wider adoption across each enterprise and tiny to
medium sized business environments.
Features of SAN

 Sharing storage typically simplifies storage administration


 Adds flexibility since cables and storage devices don’t ought to be physically stirred to shift storage
from one server to a different
 Ability to permit servers else from the SAN itself.
 SANs conjointly tend to modify more practical disaster recovery processes.
Linux tgtadm: Setup iSCSI Target ( SAN )

iSCSI target (server)

 Storage resource located on an iSCSI server known as a “target”.


 An iSCSI target usually represents nothing but hard disk storage.
 As with initiators, software to provide an iSCSI target is available for most mainstream operating
systems.
iSCSI initiator (client)

 An initiator functions as an iSCSI client.


 An initiator typically serves the same purpose to a computer as a SCSI bus adapter would, except
that instead of physically cabling SCSI devices (like hard drives and tape changers), an iSCSI
initiator sends SCSI commands over an IP network.
SAN Server Installation and Configuration on RHEL/ CentOS/ Scientific
Linux/ Oracle Linux 7/6/5

Step-I(Install the iscsi pkgs & start services)

# yum install *scsi*


# service tgtd start
Copy

Step-II(creates a target with id 1)

# tgtadm --lld iscsi --op new --mode target --tid 1 –T iqn.2011-


09.com.galaxy:storage.disk1.linux.sys1.xyz
Copy

Step-III(To view the current configuration)

# tgtadm --lld iscsi --op show --mode target


Copy

Step-IV(Add a logical unit to Target)

# tgtadm --lld iscsi --op new --mode logicalunit --tid 1--lun 1 -b /dev/sdb1
Copy

Step-V(To view the details)

# tgtadm --lld iscsi --op show --mode target


Copy

Step-VI(To enable the target to accept any initiators)

# tgtadm --lld iscsi --op bind --mode target --tid 1 -I ALL


Copy

SAN Client Side Configuration

Step-I(To discover targets at a given IP address)

# iscsiadm --mode discovery --type sendtargets--portal <iscsi_server_ip>


Copy

Step-II(To check at client run fdisk and mount)

# fdisk -l
# mkdir /mnt/clientsan
# mount /dev/sdb1 /mnt/clientsan
Copy
Congratulation now you have Installed SAN Storage Server & Configure on CentOS/RHEL 7/6/5
and any difficulties regarding to this use comment section below.

S-ar putea să vă placă și