Misc. pi commands (debian)

Listing various things

1. List hardware

There are two packages, equally useful so demonstrating both:

lshw and lspci.

# sudo apt install lshw
sudo lshw -c video

# sudo apt install pciutils
sudo lspci | grep VGA
powerstat -R -c -z

2. List CPU temperature

# CPU temperature
cat /sys/devices/platform/coretemp.0/hwmon/hwmon2/temp*_label
cat /sys/devices/platform/coretemp.0/hwmon/hwmon2/temp*_input

3. Show battery status

If the system has a battery (e.g. laptop), then you can list the current status of the battery by running these commands.

cat /sys/class/power_supply/BAT0/power_now
cat /sys/class/power_supply/BAT0/status

# more information available at
cd /sys/class/power_supply/BAT0 ; ls -l

4. List all the processes starting at boot

service --status-all

5. List free RAM and swap memory

free  # lists in kilobytes
# Parse output with awk and convert to MB
free | awk '/Mem/{printf("%.2f MB used
"), $3/1024}'

6. List open ports and softwares using the network

ss -l # only listening
ss -t # TCP only
ss -r # resolve hostnames (when possible)
ss -artu # TCP or UPD connected/listening 
ss -tlpn

7. Display system power consumption

Install powerstat and then use it to calculate your system’s power usage.

8. Search a package that contains a command or binary tool

apt-file search xclock # works horribly

powerstat -R -c -z

Mounting

1. Mount an SFTP server

Mount a file system from a SFTP file share using sshfs.

sudo sshfs -o allow_other -p 1234 bindu@192.168.100.50:/ /mnt/sshftps

2. Mount a USB drive

# List all the USB devices
lsusb

# List info about all USB devices
sudo lsblk -o UUID,NAME,FSTYPE,SIZE,MOUNTPOINT,LABEL,MODEL

# Show block device attributes 
sudo blkid

# Mount a drive (make sure to have the folder in /mnt/)
sudo mount /dev/sda1 /mnt/extdisk/

Format a USB drive

# Unmount drive
sudo umount /dev/sda1

# Format to ext4
sudo mkfs.ext4 /dev/sda1 -L untitled

# Format to exfat
# Load drivers
sudo apt-get install exfat-fuse
# Format
sudo mkfs.exfat /dev/sda1 -n untitled

# Format to FAT32
# Load Drivers
sudo apt-get install dosfstools
# Format
sudo mkfs.vfat /dev/sda1 -n untitled

3. Mounting a drive and setting up as Samba network share

Test mounting a drive

sudo mount -o uid=dietpi,gid=dietpi /dev/sdb2 MoviesHDD

sudo nano /etc/samba/smb.conf

# Add following lines to config for share config
[movie]
   comment = MoviesHDD
   path = "/mnt/MoviesHDD"
   force user = dietpi
   writable = yes
   read only = no
   create mask = 0777
   directory mask = 0777
   guest ok = yes
   
   
[NAS]
   comment = NAS
   path = "/mnt/NAS"
   force user = dietpi
   writable = yes
   read only = no
   create mask = 0777
   directory mask = 0777
   guest ok = no 
   

Edit sudo nano /etc/fstab and add/modify line regarding your drive. Check your drive’s UUID with sudo blkid before and copy it example:

/dev/sdb2: LABEL="Nikita" UUID="5CFB-9ED6" TYPE="exfat" PARTUUID="18f923fd-a2ed-4cc1-ba0e-05efc535396c"
UUID=5CFB-9ED6 /mnt/MoviesHDD exfat auto,noatime,nofail,rw 0 0
# UUID=5ECA-9CD6 /mnt/NAS exfat auto,noatime,nofail,rw 0 0

Restart samba and login and check if it works.

sudo systemctl restart smbd
sudo smbstatus

# stop samba service
sudo service smbd stop

Check Hard disk parameters and sleep time

lsblk # Lists drives and mountpoints

sudo hdparm -C /dev/[sh]d[a-z] # Drive usage state

sudo hdparm -I /dev/sd[a-z] # show drive information

sudo hdparm -M /dev/sd[a-z] # check if drive offers acoustic management

sudo hdparm -t /dev/sd[a-z] # Test drive speeds
sudo hdparm -t --direct /dev/sd[b-c] # reading directly from disk

sudo hdparm -B /dev/sd[b-z] # Query APM status
sudo hdparm -B 200 /dev/sd[b-z]
sudo hdparm -B 254 /dev/sd[b-z]

sudo hdparm -y  /dev/sd[b-z] # Put drive in standby mode

sudo hdparm -Y /dev/sd[b-z] # Put drive in sleep (might require reboot to get in use).

Wireless config ipconfig

ipconfig is deprecated. use ip a instead. iwconfig lists as this:

wlan0     IEEE 802.11  ESSID:"Koti_93DC_2.4"
          Mode:Managed  Frequency:2.432 GHz  Access Point: 00:90:40:15:12:10
          Bit Rate=52 Mb/s   Tx-Power=15 dBm
          Retry short limit:7   RTS thr:off   Fragment thr:off
          Encryption key:off
          Power Management:off
          Link Quality=30/70  Signal level=-80 dBm
          Rx invalid nwid:0  Rx invalid crypt:0  Rx invalid frag:0
          Tx excessive retries:66  Invalid misc:55   Missed beacon:0

qbittorrent Enable/disable & Tips

# Systemd service listed here: /etc/systemd/system/qbittorrent.service
# change webui-port=3232

sudo systemctl status qbittorrent.service

# To disable running on boot
# Disable
sudo systemctl stop qbittorrent.service ; sudo systemctl disable qbittorrent.service
# Move to a safe place
sudo mv /etc/systemd/system/qbittorrent.service ../qbittorrent/.
# Reload systemd
sudo systemctl daemon-reload ; sudo systemctl reset-failed

Set up

1. Set up swap

Instructions From here.

Swap size guidelines.

# check swap status
sudo swapon --show

sudo fallocate -l 6G /swapfile
# sudo dd if=/dev/zero of=/swapfile bs=1024 count=6291456
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
sudo nano /etc/fstab
# Paste following line
/swapfile swap swap defaults 0 0

#check that swap is in use
sudo swapon --show ; free -h

# check system swappiness
cat /proc/sys/vm/swappiness

# Set to 60 [range 0-100]
sudo sysctl vm.swappiness=10

sudo nano /etc/sysctl.conf
# Paste following line
vm.swappiness=10


# To remove
sudo swapoff -v /swapfile
# comment off the lines added (above) in 
sudo nano /etc/fstab
sudo nano /etc/sysctl.conf

sudo rm /swapfile

2. Setup / disable system sleep

# enable sleep
sudo systemctl mask sleep.target suspend.target hibernate.target hybrid-sleep.target

# put to sleep
sudo systemctl suspend

# disable sleep
sudo systemctl unmask sleep.target suspend.target hibernate.target hybrid-sleep.target

sudo nano /etc/systemd/logind.conf
# Paste following lines to 
[Login]
HandleLidSwitch=ignore
HandleLidSwitchDocked=ignore

systemctl restart systemd-logind.service

3. Set laptop screen brightness to zero on boot

Keep the following script in /var/lib/dietpi/postboot.d

echo "0" > /sys/class/backlight/intel_backlight/brightness

4. Setup nordvpn

Manage nordvpn from command line. More info.

# disable and turn off autostart
sudo systemctl stop nordvpnd ; sudo systemctl disable nordvpnd

sudo systemctl start nordvpnd ; sudo systemctl start nordvpnd

nordvpn login
nordvpn whitelist add port 22
nordvpn whitelist add subnet 192.168.100.0/24
nordvpn set cybersec off # Turning ON for some reason breaks ssh
nordvpn set technology NordLynx

nordvpn settings
nordvpn status

nordvpn connect
nordvpn disconnect

Misc

1. Preserve $HOME environment variable on sudo

Solution from stackoverflow.

sudo visudo

# then add this line to the file
Defaults       env_keep += "HOME"

3. Generate bash prompts

http://bashrcgenerator.com/ - Is an interactive webtool to generate prompts for bash

Previous