Friday 19 February 2016

Useful Linux commands

***********NETWORK COMMANDS***********
#To know if a host/ip is reachable
ping hostname/IP

#To see the network route how a host is reachable. Packet flow path.
traceroute hostname/IP 
or
tracepath hostname

# To add route to reach through network when you have multiple network/ethernet NIC cards in a server
route add -net 192.168.49.0 netmask 255.255.255.0 gw 192.168.200.1
        - Here any ip in 192.168.49.XX network is routed via the gateway 192.168.200.1
        - when there are more than 1 NIC, this can be helpful.

#To delete the route
route del -net 192.168.49.0 netmask 255.255.255.0 

#To check if a particular port is open or reachable in a remote server
telnet IP port
or 
nc -zvw10 IPaddress portnumner --> in case of tcp port
nc -zuw10 IPaddress portnumber --> in case of udp port
You will get "succeeded" as output if port is reachable
"Failed: no route to host" when port is not open
"failed: Connection refused" when port is open and no service available on the port.
10 is the time in seconds to wait for the response.

#Quick look on IP addresses available
ip addr

# detailed look on ip address
ifconfig



***********PACKET CAPTURE**************
#To capture packets in the network or ethernet card
tcpdump -s packetsize -i ethernetinterface -w writetofile.pcap host <ipaddress> port <number> or port <number2> 

# Quick read from pcap file one liner.
tcpdump -r filetoread.pcap



*************SYSTEM INFO****************
#To know if a linux machine is 64bit or 32 bit
uname -a 
this will tell u the kernel version, and all.
if u see x86_64, it's a 64bit or i386,i586,etc, it's 32bit

#To know the cpu info
cat /proc/cpuinfo

#To know the memory info
cat /proc/meminfo

#To know the OS name
cat /etc/*release

#To find the disk usage of a directory
du -sh <directory>
try du -sh * for summary of each sub directories and files in current directory.



***************FILE COMMANDS************
# To see top n lines of a file
head -n number filename

# To see botton n lines of a file
tail -n number filename

# To see a particular line of a file like 25th line
head -n 25 filename | tail -n1

# To cut a line into multiple fields
cut -d'<delimiter>' -f<field#> filename
where delimiter is one character and field# is the field number

#To sort above output and print unique values with the count
cut -d'<delimiter>' -f<field#> filename | sort | uniq -c




To be Continued.. 




No comments:

Post a Comment