Vous êtes sur la page 1sur 25

10 ping Command Usage Examples in Linux

This tutorial explains Linux ping command, options and its usage with examples.
ping send ICMP ECHO_REQUEST to network hosts

DESCRIPTION
PING (Packet INternet Groper) command is the best way to test connectivity between
two nodes. Whether it is Local Area Network (LAN) or Wide Area Network (WAN). Ping
use ICMP (Internet Control Message Protocol) to communicate to other devices. You
can ping host name of ip address using below command.
ping uses the ICMP protocols mandatory ECHO_REQUEST datagram to elicit an ICMP
ECHO_RESPONSE from a host or gateway. ECHO_REQUEST datagrams (pings)
have an IP and ICMP header, followed by a struct timeval and then an arbitrary number
of pad bytes used to fill out the packet.
SYNOPSIS
ping -s [-d] [-l] [-L] [-n] [-r] [-R] [-v] [ -i interface_address ] [-I interval] [-t ttl] host
[packetsize] [count]
OPTIONS :
-d
Set

the

SO_DEBUG

socket

option.

-l
Loose source route. Use this option in the IP header to send the packet to the given host
and

back

again.

Usually

specified

with

the

-R

option.

-L
Turn off loopback of multicast packets. Normally, if there are members in the host group
on the out- going interface, a copy of the multicast packets will be delivered to the local
machine.
-n
Show network addresses as numbers. ping normally displays addresses as host names.
-r
Bypass the normal routing tables and send directly to a host on an attached network. If
the host is not on a directly-attached network, an error is returned. This option can be
used to ping a local host through an interface that has been dropped by the router

daemon.
-R
Record route. Sets the IP record route option, which will store the route of the packet
inside the IP header. The contents of the record route will only be printed if the -v option
is given, and only be set on return packets if the target host preserves the record route
option

across

echos,

or

the

-l

option

is

given.

-v
Verbose output. List any ICMP packets, other than ECHO_RESPONSE, that are
received.
-i interface_address
Specify the outgoing interface address to use for multicast packets. The default interface
address for multicast packets is determined from the (unicast) routing tables.
-I

interval

Specify the interval between successive transmissions. The default is one second.
-t

ttl

Specify the IP time to live for unicast and multicast packets. The default time to live for
unicast packets is set with ndd (using the icmp_def_ttl variable). The default time to live
for

multicast

is

one

hop.

host
The

network

host.

packetsize
Specified

size

of

packetsize.

Default

is

count
Amount of times to send the ping request.

EXAMPLES
1. Ping the host to see if its alive
$ ping google.com
PING google.com (74.125.200.102) 56(84) bytes of data.
64 bytes from plus.google.com (74.125.200.102): icmp_req=1 ttl=128 time=172 ms
64 bytes from plus.google.com (74.125.200.102): icmp_req=2 ttl=128 time=164 ms
64 bytes from plus.google.com (74.125.200.102): icmp_req=4 ttl=128 time=165 ms
^C
--- google.com ping statistics --4 packets transmitted, 3 received, 25% packet loss, time 3013ms
rtt min/avg/max/mdev = 164.618/167.289/172.010/3.364 ms

2. Increase or Decrease the Time Interval Between Packets

64.

Increase
Ping
Time
Example: Wait for 5 seconds before sending the next packet.

Interval

$ ping -i 5 google.com

Decrease
Ping
Time
Example: Wait 0.1 seconds before sending the next packet.

Interval

# ping -i 0.1 google.com

Note: Only super user can specify interval less than 0.2 seconds. If not, youll
get the following error message.
3. Send N packets and stop
$ ping -c 4 google.com
PING google.com (74.125.135.100) 56(84) bytes of data.
64 bytes from plus.google.com (74.125.135.100): icmp_req=1
64 bytes from plus.google.com (74.125.135.100): icmp_req=2
64 bytes from plus.google.com (74.125.135.100): icmp_req=3
64 bytes from plus.google.com (74.125.135.100): icmp_req=4

ttl=128
ttl=128
ttl=128
ttl=128

time=251
time=180
time=179
time=179

ms
ms
ms
ms

--- google.com ping statistics --4 packets transmitted, 4 received, 0% packet loss, time 3005ms
rtt min/avg/max/mdev = 179.569/197.734/251.433/31.005 ms

4. Flood the network


Super users can send hundred or more packets per second using -f option. It
prints a . when a packet is sent, and a backspace is printed when a packet is
received.
# ping -f localhost
PING localhost (127.0.0.1) 56(84) bytes of data.
.^
--- localhost ping statistics --215594 packets transmitted, 215594 received, 0% packet loss, time 9417ms
rtt min/avg/max/mdev = 0.004/0.006/1.096/0.006 ms, ipg/ewma 0.043/0.006 ms

5. Print Only Ping Command Summary Statistics


$ ping -c 5 -q 127.0.0.1
PING 127.0.0.1 (127.0.0.1) 56(84) bytes of data.
--- 127.0.0.1 ping statistics --5 packets transmitted, 5 received, 0% packet loss, time 3999ms
rtt min/avg/max/mdev = 0.041/0.049/0.055/0.009 ms

6. Change Ping Packet Size


Example: Change the default packet size from 56 to 100.
$ ping -s 100 localhost

PING localhost (127.0.0.1) 100(128) bytes of data.


108 bytes from localhost (127.0.0.1): icmp_seq=1 ttl=64 time=0.022 ms
108 bytes from localhost (127.0.0.1): icmp_seq=2 ttl=64 time=0.021 ms
108 bytes from localhost (127.0.0.1): icmp_seq=3 ttl=64 time=0.020 ms
^C
--- localhost ping statistics --3 packets transmitted, 3 received, 0% packet loss, time 1998ms
rtt min/avg/max/mdev = 0.020/0.021/0.022/0.000 ms

Note : Ping Bytes Sent = Ping Packet Size + Ping Header Packet Size (28
bytes)
7. Timeout
The following example will ping for 5 seconds. i.e ping command will exit after
5 seconds irrespective of how many packets are sent or received.
$ ping -w 5 localhost

8. Specify path for ping to send the packet


You can also specify through which path the ping should send the packet to
destination.
$ ping 192.168.3.33 192.168.7.1 192.168.4.45

Note: If one of the hop in the path is not reachable then you will have failure in
pinging.
9. Record and print route of how ECHO_REQUEST sent and ECHO_REPLY
received
$ ping -R 192.168.1.63
PING 192.168.1.63 (192.168.1.63) 56(84) bytes of data.
64 bytes from 192.168.1.63: icmp_seq=1 ttl=61 time=2.05 ms
RR: 192.168.9.118
192.168.3.25
192.168.10.35
192.168.1.26
192.168.1.63
192.168.1.63
192.168.10.4
192.168.3.10
192.168.4.25
64 bytes from 192.168.1.63: icmp_seq=2 ttl=61 time=2.00 ms

(same route)

10. Audible ping: Give beep when the peer is reachable


$ ping -a IP

Note: It can give beep only from terminal number 1 through 7 and gnometerminal ( It will not work in console ).

10+ ifconfig Command Usage Examples in Linux


This tutorial explains Linux ifconfig command, options and its usage with
examples.
ifconfig configure a network interface.

DESCRIPTION
Ifconfig is used to configure the kernel-resident network interfaces. It is used at boot
time to set up interfaces as necessary. After that, it is usually only needed when
debugging or when system tuning is needed. If no arguments are given, ifconfig
displays the status of the currently active interfaces.
If a single interface argument is given, it displays the status of the given interface
only; if a single -a argument is given, it displays the status of all interfaces, even those
that are down. Otherwise, it configures an interface.
SYNOPSIS
ifconfig [-L] [-m] interface [create] [address_family] [address[/prefixlength]
[dest_address]]
[parameters]
ifconfig
interface
destroy
ifconfig
-a
[-L]
[-d]
[-m]
[-u]
[address_family]
ifconfig
-l
[-d]
[-u]
[address_family]
ifconfig [-L] [-d] [-m] [-u] [-C]
OPTIONS :
-a
Optionally, the -a flag may be used instead of an interface name. This flag instructs
ifconfig to display information about all interfaces in the system.
-d
The -d flag displays interfaces that are down. You can use the flag only with the -a or
-l
flag.
-l
This flag may be used to list all available interfaces on the system, with no other
additional information. Use of this flag is mutually exclusive with all other flags and
commands,
except
for
-d
and
-u.
-u
The -u flag displays interfaces that are up. You can use the flag only with the -a or -l
flag.

addressfamily This flag specifies protocols such as tcp, udp, tcp6, udp6, icmp, and
icmp6.
Parameters
alias
Establishes an additional network address for the interface. When changing network
numbers, this parameter is useful for accepting packets addressed to the old interface.
allcast
Sets the Token-Ring interface to broadcast to all rings on the network.
-allcast
Confines the Token-Ring interface to broadcast only to the local ring.
arp
Enables the ifconfig command to use the Address Resolution Protocol in mapping
between network-level addresses and link-level addresses. The arp value is the default.
-arp
Disables the use of the Address Resolution Protocol.
broadcast
Address
(inet only) Specifies the address to use to broadcast to the network. The default
broadcast address has a host part of all 1s.
checksum_offload
Enables the flag to indicate that transmit TCP checksum should be offloaded to the
adapter. The command will also reset the per-interface counter that determines
whether TCP should dynamically enable or disable offloading of checksum
computation.
-checksum_offload
Disables transmit TCP checksum offloading.
delete
Removes the specified network address. This is used when an alias is incorrectly
specified or when it is no longer needed. Incorrectly setting an ns address has the side
effect of specifying the host portion of the network address. Removing all ns
addresses allows you to re-specify the host portion.
detach
Removes an interface from the network interface list. If the last interface is detached,
the network interface driver code is unloaded. In order for the interface route of an

attached interface to be changed, that interface must be detached and added again with
ifconfig.
down
Marks an interface as inactive (down), which keeps the system from trying to transmit
messages through that interface. If possible, the ifconfig command also resets the
interface to disable reception of messages. Routes that use the interface, however, are
not automatically disabled.
group
ID
Adds a group ID to the group ID list for the interface. This list is used in determining
the route to use when forwarding packets that arrived on the interface.
-group
ID
Removes a group ID from the group ID list for the interface. This list is used in
determining the route to use when forwarding packets that arrived on the interface.
hwloop
Enables hardware loopback. The hardware loopback specifies that locally addressed
packets handled by an interface should be sent out using the associated adapter.
-hwloop
Disables hardware loop-back. The hardware loop-back specifies that locally addressed
packets handled by an interface should be sent out using the associated adapter.
ipdst
Specifies an Internet host willing to receive IP packets encapsulating ns packets bound
for a remote network. An apparent point-to-point link is constructed, and the specified
address is taken as the ns address and network of the destination.
metric
Number
Sets the routing metric of the interface to the value specified by the Number variable.
The default is 0 (zero). The routing metric is used by the routing protocol (the routed
daemon). Higher metrics have the effect of making a route less favorable. Metrics are
counted as addition hops to the destination network or host.
monitor
Enables the underlying adapter to notify the interface layer of link status changes. The
adapter must support link status callback notification. If multipath routing is used,
alternate
routes
are
selected
when
a
link
goes
down.
-monitor

Disables

adapter

link

status

monitoring.

mtu
Value
Sets the maximum IP packet size for this system. The Value variable can be any
number from 60 through 65535, but is media dependent. See Automatic configuration
of network interfaces in Networks and communication management for maximum
transmission unit (MTU) values by interface.
netmask
Mask
Specifies how much of the address to reserve for subdividing networks into
subnetworks. This parameter can be used only with an address family of inet.
up
Marks an interface as active (up). This parameter is used automatically when setting
the first address for an interface. It can also be used to enable an interface after an
ifconfig down command.
EXAMPLES
1. View All Network Setting
The ifconfig command with no arguments will display all the active interfaces
details.
# ifconfig
eth0

Link encap:Ethernet HWaddr 00:0B:CD:1C:18:5A


inet addr:172.16.25.126 Bcast:172.16.25.63 Mask:255.255.255.224
inet6 addr: fe80::20b:cdff:fe1c:185a/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:2341604 errors:0 dropped:0 overruns:0 frame:0
TX packets:2217673 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:293460932 (279.8 MiB) TX bytes:1042006549 (993.7 MiB)
Interrupt:185 Memory:f7fe0000-f7ff0000

lo

Link encap:Local Loopback


inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:5019066 errors:0 dropped:0 overruns:0 frame:0
TX packets:5019066 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:2174522634 (2.0 GiB) TX bytes:2174522634 (2.0 GiB)

tun0

Link encap:UNSPEC HWaddr 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00


inet addr:10.1.1.1 P-t-P:10.1.1.2 Mask:255.255.255.255
UP POINTOPOINT RUNNING NOARP MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:100
RX bytes:0 (0.0 b) TX bytes:0 (0.0 b)

2. Display Information of All Network Interfaces using -a option


# ifconfig -a
eth0

Link encap:Ethernet HWaddr 00:0B:CD:1C:18:5A


inet addr:172.16.25.126 Bcast:172.16.25.63 Mask:255.255.255.224
inet6 addr: fe80::20b:cdff:fe1c:185a/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:2344927 errors:0 dropped:0 overruns:0 frame:0
TX packets:2220777 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:293839516 (280.2 MiB) TX bytes:1043722206 (995.3 MiB)
Interrupt:185 Memory:f7fe0000-f7ff0000

lo

Link encap:Local Loopback


inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:5022927 errors:0 dropped:0 overruns:0 frame:0
TX packets:5022927 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:2175739488 (2.0 GiB) TX bytes:2175739488 (2.0 GiB)

sit0

Link encap:IPv6-in-IPv4
NOARP MTU:1480 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:0 (0.0 b) TX bytes:0 (0.0 b)

tun0

Link encap:UNSPEC HWaddr 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00


inet addr:10.1.1.1 P-t-P:10.1.1.2 Mask:255.255.255.255
UP POINTOPOINT RUNNING NOARP MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:100
RX bytes:0 (0.0 b) TX bytes:0 (0.0 b)

3. View Network Settings of Specific Interface

For e.g. of eth0 interface


# ifconfig eth0
eth0

Link encap:Ethernet HWaddr 00:0B:CD:1C:18:5A


inet addr:172.16.25.126 Bcast:172.16.25.63 Mask:255.255.255.224
inet6 addr: fe80::20b:cdff:fe1c:185a/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:2345583 errors:0 dropped:0 overruns:0 frame:0
TX packets:2221421 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:293912265 (280.2 MiB) TX bytes:1044100408 (995.7 MiB)
Interrupt:185 Memory:f7fe0000-f7ff0000

4. How to Enable an Network Interface


The up or ifup flag with interface name (eth0) activates an network interface, if it
is not in active state and allowing to send and receive information.
# ifconfig eth0 up

OR
# ifup eth0

5. How to Disable an Network Interface


The down or ifdown flag with interface name (eth0) deactivates the specified
network interface.
# ifconfig eth0 down

OR
# ifdown eth0

6. How to Assign a IP Address to Network Interface


To assign an IP address to an specific interface, use the following command
# ifconfig eth0 172.16.25.125

7. How to Assign a Netmask to Network Interface


Using the ifconfig command with netmask argument and interface name as (eth0)
allows you to define an netmask to an given interface.
# ifconfig eth0 netmask 255.255.255.224

8. How to Assign a Broadcast to Network Interface


Using the broadcast argument with an interface name will set the broadcast address
for the given interface.
# ifconfig eth0 broadcast 172.16.25.63

9. How to Assign a IP, Netmask and Broadcast to Network Interface

To assign an IP address, Netmask address and Broadcast address all at once using
ifconfig command with all arguments as given below.
# ifconfig eth0 172.16.25.125 netmask 255.255.255.224 broadcast 172.16.25.63

10. How to Change MTU for an Network Interface


The mtu argument set the maximum transmission unit to an interface.
# ifconfig eth0 mtu 1000

11. How to Add New Alias to Network Interface


The ifconfig utility allows you to configure additional network interfaces using alias
feature. To add alias network interface of eth0, use the following command. Please
note that alias network address in same sub-net mask.
# ifconfig eth0:0 172.16.25.127

Next, verify the newly created alias network interface address, by using ifconfig
eth0:0 command.
# ifconfig eth0:0
eth0:0 Link encap:Ethernet HWaddr 00:01:6C:99:14:68
inet addr:172.16.25.123 Bcast:172.16.25.63 Mask:255.255.255.240
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
Interrupt:17

12. How to Remove Alias to Network Interface


If you no longer required an alias network interface or you incorrectly configured it,
you can remove it by using the following command.
# ifconfig eth0:0 down

13. How to Change the MAC address of Network Interface


To change the MAC (Media Access Control) address of an eth0 network interface, use
the following command with argument hw ether. For example, see below.
# ifconfig eth0 hw ether AA:BB:CC:DD:EE:FF

Sanfoundry Global Education & Learning Series 1000 Linux Tutorials.


If you wish to look at all Linux commands and their usage examples, go to Linux Commands
Tutorial.

10 netstat Command Usage Examples in Linux


This tutorial explains Linux netstat command, options and its usage with examples.

netstat Print network connections, routing tables, interface statistics, masquerade connections,
and multicast memberships

DESCRIPTION
netstat (network statistics) is a command line tool for monitoring network connections
both incoming and outgoing as well as viewing routing tables, interface statistics etc.
netstat is available on all Unix-like Operating Systems and also available on Windows
OS as well. It is very useful in terms of network troubleshooting and performance
measurement. netstat is one of the most basic network service debugging tools, telling
you what ports are open and whether any programs are listening on ports.
SYNOPSIS
netstat [-a] [-n] [-v]
netstat [-g | -m | -p | -s | -f address_family ] [-n] [-P protocol]
netstat [ -i ] [ -I interface ] [ interval ]
netstat -r [-a] [-n] [-v ]
netstat -M [-n] [-s ]
netstat -D [ -I interface ]
OPTIONS :
-a
Show the state of all sockets and all routing table entries; normally, sockets used by
server processes are not shown and only interface, host, network, and default routes are
shown.
-n
Show network addresses as numbers. netstat normally displays addresses as symbols.
This

option

may

be

used

with

any

of

the

display

formats.

-v
Verbose. Show additional information for the sockets and the routing table.
-g
Show

the

multicast

group

memberships

for

all

interfaces.

-m
Show

the

STREAMS

statistics.

-p
Show

the

address

resolution

(ARP)

tables.

-s
Show per-protocol statistics. When used with the -M option, show multicast routing
statistics

instead.

-i
Show

the

state

of

the

interfaces

that

are

used

for

TCP/IP

traffic.

-r
Show

the

routing

tables.

-M
Show the multicast routing tables. When used with the -s option, show multicast routing
statistics

instead.

-d
Show the state of all interfaces that are under Dynamic Host Configuration Protocol
(DHCP)

control.

-D
Show

the

status

of

DHCP

configured

-f

interfaces.
address_family

imit statistics or address control block reports to those of the specified address_family,
which
inet

can
For

be
the

AF_INET

one
address

of:
family

unix For the AF_Unix address family


-P

protocol

Limit display of statistics or state of all sockets to those applicable to protocol.


-

interface

Show the state of a particular interface. interface can be any valid interface such as ie0
or
le0.
EXAMPLES
1. Listing Various Listening Ports
Listing all the LISTENING Ports of TCP and UDP connections
# netstat -a | more
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address
Foreign Address

State

tcp
tcp
tcp
tcp
tcp
udp
udp

0
0
1
0
0
0
0

0 *:sunrpc
52 192.168.0.2:ssh
0 192.168.0.2:59292
0 localhost:smtp
0 *:59482
0 *:35036
0 *:npmp-local

Active UNIX domain


Proto RefCnt Flags
unix 2
[ ACC
6fa08790553d6
unix 2
[ ACC
7058d584166d2
unix 2
[ ACC
546fe905321cc
unix 2
[ ACC
415135cb6aeab

*:*

LISTEN
192.168.0.1:egs
ESTABLISHED
www.gov.com:http
CLOSE_WAIT
*:*
LISTEN
*:*
LISTEN
*:*
*:*

sockets (servers and established)


Type
State
I-Node Path
]
STREAM
LISTENING
16972

/tmp/orbit-root/linc-76b-0-

STREAM

LISTENING

17149

/tmp/orbit-root/linc-794-0-

STREAM

LISTENING

17161

/tmp/orbit-root/linc-792-0-

STREAM

LISTENING

15938

/tmp/orbit-root/linc-74b-0-

Listing TCP Ports connections


# netstat -at
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address
Foreign Address
State
tcp
0
0 *:ssh
*:*
LISTEN
tcp
0
0 localhost:ipp
*:*
LISTEN
tcp
0
0 localhost:smtp
*:*
LISTEN
tcp
0
52 192.168.0.2:ssh
192.168.0.1:egs
ESTABLISHED
tcp
1
0 192.168.0.2:59292
www.gov.com:http
CLOSE_WAIT

Listing UDP Ports connections


# netstat -au
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address
Foreign Address
udp
0
0 *:35036
*:*
udp
0
0 *:npmp-local
*:*
udp
0
0 *:mdns
*:*

2. Listing various Listening connections


Listing all LISTENING Connections
# netstat -l

State

Active Internet connections (only servers)


Proto Recv-Q Send-Q Local Address
tcp
0
0 *:sunrpc
*:*
tcp
0
0 *:58642
*:*
tcp
0
0 *:ssh
*:*
udp
0
0 *:35036
*:*
udp
0
0 *:npmp-local
*:*
Active UNIX domain
Proto RefCnt Flags
unix 2
[ ACC
6fa08790553d6
unix 2
[ ACC
7058d584166d2
unix 2
[ ACC
546fe905321cc
unix 2
[ ACC
415135cb6aeab

Foreign Address
LISTEN
LISTEN
LISTEN

State

sockets (only servers)


Type
State
I-Node Path
]
STREAM
LISTENING
16972

/tmp/orbit-root/linc-76b-0-

STREAM

LISTENING

17149

/tmp/orbit-root/linc-794-0-

STREAM

LISTENING

17161

/tmp/orbit-root/linc-792-0-

STREAM

LISTENING

15938

/tmp/orbit-root/linc-74b-0-

Listing all TCP Listening Ports


Listing all active listening TCP ports by using option netstat -lt.
# netstat -lt
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address
Foreign
tcp
0
0 *:dctp
*:*
tcp
0
0 *:mysql
*:*
tcp
0
0 *:sunrpc
*:*
tcp
0
0 *:munin
*:*
tcp
0
0 *:ftp
*:*
tcp
0
0 localhost.localdomain:ipp *:*
tcp
0
0 localhost.localdomain:smtp *:*
tcp
0
0 *:http
*:*
tcp
0
0 *:ssh
*:*
tcp
0
0 *:https
*:*

Address
State
LISTEN
LISTEN
LISTEN
LISTEN
LISTEN
LISTEN
LISTEN
LISTEN
LISTEN
LISTEN

Listing all UDP Listening Ports


Listing all active listening UDP ports by using option netstat -lu.
# netstat -lu
Active Internet connections (only servers)

Proto Recv-Q Send-Q Local Address


udp
0
0 *:39578
*:*
udp
0
0 *:meregister
*:*
udp
0
0 *:vpps-qua
*:*
udp
0
0 *:openvpn
*:*
udp
0
0 *:mdns
*:*
udp
0
0 *:sunrpc
*:*
udp
0
0 *:ipp
*:*
udp
0
0 *:60222
*:*
udp
0
0 *:mdns
*:*

3. Showing Statistics by Different Protocols


Showing statistics of all protocols
# netstat -s
Ip:
2461 total packets received
0 forwarded
0 incoming packets discarded
2431 incoming packets delivered
2049 requests sent out
Icmp:
0 ICMP messages received
0 input ICMP message failed.
ICMP input histogram:
1 ICMP messages sent
0 ICMP messages failed
ICMP output histogram:
destination unreachable: 1
Tcp:
159 active connections openings
1 passive connection openings
4 failed connection attempts
0 connection resets received
1 connections established
2191 segments received
1745 segments send out
24 segments retransmited
0 bad segments received.
4 resets sent
Udp:
243 packets received
1 packets to unknown port received.
0 packet receive errors

Foreign Address

State

281 packets sent

Showing Statistics by TCP Protocol


# netstat -st
Tcp:
2805201 active connections openings
1597466 passive connection openings
1522484 failed connection attempts
37806 connection resets received
1 connections established
57718706 segments received
64280042 segments send out
3135688 segments retransmited
74 bad segments received.
17580 resets sent

Showing Statistics by UDP Protocol


# netstat -su
Udp:
1774823 packets received
901848 packets to unknown port received.
0 packet receive errors
2968722 packets sent

4. Displaying Service name with PID


# netstat -tp
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address
Foreign Address
State
PID/Program name
tcp
0
0 192.168.0.2:ssh
192.168.0.1:egs
ESTABLISHED 2179/sshd
tcp
1
0 192.168.0.2:59292
www.gov.com:http
CLOSE_WAIT
1939/clock-applet

5. Display Kernel IP routing table


# netstat -r
Kernel IP routing table
Destination
Gateway
192.168.0.0
*

Genmask
Flags MSS Window irtt Iface
255.255.255.0 U
00
0 eth0

link-local
default

*
255.255.0.0
192.168.0.1
0.0.0.0

U
UG

00
00

0 eth0
0 eth0

6. Showing network interface packet transactions


# netstat -i
Kernel Interface table
Iface
MTU Met RX-OK RX-ERR RX-DRP RX-OVR TX-OK TX-ERR TX-DRP TX-OVR Flg
eth0
1500 0
4459
0
0
0
4057
0
0
0 BMRU
lo
16436 0
8
0
0
0
8
0
0
0 LRU

7. Showing Kernel interface table, similar to ifconfig command.


# netstat -ie
Kernel Interface table
eth0
Link encap:Ethernet HWaddr 00:0C:29:B4:DA:21
inet addr:192.168.0.2 Bcast:192.168.0.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:feb4:da21/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:4486 errors:0 dropped:0 overruns:0 frame:0
TX packets:4077 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:2720253 (2.5 MiB) TX bytes:1161745 (1.1 MiB)
Interrupt:18 Base address:0x2000
lo

Link encap:Local Loopback


inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:8 errors:0 dropped:0 overruns:0 frame:0
TX packets:8 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:480 (480.0 b) TX bytes:480 (480.0 b)

8. Print Netstat Information Continuously


# netstat -c
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address
Foreign Address
State
tcp
0
0 tecmint.com:http sg2nlhg007.shr.prod.s:36944 TIME_WAIT
tcp
0
0 tecmint.com:http sg2nlhg010.shr.prod.s:42110 TIME_WAIT
tcp
0 132 tecmint.com:ssh 115.113.134.3.static-:64662 ESTABLISHED
tcp
0
0 tecmint.com:http crawl-66-249-71-240.g:41166 TIME_WAIT

tcp
tcp
tcp
tcp

0
0
0
0

0
0
0
0

localhost.localdomain:54823 localhost.localdomain:smtp TIME_WAIT


localhost.localdomain:54822 localhost.localdomain:smtp TIME_WAIT
tecmint.com:http sg2nlhg010.shr.prod.s:42091 TIME_WAIT
tecmint.com:http sg2nlhg007.shr.prod.s:36998 TIME_WAIT

9. Finding Listening Programs


# netstat -ap | grep http
tcp
tcp
tcp
tcp
tcp
tcp
tcp
tcp
unix 2

0
0
0
0
0
0
0
0

0
0
0
0
0
0
0
0
[]

*:http
*:*
LISTEN
9056/httpd
*:https
*:*
LISTEN
9056/httpd
tecmint.com:http sg2nlhg008.shr.prod.s:35248 TIME_WAIT
tecmint.com:http sg2nlhg007.shr.prod.s:57783 TIME_WAIT
tecmint.com:http sg2nlhg007.shr.prod.s:57769 TIME_WAIT
tecmint.com:http sg2nlhg008.shr.prod.s:35270 TIME_WAIT
tecmint.com:http sg2nlhg009.shr.prod.s:41637 TIME_WAIT
tecmint.com:http sg2nlhg009.shr.prod.s:41614 TIME_WAIT
STREAM
CONNECTED
88586726 10394/httpd

10. Displaying RAW Network Statistics


# netstat --statistics --raw
Ip:
62175683 total packets received
52970 with invalid addresses
0 forwarded
Icmp:
875519 ICMP messages received
destination unreachable: 901671
echo request: 8
echo replies: 16253
IcmpMsg:
InType0: 83
IpExt:
InMcastPkts: 117

Sanfoundry Global Education & Learning Series 1000 Linux Tutorials.


If you wish to look at all Linux commands and their usage examples, go to Linux Commands
Tutorial.

5 nslookup Command Usage Examples in Linux

This tutorial explains Linux nslookup command, options and its usage with examples.
nslookup query Internet name servers interactively

DESCRIPTION
nslookup is a network administration tool for querying the Domain Name System (DNS)
to obtain domain name or IP address mapping or any other specific DNS record.
It is also used to troubleshoot DNS related problems. This article provides few examples
on using the nslookup command.
nslookup can operate on both Interactive mode and Non-Interactive mode. Interactive
mode allows the user to query the DNS-Server about various host, and domains. NonInteractive mode allows the user to query the information for a host or domain.
In this article, all the commands explained are Non-Interactive mode.
Authoritative Answer vs Non-Authoritative Answer
Any answer that originates from the DNS Server which has the complete zone file
information available for the domain is said to be authoritative answer.
In many cases, DNS servers will not have the complete zone file information available
for a given domain. Instead, it maintains a cache file which has the results of all queries
performed in the past for which it has gotten authoritative response. When a DNS query
is given, it searches the cache file, and return the information available as NonAuthoritative Answer.
SYNOPSIS
nslookup [ - option ] host [ server ]

OPTIONS
all
List the current settings
d2
Set exhaustive debug mode on

nod2
Set exhaustive debug mode off
debug
Set debug mode on
nodebug
Set debug mode off
defname
Set domain-appending mode on
nodefname
Set domain-appending mode off
domain=string
Establish the appendable domain
ignoretc
Set it to ignore packet truncation errors
noignoretc
Set it to acknowledge packet truncation errors
host
Inquires about the specified host. In this non-interactive command format, nslookup
Does not prompt for additional commands.
Causes nslookup to prompt for more information, such as host names, before sending
one or more queries.
server
Directs inquiries to the name server specified here in the command line rather than the
one read from the /etc/resolv.conf file. server can be either a name or an Internet
address. If the specified host cannot be reached, nslookup resorts to using the name
server specified in /etc/resolv.conf.
EXAMPLES
1. Simple Example

Looking up google.com
$ nslookup google.com
Server:
127.0.1.1
Address: 127.0.1.1#53
Non-authoritative answer:
Name: google.com
Address: 74.125.200.138
Name: google.com
Address: 74.125.200.113
Name: google.com
Address: 74.125.200.102
Name: google.com
Address: 74.125.200.100
Name: google.com
Address: 74.125.200.101
Name: google.com
Address: 74.125.200.139

2. Query the MX Record using -query=mx


MX ( Mail Exchange ) record maps a domain name to a list of mail exchange servers for
that domain.
$ nslookup -query=mx google.com
Server:
127.0.1.1
Address: 127.0.1.1#53
Non-authoritative
google.com
google.com
google.com
google.com
google.com

answer:
mail exchanger
mail exchanger
mail exchanger
mail exchanger
mail exchanger

=
=
=
=
=

10
50
40
30
20

aspmx.l.google.com.
alt4.aspmx.l.google.com.
alt3.aspmx.l.google.com.
alt2.aspmx.l.google.com.
alt1.aspmx.l.google.com.

Authoritative answers can be found from:


google.com
nameserver = ns4.google.com.
google.com
nameserver = ns3.google.com.
google.com
nameserver = ns1.google.com.
google.com
nameserver = ns2.google.com.
alt2.aspmx.l.google.com internet address = 74.125.137.27
alt1.aspmx.l.google.com internet address = 74.125.142.26
aspmx.l.google.com
internet address = 74.125.129.27
alt4.aspmx.l.google.com internet address = 173.194.75.27

alt3.aspmx.l.google.com
ns2.google.com internet
ns3.google.com internet
ns4.google.com internet
ns1.google.com internet

internet address = 173.194.68.26


address = 216.239.34.10
address = 216.239.36.10
address = 216.239.38.10
address = 216.239.32.10

3. View available DNS records using -query=any


Using -query=any, we can get all records
$ nslookup -query=any google.com
Server:
127.0.1.1
Address: 127.0.1.1#53
Non-authoritative answer:
google.com
origin = ns1.google.com
mail addr = dns-admin.google.com
serial = 2013121300
refresh = 7200
retry = 1800
expire = 1209600
minimum = 300
google.com
mail exchanger = 10 aspmx.l.google.com.
google.com
mail exchanger = 40 alt3.aspmx.l.google.com.
google.com
mail exchanger = 30 alt2.aspmx.l.google.com.
google.com
mail exchanger = 50 alt4.aspmx.l.google.com.
google.com
mail exchanger = 20 alt1.aspmx.l.google.com.
google.com
has AAAA address 2404:6800:4003:c00::65
Name: google.com
Address: 74.125.200.139
Name: google.com
Address: 74.125.200.102
Name: google.com
Address: 74.125.200.138
Name: google.com
Address: 74.125.200.113
Name: google.com
Address: 74.125.200.100
Name: google.com
Address: 74.125.200.101
google.com
nameserver = ns4.google.com.
google.com
nameserver = ns2.google.com.
google.com
nameserver = ns3.google.com.
google.com
nameserver = ns1.google.com.

Authoritative answers can be found from:


google.com
nameserver = ns3.google.com.
google.com
nameserver = ns4.google.com.
google.com
nameserver = ns2.google.com.
google.com
nameserver = ns1.google.com.
alt3.aspmx.l.google.com internet address = 173.194.68.27
alt2.aspmx.l.google.com internet address = 74.125.137.26
alt4.aspmx.l.google.com internet address = 173.194.75.27
alt1.aspmx.l.google.com internet address = 74.125.142.26

4. Reverse DNS lookup


Looking up IP address to get the domainname.
$ nslookup 173.194.68.27
Server:
127.0.1.1
Address: 127.0.1.1#53
Non-authoritative answer:
27.68.194.173.in-addr.arpaname = qa-in-f27.1e100.net.
Authoritative answers can be found from:
194.173.in-addr.arpa
nameserver = NS2.GOOGLE.COM.
194.173.in-addr.arpa
nameserver = NS4.GOOGLE.COM.
194.173.in-addr.arpa
nameserver = NS3.GOOGLE.COM.
194.173.in-addr.arpa
nameserver = NS1.GOOGLE.COM.
NS3.GOOGLE.COM
internet address = 216.239.36.10
NS4.GOOGLE.COM
internet address = 216.239.38.10
NS2.GOOGLE.COM
internet address = 216.239.34.10
NS1.GOOGLE.COM
internet address = 216.239.32.10

5. Using Specific DNS server


In the following wxample ns1.google.com is the specific name server which is being
looked up.
$ nslookup google.com ns1.google.com
Server:
ns1.google.com
Address: 216.239.32.10#53
Name: google.com
Address: 173.194.36.0
Name: google.com
Address: 173.194.36.5
Name: google.com

Address: 173.194.36.7
Name: google.com
Address: 173.194.36.9
Name: google.com
Address: 173.194.36.2
Name: google.com
Address: 173.194.36.1
Name: google.com
Address: 173.194.36.8
Name: google.com
Address: 173.194.36.14
Name: google.com
Address: 173.194.36.3
Name: google.com
Address: 173.194.36.4
Name: google.com
Address: 173.194.36.6

Here you may notice that, we dont get any Non-authoritative answer: header, since
ns1.redhat.com has all the zone information of redhat.com

Vous aimerez peut-être aussi