Useful Commands

Remote Reboot

net rpc shutdown -r -t 1000 -c "system is going to shutdown " -I <ip address> -U administrator

Remote Services

net rpc service list -I IPADDRESS -U USERNAME

SSL Certificate Checking

echo | openssl s_client -connect 2> /dev/null | openssl x509 -noout -enddate

Raid Checking

  • get the raid disk
    cat /proc/mdstat
  • check the raid status
    mdadm --detail /dev/md0
  • full disk check
    mdadm --detail /dev/md0

Directory Usage

du ./* --max-depth=0 -m | sort -nr | head -n 10

Apache Access Log - IP Address Usage

cat access.* | awk '{ print $1 }' | sort | uniq -c | sort -n

SSH Tunnel Control

user@host:~$ ssh -M -S my-ctrl-socket -fnNT -L 50000:localhost:3306
user@host:~$ ssh -S my-ctrl-socket -O check
Master running (pid=3517)
user@host:~$ ssh -S my-ctrl-socket -O exit
Exit request sent.

Reset Home Directory Permissions

  find /home/user -type d -print0 | xargs -0 chmod 0775
  find /home/user -type f -print0 | xargs -0 chmod 0664

Memory Usage

Remove Password From PDF

  • QPDF
    qpdf --password=somepassword--decrypt locked.pdf unlocked.pdf

Convert Video Files

  • MKV → MPG
    ffmpeg -i "file.mkv" -c:a copy -c:v copy "file.mp4"

Display Log Files By Revision

  • Linux -
    cat $(ls -rv /var/log/auth.log*) | grep do something or other
  • Windows -
    for /f %i in ( 'dir buncha.logs* /o:d /b' ) do cat %i | grep -i "lookingfor" >> found.txt

Network Fun

  • Scan a network for mac addresses -
    nmap -sn <subnet>.0/24
  • Get a mac addresses from an IP address -
    arp -a <ipaddress>

Testing crontab command


Add the following line to the crontab, e.g. using crontab -e

* * * * *  /usr/bin/env > /home/username/cron-env

Create a shell script which executes a command in the same environment as cron jobs run:


. "$1"
exec /usr/bin/env -i "$SHELL" -c ". $1; $2"


run-as-cron <cron-environment> <command>


run-as-cron /home/username/cron-env 'echo $PATH'

Note that the second argument needs to be quoted if it requires an argument. The first line of the script loads a POSIX shell as interpreter. The second line sources the cron environment file. This is required to load the correct shell, which is stored in the environment variable SHELL. Then it loads an empty environment (to prevent leaking of environment variables into the new shell), launches the same shell which is used for cronjobs and loads the cron environment variables. Finally the command is executed.

Catching ctrl-c

From here – You can use the trap builtin to handle a user pressing ctrl-c during the execution of a Bash script. e.g. if you need to perform some cleanup functions.


# trap ctrl-c and call ctrl_c()
trap ctrl_c INT

function ctrl_c() {
        echo "** Trapped CTRL-C"

for i in `seq 1 5`; do
    sleep 1
    echo -n "."

LVM Information

LVM is a flexible abstraction layer between physical disk up to the filesystem (disk ⇒ partition ⇒ LVM's physical volume [PV] ⇒ LVM's volume group [VG] ⇒ LVM logical volume [LV] ⇒ filesystem).

Due to some LVM features (aggregation, mirror, stripes, snapshot…), the physical layout can become complex… thus some caveats:

Use lvs manpage about –all to view internal details (of mirrored volumes), if wanted. Obviously, when you run a command for a given objet, only that object's parents and children are show (which may be incomplete, if LV is spanning on multiple PV for example) For a quick overview, I recommend lsblk (which is a standard tool, not LVM specific).

$ lsblk
sda                        8:0    0 223,6G  0 disk 
├─sda1                     8:1    0   350M  0 part 
├─sda2                     8:2    0    29G  0 part 
├─sda3                     8:3    0   488M  0 part /boot
├─sda4                     8:4    0     1K  0 part 
└─sda5                     8:5    0 193,8G  0 part 
  ├─vg_ssd-lv_root_solid 254:0    0  13,3G  0 lvm  /
  ├─vg_ssd-lv_srv_solid  254:2    0  46,6G  0 lvm  /srv
  └─vg_ssd-lv_home_solid 254:3    0   107G  0 lvm  /home
sdb                        8:16   0  74,5G  0 disk 
└─sdb1                     8:17   0  74,5G  0 part 
  ├─vg_ssd-lv_swap_solid 254:1    0   3,7G  0 lvm  [SWAP]
  └─vg_ssd-lv_videos     254:4    0    28G  0 lvm  /mnt/videos

LVM specific tools To get LVM's internal view and details, use LVM commands:

Lists : use the lvs and pvs commands with option –segments Detailed view: use the lvdisplay and pvdisplay commands with option -m LVM tools exmples List the physical segments used by a logical volume :

$ lvs --segments /dev/vg_ssd/lv_videos
LV        VG     Attr       #Str Type   SSize 
lv_videos vg_ssd -wi-ao----    1 linear 20,95g
lv_videos vg_ssd -wi-ao----    1 linear  7,05g

same with more details:

$ lvs --segments /dev/vg_ssd/lv_videos -o +lv_size,devices
  LV        VG     Attr       #Str Type   SSize  LSize  Devices         
  lv_videos vg_ssd -wi-ao----    1 linear 20,95g 28,00g /dev/sdb1(12729)
  lv_videos vg_ssd -wi-ao----    1 linear  7,05g 28,00g /dev/sdb1(3534) 

List the physical extents of a given LV. Useful to move those segments (using pvmove):

$ lvs  /dev/vg_ssd/lv_videos -o seg_pe_ranges
  PE Ranges

The opposite way, list the logical volume (segments) inside a given Physical Volume:

$ pvs  /dev/sdb1  --segments  -o +lv_name,lv_size
  PV         VG     Fmt  Attr PSize  PFree  Start SSize LV            LSize 
  /dev/sdb1  vg_ssd lvm2 a--  74,53g 22,80g     0  3534                   0 
  /dev/sdb1  vg_ssd lvm2 a--  74,53g 22,80g  3534  1806 lv_videos     28,00g
  /dev/sdb1  vg_ssd lvm2 a--  74,53g 22,80g  5340  1316                   0 
  /dev/sdb1  vg_ssd lvm2 a--  74,53g 22,80g 11776   953 lv_swap_solid  3,72g
  /dev/sdb1  vg_ssd lvm2 a--  74,53g 22,80g 12729  5362 lv_videos     28,00g
  /dev/sdb1  vg_ssd lvm2 a--  74,53g 22,80g 18091   988                   0 

List the physical segments of a given logical volume, among other information:

$ lvdisplay -m /dev/vg_ssd/lv_videos
  --- Segments ---
  Logical extents 0 to 5361:
    Type        linear
    Physical volume /dev/sdb1
    Physical extents    12729 to 18090

  Logical extents 5362 to 7167:
    Type        linear
    Physical volume /dev/sdb1
    Physical extents    3534 to 5339    

Display the Logical volume associated with a given physical volume , among other information:

$ pvdisplay -m /dev/sdb1
  --- Physical Segments ---
  Physical extent 0 to 3533:
  Physical extent 0 to 5339:
    Logical volume  /dev/vg_ssd/lv_videos
    Logical extents 5362 to 7167
  Physical extent 5340 to 11775:
  Physical extent 11776 to 12728:
    Logical volume  /dev/vg_ssd/lv_swap_solid
    Logical extents 0 to 952
  Physical extent 12729 to 18090:
    Logical volume  /dev/vg_ssd/lv_videos
    Logical extents 0 to 5361
  Physical extent 18091 to 19078:

Complex command, but full list :

$ pvs   --segments  -o pv_name,pv_size,seg_size,vg_name,lv_name,lv_size,seg_pe_ranges
  PV         PSize   SSize   VG     LV            LSize   PE Ranges            
  /dev/sda5  193,79g   9,31g vg_ssd lv_root_solid  13,31g /dev/sda5:0-2383     
  /dev/sda5  193,79g   3,72g vg_ssd lv_home_solid 107,00g /dev/sda5:2384-3336  
  /dev/sda5  193,79g  46,56g vg_ssd lv_srv_solid   46,56g /dev/sda5:3337-15256 
  /dev/sda5  193,79g  60,00g vg_ssd lv_home_solid 107,00g /dev/sda5:15257-30616
  /dev/sda5  193,79g 200,00m vg_ssd lv_home_solid 107,00g /dev/sda5:30617-30666
  /dev/sda5  193,79g   1,05g vg_ssd                    0                       
  /dev/sda5  193,79g   8,00g vg_ssd lv_home_solid 107,00g /dev/sda5:30937-32984
  /dev/sda5  193,79g   4,00g vg_ssd lv_root_solid  13,31g /dev/sda5:32985-34008
  /dev/sda5  193,79g  20,00g vg_ssd lv_home_solid 107,00g /dev/sda5:34009-39128
  /dev/sda5  193,79g   9,80g vg_ssd lv_home_solid 107,00g /dev/sda5:41689-44198
  /dev/sda5  193,79g   1,28g vg_ssd lv_home_solid 107,00g /dev/sda5:44199-44525
  /dev/sda5  193,79g  15,86g vg_ssd                    0                       
  /dev/sda5  193,79g   4,00g vg_ssd lv_home_solid 107,00g /dev/sda5:48587-49610
  /dev/sdb1   74,53g  13,80g vg_ssd                    0                       
  /dev/sdb1   74,53g   7,05g vg_ssd lv_videos      28,00g /dev/sdb1:3534-5339  
  /dev/sdb1   74,53g   5,14g vg_ssd                    0                       
  /dev/sdb1   74,53g   3,72g vg_ssd lv_swap_solid   3,72g /dev/sdb1:11776-12728
  /dev/sdb1   74,53g  20,95g vg_ssd lv_videos      28,00g /dev/sdb1:12729-18090
  /dev/sdb1   74,53g   3,86g vg_ssd                     0                       
 commands.txt · Last modified: 2020/01/04 07:33 by bpienig
Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Share Alike 4.0 International
Recent changes RSS feed Driven by DokuWiki