Ubuntu General Quick Reference
January 14, 2019

Ubuntu General Quick Reference

Just a place for me to jot down some useful commands when working with the Ubuntu command line.

General Commands

Commands I use often:

Command Explanation
pwd Print the working directory
ls -lha List the contents of the working directory, +all files +human readable filesizes +long format
cd - Go back to the previous directory
df -h Show used / available space by partition
top or htop Show current running processes
ps -A | less & kill ... List all processes, kill a process
rm -rf Remove a directory +recursive +force
sudo -u www-data COMMAND Run a COMMAND as another user
addyser USERNAME Add a user
usermod -aG sudo USERNAME or adduser USERNAME sudo Add user USERNAME to the sudo group
sudo passwd USERNAME (Re)set the password for a user
sudo chown -R www-data:www-data * Recursively change the ownership or all files and folders in/under the current directory (to user/group www-data)
sudo chmod g+w -R /var/www/xyz.com/ Add write privilege for the group recursively for the given directory
hostnamectl set-hostname new-hostname Set the host name
vi /etc/hosts Edit the hosts file
tail -f ... Watch the "tail" of a file
su -www-data Switch to user www-data

Read UsingTheTerminal for a good overview of...using the terminal!

Services

Command Explanation
service --status-all List all services

Performance and Resource Monitoring

Command Explanation
free -m Overview of aggregate memory usage
top Listof processes ordered by CPU and memory usage
free -m
top

Or install htop:

sudo apt-get install htop and then sudo htop

Midnight Commander

Keys Explanation
INS Select files
* Reverse selection (or select all files, if nothing was selected)
CTRL+o Access the quick shell
ALT+, Switch panel layout between horizontal and vertical
ALT+i Show current directory in the other panel as well
CTRL+u Swap panels
ALT+SHIFT+h View your directory history
ALT+? Find a file
CTRL+SPACE Calculate file size (use it on .. to calculate all)
CTRL+x...d Compare directories

Certbot

CertBot for deploying https: https://certbot.eff.org/lets-encrypt/ubuntuxenial-apache


CRON

You can place scripts to run every day/week/month into /etc/cron.daily/weekly/monthly, but make sure that the files are executable and that they don't contain any special characters (or even an file ending).

Make a file executable: sudo chmod +x /etc/cron.hourly/backup.sh

Better yet, make sure they have the correct permissions: chmod 600 /etc/cron.d/*

You can find out about when these scripts will be run with cat /etc/crontab

# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.

SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

# m h dom mon dow user  command
17 *    * * *   root    cd / && run-parts --report /etc/cron.hourly
25 6    * * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
47 6    * * 7   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
52 6    1 * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )

You can see if your script runs by running the appropriate part from the above output, for example to run your weekly cron jobs right now: test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )

Force a cron job to run by "touching" it's definition: touch /etc/cron.d/mycronscript

(root) INSECURE MODE (mode 0600 expected) (crontabs/root)

Fix this with:

 sudo chmod 600 /var/spool/cron/crontabs/root
 sudo touch /var/spool/cron/crontabs

Debug a Cron Job

Two methods come in handy. The first one from a post by rubiojr on askubuntu:

Do not redirect error output to /dev/null and grep /var/log/syslog for cron output.
grep cron /var/log/syslog
You can immediately show errors when saving a file after editing /etc/crontab or files inside /etc/cron.d/ with:
tail -f /var/log/syslog | grep --line-buffered cron
If the edit is ok, you will only see the RELOAD notice, errors will occur like
Jul 9 09:39:01 vm cron[1129]: Error: bad day-of-month; while reading /etc/cron.d/new

You can also see your active cron jobs with crontab -l. But this doesn't show you jobs set in cron.d or cron.daily etc.

The second method is to make your cron job log to a file! See below.

Make a Cron Job Log to a File (with timestamp prepended!)

The following worked for me to redirect output of a cron job to a log file, and to prepend a timestamp at the beginning of the first line. It's also really a great way to debug!

SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

* * * * * www-data { printf "\%s: " "$(date "+\%Y-\%m-\%d \%H:\%M:\%S")"; php /var/www/domain.com/public_html/artisan schedule:run ; } >>/var/www/comain.com/cron_log.log 2>&1

Or if you don't need to prepend a timestamp, just output it to a temprorary log file by appending this to your cron line: >>/tmp/cron_log.log 2>&1

You can also simulate a cron job run with run-parts /etc/cron.daily (for example). However, this doesn't fully simulate the cron environment, so you should also set it to run once a minute and wait for the output to be written to your logfile.

The example task above if for running the OctoberCMS scheduler once every minute.


Free Up Disk Space

Methods for finding large files/folders:

(1) apt-get install ncdu or gt5 & run from the root directory!

(2) Or resort to the command line:

du -a | sort -nr | head
sudo du -a | sort -nr -T /media/usb-key
-- Or if disk is full and the above doesn't work because the temp folder is full:
sudo du -ah --max-depth=1  / | sort -hr
-- You can also find files larger than X mb:
find / -size +10M -ls

Often there are lots of files at: /var/cache/apt/archives
Old apt files are archived here. You can delete them with:

sudo apt-get clean
sudo apt-get autoclean

Antivirus

Supposedly you don't need one, but ClamAV is free to use and simple enough.

Installation and use:

sudo apt install clamav clamav-daemon
sudo freshclam

Scan the whole system, report on any infected files (but don't remove):
sudo clamscan --max-filesize=3999M --max-scansize=3999M --exclude-dir=/sys/* -i -r /

Scan the home directory and remove and move infected files:
clamscan --infected --remove --move=/home/<user>/viruses --recursive /home

There is also a gui client: sudo apt install clamtk

Other options:


Upgrade your Ubuntu Version

sudo apt-get update
sudo apt-get upgrade
sudo apt-get dist-upgrade
do-release-upgrade

Security Audit

Identify services running locally

sudo netstat -plunt

Scan for open ports on another host

  1. Install nmap: sudo apt-get install nmap
  2. Scan for every TCP and UDP open port:
    sudo nmap -n -PN -sT -sU -p- remote_host
  3. See what service is running on a port:
    sudo nmap -PN -p port_number -sV remote_host

Sources:

Ubuntu General Quick Reference
Share this