commands for linux troubleshooting (draft)
14 June 2016
port
List open ports
sudo netstat -tulpn | grep LISTEN
view port number and mapped service
cat /etc/services
grep -w '80/tcp' /etc/services
grep -w '443/tcp' /etc/services
grep -E -w '22/(tcp|udp)' /etc/services
grep -E -w '2020/(tcp|udp)' /etc/services
cat /etc/services | grep 8080
list the programs that utilize listening ports
# Display a list of ports in use
sudo lsof -nP -iTCP -sTCP:LISTEN
# Check a specific port number is in use
# If the port is free, the command shows no output
sudo lsof -nP -i :8080
sudo lsof -nP -iUDP:53 # to check if the UDP port 53 is open
sudo fuser 9092/tcp
sudo fuser -n tcp 9092
# display the listening ports/sockets by checking the State column
sudo netstat -tunpl
sudo netstat -ant | grep :2181
sudo netstat -peanut | grep ":5140"
kill process on specific port
fuser -k 8080/tcp
kill -9 $(lsof -t -i:8080)
kill -9 $(lsof -t -i:9092)
fuser -k 9092/tcp
connecting to open port
sudo yum install telnet
telnet localhost 8080
scanning port for open port & running services on it
sudo yum install nmap
sudo nmap -sT -O localhost
sudo nmap -sT -O 127.0.0.1 #[ list open TCP ports ]##
sudo nmap -sU -O 192.168.2.254 #[ list open UDP ports ]##
sudo nmap -sTU -O 192.168.2.24
viewing port info using iptables
# list out all of the active iptables rules by specification
sudo iptables -S
# output all of the active iptables rules in a table
sudo iptables -L
Opening a Port on Linux to Allow TCP Connections
# accept all traffic on your loopback interface
sudo iptables -A INPUT -i lo -j ACCEPT
sudo iptables -A OUTPUT -o lo -j ACCEPT
# Allowing All Incoming fluent-bit port (2020)
sudo iptables -A INPUT -p tcp --dport 2020 -m conntrack --ctstate NEW,ESTABLISHED -j ACCEPT
sudo iptables -A OUTPUT -p tcp --sport 2020 -m conntrack --ctstate ESTABLISHED -j ACCEPT
permission
Changing the ownership of all sub-directories
sudo chown -R snikam:snikam ./Prod/
process
- process id of running program
ps aux | grep java
- kill prcess with given id
ps aux | grep -i firefox | awk {'print $2'} | xargs kill -9
Shutdown/Restart
- Restart now
sudo shutdown -r now
- shutdown now (immediately)
sudo ?
networking
Assign static ip
- Check if it is configured
sudo ifdown eth0
- Edit like this (just the important lines):
sudo gedit /etc/network/interfaces
auto eth0
iface eth0 inet static
address <static IP address>
netmask <netmask>
gateway <default gateway>
pre-up sleep 2
- Activate the new IP
sudo ifup eth0
Python
ModuleNotFoundError: no module named <X> Error
import sys
import os
# add dir to PYTHONPATH
sys.path.append(os.getcwd())
export PYTHONPATH=${PYTHONPATH}:${HOME}/abcPythonModule
other
vs code - column selction on Mac
Shift + option + Command + Right/Left