Skip to main content

Other

To check system time with “date” command and fix if it’s in wrong timezone

sudo ln -sf /usr/share/zoneinfo/Europe/Istanbul /etc/localtime

To manually set date if it’s in correct timezone but still wrong

date -s '2014-12-25 12:34:56'

If Ubuntu no-key error is received

# W: GPG error: https://download.docker.com/linux/ubuntu focal InRelease: The following signatures couldn't be verified because the public key is not available: NO_PUBKEY 7EA0A9C3F273FCD8

sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 7EA0A9C3F273FCD8

To fix SSH working with dash instead of bash due to user definition

chsh -s /bin/bash $(whoami)

To see applications killed by OOM Killer

dmesg -T | egrep -i 'killed process'

Disk Checks

To See Current Usage and Limits on All Disks

df -h

To See Current Usage and Limits at a Specific Address

du -sh <FILE_PATH>

Memory Checks

Memory Information

cat /proc/meminfo

To See Current Memory Amount and Limits

free -g

Processor Checks

CPU Information

less /proc/cpuinfo
lscpu

To See Only Processor Count

cat /proc/cpuinfo | grep processor | wc -l

To See Total Thread Count

ps -eo nlwp | tail -n +2 | awk '{ num_threads += $1 } END { print num_threads }'

cat /proc/sys/kernel/threads-max

To See Total Thread Count Belonging to Java Application

ps -ef | grep java
# Java's id is written below, example: 10057
ps huH p 10057 | wc -l

Taking dump

# jcmd -> when under JRE
sudo -u apinizer ./jcmd 26032 Thread.print > ./Thread.dump
sudo -u apinizer ./jcmd 26032 GC.heap_dump ./Heap.dump

Determining Process Count for Current User

ulimit -Su 30000

Jar Operations

Extracting File from Jar

jar -xf apinizer-gateway-2.1.2.jar

Creating Jar from File

jar -cf apinizer-gateway-2.1.2.jar **

Creating Jar from File with manifest File

/opt/apinizerManager/jre8162linux/bin/jar -cfm apinizer-gateway-2.1.2.jar ./META-INF/MANIFEST.MF **

File Operations

Transferring Folder to Another Server with SCP

scp -r /opt/apinizerManager/* <REMOTE_SERVER_USER>@<REMOTE_SERVER_ID>:<PATH_TO_COPY_TO>

To Convert tar.gz File to zip

tar xzf jre282linux.tar.gz && zip jre282linux.zip $(tar tf jre282linux.tar.gz)

To See Open Files

lsof | wc -l
cat /etc/sysctl.conf
# system-wide maximum number of files
cat /proc/sys/fs/file-max
cat /proc/sys/fs/file-nr

To See Top Ten File Descriptors with Most Open Files

cd /proc
for pid in [0-9]*
do
    echo "PID = $pid with $(ls /proc/$pid/fd/ | wc -l) file descriptors"
done | sort -rn -k5 | head | while read -r _ _ pid _ fdcount _
do
  command=$(ps -o cmd -p "$pid" -hc)
  printf "pid = %5d with %4d fds: %s\n" "$pid" "$fdcount" "$command"
done

Processes with Most Open Files

lsof -Fnst | awk '
    { field = substr($0,1,1); sub(/^./,""); }
    field == "p" { pid = $0; }
    field == "t" { if ($0 == "REG") size = 0; else next; }
    field == "s" { size = $0; }
    field == "n" && size != 0 { print size, $0; }
' | sort -k1n -u | tail -n42 | sed 's/^[0-9]* //'

Firewall Operations

sudo iptables -A PREROUTING -t nat -i ens160 -p tcp --dport 443 -j REDIRECT --to-port 9443
iptables-save > /opt/apinizerManager/iptables.conf
# The following is written into startManager
iptables-restore < /opt/apinizerManager/iptables.conf

max_allowed_packet=500M
lower_case_table_names=1
innodb_log_file_size=2G

Wget Operations

Downloading File from Google Drive with Wget

wget --load-cookies /tmp/cookies.txt "https://docs.google.com/uc?export=download&confirm=$(wget --quiet --save-cookies /tmp/cookies.txt --keep-session-cookies --no-check-certificate 'https://docs.google.com/uc?export=download&id=Adres_Unique_Idsi_yZ8JlXtWwdOQg_5h' -O- | sed -rn 's/.*confirm=([0-9A-Za-z_]+).*/\1\n/p')&id=Adres_Unique_Idsi_yZ8JlXtWwdOQg_5h" -O apinizer.zip && rm -rf /tmp/cookies.txt

Sorting by File Size

sudo lsof | grep REG | grep -v "stat: No such file or directory" | grep -v DEL | awk '{if ($NF=="(deleted)") {x=3;y=1} else {x=2;y=0}; {print $(NF-x) "  " $(NF-y) } }' | sort -n -u | numfmt --field=1 --to=iec

Network Operations

To See Process or Service Using a Specific Port

netstat -ltnp | grep -w ':80'

Running Ports

ss -antp
Example output:
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 0.0.0.0:22 0.0.0.0:* users:(("sshd",pid=734,fd=4))
ESTAB 0 0 173.249.11.199:22 3.83.11.233:48610 users:(("sshd",pid=44179,fd=5),("sshd",pid=44178,fd=5))
Other frequently used parameters:
  • -t: Show only TCP sockets on Linux
  • -u: Display only UDP sockets on Linux
  • -l: Show listening sockets. For example, TCP port 22 is opened by SSHD server.
  • -p: List process name that opened sockets
  • -n: Don’t resolve service names i.e. don’t use DNS

Certificate Operations

Getting server certificate from Linux server

openssl s_client -showcerts -connect <URL>:443 </dev/null 2>/dev/null|openssl x509 -outform PEM > mycertfile.pem

Certificate import to jar file application

/opt/apinizerManager/jre8282linux/jre/bin/keytool -import -trustcacerts -keystore /opt/apinizerManager/jre8282linux/jre/lib/security/cacerts -storepass changeit -alias <CERTIFICATE_ALIAS> -import -file <CERTIFICATE_FILE>

Vi Editor Operations

To write all commands related to Vi and vim, first press the escape key to enter command mode and most should start with : (colon) character.

To exit without saving edits

:q!

To exit by saving edits

:wq

Replace All

:%s/old_word/new_word/g

CURL Operations

Parameters:
  • -k: To go to SSL addresses without verification
  • -v: For log
  • -m 10: For timeout
  • -u <USERNAME>: To send username and prompt for password
  • --output response.txt: To save output

Sending SOAP request and logging response

curl -D - --header "Content-Type:text/xml;charset=UTF-8" --header "SOAPAction:XXX" -d '<SOAP_BODY_GOES_HERE>' https://api.adres/apigateway/relativepath >> response.txt

Accessing Basic auth WSDL address (will ask for password) and saving it

curl -u username "http://api.address.com?wsdl" --output response.wsdl