Link Search Menu Expand Document

Linux

Table of contents

  1. Setup
    1. Install Firewall
    2. Install FTP server
    3. Cron - Job scheduler
  2. Tips & Tricks
    1. Command-line kung-fu
      1. Screen Terminal Multiplexer
    2. CPU & Processes
    3. Disk & Data
      1. View SMART disk data
      2. Test read/write speed
      3. Format & mount a USB stick
    4. Power & Battery
  3. Recover
    1. Check logs
    2. Bootloader
    3. Services with systemctl

Setup

  • On a laptop: disable standby on lid close: in /etc/systemd/logind.conf, uncomment and change HandleLidSwitch=ignore

Install Firewall

  • Install sudo apt install ufw
  • Configuration
    • Check status & list configuration sudo ufw status
    • Allow a port sudo ufw allow PORT
    • Remove an allowance sudo ufw remove PORT
    • Rate limit a port (>6 connections within 30 sec) sudo ufw limit PORT
    • Enable sudo ufw enable
  • Verify
    • Check added rules sudo ufw show added
    • Check existing rules with numbers sudo ufw status numbered
  • Enable sudo ufw enable

Install FTP server

  1. sudo apt install vsftpd
  2. sudo nano /etc/vsftpd.conf to edit configuration
  3. sudo ufw allow 21
  4. sudo systemctl restart vsftpd

Cron - Job scheduler

  • Edit cron of an user sudo crontab -u USER -e
  • Check if the cron expression is correct: crontab guru

Tips & Tricks

  • Date & Time
    • timedatectl to check
    • sudo timedatectl set-timezone TZ

Command-line kung-fu

  • less to redirect output to a pager
  • watch to watch regularly an output
  • | grep x to pipe to grep which is going to select lines with x
  • id $user to get user PUID & GUID
  • tar -xvzf file.tar.gz to extract tarball and zip

Screen Terminal Multiplexer

Complete guide here

  • Start screen
  • List sessions ID screen -ls
  • In a session:
    • Detach CTRL+A d
    • Rename CTRL+A :sessionname <Your_session_name>
  • Reattach screen -r session_ID

CPU & Processes

  • Check system stats
    • htop for processes
    • nmon for the whole system
  • Health
    • Check CPU frequency cat /proc/cpuinfo

Disk & Data

  • Check free disk space df -h
  • Tree file & folder with space taken ncdu
  • Check for partition corruption fsck /dev/sdaXX00
  • Stop a disk
    1. Unmount sudo umount /dev/sdXX
    2. Spin down sudo hdparm -Y /dev/sdXX
  • Erase a disk sudo dd if=/dev/urandom of=/dev/sdXX bs=10M - Source

View SMART disk data

Source

  1. Install smartmontools
  2. View SMART data & status sudo smartctl -i /dev/sdXX
  3. Find estimate & current SMART test sudo smartctl -c /dev/sdXX
  4. Launch a test: sudo smartctl -t short /dev/sdXX
    • Possible options instead of short:
    • long
    • conveyance (after transport) for ATA disks
    • If the HDD go to sleep (especially true for USB ones), try the following:

        while true; do
            dd if=/dev/sdXX iflag=direct count=1 of=/dev/null
            sleep 60
        done
      
  5. Read the result sudo smartctl -l selftest /dev/sdXX

Test read/write speed

Source

  1. sync; dd if=/dev/zero of=tempfile bs=1M count=256; sync: write to tempfile 256 Mb
  2. sudo /sbin/sysctl -w vm.drop_caches=3: clear the cache
  3. dd if=tempfile of=/dev/null bs=1M count=256: read tempfile

Format & mount a USB stick

  1. Several options to see devices
    • sudo blkid to locate block devices
    • sudo lsblk to list mounted block devices
    • ls /dev to check where the usb stick is mounted
  2. sudo fdisk /dev/sdXX
    1. p to list existing partition
    2. g to create a new GPT partition table or o for a DOS partition table
    3. n to create a new partition
    4. w write to disk and exit
  3. sudo mkfs.ext4 /dev/sdXX00 to create a ext4 partition
  4. sudo mkdir /media/usbdrive to create a directory that will host the partition
    • Choose /mnt for temporary mounts
    • Choose /media for automatics mounts
  5. sudo mount /dev/sdXX00 /media/usbdrive -o umask=000 to mount the usb key with all user access
  6. sudo umount /dev/sdXX00 to unmount
  7. If you want to make it permanent

Power & Battery

  • Check system temperature
    1. sudo apt install lm-sensors
    2. sudo apt-get install lm-sensors
    3. sudo /etc/init.d/kmod start
    4. sensors
  • Check status & stats about battery
    1. List paths upower -e
    2. Get status upower -i <path>

Recover

Check logs

  • System messages: cat /var/log/messages | tail -n 50

Bootloader

Try Super Grub2 Disk

Services with systemctl

  • sudo systemctl status SERVICE
  • Service management
    • sudo systemctl restart SERVICE
    • sudo systemctl start SERVICE
    • sudo systemctl stop SERVICE
    • sudo systemctl kill SERVICE
  • Service enable/disable
    • sudo systemctl enable SERVICE
    • sudo systemctl disable SERVICE
    • sudo systemctl is-enabled SERVICE