Ubuntu Basics

New apt vs apt-get

apt-get update downloads the package lists from the repositories and "updates" them to get information on the newest versions of packages and their dependencies.

apt-get upgrade will fetch new versions of packages existing on the machine if APT knows about these new versions by way of apt-get update.

25 Useful commands of "apt-get and apt-cache"

Find out time zones
#timedatectl list-timezones

Set your timezone
#sudo timedatectl set-timezone America/Toronto

Enable timesyncd
#sudo timedatectl set-ntp on

Add user
adduser webadmin
chmod -aG sudo webadmin (change secondary group to sudo -a: append)
Show user groups
groups webadmin

deluser, delgroup (userdel, groupdel, low-level utilities)

Find users and groups
cat /etc/passwd
cat /etc/group

usermod (Modify user account)
usermod -g (change primary group)
usermod -G (change secondary group)
usermod ©d /srv/files/ftp ftp (change home directory)

Uninstall software/package

$sudo apt-get remove phpmyadmin
$sudo apt-get purge phpmyadmin
$sudo apt-get autoremove

The "apt-get remove phpmyadmin" command will uninstall the phpmyadmin package, the purge command will remove configuration files related to Ubuntu phpMyAdmin and the autoremove command will uninstall all the dependency packages that are no longer required.

Get to bash automatically and display correct prompt
less /etc/passwd

In .bashrc
PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '


Check ports open
netstat -ntlp | grep LISTEN

change Time Zone
sudo dpkg-reconfigure tzdata

Change IP

find out interface: ifconfig -a

sudo vi /etc/network/interfaces

Edit file interfaces:
auto eth0
iface eth0 inet static

Find installed either network ID:

shut down network: #ifdown -a
start the network: #ifup -a

Restart the demon: sudo service networking restart

Ubuntu 18.10 or later

Enter the super user shell
sudo -i

sudo su

Use 'awk' to view files
sudo awk '{ FS=":"; } { print $1; }' /etc/passwd
get the first column (saperated by ":")

Full syntax:
awk 'BEGIN { action; }
/search/ { action; }
END { action; }' input_file

check open ports
nmap external IP address

Firewall ufw (refer to the pdf for more details)
1. ufw enable
2. ufw allow 25/tcp

root@ubuntu:~# ufw status numbered
Status: active

To Action From
-- ------ ----
[ 1] 80 ALLOW IN Anywhere
[ 2] 80/tcp DENY IN
[ 3] 80/tcp DENY IN
[ 4] 80/tcp DENY IN
[ 5] 80/tcp DENY IN

Check status of services
service --status-all

nano search: contro+w, find next: alt+w

check which group a user belongs to
groups mji

'top' show information like tasks, memory, cpu and swap. Press ‘q‘ to quit window.

Check performance - others
iotop (showing I/O on the system)
netstat -t (showing connections)
Take a look at the apache logfiles and find out what the server did last
set some RLimits for the apache process. When these limits are reached the process will be killed, giving you some more information

List installed packages
apt list --installed

dig google.com - find out the A record of a domain

Search for a string in all the files in a directory
example: grep -r "mydomain.com" /etc/apache2/

Check open ports
netstat -anltp | grep "LISTEN"

grep minda -r, grep -w "minda" -r (search for exact word)

Using tar to compress a directory or file
$tar -czvf archive.tar.gz stuff (stuff is a folder in the current directory)

-c: Create an archive.
-z: Compress the archive with gzip.
-v: Display progress in the terminal while creating the archive, also known as “verbose” mode. The v is always optional in these commands, but it’s helpful.
-f: Allows you to specify the filename of the archive.
-p: preserve the permissions

$tar -xzvf archive.tar.gz (same, but use 'x' instead of 'c')

Exclude a directory
tar --exclude="/var/www/html/zota/pub/media/catalog/product/cache" -czvf /home/mji/backup/zota/zota_media$(date +%y%m%d_%H%M).tar.gz  /var/www/html/zota/pub/media/catalog/product/

List content of a compressed file
sudo tar -ztvf /backup/files.backup.Nov_6_2009.tar.gz

Find errors with tar
tar --exclude="/var/www/html/zota/var/backups" -czvf /home/mindaji/backup/zota/zota_$(date +%y%m%d_%H%M).tar.gz /var/www/html/zota/ > /dev/null

Using unzip
unzip file.zip -d destination_folder

apt-get update: downloads the package lists from the repositories and "updates" them to get information on the newest versions of packages and their dependencies. It will do this for all repositories and PPAs.

apt-get upgrade: will fetch new versions of packages existing on the machine if APT knows about these new versions by way of apt-get update.

apt-get dist-upgrade: will do the same job which is done by apt-get upgrade, plus it will also intelligently handle the dependencies, so it might remove obsolete packages or add new ones.

Check status of services
service --status-all

Check User ID and groups
id mji

Check secondary groups you are in:
groups $(whoami)

Change my primary group to www-data
sudo usermod -g www-data mji

Using rsync to copy
sudo rsync -rav magento2ce/ magento2ce_setup2/

Copy directory
cp -R source destination/

Copy or back up locally or remotely
rsync options source destination
-v : verbose
-r : copies data recursively (but don’t preserve timestamps and permission while transferring data
-a : archive mode, archive mode allows copying files recursively and it also preserves symbolic links, file permissions, user & group ownerships and timestamps
-z : compress file data
-h : human-readable, output numbers in a human-readable format

tar --exclude="/var/www/html/zota/pub/media/catalog/product/cache" -czvf /home/mji/backup/zota/zota_system$(date +%y%m%d_%H%M).tar.gz /var/www/html/zota/

Using RCP to copy file to and from remote computers

$rcp longwise_191222_1917.* mji@www.dataclubus.com:/home/mji/


rsync -avzh /root/rpmpkgs /tmp/backups/
To a remote computer: rsync -avz rpmpkgs/ root@
To the local Machine: rsync -avzh root@ /tmp/myrpms


Ubuntu 18.04

Enable root login

Set new password (no password assigned after new installation)
sudo passwd root

Unlock root (It is locked when installed newly)
sudo passwd -u root
Resize root filesystem on Ubuntu 18.04 with LVM
Ubuntu 18.04 default lvm layout comes with only a 4 GB root partition. Extend it to occupy the full free space in the volume group:
sudo lvextend -l 100%FREE --resizefs /dev/mapper/ubuntu--vg-ubuntu--lv

Set Static IP

Ubuntu 18.04 LTS change hostname permanently
#sudo hostnamectl set-hostname newNameHere

Futhermore, check for the existence of /etc/cloud/cloud.cfg configuration. If the file exists edit the file and change the settings within:
FROM: preserve_hostname: false
TO: preserve_hostname: true



Find LAMP versions
Ubuntu: lsb_release -a
PHP: php -v
MySQL: mysql -V


Conditional forwarder to another dns server for specific domain names resolving
add in named.conf.local
zone "dataclub.com" {
type forward
forwarders {;};

Conditional forwarder to another dns server for all domain names resolving
add in named.conf.options
fowarders {;

Find out group members
grep 'group_name_here:' /etc/group
awk '/www-data/' /etc/group

Find out where "php" is
#which php


Related to Apache

The user running web server
ps aux | grep httpd

Install ssl/https server (self assigned SSL certificate)
1. sudo a2enmod ssl
2. sudo mkdir /etc/apache2/ssl
3. sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /etc/apache2/ssl/apache.key -out /etc/apache2/ssl/apache.crt
4. Answer the following questions:
Country Name (2 letter code) [AU]:US
State or Province Name (full name) [Some-State]:New York
Locality Name (eg, city) []:New York City
Organization Name (eg, company) [Internet Widgits Pty Ltd]:Your Company
Organizational Unit Name (eg, section) []:Department of Kittens
Common Name (e.g. server FQDN or YOUR name) []:your_domain.com
Email Address []:your_email@domain.com

5. edit longwise.conf:

<IfModule mod_ssl.c>
    <VirtualHost _default_:443>
        ServerAdmin admin@example.com
        ServerName your_domain.com
        ServerAlias www.your_domain.com
        DocumentRoot /var/www/html
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
        SSLEngine on
        SSLCertificateFile /etc/apache2/ssl/apache.crt
        SSLCertificateKeyFile /etc/apache2/ssl/apache.key
        <FilesMatch "\.(cgi|shtml|phtml|php)$">
                        SSLOptions +StdEnvVars
        <Directory /usr/lib/cgi-bin>
                        SSLOptions +StdEnvVars
        BrowserMatch "MSIE [2-6]" \
                        nokeepalive ssl-unclean-shutdown \
                        downgrade-1.0 force-response-1.0
        BrowserMatch "MSIE [17-9]" ssl-unclean-shutdown

6. sudo a2ensite default-ssl.conf
7. sudo systemctl restart apache2

More details: https://www.digitalocean.com/community/tutorials/how-to-create-a-ssl-certificate-on-apache-for-ubuntu-14-04

For certificate issued by CA Authorities
see https://www.digitalocean.com/community/tutorials/how-to-install-an-ssl-certificate-from-a-commercial-certificate-authority

Disk Management

Expand a Hard Disk with Ubuntu LVM

(Must be with the partition type of LVM


1. Add disk space from VMW

2. Check free space:
sudo parted

3. Create a new partition using:
sudo cfdisk

4. Check the new partition:
sudo fdisk -l /dev/sda 

5. Create physical volume:
sudo pvcreate /dev/sda3  (It may require reboot before creating it.)

6. Check the new physical volume:
sudo pvdisplay

7. Extend my volume group into our new physical volume (/dev/sda3):
vgextend ubuntu02-vg /dev/sda3

8. Check logical volume:
sudo lvdisplay

9. Extend the logical volume to all free space available:
lvextend -l+100%FREE /dev/ubuntu02-vg/root

10. Extend the filesystem:
sudo resize2fs /dev/ubuntu02-vg/root

How to Create a Partition and Mount It

List logical disks and partitions
sudo fdisk -l

Partition the disk
sudo fdisk /dev/sdb

Press n to create a partition
Press p or l to create primary or logical partitions
Press w to write your changes or q to quit

Format the partition
sudo mkfs -t ext4 /dev/sdb1

Mount disk
mount - Shows what is mounted
mkdir /mnt/mydrive
mount -t ext4 /dev/sdb1 /mnt/mydrive

Get disk's UUID
ls -al /dev/disk/by-uuid/

Mount at boot
Add the following line to your /etc/fstab file adjusting the UUID to your device's id and the directory to where you want to mount:

UUID=811d3de0-ca6b-4b61-9445-af2e306d9999 /mnt/mydrive ext4 defaults 0 0

mount -a - remounts filesystems from /etc/fstab

Create LVM partition and mount it

Some useful commands:

Remove Logical Volume

Remove Physical Volumes
#vgreduce my_volume_group /dev/hda1

Displaying Information about All LVM Compatible Block Storage Devices

Displaying Information about Physical Volumes
#sudo lvmdiskscan -l

#sudo pvdisplay

Create ext4 file system on logical volume:
#mkfs.ext4 /dev/my_vg/my_lv

Ubuntu Basics
Tagged on: