Ubuntu Command Reference

When installing or configuring an Ubuntu server, there are common commands that one typically needs to use. This page contains a no-nonsense list of the most used commands.

General:

Set the root user's password and enable root login

sudo passwd

Check for updates

apt update

Install updates

apt upgrade

Reboot the server

reboot

SSH server:

Install SSH server

apt install openssh-server

Enable root login with SSH

Edit /etc/ssh/sshd_config and add or uncomment the line:

PermitRootLogin yes

Restart the SSH service

service ssh restart

Create SSH key for passwordless login

Run these commands on your client machine:

ssh-keygen

Save the key to the default location of /your_home/.ssh/id_rsa

ssh-copy-id username@remote_host

Key is saved on the remote host in /your_home/.ssh/authorized_keys

ssh username@remote_host

UFW firewall:

Enable/disable UFW firewall

ufw enable
ufw disable

Once enabled UFW denies all inbound traffic by default, so make sure to create an allow SSH rule for yourself first if configuring over SSH.

Add an allow rule

ufw allow from any to any port 22 [proto tcp]

You can replace "any" with an IP address or IP subnet with CIDR notation.

List rules

ufw status numbered

Insert a new rule at line number

ufw insert 2 allow from any to any port 80 [proto tcp]

Delete a rule by line number

ufw delete 2

Filesystem:

Fix NTFS Drive that mounts as read-only

Umount the drive and run the ntfsfix command, then remount the drive

umount /dev/sdxX
ntfsfix /dev/sdxX

Search all text files in a directory for a string (recursive)

grep -Rnw '/path/to/somewhere/' -e 'pattern'

OpenSSL:

Convert a .cer file to PKCS12 format

openssl pkcs12 -export -out OUTPUTNAME.pfx -inkey KEY.key -in FILENAME.cer [-certfile CACert.crt]

MySQL Commands (run as mysql root user):

Create a new user with password:

CREATE user 'itpal'@'host' IDENTIFIED by 'mypassword';

Grant privileges to user:

GRANT ALL PRIVILEGES on dbname.* to 'itpal'@'host';

Grant superuser privileges to user (same as root):

GRANT ALL PRIVILEGES on *.* to 'itpal'@'host' with grant option;

Delete a user:

DROP USER itpal@host;