Showing posts with label Cheat Sheet. Show all posts
Showing posts with label Cheat Sheet. Show all posts

Wednesday, September 20, 2023

SS command Cheat Sheet

ss Command Cheat Sheet

This cheat sheet provides an overview of the ss command and its commonly used options for examining socket statistics and network connections in Linux. Adjust the options as needed to match your specific requirements when working with ss.

 

1. Displaying Sockets:

  • ss: Display a summary of all sockets.
  • ss -t: Display TCP sockets.
  • ss -u: Display UDP sockets.
  • ss -w: Display raw sockets.
  • ss -x: Display UNIX domain sockets.

 

2. Filtering and Displaying Specific Sockets:

  • ss -tuln: Display all listening TCP and UDP sockets without resolving names.
  • ss -tul: Display all listening TCP and UDP sockets with name resolution.
  • ss -t4: Display IPv4 sockets.
  • ss -t6: Display IPv6 sockets.

 

3. Display Socket Statistics:

  • ss -s: Display socket statistics summary.
  • ss -t -s: Display TCP socket statistics.
  • ss -u -s: Display UDP socket statistics.
  • ss -w -s: Display raw socket statistics.
  • ss -x -s: Display UNIX domain socket statistics.

 

4. Display Extended Information:

  • ss -e: Display extended information, including socket UID and inode.
  • ss -t -a: Display all sockets (listening and non-listening).

 

5. Display Processes Associated with Sockets:

  • ss -t -p: Show the processes associated with each socket.
  • ss -t -t -p: Display TCP sockets along with their associated processes.

 

6. Display Socket Timers:

  • ss -o: Show socket timers (e.g., TCP retransmit timeout).
  • ss -t -o: Display TCP socket timers.
  • ss -u -o: Display UDP socket timers.

 

7. Show Header Information:

  • ss -H: Display header lines to label each column.
  • ss -i: Display information about network interfaces.

 

8. Filter by State:

  • ss state FIN-WAIT-1: Display sockets in a specific state (e.g., FIN-WAIT-1).
  • ss state connected: Show connected sockets.

 

9. Sort Output:

  • ss -t state established -o: Sort and display established TCP connections.
  • ss -n -o state established | sort -r -k 5: Sort established connections by data transfer rate.

 

10. Output in JSON Format:

  • ss -t -j: Display socket information in JSON format.

 

11. Display Help:

  • ss --help: Display the ss command's help and usage information.

 

12. Clear Timers and Counters:

  • ss -t -E: Clear socket timers.
  • ss -t -Z: Clear socket counters.

 

13. Show Reverse DNS Lookups:

  • ss -r: Show reverse DNS lookups in the output.

 

Tuesday, September 19, 2023

IP command Cheat Sheet

Several of the most used Linux commands/tools such as ifconfig, netstat, route is deprecated. It’s time to start using the versatile ip command line utility.

 

Ip command line utility provided by the ‘net-tools’ package is used for various network administration tasks on Linux nodes.

 

Syntax of the ip command line utility:

 

# ip [ OPTIONS ] OBJECT { COMMAND | help}

 

OPTIONS are used to define global parameters. Commonly used ones being:

-4 -> shows IPv4 specific details only.

-6 -> shows IPv6 specific details only.

-s -> shows statistics of the various network interfaces.

-t -> display current time when using monitor option.

 

 

OBJECTS are the items the command would action on.

Commonly used OBJECTS are the follows:

 

OBJECT

Abbreviation

Used for

address

a, addr

IP address on the device

link

l

Network device

route

r

Routing table entry

rule

ru

Rule in the routing policy database

neighbour

n, neigh

Manage ARP/NDSC cache entries

maddress

m, maddr

Multicast address

monitor

 

Watch netlink messages

 

 

This cheat sheet offers a concise overview of the ip command and its available options. It serves as a valuable resource for troubleshooting network and system issues."

 

NOTE: The commands have certain parameters mentioned in capital letters like ‘INTERFACE’, this needs to be changed depending on your use case.

1. Displaying Network Information:

  • ip address show or ip a : Show information about all network interfaces.
  • ip link show or ip l : Display link layer information.
  • ip route show or ip r : Display the routing table.

2. Managing Network Interfaces:

  • ip link set dev INTERFACE up/down : Enable or disable a network interface.
  • ip link set dev INTERFACE name NEW_NAME : Rename a network interface.
  • ip link set dev INTERFACE mtu MTU_VALUE : Set the Maximum Transmission Unit (MTU) for an interface.
  • ip link add link INTERFACE name NEW_INTERFACE type TYPE : Create a new virtual interface based on an existing one.

3. Configuring IP Addresses:

  • ip address add IP_ADDRESS/NETMASK dev INTERFACE : Assign an IP address to an interface.
  • ip address delete IP_ADDRESS/NETMASK dev INTERFACE : Remove an IP address from an interface.
  • ip address show dev INTERFACE : Display IP address information for a specific interface.

4. Managing Routes:

  • ip route add NETWORK via GATEWAY : Add a static route.
  • ip route delete NETWORK : Delete a static route.
  • ip route show : Display the routing table.
  • ip route flush cache : Clear the routing cache.

5. Managing ARP Cache:

  • ip neigh show : Display the ARP cache.
  • ip neigh add IP_ADDRESS lladdr MAC_ADDRESS dev INTERFACE : Add an ARP cache entry manually.
  • ip neigh delete IP_ADDRESS dev INTERFACE : Delete an entry from the ARP cache.

6. Managing VLANs (Virtual LANs):

  • ip link add link INTERFACE name VLAN_NAME type vlan id VLAN_ID : Create a VLAN interface.
  • ip link show VLAN_NAME : Display information about a VLAN interface.

7. Managing Bridge Interfaces:

  • ip link add name BRIDGE_NAME type bridge : Create a bridge interface.
  • ip link set dev INTERFACE master BRIDGE_NAME : Add an interface to a bridge.
  • ip link set dev INTERFACE nomaster : Remove an interface from a bridge.
  • ip link show BRIDGE_NAME : Display information about a bridge.

8. Managing Tunnels:

  • ip tunnel add TUNNEL_NAME mode MODE remote REMOTE_IP local LOCAL_IP : Create a tunnel interface.
  • ip tunnel delete TUNNEL_NAME : Delete a tunnel interface.
  • ip tunnel show : Display information about tunnel interfaces.

9. Managing QoS and Traffic Control:

  • ip link set dev INTERFACE qlen QUEUE_LENGTH : Set the length of the transmit queue.
  • ip link set dev INTERFACE txqueuelen QUEUE_LENGTH : Set the length of the transmit queue.
  • tc : Use the tc command for advanced traffic control and QoS configuration.

10. Managing Multicast:

  • ip maddr show : Display multicast group memberships.
  • ip maddr add IP_ADDRESS dev INTERFACE : This adds the INTERFACE to the membership of the multicast group with an  IPv4 address of IP_ADDRESS.
  • ip maddr delete IP_ADDRESS dev INTERFACE : Leave the INTERFACE from the multicast group with ip address IP_ADDRESS.

This ip command cheat sheet should provide you with a quick reference for managing network configurations and interfaces in Linux.

Remember to replace INTERFACE, IP_ADDRESS, NETMASK, NETWORK, GATEWAY, VLAN_NAME, BRIDGE_NAME, TUNNEL_NAME, REMOTE_IP, LOCAL_IP, QUEUE_LENGTH, and other placeholders with your specific values when using the commands.

 

Wednesday, June 12, 2013

Cut command reference

Cut is a simpler alternative to the sed command. Cut is also very efficient and flexible..

The examples specified below is from Solaris 11.1 and  RedHat Enterprise Linux 6.4, however most of the Linux / Unix distributions supports the cut command.

We shall use the below file for our examples.

root@sol1:~/jk# cat testfile.txt
This is a test file to demonstrate "cut" command.
Cut can be used for line filtering.
Cut is simpler than awk and sed commands.
root@sol1:~/jk#


1. Select a field from a file.
You can combine -f and -d to print required field. -f specifies the field and -d specified the delimiter to be used to find the filed.

root@sol1:~/jk# cut -d':' -f1 /etc/passwd | head -5
root
daemon
bin
sys
adm
root@sol1:~/jk#


2. Select multiple fields from a file.
To select multiple fields use comma separated numbers with -f.

root@sol1:~/jk# cut -d' ' -f 1,5 testfile.txt
This file
Cut for
Cut awk
root@sol1:~/jk#
Here 'space' is the delimiter.


3. Select Column of characters.
To print selected column or character from a file use -c option.

root@sol1:~/jk# cut -c2 testfile.txt
h
u
u
root@sol1:~/jk#
 
As you can see, h,u,u are the 2nd character on each line.


4.  Select column of characters using range.
 
To print from 3rd character to the end :

root@sol1:~/jk# cut -c3- testfile.txt
is is a test file to demonstrate "cut" command.
t can be used for line filtering.
t is simpler than awk and sed commands.
root@sol1:~/jk#

To print till 5th character :

root@sol1:~/jk# cut -c-5 testfile.txt
This
Cut c
Cut i
root@sol1:~/jk#

To print from 5th character to 10th character.
 
root@sol1:~/jk# cut -c5-10 testfile.txt
 is a
can be
is sim
root@sol1:~/jk#

Whole lines will be print if no number is specified before or after '-'.

root@sol1:~/jk# cut -c- testfile.txt
This is a test file to demonstrate "cut" command.
Cut can be used for line filtering.
Cut is simpler than awk and sed commands.
root@sol1:~/jk#

5. Print fields only when the delimiter specified is present.

If the delimiter specified is not present in the file, cut will print the whole lines.

root@sol1:~/jk# cut -d'|' -f1 /etc/passwd | head -5
root:x:0:0:Super-User:/root:/usr/bin/bash
daemon:x:1:1::/:
bin:x:2:2::/usr/bin:
sys:x:3:3::/:
adm:x:4:4:Admin:/var/adm:
root@sol1:~/jk#

Use '-s' to prevent this behaviour.

root@sol1:~/jk# cut -d'|' -s -f1 /etc/passwd | head -5
root@sol1:~/jk#

6. To select all fields except the ones specified.
Use --complement to invert cut's behavior. Note '--complement' is not available in Solaris.

root@sol1:~/jk# cut -d' ' --complement -f1,3 testfile.txt
is test file to demonstrate "cut" command.
can used for line filtering.
is than awk and sed commands.
root@sol1:~/jk#


7. Change output delimiter for ease of reading.
You can use '--output-delimiter' to custom specify the delimiter to print. Note : --output-delimiter is not available in Solaris.

root@rhel:~/jk# grep "/bin/bash" /etc/passwd | cut -d':'  -s -f1,6,7 --output-delimiter='#'
root#/root#/usr/bin/bash
root@rhel:~/jk#

You can also use the escape characters as follows. For changing the delimiter to "new line":

root@sol1:~/jk# grep "/bin/bash" /etc/passwd | cut -d':'  -s -f1,6,7 --output-delimiter=$'\n'
root
/root
/usr/bin/bash
root@sol1:~/jk#

Remember to use "$" as the escape character.

Cut is a very cool and simple utility. Master it !!

JK.