Ubuntu Basics

New apt vs apt-get
https://itsfoss.com/apt-vs-apt-get-difference/

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"


Date
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 (package_name) # Removes only the package. Leaves config files and dependent packages untouched
sudo apt-get purge (package_name) # Removes the package and data. Leaves dependent packages untouched
sudo apt-get autoremove (package_name) # Removes packages and unneeded dependent packages, but leaves all data.
sudo apt-get autoremove - -purge (package_name) # Removes package, unneeded dependent packages and all data.

$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
mindaji:x:1000:1000::/home/mindaji:/bin/bash

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

Read more...

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
address 192.168.1.100
netmask 255.255.255.0
network 192.168.1.0
broadcast 192.168.1.255
gateway 192.168.1.1
dns-servernames 192.168.2.32 8.8.8.8

Find installed either network ID:
#ifconfig

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

Restart the demon: sudo service networking restart

Ubuntu 18.10 or later
https://linuxconfig.org/how-to-configure-static-ip-address-on-ubuntu-18-10-cosmic-cuttlefish-linux

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 162.159.209.77
[ 3] 80/tcp DENY IN 162.159.208.77
[ 4] 80/tcp DENY IN 191.96.249.53
[ 5] 80/tcp DENY IN 191.96.249.54

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

extract
$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

Exclude:
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/

https://www.lifewire.com/uses-of-command-rcp-2201078

Example:
rsync -avzh /root/rpmpkgs /tmp/backups/
To a remote computer: rsync -avz rpmpkgs/ root@192.168.0.101:/home/
To the local Machine: rsync -avzh root@192.168.0.100:/home/tarunika/rpmpkgs /tmp/myrpms

More...

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
dl
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
https://linuxconfig.org/how-to-configure-static-ip-address-on-ubuntu-18-10-cosmic-cuttlefish-linux

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

https://linuxconfig.org/how-to-change-hostname-on-ubuntu-18-04-bionic-beaver-linux

Others

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

DNS

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

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

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

Find out where "php" is
#which php

Repositories
https://help.ubuntu.com/lts/serverguide/configuration.html
https://help.ubuntu.com/community/Repositories/Ubuntu

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
        </FilesMatch>
        <Directory /usr/lib/cgi-bin>
                        SSLOptions +StdEnvVars
        </Directory>
        BrowserMatch "MSIE [2-6]" \
                        nokeepalive ssl-unclean-shutdown \
                        downgrade-1.0 force-response-1.0
        BrowserMatch "MSIE [17-9]" ssl-unclean-shutdown
    </VirtualHost>
</IfModule>

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

)
http://www.geoffstratton.com/expand-hard-disk-ubuntu-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/
or
blkid

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
https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/7/html/high_availability_add-on_administration/s1-lvmsetupnfs-haaa

Some useful commands:

Remove Logical Volume
#lvremove

Remove Physical Volumes
#vgreduce my_volume_group /dev/hda1

Displaying Information about All LVM Compatible Block Storage Devices
#lvmdiskscan

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: