> ## Documentation Index
> Fetch the complete documentation index at: https://docs.apinizer.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Linux Possible Issues, Solutions and Helper Commands

> Provides common issues and solutions that may be encountered in Linux systems. Offers practical commands and solutions for topics such as system time settings, disk and memory controls, processor information, file operations, network configurations, and certificate management.

## Other

### To check system time with "date" command and fix if it's in wrong timezone

```bash theme={null}
sudo ln -sf /usr/share/zoneinfo/Europe/Istanbul /etc/localtime
```

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

```bash theme={null}
date -s '2014-12-25 12:34:56'
```

### If Ubuntu no-key error is received

```bash theme={null}
# 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

```bash theme={null}
chsh -s /bin/bash $(whoami)
```

### To see applications killed by OOM Killer

```bash theme={null}
dmesg -T | egrep -i 'killed process'
```

***

## Disk Checks

### To See Current Usage and Limits on All Disks

```bash theme={null}
df -h
```

### To See Current Usage and Limits at a Specific Address

```bash theme={null}
du -sh <FILE_PATH>
```

## Memory Checks

### Memory Information

```bash theme={null}
cat /proc/meminfo
```

### To See Current Memory Amount and Limits

```bash theme={null}
free -g
```

## Processor Checks

### CPU Information

```bash theme={null}
less /proc/cpuinfo
lscpu
```

### To See Only Processor Count

```bash theme={null}
cat /proc/cpuinfo | grep processor | wc -l
```

### To See Total Thread Count

```bash theme={null}
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

```bash theme={null}
ps -ef | grep java
# Java's id is written below, example: 10057
ps huH p 10057 | wc -l
```

### Taking dump

```bash theme={null}
# 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

```bash theme={null}
ulimit -Su 30000
```

***

## Jar Operations

### Extracting File from Jar

```bash theme={null}
jar -xf apinizer-gateway-2.1.2.jar
```

### Creating Jar from File

```bash theme={null}
jar -cf apinizer-gateway-2.1.2.jar **
```

### Creating Jar from File with manifest File

```bash theme={null}
/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

```bash theme={null}
scp -r /opt/apinizerManager/* <REMOTE_SERVER_USER>@<REMOTE_SERVER_ID>:<PATH_TO_COPY_TO>
```

### To Convert tar.gz File to zip

```bash theme={null}
tar xzf jre282linux.tar.gz && zip jre282linux.zip $(tar tf jre282linux.tar.gz)
```

### To See Open Files

```bash theme={null}
lsof | wc -l
```

### To See Settings Related to Files

```bash theme={null}
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

```bash theme={null}
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

```bash theme={null}
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

```bash theme={null}
sudo iptables -A PREROUTING -t nat -i ens160 -p tcp --dport 443 -j REDIRECT --to-port 9443
iptables-save > /opt/apinizerManager/iptables.conf
```

```bash theme={null}
# 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

```bash theme={null}
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

```bash theme={null}
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

```bash theme={null}
netstat -ltnp | grep -w ':80'
```

### Running Ports

```bash theme={null}
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

```bash theme={null}
openssl s_client -showcerts -connect <URL>:443 </dev/null 2>/dev/null|openssl x509 -outform PEM > mycertfile.pem
```

### Certificate import to jar file application

```bash theme={null}
/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

```bash theme={null}
:q!
```

### To exit by saving edits

```bash theme={null}
:wq
```

### Replace All

```bash theme={null}
:%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

```bash theme={null}
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

```bash theme={null}
curl -u username "http://api.address.com?wsdl" --output response.wsdl
```
