1 - Tips and Tricks for Ubuntu

Managing Packages with apt

apt update

Before we begin, we update our packages list with the following command:

sudo apt update

apt upgrade

When there are packages that can be upgraded, we can do so using the following command:

sudo apt upgrade

After running this command, you will be prompted to confirm whether or not you want to proceed with the updates. Simply press Y followed by ENTER.

Combining update and upgrade commands

To save time, you can also combine the apt update and apt upgrade commands into a single command:

sudo apt update && sudo apt upgrade -y

The -y flag automatically answers Y to any prompts that may appear during the upgrade process.

Installing packages with apt

To install a package, use the following command:

sudo apt install packagename

Replace packagename with the name of the package you want to install. For example, if you wanted to install the filezilla FTP client, you would use the following command:

sudo apt install filezilla

Removing packages with apt

To remove a package and its configuration files, use the following command:

sudo apt remove packagename

Replace packagename with the name of the package you want to remove. For example, if you wanted to remove the filezilla FTP client, you would use the following command:

sudo apt remove filezilla

Purging packages with apt

If you want to remove a package as well as any configuration files that may have been created by it, use the following command:

sudo apt purge packagename

Replace packagename with the name of the package you want to remove. For example, if you wanted to remove the filezilla FTP client along with any associated configuration files, you would use the following command:

sudo apt purge filezilla

Cleaning up apt packages

To free up disk space by removing downloaded package files that are no longer needed, use the following command:

sudo apt clean

Moving and renaming files/directories

To move a file or directory to another location on your system, use the mv command followed by the source file or directory and its destination path. For example, if you wanted to move a file named sourcefile.txt from your home directory to the /home/destination/ directory, you would use the following command:

mv /home/sourcefile.txt /home/destination/

To rename a file or directory, use the same mv command followed by the source file or directory and its new name. For example, if you wanted to rename a file named sourcefile.txt to destfile.txt, you would use the following command:

mv /home/sourcefile.txt /home/destfile.txt

Copying files/directories

To copy a file or directory from one location on your system to another, use the cp command followed by the source file or directory and its destination path. For example, if you wanted to copy a file named sourcefile.txt from your home directory to the /home/destination/ directory, you would use the following command:

cp /home/sourcefile.txt /home/destination/

To create an empty file or directory, use the touch command followed by the path of the new item. For example, if you wanted to create an empty text file named newfile.txt in your home directory, you would use the following command:

touch /home/newfile.txt

To create a new directory with the specified name, use the mkdir command followed by the path of the new directory. For example, if you wanted to create a new directory named newfolder in your home directory, you would use the following command:

mkdir /home/newfolder

Removing files/directories

To remove a file or directory from your system, use the rm command followed by the path of the item to be removed. For example, if you wanted to remove a file named sourcefile.txt from your home directory, you would use the following command:

rm /home/sourcefile.txt

To remove a directory along with its contents, use the -r flag with the rm command followed by the path of the directory to be removed. For example, if you wanted to remove a directory named newfolder along with its contents from your home directory, you would use the following command:

rm -r /home/newfolder

Changing directories

To navigate to a different directory on your system, use the cd command followed by the path of the directory you want to move to. For example, if you wanted to change to the /home/destination/ directory from your current location, you would use the following command:

cd /home/destination/

To move up one level in the directory hierarchy, use the .. notation with the cd command. For example, if you were in the /home/destination/ directory and wanted to move back to your home directory, you would use the following command:

cd ..

Rebooting and shutting down your system

To restart your server, use the following command:

sudo reboot now

This will prompt your server to perform a graceful reboot. If you prefer, you can also use the following shorthand version of the command:

sudo reboot .

To shut down your server, use the following command:

sudo shutdown now

This will immediately initiate a system shutdown. Alternatively, you can use the following command to power off your machine:

sudo poweroff

Switching to the root user account

To switch to the root user account and gain full administrative privileges on your system, use either of the following commands:

su -
sudo su -

The first command will prompt you for the root password, while the second command will prompt you for the password of the current user.

Clearing the terminal screen

To clear all text from your terminal screen, use the following command:

clear

This will erase any output displayed on the screen, leaving only your system’s command prompt.

Forcing package upgrades

If a package is not being upgraded when you run the apt upgrade command, you can try using the following command to force an upgrade:

sudo apt dist-upgrade

This will attempt to resolve any dependencies that may be preventing a package from being upgraded.

2 - How to install CUDA toolkit from Ubuntu Repository

Simple tutotial for how to install CUDA toolkit from Ubuntu Repository

Although you might not end up witht he latest CUDA toolkit version, the easiest way to install CUDA on Ubuntu 20.04 is to perform the installation from Ubuntu’s standard repositories.

To install CUDA execute the following commands:

sudo apt update &&\
sudo apt install nvidia-cuda-toolkit

All should be ready now. Check your CUDA version:

nvcc --version
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2019 NVIDIA Corporation
Built on Sun_Jul_28_19:07:16_PDT_2019
Cuda compilation tools, release 10.1, V10.1.243

3 - How to Install GCC Compiler on Ubuntu 18.04

Installing GCC Compiler on a Ubuntu machine with just two commands.

The GNU Compiler Collection (GCC) is a collection of compilers and libraries for C, C++, Objective-C, Fortran, Ada, Go , and D programming languages. Many open-source projects, including the GNU tools and the Linux kernel, are compiled with GCC.
This tutorial covers the steps required to install the GCC compiler on Ubuntu 18.04. We will show you how to install the distro stable version and the latest version of GCC. The same instructions apply for Ubuntu 16.04 and any Ubuntu-based distribution, including Kubuntu, Linux Mint and Elementary OS.

Prerequisites

Te able to add new repositories and install packages on your Ubuntu system, you must be logged in as root or user with sudo privileges .

Installing GCC on Ubuntu

The default Ubuntu repositories contain a meta-package named build-essential that contains the GCC compiler and a lot of libraries and other utilities required for compiling software.

Perform the steps below to install the GCC Compiler Ubuntu 18.04:

  • Start by updating the packages list:
    sudo apt update
    
  • Install the build-essential package by typing:
    sudo apt install build-essential
    
    The command installs a bunch of new packages including gcc, g++ and make.
  • To validate that the GCC compiler is successfully installed, use the gcc --version command which prints the GCC version:
    gcc --version
    

4 - How to shutdown or reboot

How to shutdown or reboot your Ubuntu machine from the command line?

Well it is actually one of the first things you learn when you administer servers via ssh, let’s start with restart.

reboot

You need to be root to be able to restart a Ubuntu system, or you can use sudo (provided you are in the sudoers file)

sudo reboot

Another way to do it is to use shutdown command.

shutdown -r now

The -r switch will That will reboot the machine inmediately, if you want to add a delay.

shutdown -r +15

That command will reboot the machine 15 minutes after the issue of the command. You might also want to reboot the machine at a specific time:

shutdown -r 17:15

This is pretty self explanatory, it will restart at 17:15 hours, be sure to use the 24 hours format. Let’s now write about shutdown instead of reboot.

We are going to use the same shutdown command, just this time with h switch instead of r. The h stands for halt and the r for reboot. So to turn off the machine at this very moment use this command.

shutdown -h now

And if you want to do it with a delay.

shutdown -h +15

That will enter a fifteen minutes delay after the issue of the command. If you want to turn it off at a specific time.

shutdown -h 17:15

Just like with the reboot example, this will turn the Ubuntu machine off at 17:15, and once again you need to use the 24 time format.

5 - Wipe your SD/SSD disks with dd

soo i found this on the internet and i thought its very useful to make sure your micro SD card is nulled so we can make a fresh clean install on the SD and be sure there no left overs on the card

So i know there are more ways to do this but this is what i found and also use

You can wipe the SD card out by the following command (let’s assume, that your SD card is /dev/sdb): and to be really sure use the

lsblk

So now we know for sure it’s on the right one let’s go to the next step

Do not interrupt this command or it could possibly brick the SD card.

sudo dd if=/dev/zero of=/dev/sdd bs=8192

Note: If this command does not complete successfully and you have to abort it, then most likely it is recoverable with disk partition recovery programs covered in other posts. It can take some time depending on size and speed of SD card.

If you are convinced, that CIA would like to recover your files, then overwrite the SD card with urandom instead of zero:

sudo dd if=/dev/urandom of=/dev/sdd bs=8192

dd command from above examples will erase whole SD card, leaving it without any partitions, even with no partition table. So you will need to recreate partition on SD card. You can do this by any partitioning tool like cfdisk, parted (my recommendation) or gparted.
And one more thing: be extremely careful when calling dd command. A typo in of= argument value can cause disaster.

6 - How To Create A New Sudo Enabled User on Ubuntu 20.04

This tutorials will explain how to create a sudo user on Ubuntu 20.04

Introduction

When managing a server, you’ll sometimes want to allow users to execute commands as “root,” the administrator-level user. The sudo command provides system administrators with a way to grant administrator privileges — ordinarily only available to the root user — to normal users.

In this tutorial, you’ll learn how to create a new user with sudo access on Ubuntu 20.04 without having to modify your server’s /etc/sudoers file.

Step 1 — Logging Into Your Server

SSH in to your server as the root user:

ssh root@your_server_ip_address

Step 2 — Adding a New User to the System

Use the adduser command to add a new user to your system:

adduser sammy

Be sure to replace sammy with the username that you want to create. You will be prompted to create and verify a password for the user:

Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully

Next, you’ll be asked to fill in some information about the new user. It is fine to accept the defaults and leave this information blank:

Changing the user information for sammy
Enter the new value, or press ENTER for the default
    Full Name []:
    Room Number []:
    Work Phone []:
    Home Phone []:
    Other []:
Is the information correct? [Y/n]

Step 3 — Adding the User to the sudo Group

Use the usermod command to add the user to the sudo group:

usermod -aG sudo sammy

Again, be sure to replace sammy with the username you just added. By default on Ubuntu, all members of the sudo group have full sudo privileges.

Step 4 — Testing sudo Access

To test that the new sudo permissions are working, first use the su command to switch to the new user account:

su - sammy

As the new user, verify that you can use sudo by prepending sudo to the command that you want to run with superuser privileges:

sudo command_to_run

For example, you can list the contents of the /root directory, which is normally only accessible to the root user:

sudo ls -la /root

The first time you use sudo in a session, you will be prompted for the password of that user’s account. Enter the password to proceed:

[sudo] password for sammy:

If your user is in the proper group and you entered the password correctly, the command that you issued with sudo will run with root privileges.

Conclusion

In this quickstart tutorial, we created a new user account and added it to the sudo group to enable sudo access.

7 - How To Create A New Sudo Enabled User on Ubuntu 22.04

This tutorial will explain how to create a sudo user on Ubuntu 220.04

Introduction

When managing a server, you’ll sometimes want to allow users to execute commands as “root,” the administrator-level user. The sudo command provides system administrators with a way to grant administrator privileges — ordinarily only available to the root user — to normal users.

In this tutorial, you’ll learn how to create a new user with sudo access on Ubuntu 22.04 without having to modify your server’s /etc/sudoers file.

Step 1 — Logging Into Your Server

SSH in to your server as the root user:

ssh root@your_server_ip_address

Step 2 — Adding a New User to the System

Use the adduser command to add a new user to your system:

adduser sammy

Be sure to replace sammy with the username that you want to create. You will be prompted to create and verify a password for the user:

Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully

Next, you’ll be asked to fill in some information about the new user. It is fine to accept the defaults and leave this information blank:

Changing the user information for sammy
Enter the new value, or press ENTER for the default
    Full Name []:
    Room Number []:
    Work Phone []:
    Home Phone []:
    Other []:
Is the information correct? [Y/n]

Step 3 — Adding the User to the sudo Group

Use the usermod command to add the user to the sudo group:

usermod -aG sudo sammy

Again, be sure to replace sammy with the username you just added. By default on Ubuntu, all members of the sudo group have full sudo privileges.

Step 4 — Testing sudo Access

To test that the new sudo permissions are working, first use the su command to switch to the new user account:

su - sammy

As the new user, verify that you can use sudo by prepending sudo to the command that you want to run with superuser privileges:

sudo command_to_run

For example, you can list the contents of the /root directory, which is normally only accessible to the root user:

sudo ls -la /root

The first time you use sudo in a session, you will be prompted for the password of that user’s account. Enter the password to proceed:

[sudo] password for sammy:

If your user is in the proper group and you entered the password correctly, the command that you issued with sudo will run with root privileges.

Conclusion

In this quickstart tutorial, we created a new user account and added it to the sudo group to enable sudo access.

8 - Cutsom install mariaDB

Cutsom install for mariaDB on Ubuntu server or desktop

Custom tutorial for MariaDB on Ubuntu.

Step 1

  1. Before we begin we need to update our apt packages with
    sudo apt updates
    
  2. After we updates our packages we can install mariadb-server
    sudo apt install mariadb-server
    
  3. Now MariaDB server is installed we can check if it’s running
    sudo systemctl status mariadb
    

Step 2

  1. Now we need to secure our MariaDB so type
    sudo mysql_secure_installation
    
  2. It will start with asking for root password for mariadb, just press enter

Next

Now we going to configure the mariadb server and securing it by the following steps

sudo mysql_secure_installation

BE NOTED THIS WILL BE FIXED SOME TIME… BUT NOW NOW

examples for localhost, %, specific

  • Only localhost is allowd to connect…

    CREATE USER 'local_user'@'localhost' IDENTIFIED BY 'password';
    
  • Now you can login from any IP to your

    CREATE USER 'local_user'@'%' IDENTIFIED BY 'password';
    
  • This will allow you only to login from specific IP

    CREATE USER 'local_user'@'192.168.1.1' IDENTIFIED BY 'password';
    

Giving user grant all privileges

  1. Let’s login with our root use in MariaDB

    mysql -u root -p
    
  2. Replace syslogine with your own enw created used and replace localhost if want.

    GRANT ALL PRIVILEGES ON *.* TO 'syslogine'@'localhost' WITH GRANT OPTION;
    
  3. Lets save it.

    FLUSH PRIVILEGES;
    
  4. Yes now we can exit the mysql

    exit
    

Edit MariaDB 50-server.cnf

cd /etc/mysql



LOLLLLLLLLLLLL

9 - How To Install MariaDB on Ubuntu 20.04

This tutorial will show you how to install a MariaDB on Ubuntu 20.04

Introduction

MariaDB is an open-source relational database management system, commonly used as an alternative for MySQL as the database portion of the popular LAMP (Linux, Apache, MySQL, PHP/Python/Perl) stack. It is intended to be a drop-in replacement for MySQL.

The short version of this installation guide consists of these three steps:

  • Update your package index using apt
  • Install the mariadb-server package using apt. The package also pulls in related tools to interact with MariaDB
  • Run the included mysql_secure_installation security script to restrict access to the server
sudo apt update
sudo apt install mariadb-server
sudo mysql_secure_installation
sudo apt update &&\
sudo apt install mariadb-server -y &&\
sudo mysql_secure_installation

This tutorial will explain how to install MariaDB on an Ubuntu 20.04 server and verify that it is running and has a safe initial configuration.

Prerequisites

To follow this tutorial, you will need a server running Ubuntu 20.04. This server should have a non-root administrative user and a firewall configured with UFW. Set this up by following our initial server setup guide for Ubuntu 20.04.

Step 1 — Installing MariaDB

As of this writing, Ubuntu 20.04’s default APT repositories include MariaDB version 10.3.

To install it, update the package index on your server with apt:

sudo apt update

Then install the package:

sudo apt install mariadb-server

Ensure that MariaDB is running with the systemctl start command:

sudo systemctl start mariadb.service

These commands will install and start MariaDB, but will not prompt you to set a password or make any other configuration changes. Because the default configuration leaves your installation of MariaDB insecure, we will use a script that the mariadb-server package provides to restrict access to the server and remove unused accounts.

Step 2 — Configuring MariaDB

For new MariaDB installations, the next step is to run the included security script. This script changes some of the less secure default options for things like remote root logins and sample users.

Run the security script:

sudo mysql_secure_installation

This will take you through a series of prompts where you can make some changes to your MariaDB installation’s security options. The first prompt will ask you to enter the current database root password. Since you have not set one up yet, press ENTER to indicate “none”.

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none):

The next prompt asks you whether you’d like to set up a database root password. On Ubuntu, the root account for MariaDB is tied closely to automated system maintenance, so we should not change the configured authentication methods for that account. Doing so would make it possible for a package update to break the database system by removing access to the administrative account. Type N and then press ENTER.

. . .
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.

Set root password? [Y/n] N

Later, we will cover how to set up an additional administrative account for password access if socket authentication is not appropriate for your use case.

From there, you can press Y and then ENTER to accept the defaults for all the subsequent questions. This will remove some anonymous users and the test database, disable remote root logins, and load these new rules so that MariaDB immediately implements the changes you have made.

With that, you’ve finished MariaDB’s initial security configuration. The next step is an optional one, though you should follow it if you prefer to authenticate to your MariaDB server with a password.

Step 3 — (Optional) Creating an Administrative User that Employs Password Authentication

On Ubuntu systems running MariaDB 10.3, the root MariaDB user is set to authenticate using the unix_socket plugin by default rather than with a password. This allows for some greater security and usability in many cases, but it can also complicate things when you need to allow an external program (e.g., phpMyAdmin) administrative rights.

Because the server uses the root account for tasks like log rotation and starting and stopping the server, it is best not to change the root account’s authentication details. Changing credentials in the /etc/mysql/debian.cnf configuration file may work initially, but package updates could potentially overwrite those changes. Instead of modifying the root account, the package maintainers recommend creating a separate administrative account for password-based access.

To this end, we will create a new account called admin with the same capabilities as the root account, but configured for password authentication. Open up the MariaDB prompt from your terminal:

sudo mariadb

Then create a new user with root privileges and password-based access. Be sure to change the username and password to match your preferences:

GRANT ALL ON *.* TO 'admin'@'localhost' IDENTIFIED BY 'password' WITH GRANT OPTION;

Flush the privileges to ensure that they are saved and available in the current session:

FLUSH PRIVILEGES;

Following this, exit the MariaDB shell:

exit

Finally, let’s test the MariaDB installation.

Step 4 — Testing MariaDB

When installed from the default repositories, MariaDB will start running automatically. To test this, check its status.

sudo systemctl status mariadb

You’ll receive output that is similar to the following:

● mariadb.service - MariaDB 10.3.22 database server
     Loaded: loaded (/lib/systemd/system/mariadb.service; enabled; vendor preset: enabled)
     Active: active (running) since Tue 2020-05-12 13:38:18 UTC; 3min 55s ago
       Docs: man:mysqld(8)
             https://mariadb.com/kb/en/library/systemd/
   Main PID: 25914 (mysqld)
     Status: "Taking your SQL requests now..."
      Tasks: 31 (limit: 2345)
     Memory: 65.6M
     CGroup: /system.slice/mariadb.service
             └─25914 /usr/sbin/mysqld
. . .

If MariaDB isn’t running, you can start it with the command sudo systemctl start mariadb.

For an additional check, you can try connecting to the database using the mysqladmin tool, which is a client that lets you run administrative commands. For example, this command says to connect to MariaDB as root using the Unix socket and return the version:

sudo mysqladmin version

You will receive output similar to this:

mysqladmin  Ver 9.1 Distrib 10.3.22-MariaDB, for debian-linux-gnu on x86_64
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Server version		10.3.22-MariaDB-1ubuntu1
Protocol version	10
Connection		Localhost via UNIX socket
UNIX socket		/var/run/mysqld/mysqld.sock
Uptime:			4 min 49 sec

Threads: 7  Questions: 467  Slow queries: 0  Opens: 177  Flush tables: 1  Open tables: 31  Queries per second avg: 1.615

If you configured a separate administrative user with password authentication, you could perform the same operation by typing:

mysqladmin -u admin -p version

This means that MariaDB is up and running and that your user is able to authenticate successfully.

Conclusion

In this guide you installed the MariaDB relational database management system, and secured it using the mysql_secure_installation script that it came installed with. You also had the option to create a new administrative user that uses password authentication before testing the MariaDB server’s functionality.

10 - How To Install MariaDB on Ubuntu 22.04

Introduction

MariaDB is an open-source relational database management system, commonly used as an alternative for MySQL as the database portion of the popular LAMP (Linux, Apache, MySQL, PHP/Python/Perl) stack. It is intended to be a drop-in replacement for MySQL.

The short version of this installation guide consists of these three steps:

  • Update your package index using apt
  • Install the mariadb-server package using apt. The package also pulls in related tools to interact with MariaDB
  • Run the included mysql_secure_installation security script to restrict access to the server
sudo apt update
sudo apt install mariadb-server
sudo mysql_secure_installation

This tutorial will explain how to install MariaDB on an Ubuntu 22.04 server and verify that it is running and has a safe initial configuration.

Prerequisites

To follow this tutorial, you will need a server running Ubuntu 22.04. This server should have a non-root administrative user and a firewall configured with UFW. Set this up by following our initial server setup guide for Ubuntu 22.04.

Step 1 — Installing MariaDB

As of this writing, Ubuntu 22.04’s default APT repositories include MariaDB version 10.5.12.

To install it, update the package index on your server with apt:

sudo apt update

Then install the package:

sudo apt install mariadb-server

These commands will install MariaDB, but will not prompt you to set a password or make any other configuration changes. Because the default configuration leaves your installation of MariaDB insecure, you will use a script that the mariadb-server package provides to restrict access to the server and remove unused accounts.

Step 2 — Configuring MariaDB

For new MariaDB installations, the next step is to run the included security script. This script changes some of the less secure default options for things like remote root logins and sample users.

Run the security script:

sudo mysql_secure_installation

This will take you through a series of prompts where you can make some changes to your MariaDB installation’s security options. The first prompt will ask you to enter the current database root password. Since you have not set one up yet, press ENTER to indicate “none”.

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, you'll need the current
password for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none):

You’ll be asked if you want to switch to unix socket authentication. Since you already have a protected root account, you can skip this step. Type n and then press ENTER.

. . .
Setting the root password or using the unix_socket ensures that nobody
can log into the MariaDB root user without the proper authorisation.

You already have your root account protected, so you can safely answer 'n'.

Switch to unix_socket authentication [Y/n] n

The next prompt asks you whether you’d like to set up a database root password. On Ubuntu, the root account for MariaDB is tied closely to automated system maintenance, so you should not change the configured authentication methods for that account.

Doing so would make it possible for a package update to break the database system by removing access to the administrative account. Type n and then press ENTER.

. . .
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.

Set root password? [Y/n] n

Later, you’ll go over how to set up an additional administrative account for password access if socket authentication is not appropriate for your use case.

From there, you can press Y and then ENTER to accept the defaults for all the subsequent questions. This will remove some anonymous users and the test database, disable remote root logins, and load these new rules so that MariaDB immediately implements the changes you have made.

With that, you’ve finished MariaDB’s initial security configuration. The next step is an optional one, though you should follow it if you prefer to authenticate to your MariaDB server with a password.

Step 3 — (Optional) Creating an Administrative User that Employs Password Authentication

On Ubuntu systems running MariaDB 10.5, the root MariaDB user is set to authenticate using the unix_socket plugin by default rather than with a password. This allows for some greater security and usability in many cases, but it can also complicate things when you need to allow an external program (e.g., phpMyAdmin) administrative rights.

Because the server uses the root account for tasks like log rotation and starting and stopping the server, it is best not to change the root account’s authentication details. Changing credentials in the /etc/mysql/debian.cnf configuration file may work initially, but package updates could potentially overwrite those changes. Instead of modifying the root account, the package maintainers recommend creating a separate administrative account for password-based access.

To this end, we will create a new account called admin with the same capabilities as the root account, but configured for password authentication. Open up the MariaDB prompt from your terminal:

sudo mariadb

Then create a new user with root privileges and password-based access. Be sure to change the username and password to match your preferences:

GRANT ALL ON *.* TO 'admin'@'localhost' IDENTIFIED BY 'password' WITH GRANT OPTION;

Flush the privileges to ensure that they are saved and available in the current session:

FLUSH PRIVILEGES;

Following this, exit the MariaDB shell:

exit

Finally, let’s test the MariaDB installation.

Step 4 — Testing MariaDB

When installed from the default repositories, MariaDB will start running automatically. To test this, check its status.

sudo systemctl status mariadb

You’ll receive output that is similar to the following:

● mariadb.service - MariaDB 10.5.12 database server
     Loaded: loaded (/lib/systemd/system/mariadb.service; enabled; vendor preset: enabled)
     Active: active (running) since Fri 2022-03-11 22:01:33 UTC; 14min ago
       Docs: man:mariadbd(8)
             https://mariadb.com/kb/en/library/systemd/
. . .

If MariaDB isn’t running, you can start it with the command sudo systemctl start mariadb.

For an additional check, you can try connecting to the database using the mysqladmin tool, which is a client that allows you to run administrative commands. For example, this command says to connect to MariaDB as root using the Unix socket and return the version:

sudo mysqladmin version

You will receive output similar to this:

mysqladmin  Ver 9.1 Distrib 10.5.12-MariaDB, for debian-linux-gnu on x86_64
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Server version        10.5.12-MariaDB-1build1
Protocol version    10
Connection        Localhost via UNIX socket
UNIX socket        /run/mysqld/mysqld.sock
Uptime:            15 min 53 sec

Threads: 1  Questions: 482  Slow queries: 0  Opens: 171  Open tables: 28  Queries per second avg: 0.505

Conclusion

In this guide you installed the MariaDB relational database management system, and secured it using the mysql_secure_installation script that it came installed with. You also had the option to create a new administrative user that uses password authentication before testing the MariaDB server’s functionality.

11 - How To Install MySQL on Ubuntu 20.04

Introduction

MySQL is an open-source database management system, commonly installed as part of the popular LAMP (Linux, Apache, MySQL, PHP/Python/Perl) stack. It implements the relational model and uses Structured Query Language (better known as SQL) to manage its data.

This tutorial will go over how to install MySQL version 8.0 on an Ubuntu 20.04 server. By completing it, you will have a working relational database that you can use to build your next website or application.

Prerequisites

To follow this tutorial, you will need:

  • One Ubuntu 20.04 server with a non-root administrative user and a firewall configured with UFW.

Step 1 — Installing MySQL

On Ubuntu 20.04, you can install MySQL using the APT package repository. At the time of this writing, the version of MySQL available in the default Ubuntu repository is version 8.0.27.

To install it, update the package index on your server if you’ve not done so recently:

sudo apt update

Then install the mysql-server package:

sudo apt install mysql-server

Ensure that the server is running using the systemctl start command:

sudo systemctl start mysql.service

These commands will install and start MySQL, but will not prompt you to set a password or make any other configuration changes. Because this leaves your installation of MySQL insecure, we will address this next.

Step 2 — Configuring MySQL

For fresh installations of MySQL, you’ll want to run the DBMS’s included security script. This script changes some of the less secure default options for things like remote root logins and sample users.

Run the security script with sudo:

sudo mysql_secure_installation

This will take you through a series of prompts where you can make some changes to your MySQL installation’s security options. The first prompt will ask whether you’d like to set up the Validate Password Plugin, which can be used to test the password strength of new MySQL users before deeming them valid.

If you elect to set up the Validate Password Plugin, any MySQL user you create that authenticates with a password will be required to have a password that satisfies the policy you select. The strongest policy level — which you can select by entering 2 — will require passwords to be at least eight characters long and include a mix of uppercase, lowercase, numeric, and special characters:

Securing the MySQL server deployment.

Connecting to MySQL using a blank password.

VALIDATE PASSWORD COMPONENT can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD component?

Press y|Y for Yes, any other key for No: Y

There are three levels of password validation policy:

LOW    Length >= 8
MEDIUM Length >= 8, numeric, mixed case, and special characters
STRONG Length >= 8, numeric, mixed case, special characters and dictionary                  file

Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG:
 2

Regardless of whether you choose to set up the Validate Password Plugin, the next prompt will be to set a password for the MySQL root user. Enter and then confirm a secure password of your choice:

Please set the password for root here.


New password:

Re-enter new password:

Note that even though you’ve set a password for the root MySQL user, this user is not currently configured to authenticate with a password when connecting to the MySQL shell.

If you used the Validate Password Plugin, you’ll receive feedback on the strength of your new password. Then the script will ask if you want to continue with the password you just entered or if you want to enter a new one. Assuming you’re satisfied with the strength of the password you just entered, enter Y to continue the script:

Estimated strength of the password: 100
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : Y

From there, you can press Y and then ENTER to accept the defaults for all the subsequent questions. This will remove some anonymous users and the test database, disable remote root logins, and load these new rules so that MySQL immediately respects the changes you have made.

Once the script completes, your MySQL installation will be secured. You can now move on to creating a dedicated database user with the MySQL client.

Step 3 — Creating a Dedicated MySQL User and Granting Privileges

Upon installation, MySQL creates a root user account which you can use to manage your database. This user has full privileges over the MySQL server, meaning it has complete control over every database, table, user, and so on. Because of this, it’s best to avoid using this account outside of administrative functions. This step outlines how to use the root MySQL user to create a new user account and grant it privileges.

In Ubuntu systems running MySQL 5.7 (and later versions), the root MySQL user is set to authenticate using the auth_socket plugin by default rather than with a password. This plugin requires that the name of the operating system user that invokes the MySQL client matches the name of the MySQL user specified in the command, so you must invoke mysql with sudo privileges to gain access to the root MySQL user:

sudo mysql

Once you have access to the MySQL prompt, you can create a new user with a CREATE USER statement. These follow this general syntax:

CREATE USER 'username'@'host' IDENTIFIED WITH authentication_plugin BY 'password';

After CREATE USER, you specify a username. This is immediately followed by an @ sign and then the hostname from which this user will connect. If you only plan to access this user locally from your Ubuntu server, you can specify localhost. Wrapping both the username and host in single quotes isn’t always necessary, but doing so can help to prevent errors.

You have several options when it comes to choosing your user’s authentication plugin. The auth_socket plugin mentioned previously can be convenient, as it provides strong security without requiring valid users to enter a password to access the database. But it also prevents remote connections, which can complicate things when external programs need to interact with MySQL.

As an alternative, you can leave out the WITH authentication_plugin portion of the syntax entirely to have the user authenticate with MySQL’s default plugin, caching_sha2_password. The MySQL documentation recommends this plugin for users who want to log in with a password due to its strong security features.

Run the following command to create a user that authenticates with caching_sha2_password. Be sure to change sammy to your preferred username and password to a strong password of your choosing:

CREATE USER 'sammy'@'localhost' IDENTIFIED BY 'password';

After creating your new user, you can grant them the appropriate privileges. The general syntax for granting user privileges is as follows:

GRANT PRIVILEGE ON database.table TO 'username'@'host';

The PRIVILEGE value in this example syntax defines what actions the user is allowed to perform on the specified database and table. You can grant multiple privileges to the same user in one command by separating each with a comma. You can also grant a user privileges globally by entering asterisks (*) in place of the database and table names. In SQL, asterisks are special characters used to represent “all” databases or tables.

To illustrate, the following command grants a user global privileges to CREATE, ALTER, and DROP databases, tables, and users, as well as the power to INSERT, UPDATE, and DELETE data from any table on the server. It also grants the user the ability to query data with SELECT, create foreign keys with the REFERENCES keyword, and perform FLUSH operations with the RELOAD privilege. However, you should only grant users the permissions they need, so feel free to adjust your own user’s privileges as necessary.

You can find the full list of available privileges in the official MySQL documentation.

Run this GRANT statement, replacing sammy with your own MySQL user’s name, to grant these privileges to your user:

GRANT CREATE, ALTER, DROP, INSERT, UPDATE, INDEX, DELETE, SELECT, REFERENCES, RELOAD on *.* TO 'sammy'@'localhost' WITH GRANT OPTION;

Note that this statement also includes WITH GRANT OPTION. This will allow your MySQL user to grant any permissions that it has to other users on the system.

Following this, it’s good practice to run the FLUSH PRIVILEGES command. This will free up any memory that the server cached as a result of the preceding CREATE USER and GRANT statements:

FLUSH PRIVILEGES;

Then you can exit the MySQL client:

exit

In the future, to log in as your new MySQL user, you’d use a command like the following:

mysql -u sammy -p

The -p flag will cause the MySQL client to prompt you for your MySQL user’s password in order to authenticate.

Finally, let’s test the MySQL installation.

Step 4 — Testing MySQL

Regardless of how you installed it, MySQL should have started running automatically. To test this, check its status.

systemctl status mysql.service

You’ll see output similar to the following:

● mysql.service - MySQL Community Server
     Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
     Active: active (running) since Tue 2020-04-21 12:56:48 UTC; 6min ago
   Main PID: 10382 (mysqld)
     Status: "Server is operational"
      Tasks: 39 (limit: 1137)
     Memory: 370.0M
     CGroup: /system.slice/mysql.service
             └─10382 /usr/sbin/mysqld

If MySQL isn’t running, you can start it with sudo systemctl start mysql.

For an additional check, you can try connecting to the database using the mysqladmin tool, which is a client that lets you run administrative commands. For example, this command says to connect as a MySQL user named sammy (-u sammy), prompt for a password (-p), and return the version. Be sure to change sammy to the name of your dedicated MySQL user, and enter that user’s password when prompted:

sudo mysqladmin -p -u sammy version

You should see output similar to this:

mysqladmin  Ver 8.0.19-0ubuntu5 for Linux on x86_64 ((Ubuntu))
Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Server version        8.0.19-0ubuntu5
Protocol version    10
Connection        Localhost via UNIX socket
UNIX socket        /var/run/mysqld/mysqld.sock
Uptime:            10 min 44 sec

Threads: 2  Questions: 25  Slow queries: 0  Opens: 149  Flush tables: 3  Open tables: 69  Queries per second avg: 0.038

This means MySQL is up and running.

Conclusion

You now have a basic MySQL setup installed on your server.