Ubuntu

6 minute read

Published:

All instructions written for Ubuntu 16.04 and 18.04

Download Ubuntu iso

Install Ubuntu tutorial

Ubuntu images

Activate superuser permissions

Activating superuser permissions in Ubuntu will require root privileges on the account being accessed from, or access to the root password

Open a terminal with Ctrl+Alt+T. Enter sudo -i or sudo -s and enter your password (or the root password if missing root privileges)

Mount a hard drive

This requires root permissions

Create a folder to mount the drive to sudo mkdir /mnt/hdd1

Open fstab with nano/vim with sudo active sudo nano /etc/fstab

Add the following to the end of the file: /dev/drive-partition-name /mnt/hdd1 ext4 defaults 0 0 Save and close the file.

Mount the drive sudo mount /dev/drive-partition-name /mnt/hdd1

Backup between local hard drives using cron and rsync

rsync to backup files between local hard drives

Tutorial for Rsync

Execute rsync in terminal with archive mode active (recommended) rsync -a from_dir to_dir

Useful rsync options

-r, --recursive		recurse into directories
-v, --verbose		increase verbosity
-q, --quiet		suppress non-error messages
-a, --archive		archive mode
-u, --update		skip files that are newer on the receiver
-d, --dirs		transfer directories without recursing
-n, --dry-run		perform a trial run with no changes mode

rsync to backup files between remote hard drives

Syncing a local file or directory to a remote destination

rsync -a /path/to/local/dir username@remote_host:/path/to/remote/destination

Syncing a remote file or directory to a local destination

rsync -a username@remote_host:/path/to/remote/dir /path/to/local/destination

cron scheduling

Tutorial on using cron in Linux

The cron utility can be used to schedule command execution. This is particularly useful for scheduling backups with rsync, which is the principal use here

cron runs in /etc/cron.d

Individual cron runs can be made within /etc/cron.d/. These files can execute SHELL commands

cron runs in crontab

crontab is a user-specific table of cron runs to be executed. Access crontab with crontab -e command in terminal. A prompt will appear asking for an editor. Select whichever number corresponds to /bin/nano and the crontab will appear. It will have instructions for adding a scheduled cron run to the end of the file. This is the most practical way to make a cron run

A useful outline of a job definition by David Both is

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name  command to be executed

If no user-name is provided, then the cron run is executed as the user who the crontab file (/var/spool/cron/crontabs) belongs to

There are many other utilities which can make cron more useful, for example emailing the user when the command is executed with MAILTO

View all users crontabs on a computer

In a terminal, access all users crontabs in /var/spool/cron/crontabs. Each users crontab will be a single file accessible with nano, vim, etc.

Ex. If my username were dc, my crontabs could be read with nano /var/spool/cron/crontabs/dc

Select a block of text in nano

While the cursor is at the line of the section to be selected, press Alt+A, then move the cursor with the arrow keys to select the desired block of text

Indent or unindent multiple lines of text in nano

Indent

Alt+Shift+] (also written Alt+})

Unindent

Alt+Shift+[ (also written Alt+{)

See directory and file sizes in terminal with du

du manual

General usage

du /path/to/dir will report estimates for the size of all files and folders (including specific files/subfolders therein) in /path/to/dir

From the manual:

Display  values  are  in  units  of  the  first  available SIZE from --block-size, and the
DU_BLOCK_SIZE, BLOCK_SIZE and BLOCKSIZE environment variables.  Otherwise,  units  default
to 1024 bytes (or 512 if POSIXLY_CORRECT is set).

Human-readable

du -h /path/to/dir will print the estimated sizes in human readble formats (e.g., K, M, G for kilobyte, megabyte, and gigabyte)

Total size

du -s /path/to/dir will print the summarized size of /path/to/dir

Max depth

du /path/to/dir -d N where N is an integer, will report the total size for files and directories N or fewer levels from /path/to/dir

Examples

Report the summarized size of files and folders in the current directory in human-readable format:

du -h . -d 1

This is particularly useful when ls can only show a folders dir name data block (4096 bytes), not the actual folder size

Local installation of python

This method does not require sudo privileges, just an internet connection. Following along with Installation of Python in your home directory

Select desired version of python from the Official Python Website. I am currently installing Python v.3.8.9 (to run some analysis with the statsmodels package), so I will select Python-3.8.9 and identify the installation file I need, specifically Python-3.8.9.tgz

In terminal, orient to the Downloads directory and download the compressed folder there with wget http://www.python.org/ftp/python/3.8.9/Python-3.8.9.tgz

Uncompress the folder wtih tar -zxvf Python-3.8.9.tgz

Orient to the new directory cd Python-3.8.9

Create a destination folder for the installation. I wanted to install in my home directory, so I did mkdir ~/.localpython

Configure the environment with ./configure --prefix=/mnt/hdd1/Dan/.localpython, then build with make and install with make install

To manually use this version of python, I would need to execute ~/.localpython/bin/python3 or ~/.localpython/bin/python3.8

An easier way is to add this to your PATH variable. Do so by opening ~/.bashrc and appending export PATH=$PATH:~/.localpython/bin then running source ~/.bashrc. Now you can start python3.8 just by typing python3 (may fail if you have several python3 versions) or python3.8

Create an alias to set your python version exactly to python3.8.9 and be able to be called with just python. There are many ways to do this, depending on needs. If you want to have this alias be permanent, and still don’t have sudo privileges, open ~/.bash_aliases and write alias python="python3.8", then run source ~/.bashrc.