Linux

Networking

# Find the IP address you use to communicate with the outside world.
## IPv4 only
curl ip.twis7.com
## Both IPv4 and IPv6. If you have IPv6, IPv4 will not be displayed.
curl https://ip.novoselski.eu

# Check for listening ports
sudo netstat -tulpn | grep LISTEN

# Check outgoing connections
sudo netstat -nputw

# Netcat - Test for open port / tcp conection, when you do not have telnet
nc -vz google.com 80

# Firewall - When you forget the difference between statefull vs stateless firewall
Security groups are stateful—if you send a request from your instance, the response traffic for that request is allowed to flow in regardless of inbound security group rules.

# hping a.k.a telnet used as ping
sudo hping3 -S 192.168.141.120 -p 80

#SSH via jumphost
##different keys
ssh -i /home/dimitar/.ssh/key -J user@jumphost -i /home/dimitar/.ssh/key user@destination

##same keys
ssh -i .ssh/key -J user@jumphost user@destination

## ubuntu flush dns
sudo resolvectl flush-caches

Troubleshooting

# check all services like ps aux
service --status-all

# Processes using most CPU or RAM
ps -eo pid,comm,%cpu,%mem --sort=-%cpu | head -n 5

# To see top 4 memory consuming process is
ps -eo pid,comm,%cpu,%mem --sort=-%mem | head -n 5
Where,
\-e : This flag is used to select all process
\-o : This flag is used to format as user-defined.
pid : This argument used for showing pid
comm: This argument used for showing command name only. To get full command use args, cmd or command
%cpu: This argument shows the percentage of cpu utilization of the process in "##.#" format. Here pcpu can also be used.
%mem: This argument shows the ratio of the process's resident set size to the physical memory on the machine, expressed as a percentage. Here pmem can also be used.
\--sort: Specify sorting order. After = the - sign is used to sort highest value at the top where the default option which is + is to list increasing numerical order \[i.e 0 to n\].

#List open file
lsof /var/log/syslog #-> check which proccess is using a file
lsof -i TCP:22       #->  what process uses a particular TCP port
lsof -p 890          #-> you need to know what files a particular process has open

# See open files and executed scripts by PID
sudo lsof -p 329953

Working with Files

# find folder storage size. Change 1, with 2 or 3 for subfolders
sudo du -hd 1 /home/dimitar

# Live tail, show last 100 results
tail -100f /var/log/mylog.log

# Searche for text inside files, recursively (it will check subfolders too)
grep -Hrn 'search term' path/to/files
### you can use also
grep -Rnw '/path/' -e 'patern' 

# To parse all instances of the words error or failure in /var/log/cfn or /var/log/cloud-init files, run the following command:
grep -ni 'error|failure' $(sudo find /var/log -name cfn\* -or -name cloud-init\*)
To parse only in one file:
grep -ni 'error|failure' /var/log/cfn-init.log

# Find file only, recursively (it will check subfolders too)
find /path/to/file -name "DetailsMessage.php"

# Delete folder content including hidden files
sudo rm -rf FolderName/.\[^.\]\* 

# Copy files including hidden ones (with dot). Don't specify the files, (Note that /home/FolderToPaste must not exist already, or else it will create /home/FolderToCopy inside it 
cp -r /home/FolderToCopy /home/FolderToPate

# find full path of a file. Usefull when want to copy/paste file path inside terminal
realpath file.txt  # output /home/dimitar/downloads/file.txt
or
readlink -f file.txt # same as realpath