# Linux - Bag of Tricks (Part 2): Certificates, Databases, Disks & Boot

<p class="callout info">This page continues [Linux - Bag of Tricks](https://docs.thehomestack.io/books/linux/page/linux-bag-of-tricks).</p>

### Create a CERT

First, you need to generate the private key and the Certificate Signing Request (CSR). You can do this via the <span class="code" spellcheck="false">openssl</span> command:

```shell
openssl req -nodes -newkey rsa:2048 -keyout privatekey.key -out mail.csr  
```

Then, generate a signing request

```shell
openssl x509 -req -days 365 -in mail.csr -signkey privatekey.key -out secure.crt
```

Create a localhost cert on the server

```shell
openssl req -newkey rsa:2048 -nodes -keyout /etc/pki/tls/private/localhost.key -x509 -days 365 -out /etc/pki/tls/certs/localhost.crt
```

#### Mariadb Log Rotate

If log file is large, try if the logrotate

<span lang="FR-CA" style="mso-ansi-language: FR-CA;">logrotate --force /etc/logrotate.d/mariadb</span>

#### MySQL Fail to Start

If MySQL does not restart, it probably will not as the index of the log files will not be changed

<table border="0" cellpadding="0" cellspacing="0" class="MsoNormalTable" id="bkmrk-cd-%2Fvar%2Flib%2Fmysqlmv-" style="width: 580.5pt; margin-left: -9.5pt; border-collapse: collapse; mso-yfti-tbllook: 1184; mso-padding-alt: 0in 0in 0in 0in;" width="774"><tbody><tr style="mso-yfti-irow: 0; mso-yfti-firstrow: yes; mso-yfti-lastrow: yes;"><td style="width: 580.5pt; border: solid windowtext 1.0pt; padding: 0in 5.4pt 0in 5.4pt;" valign="top" width="774">cd /var/lib/mysql  
mv ib\_logfile0 ib\_logfile0.old  
mv ib\_logfile1 ib\_logfile1.old  
systemctl restart mariadb

</td></tr></tbody></table>

<span style="mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri; mso-bidi-font-family: Calibri;"> </span>

#### Configure Rsync

<span style="mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-family: 'Courier New'; mso-bidi-font-weight: bold;">Useful for system migrations</span>

<span style="mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-family: 'Courier New'; mso-bidi-font-weight: bold;">Create a “</span><span style="mso-bookmark: OLE_LINK22;"><span style="font-family: 'Courier New'; mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-weight: bold;">/etc/rsyncd.conf</span></span><span style="mso-bookmark: OLE_LINK22;"><span style="mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-family: 'Courier New'; mso-bidi-font-weight: bold;">” </span></span><span style="mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-family: 'Courier New'; mso-bidi-font-weight: bold;">containing:</span>

<span style="mso-bookmark: OLE_LINK24;"><span lang="FR-CA" style="font-family: 'Courier New'; mso-fareast-font-family: 'Times New Roman'; mso-ansi-language: FR-CA; mso-bidi-font-weight: bold;">\[root\]</span></span>

<span style="mso-bookmark: OLE_LINK25;"><span style="mso-bookmark: OLE_LINK24;"><span lang="FR-CA" style="font-family: 'Courier New'; mso-fareast-font-family: 'Times New Roman'; mso-ansi-language: FR-CA; mso-bidi-font-weight: bold;">exclude = <span style="mso-bookmark: OLE_LINK142;">/dev /etc/fstab /proc /sys</span></span></span></span>

<span style="mso-bookmark: OLE_LINK25;"><span style="mso-bookmark: OLE_LINK24;"><span style="mso-bookmark: OLE_LINK144;"><span style="font-family: 'Courier New'; mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-weight: bold;">path = /</span></span></span></span>

<span style="mso-bookmark: OLE_LINK25;"><span style="mso-bookmark: OLE_LINK24;"><span style="mso-bookmark: OLE_LINK145;"><span style="mso-bookmark: OLE_LINK144;"><span style="font-family: 'Courier New'; mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-weight: bold;">read only = yes</span></span></span></span></span>

<span style="mso-bookmark: OLE_LINK25;"><span style="mso-bookmark: OLE_LINK24;"><span style="mso-bookmark: OLE_LINK145;"><span style="mso-bookmark: OLE_LINK144;"><span style="font-family: 'Courier New'; mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-weight: bold;">list = yes</span></span></span></span></span>

<span style="mso-bookmark: OLE_LINK25;"><span style="mso-bookmark: OLE_LINK24;"><span style="font-family: 'Courier New'; mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-weight: bold;">uid = root</span></span></span>

<span style="mso-bookmark: OLE_LINK25;"><span style="mso-bookmark: OLE_LINK24;"><span style="font-family: 'Courier New'; mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-weight: bold;">gid = root</span></span></span>

<span style="mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-family: 'Courier New'; mso-bidi-font-weight: bold;"> </span>

<span style="mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-family: 'Courier New'; mso-bidi-font-weight: bold;">Enable and start:</span>

<span style="font-family: 'Courier New'; mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-weight: bold;">systemctl enable rsyncd.service </span>

<span style="mso-bookmark: OLE_LINK39;"><span style="mso-bookmark: OLE_LINK26;"><span style="font-family: 'Courier New'; mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-weight: bold;">systemctl start rsyncd.service<span style="mso-spacerun: yes;"> </span></span></span></span>

#### Change Run level

<span style="mso-bookmark: OLE_LINK136;"><span style="font-family: Consolas; mso-bidi-font-family: 'Courier New';">systemctl set-default multi-user.target</span></span>

To switch from graphical to multi-user:

<span style="font-family: Consolas; mso-bidi-font-family: 'Courier New';">systemctl isolate multi-user.target;</span>

#### Change Local settings

<span style="font-family: Consolas;">\# timedatectl set-timezone Europe/London<span style="mso-spacerun: yes;"> </span></span>

<span style="font-family: Consolas;">\# localectl set-locale LANG=en\_GB.UTF-8</span>

<span style="font-family: Consolas;">\# localectl set-keymap uk</span>

Temporary change

$ loadkeys us

#### Configure Alternate Authentication

<span style="mso-bookmark: OLE_LINK67;"><span style="mso-bookmark: OLE_LINK66;">authconfig-tui</span></span>

#### <span style="mso-bookmark: _Hlk96703517;">SSD Considerations</span>

<span style="mso-bookmark: _Hlk96703517;">Change the value of “</span><span style="mso-bookmark: _Hlk96703517;">`<span style="font-size: 10.0pt; line-height: 107%; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin;">issue_discards”</span>` option from 0 to 1 in “</span><span style="mso-bookmark: _Hlk96703517;">`<span style="font-size: 10.0pt; line-height: 107%; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin;">/etc/lvm/lvm.conf”</span>`</span>

<span style="mso-bookmark: _Hlk96703517;">`<span style="font-size: 10.0pt; line-height: 107%; font-family: Consolas; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin;">#systemctl enable fstrim.timer</span>`</span>

<span style="mso-bookmark: _Hlk96703517;"><span style="mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-family: 'Courier New'; mso-bidi-font-weight: bold;">Adjust “/etc/fstab</span></span><span style="mso-bookmark: _Hlk96703517;"><span style="font-family: Consolas; mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-family: 'Courier New'; mso-bidi-font-weight: bold;">”</span></span>

<span style="mso-bookmark: _Hlk96703517;"><span style="font-family: Consolas; mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-family: 'Courier New'; mso-bidi-font-weight: bold;">/dev/mapper/xxx /XXX<span style="mso-spacerun: yes;"> </span>xfs<span style="mso-spacerun: yes;"> </span>defaults<span style="background: yellow; mso-highlight: yellow;">,noatime,discard</span><span style="mso-spacerun: yes;"> </span>0 0</span></span>

<span style="mso-bookmark: _Hlk96703517;"><span class="st">Optionally set /tmp in RAM</span></span>

<span style="mso-bookmark: _Hlk96703517;"><span class="st"><span style="font-family: Consolas; mso-bidi-font-weight: bold;">\# systemctl enable tmp.mount</span></span></span>

#### <span style="mso-bookmark: _Hlk96703517;"><span class="st"><span style="mso-bidi-font-weight: bold;">Adding a Disk</span></span></span>

<span style="mso-bookmark: _Hlk96703517;"><span style="mso-bookmark: OLE_LINK109;"><span class="st"><span style="font-family: Consolas;">\# parted /dev/sdx</span></span></span></span>

<span style="mso-bookmark: _Hlk96703517;"><span class="st"> </span></span>

<span style="mso-bookmark: _Hlk96703517;"><span style="mso-bookmark: OLE_LINK111;"><span class="st"><span style="font-family: Consolas; mso-bidi-font-family: 'Courier New';">mklabel gpt</span></span></span></span>

<span style="mso-bookmark: _Hlk96703517;"><span style="mso-bookmark: OLE_LINK112;"><span style="mso-bookmark: OLE_LINK111;"><span class="st"><span style="font-family: Consolas; mso-bidi-font-family: 'Courier New';">unit s</span></span></span></span></span>

<span style="mso-bookmark: _Hlk96703517;"><span style="mso-bookmark: OLE_LINK112;"><span style="mso-bookmark: OLE_LINK111;"><span class="st"><span style="font-family: Consolas; mso-bidi-font-family: 'Courier New';">mkpart primary 2048s 100%</span></span></span></span></span>

<span style="mso-bookmark: _Hlk96703517;"><span class="st"><span style="font-family: Consolas; mso-bidi-font-family: 'Courier New';">set 1 lvm on</span></span></span>

<span style="mso-bookmark: _Hlk96703517;"><span class="st"><span style="font-family: Consolas; mso-bidi-font-family: 'Courier New';">quit</span></span></span>

<span style="mso-bookmark: _Hlk96703517;"><span class="st"><span style="font-family: 'Courier New';"> </span></span></span>

<span style="mso-bookmark: _Hlk96703517;"><span class="st"><span style="font-family: Consolas; mso-bidi-font-family: 'Courier New';">\# pvcreate /dev/sdx1</span></span></span>

<span style="mso-bookmark: _Hlk96703517;"><span style="font-family: Consolas; mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-family: 'Courier New';">\# vgcreate rl\_ssd /dev/sdx1</span></span>

<span style="mso-bookmark: _Hlk96703517;"><span class="st"><span style="font-family: Consolas; mso-bidi-font-family: 'Courier New';">\# lvcreate –L 50GB -n mysql rl\_</span></span></span><span style="mso-bookmark: _Hlk96703517;"><span style="font-family: Consolas; mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-family: 'Courier New';">ssd</span></span>

<span style="mso-bookmark: _Hlk96703517;"><span class="st"><span style="font-family: Consolas; mso-bidi-font-family: 'Courier New';">\# mkfs.xfs /dev/</span></span></span><span style="mso-bookmark: _Hlk96703517;"><span style="font-family: Consolas; mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-family: 'Courier New';">rl-ssd</span></span><span style="mso-bookmark: _Hlk96703517;"><span class="st"><span style="font-family: Consolas; mso-bidi-font-family: 'Courier New';">/mysql</span></span></span>

<span style="mso-bookmark: _Hlk96703517;"><span class="st"><span style="font-family: Consolas;">\# blkid /dev/sdc</span></span></span>

<span style="mso-bookmark: _Hlk96703517;"><span class="st"><span style="font-family: Consolas;">\# chown mysql:mysql /var/lib/mysql</span></span></span>

#### <span class="st">Growing a lvm partition</span>

<span class="st"><span style="font-family: Consolas;">\# parted /dev/sdc</span></span>

<span class="st"><span style="font-family: Consolas;"> </span></span>

<span class="st"><span style="font-family: Consolas;">(parted) unit b</span></span>

<span class="st"><span style="font-family: Consolas;">(parted) print free</span></span>

<span class="st"><span style="font-family: Consolas;">Number<span style="mso-spacerun: yes;"> </span>Start<span style="mso-spacerun: yes;"> </span>End<span style="mso-spacerun: yes;"> </span>Size<span style="mso-spacerun: yes;"> </span>Type<span style="mso-spacerun: yes;"> </span>File system<span style="mso-spacerun: yes;"> </span>Flags</span></span>

<span class="st"><span style="font-family: Consolas;"><span style="mso-spacerun: yes;"> </span>1<span style="mso-spacerun: yes;"> </span>31744B<span style="mso-spacerun: yes;"> </span>5368709119B<span style="mso-spacerun: yes;"> </span>5368677376B<span style="mso-spacerun: yes;"> </span>primary</span></span>

<span class="st"><span style="font-family: Consolas;"><span style="mso-spacerun: yes;"> </span>5368709120B<span style="mso-spacerun: yes;"> </span>21474836479B<span style="mso-spacerun: yes;"> </span>16106127360B<span style="mso-spacerun: yes;"> </span><span style="mso-spacerun: yes;"> </span>Free Space</span></span>

<span class="st"><span style="font-family: Consolas;">(parted) resizepart 1<span style="mso-spacerun: yes;"> </span>21474836479B</span></span>

<span class="st"><span style="font-family: Consolas;">(parted) quit</span></span>

<span class="st"><span style="font-family: Consolas;"> </span></span>

<span class="st"><span style="font-family: Consolas;">\# pvresize /dev/sdc1</span></span>

**<span style="font-size: 16.0pt; line-height: 107%;"> </span>**

<span class="st"> </span>

#### <span class="st">**Updating Bootloader configuration**</span>

<span class="st">/etc/default/grub</span>

<span class="st">grub2-mkconfig -o /boot/grub2/grub.cfg</span>

NMAP Scan for all Open Ports

TCP

```shell
sudo nmap -sT -p- example.com
```

UDP

```shell
sudo nmap -sU -p- example.com
```

Look for open Ports

```bash
nc -vz 203.0.113.10 514
```

Trace route

```bash
sudo tracepath 203.0.113.10
```

Looking at the Journal, this is an example to look at the mariadb process

```bash
journalctl -u mariadb -f
```

Search in sub folders

```bash
grep -r "MYSQL_HOST" . --include="compose.yaml" --include="docker-compose.yml"
```

Change Lines in KIDSENV and make a backup of it with a .bak extentions

```bash
find . -type f -name "KIDSENV" -exec sed -i.bak 's/^KWSQL_LOG=query/#KWSQL_LOG=query/' {} +
```

Delete file on the server

```bash
find . -type f \( -name "SQLQRY*" -o -name "SQLINFO" -o -name "SQLERROR" \) -delete
```

Delete files over 100M

```bash
find . -type f -size +100M
```

Delete files over 100M and list them

```bash
find . -type f -size +100M -exec ls -lh {} +
```

Grep files named TX\*.DA that are only 1 month back and files lines that have RETDT or RETHD

```bash
find . -type f -name "TX*.DA" -mtime -30 -exec grep -iE "RETDT|RETHD" {} +
```

Remove certain directory

```
find /path/to/drive/root -type d -name "@eaDir" -print0 | xargs -0 rm -rf
```