# Common Commands

# Server Administration

## Smartctl

* check all attributes about a drive  
`sudo smartctl -i /dev/sdX -a`

* megaraid controllers (LSI something)  
`sudo smartctl -d megaraid,X -i /dev/sda -a`  
The drive polled (`/dev/sda`) doesn't seem to make a difference here

# LVM - Add new disk and extend FS

### Rescan for newly added disks without rebooting:

The command will iterate over all found host adapters and trigger a rescan.
```
>> for host in /sys/class/scsi_host/*; do echo "- - -" | sudo tee $host/scan; ls /dev/sd* ; done
- - -
/dev/sda  /dev/sda1  /dev/sda2  /dev/sdb  /dev/sdb1  /dev/sdb2  /dev/sdc  /dev/sdc1
- - -
/dev/sda  /dev/sda1  /dev/sda2  /dev/sdb  /dev/sdb1  /dev/sdb2  /dev/sdc  /dev/sdc1
- - -
/dev/sda  /dev/sda1  /dev/sda2  /dev/sdb  /dev/sdb1  /dev/sdb2  /dev/sdc  /dev/sdc1  /dev/sdd
- - -
/dev/sda  /dev/sda1  /dev/sda2  /dev/sdb  /dev/sdb1  /dev/sdb2  /dev/sdc  /dev/sdc1  /dev/sdd  /dev/sdd1
```
The output shows when a new drive has been discovered.

### LVM add "physical" disk and extend FS

* Make sure there is a Partition Table and a Partition for LVM  
```
fdisk /dev/<disk>
>> create new gpt table: g
>> create new partition: n
>> set type of partition to lvm: t 
>> show partition codes: L
>> insert type: <type from L (Linux LVM)>
>> write changes: w
```
* Expand existing LVM VG
```sh
pvcreate /dev/<disk-partition>
vgextend <existing-vg> /dev/<disk-partition>
```

* Expand existing LVM LV
```sh
# find the correct logical volume
lvdisplay | grep Path 

# expand found lv
lvresize -l +100%free <lv path>
```

* Expand the ext4 filesystem on it
```sh
resize2fs <lv path>
```

#### Sources:
* Add disk: [https://tylersguides.com/guides/how-to-add-a-disk-to-lvm/]()
* Extend FS: [https://www.systutorials.com/extending-a-mounted-ext4-file-system-on-lvm-in-linux/]()

# SSD Secure Erase

### Secure erase commands:

Check current status of the disk:
```
>> sudo hdparm -I /dev/sdX

-----
Security:
	Master password revision code = 65534
		supported
	not	enabled   	
	not	locked
	not	frozen
	not	expired: security count
		supported: enhanced erase
```

The disk needs to be not locked, not frozen but enabled.
To enable secure erase set a password for the master user on the disk.

```
>> sudo hdparm --user-master u --security-set-pass password /dev/sdX

-----
security_password: "password"

/dev/sdd:
 Issuing SECURITY_SET_PASS command, password="password", user=user, mode=high
```

If something else appears, like I/O errors or so, check if you are connected to a motherboard port that supports these ATA commands.
e.g. my USB Sata adapter did not, thus the commands could not be sent to the drive.

Afterwards we can erase the drive
```
sudo hdparm --user-master u --security-erase password /dev/sdX

-----
security_password: "password"

/dev/sdd:
 Issuing SECURITY_ERASE command, password="password", user=user
```

After this, the drive should appear unformated and without a partition table.

# User Management

### SSH2 Public Key to OpenSSL format for SSH Server

To convert a given Public Key to a useful format for SSH servers use the following command.
It will print out the expected format to use.

```bash
ssh-keygen -i -f /path/to/file
```