These are mainly for my benefit, collected from a variety of sources. Will be updated as required.

System

Swappiness

Let’s only use our hard drive instead of RAM if we need to

$ cat /proc/sys/vm/swappiness
$ 60
# sysctl vm.swappiness=10

Mail

Permit domains you own to send emails using the Sender Policy Framework usually to allow a server to send mails on behalf of the domain. Add a TXT or (less commonly in my experience) an SPF record to your DNS settings substituting your domains of course. In the example below, emailprovider.tld is whoever hosts my email account and domain1.tld and domain2.tld are servers that host services connected to that domain.

 v=spf1 include:emailprovider.tld include:domain1.tld include:domain2.tld ~all

Bash

Run multiple commands on command line and pipe output to mail

(du -hc -d1 /var/www/ ; echo ; df -h) | mail -s '[INFO] Disk Usage' someone@somewhere

add to your ~/.bashrc

alias up='sudo apt-get update && sudo apt-get upgrade'                          
alias ll='ls -lha' 

The next one let’s your run $ mem sshd or $ mem python3 to see memory usage for all processes.

mem()
{
    ps -eo rss,pid,euser,args:100 --sort %mem | grep -v grep | grep -i $@ | awk '{printf $1/1024 "MB"; $1=""; print }'
}

Programs

Backup to server using rsync

simon@computer:~/books/pdf$ rsync -avP . simon@server:/home/simon/backup/books/pdf

Compile a program without root / sudo privileges:

$ ./configure --prefix=$HOME/bin
$ make
$ make install

Files

Get disk usage for top level directories. I use this to check how much disk space is being used per site in my /var/www

simon@server:/var/www [ssh] $ du -hc -d1
24M	./site1
15M	./site2
39M	.
39M	total

Copy file permissions

simon@computer:~/web/workdir/content/img$ chmod --reference=eczema-arm.
jpg compost-2021-05-13-*

Count files in a directory

ls | wc -l

Reduce the file size of a pdf (experiment with /printer /ebook, /screen highest to lowest quality). Requires ghostscript.

$ ps2pdf -dPDFSETTINGS=/printer "Some Massive Document.pdf" "Smaller Document.pdf"

Rotate pages in a pdf file. pdftk or pdftk-java on Debian is the swiss army knife for pdf files. In the example below, the second page is upside down. This will sort it out.

$ pdftk test.pdf cat 1 2-south output out.pdf

Split a pdf into individual pages with a common filename prefix

$ pdftk "Supplier SHEQ-PACK-2021.pdf" burst output Supplier-2021-%d.pdf

SSH

Put this in your ~/.bashrc on your server

# show when we are using SSH                                                    
if [ -n "$SSH_CLIENT" ]; then text=" [ssh] "                                
fi

Web

Apache2 (httpd)

simon@server:~ [ssh] $ sudo apachectl
Usage: /usr/sbin/apachectl start|stop|restart|graceful|graceful-stop|configtest|status|fullstatus|help
       /usr/sbin/apachectl <apache2 args>
       /usr/sbin/apachectl -h            (for help on <apache2 args>)

Enable a module: sudo a2enmod rewrite

Check conf files /etc/apache2/sites-available/website.conf

simon@server:~ [ssh] $ sudo apachectl configtest
Syntax OK

Get apache configuration details:

simon@server:~ [ssh] $ sudo apachectl -V
Server version: Apache/2.4.46 (Debian)
Server built:   2021-01-20T07:40:46
Server's Module Magic Number: 20120211:93
Server loaded:  APR 1.6.5, APR-UTIL 1.6.1
Compiled using: APR 1.6.5, APR-UTIL 1.6.1
Architecture:   64-bit
Server MPM:     event
  threaded:     yes (fixed thread count)
    forked:     yes (variable process count)
Server compiled with....
 -D APR_HAS_SENDFILE
 -D APR_HAS_MMAP
 -D APR_HAVE_IPV6 (IPv4-mapped addresses enabled)
 -D APR_USE_SYSVSEM_SERIALIZE
 -D APR_USE_PTHREAD_SERIALIZE
 -D SINGLE_LISTEN_UNSERIALIZED_ACCEPT
 -D APR_HAS_OTHER_CHILD
 -D AP_HAVE_RELIABLE_PIPED_LOGS
 -D DYNAMIC_MODULE_LIMIT=256
 -D HTTPD_ROOT="/etc/apache2"
 -D SUEXEC_BIN="/usr/lib/apache2/suexec"
 -D DEFAULT_PIDLOG="/var/run/apache2.pid"
 -D DEFAULT_SCOREBOARD="logs/apache_runtime_status"
 -D DEFAULT_ERRORLOG="logs/error_log"
 -D AP_TYPES_CONFIG_FILE="mime.types"
 -D SERVER_CONFIG_FILE="apache2.conf"

which modules are loaded?

simon@server:~ [ssh] $ sudo apachectl -M
Loaded Modules:
 core_module (static)
 so_module (static)
 watchdog_module (static)
 http_module (static)
 log_config_module (static)
 logio_module (static)
 version_module (static)
 unixd_module (static)
 access_compat_module (shared)
 alias_module (shared)
 auth_basic_module (shared)
 authn_core_module (shared)
 authn_file_module (shared)
 authz_core_module (shared)
 authz_host_module (shared)
 authz_user_module (shared)
 autoindex_module (shared)
 brotli_module (shared)
 deflate_module (shared)
 dir_module (shared)
 env_module (shared)
 expires_module (shared)
 filter_module (shared)
 headers_module (shared)
 mime_module (shared)
 mpm_event_module (shared)
 negotiation_module (shared)
 pagespeed_module (shared)
 reqtimeout_module (shared)
 rewrite_module (shared)
 setenvif_module (shared)
 socache_shmcb_module (shared)
 ssl_module (shared)
 status_module (shared)

Enable a site: sudo a2ensite somewebsite.conf

Read the docs:

man apachectl and man a2ensite

Multimedia

ffmpeg

Some videos are way bigger than they should be. You’ll need to run mkdir conv in the target directory as this is where the sanitised vids will be saved.

for name in *.mp4;
do ffmpeg -i "$name" -vcodec libx264 -crf 28 -preset faster -tune film "conv/${name%.*}.mkv" ;
done

As above but also resize from 1080p to 720p

for name in *.mp4; 
do ffmpeg -i "$name" -vf scale=-1:720 -vcodec libx264 -crf 28 -preset faster -tune film "conv/${name%.*}.mkv";
done