Thursday, September 10, 2020

Using PGP (Pretty Good Privacy) on Ubuntu 20.04

Pretty Good Privacy (PGP) is an encryption program that provides cryptographic privacy and authentication for data communication. PGP is used for signing, encrypting, and decrypting texts, e-mails, files, directories, and whole disk partitions and to increase the security of e-mail communications. [Wikipedia] 

 GnuPG or GNU Privacy Guard (GPG) is the GNU implementation of the PGP. To install on Ubuntu 20.04 (if it is still not installed by default), issue the following command: 

 # apt-get install gnupgp 

To determine if the program is installed: 

 # which gpg 

The display should display the directory where the gpg binary is located. 

To generate the keys:

 $ gpg --generate-key

Then, fill-out the prompts.  The keys will be installed in your home directory under the hidden folder ".gnupg".


Using GPG

The following assumes that you have generated your private and public key pairs needed for encryption and decryption.  If you have not, you can always generated them later on using a GUI program mentioned below.

a)  Using GPG to sign your file

To sign your file, issue the command: 

$ gpg  --output Myfile.docx.sig --detach-sig Myfile.docx

This will prompt you for the password of your private key and create a signature file "Myfile.docx.sig" for the document file "Myfile.docx".

To verify if the "Myfile.docx" has not been altered in any way, issue the command:

$ gpg --verify Myfile.docx.sig Myfile.docx

If the file has not been altered, it will display a similar text as follows:

gpg: Signature made Friday, 11 September, 2020 11:29:56 AM PST
gpg: using RSA key 24A7CC8FD2D0D2E189D90E15079F79242255ECA9
gpg: Good signature from "Clement L. Rasul <clemrasul@gmail.com>" [ultimate]

When it says "Good signature ...", it means the "Myfile.docx" has not been altered in any way.  If the file is altered, it will display a text similar to this:

gpg: Signature made Friday, 11 September, 2020 11:29:56 AM PST
gpg: using RSA key 24A7CC8FD2D0D2E189D90E15079F79242255ECA9
gpg: BAD signature from "Clement L. Rasul <clemrasul@gmail.com>" [ultimate]


b) Using GPG to Encypt and Decrypt your file

To encrypt your file, issue the command:

$ gpg --output Myfile.docx.gpg --encrypt --recipient Myfile.docx

This will encrypt and create the encrypted file "Myfile.docx.gpg".  This file is slightly larger than "Myfile.doc".

To decrypt your file, issue the command:

$ gpg --output Myfile.docx --decrypt Myfile.docx.gpg


c)  View your keys

On Ubuntu 20.04, your password and keys can be viewed on "Passwords and Keys".










Public keys sent to and accepted by you, gets displayed here.  This is just a viewer to the file stored in a hidden sub-directory .gnupg in your home directory.  On mine, it is in /home/clemrasul.


d)  Installing a GUI interface integrated with the Nautilus file manager

Issue, the command:

# apt-get install seahorse-nautilus

This will install the seahorse addin to your Nautilus file manager and will add Encrypt, Sign and Decrypt items using the right-click.


e)  Installing a GPG GUI on Ubuntu 20.04

A good overall GUI manager for Ubuntu 20.04 is Kleopatra.  It is part of the KDE desktop application set but will run on GNOME as fine.  To install, issue the command:

# apt-get install kleopatra
# apt-get install scdaemon




Note:  Kleopatra is also available on Windows and is the preferred application for GPG on Windows.  It is available on "GPG for Windows" website (http://www.gpg4win.org).


Tuesday, July 28, 2020

Virtual or Web Hosting on Ubuntu 18.04 LTS (Bionic Beaver)

The following virtual or web hosting setup intends to:

• Make the Apache2 home directory for the virtual hosts in "/home"
• Virtual hosts will be located in their own directory (e.g., user "rasul" will be located in "/home/rasul") and accessible via remote SSH
• Web pages of each virtual host (e.g., user "rasul") will be located in "/home/rasul/www/html"
• Error pages of Apache2 will be located in "/home/rasul/www/error"
• Access log pages of Apache2 will be located in "/home/rasul/www/access"


My preferred editor in here is "joe", a WordStar-like text editor, but you can use your own preferred editor like "vi", "vim", or "nano". This also assumes that you have made the relevant DNS entries for the web host and pointed to the IP address of the Apache webserver.
Step 1: Edit the "apache2.conf" configuration file and add the following entries that will allow apache2 to enter the "/home" directory:

# cd /etc/apache2
# joe apache2.conf

< Directory /home/ >
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
< Directory >



Step 2: Edit the "000-default.conf" in the subdirectory "/etc/apache2/sites-available" and enter the following entries that will define the web pages directory, error pages, and access logs:

# cd /etc/apache2/sites-available
# joe 000-default.conf

< VirtualHost *:80 >
ServerName www.rasul.ph
ServerAdmin webmaster@rasul.ph
DocumentRoot /home/rasul/www/html
ErrorLog /home/rasul/www/error/error.log
CustomLog /home/rasul/www/log/access.log combined
< VirtualHost >



Step 3: Create the virtual host "username" and the corresponing directories, subdirectories, and files:

# adduser rasul
# mkdir /home/rasul/www
# mkdir /home/rasul/www/html
# mkdir /home/rasul/www/error
# mkdir /home/rasul/www/log
# touch /home/rasul/www/error/error.log
# touch /home/rasul/www/log/access.log


Step 4: Change group of the virtual hosts into "www-data". Ubuntu's Apache uses the username "www-data" and group "www-data".

# cd /home
# chown -R rasul.www-data rasul

Listing the directory should display as:

# ls -l
drwxr-xr-x 2 rasul www-data 4096 Jul 29 10:37 rasul




Step 5: Restart the Apache webserver to load the virtual hosts settings

# systemctl restart apache2

Step 6: Check to see by placing a sample webpage in the html directory of the virtual hosts and viewing it in your browser.

Saturday, July 25, 2020

Installing Apache Webserver with PHP scripting, and MySQL and PostgreSQL PHP connection driver under Ubuntu 18.04 LTS (Bionic Beaver)

Clement L. Rasul 1.  Download and install the Apache webserver software

# apt-get install apache2

If there are errors displayed during the installation, issue the update command before repeating the installation command.

# apt-get update --fix-missing
# apt-get install apache2


2.  Download and install the PHP scripting language

# apt-get install php

If there are errors displayed during the installation, issue the update command before repeating the installation command.

# apt-get update --fix-missing
# apt-get install php libapache2-mod-php

3.  Download and install the MySQL database server PHP connection driver.

# apt-get install php-mysql

If there are errors displayed during the installation, issue the update command before repeating the installation command.

# apt-get update --fix-missing
# apt-get install php-mysql


4.  Download and install the PostgreSQL database server PHP connection driver as follows:

# apt-get install php-pgsql

If there are errors displayed during the installation, issue the update command before repeating the installation command.

# apt-get update --fix-missing
# apt-get install php-pgsql



5.  By default, the Apache webserver should be running after installation.  You should restart the Apache webserver in order to load the PHP, MySQL and PostgreSQL support.  Issue the command:

# systemctl restart apache2.service


6. To check if the Apache process is running, issue the command

# ps ax | grep apache2

This should produce a display similar to this if it is running:

23263 ?        Ss     0:00 /usr/sbin/apache2 -k start
23265 ?        S      0:00 /usr/sbin/apache2 -k start
23266 ?        S      0:00 /usr/sbin/apache2 -k start
23267 ?        S      0:00 /usr/sbin/apache2 -k start
23268 ?        S      0:00 /usr/sbin/apache2 -k start
23269 ?        S      0:00 /usr/sbin/apache2 -k start
25621 pts/0    S+     0:00 grep --color=auto apache2

If the apache2 process is not running, the display should just produce this entry:

6433 pts/0    S+     0:00 grep --color=auto apache2

7.  To test if the Apache webserver is running using an Internet browser, type the address where the webserver is running.  On the local machine, the address is 127.0.0.1 (also known as the loopback address).  This should display a page similar to this.


This is the default page of the Apache web server.


8.  To change the default page of the Apache webserver, remove the "index.html" file in the subdirectory "/var/www/html" and create a sample HTML file called "index.html" as follows:

# cd /var/www/html
# rm index.html
# vi index.html


< html >
< head >
< title > My First HTML Page < /title >
< /head >
< body >
Hello World!< /body >
< /html >

My assumption here is, you are familiar with using the vi text editor in Linux.  You may use your own preferred text editor to create the HTML file.  After refreshing your browser, the display should look like this.




9.  To test if the Apache webserver has PHP scripting support, create the following "test.php" file and place it in the subdirectory "/var/www/html".

# cd /var/www/html
# vi test.php

< ?php 
phpinfo();
? >

Type in the address bar of the browser "http://127.0.0.1/test.php", the browser should display a page similar to this.




10.  To know if the MySQL database PHP connection driver is installed, you should see and entry in the PHP test page similar to this:




11.  Likewise, to know if the PostgreSQL database PHP connection driver is installed, you should see an entry in the PHP test page similar to this:



12.  Should there be a need to uninstall the Apache webserver, shut down the running apache2 process first before uninstalling.

# systemctl stop apache2.service
# apt-get remove apache2



Installation and Configuration of Master and Caching DNS Server on Ubuntu 18.04 LTS (Bionic Beaver)

1.  Download and install BIND9 software

#apt-get install bind9

By default, the BIND9 software will start running after a successful installation.  If your objective is just to set up a "Caching DNS Server," then your installation is complete already and you can proceed to testing the functionality of the DNS server in Step No. 5.  The succeeding steps below will configure the BIND9 software as a "Master DNS Server".


2.  Configure file "/etc/bind/named.conf.local" by adding the following entries:

//forward
zone "rasul.ph" IN { // Domain name
     type master; // Primary DNS
     file "/etc/bind/forward.rasul.ph.db"; // Forward lookup file
     allow-update { none; }; // Since this is the primary DNS, it should be none.
};

//reverse
zone "0.16.172.in-addr.arpa" IN { //Reverse lookup name, should match your network in reverse order
     type master; // Primary DNS
     file "/etc/bind/reverse.rasul.ph.db"; //Reverse lookup file
     allow-update { none; }; //Since this is the primary DNS, it should be none.
};


3.  Create entries in the zone and reverse lookup files (Forward & Reverse)


3.1  Create the following entries in the Zone lookup file "/etc/bind/forward.rasul.ph.db"

$TTL    604800
@       IN      SOA     ns1.rasul.ph. root.rasul.ph. (
                              3         ; Serial
                         604800         ; Refresh
                          86400         ; Retry
                        2419200         ; Expire
                         604800 )       ; Negative Cache TTL
;
;@      IN      NS      localhost.
;@      IN      A       127.0.0.1
;@      IN      AAAA    ::1

;Name Server Information
@        IN      NS      ns1.rasul.ph.

;IP address of Name Server
ns1     IN      A       172.16.0.1

;Mail Exchanger
rasul.ph.   IN     MX   10   mail.rasul.ph.

;A – Record HostName To Ip Address

www     IN       A      172.16.0.1
mail    IN       A      172.16.0.2

clem    IN      A       172.16.0.2
mike    IN      A       172.16.0.1

rasul.ph.       IN      A       172.16.0.1

;CNAME record
;ftp     IN      CNAME   www.rasul.ph.


3.2  Create and add entries in the Reverse lookup file "/etc/bind/reverse.rasul.ph.db"

$TTL    604800
@       IN      SOA     rasul.ph. root.rasul.ph. (
                              3         ; Serial
                         604800         ; Refresh
                          86400         ; Retry
                        2419200         ; Expire
                         604800 )       ; Negative Cache TTL
;
;@      IN      NS      localhost.
;1.0.0  IN      PTR     localhost.

;Name Server Information

@       IN      NS     ns1.rasul.ph.

;Reverse lookup for Name Server
;1      IN      PTR    ns1.rasul.ph.

;PTR Record IP address to HostName

1     IN      PTR    www.rasul.ph.
2     IN      PTR    mail.rasul.ph.


4.  Check bind configuration syntax by issuing the command

# named-checkconf


5.  Test the DNS response from a client utility using nslookup or host as follows:


5.1  On the client computer, make changes to the name resolution resolver file "/etc/resolv.conf"

# cd /etc
# vi resolv.conf

nameserver 172.16.0.1

172.16.0.1 is the IP address of the nameserver where BIND9 is installed


5.2  Test the name resolution using nslookup

$nslookup www.rasul.ph

The response should indicate 172.16.0.1

$ nslookup 172.16.0.1

The response should indicate www.rasul.ph


5.3  Alternatively, you can also test name resolution using host command

$ host www.rasul.ph

The response should indicate 172.16.0.1

$ host 172.16.0.1

The response should indicate www.rasul.ph

At this point, your master DNS server installation is complete. 


6.  On occasions, there may be a need for you to restart, stop and start the BIND9 software.  This can be achieved by issuing the following command:

To restart the server, issue the command

# systemctl restart bind9.service


To stop the server, issue the command

# systemctl stop bind9.service


To start the server, issue the command

# systemctl start bind9.service


7.  The BIND9 software runs under the process called "named".  To see if the process is running, issue the command

# ps ax | grep named

You should be able to see the "named" process as running as follows:

11305 ?        Ssl    0:00 /usr/sbin/named -f -u bind
11313 pts/0    S+     0:00 grep --color=auto named


If the process is not running, display will just be as follows:

11219 pts/0    S+     0:00 grep --color=auto named


8.  Should there be a need for you to uninstall the BIND9 software, issue the command after stopping the BIND9 service as follows:

# systemctl stop bind9.service
# apt-get remove bind9

This will uninstall the BIND9 software.




Friday, May 15, 2020

Simple Regression using SQL

A.  Calculating for the Slope

Given the following data in the MariaDB database table named "simreg":

XY
11234
21768
32234
42555
52800

Where X is the independent variable and Y is the dependent variable, the slope can be calculated using the following SQL command:

SELECT (sum((x - (select avg(x) from simreg)) * (y - (select avg(y) from simreg)))) / sum(power(x - (select avg(x) from simreg),2)) AS 'Slope' FROM simreg;

This query produces the following output:

Slope
391.9

The slope value of 391.9 is correct.  We can convert the query into an SQL function as follows:

DELIMITER |
CREATE FUNCTION c_slope() RETURNS DOUBLE
BEGIN
    DECLARE myres DOUBLE;
    SELECT (sum((x - (select avg(x) from simreg)) * (y - (select avg(y) from simreg)))) / sum(power(x - (select avg(x) from simreg),2)) INTO myres FROM simreg;
    RETURN myres;
END |
DELIMITER ;

Such will create the function c_slope() and can be queried using the command:

SELECT c_slope() AS 'Slope';


B.  Calculating for the Intercept

The Intercept can be calculated by issuing the following SQL command:

SELECT avg(y) - (sum((x - (select avg(x) from simreg)) * (y - (select avg(y) from simreg)))) / (sum((x - (select avg(x) from simreg))* (x - (select avg(x) from simreg)))) * avg(x) AS 'Intercept' FROM simreg;

This query produces the following output:

Intercept
942.5

The Intercept value of 942.5 is correct.  Such query can be converted into an SQL function as follows:

DELIMITER |
CREATE FUNCTION c_intercept() RETURNS DOUBLE
BEGIN
     DECLARE myres DOUBLE;
     SELECT avg(y) - (sum((x - (select avg(x) from simreg)) * (y - (select avg(y) from simreg)))) / (sum((x - (select avg(x) from simreg))* (x - (select avg(x) from simreg)))) * avg(x) INTO myres FROM simreg;
     RETURN myres;
END |
DELIMITER ;

Likewise, such will create the function c_intercept() and can be queried using the command:

SELECT c_intercept() AS 'Intercept';


C.  Forecasting

Given the Slope (b) and Intercept (a), the equation of the straight line is "Y = a + b X".  We can use this equation to forecast the value of Y by supplying the value of X.  To execute this in SQL, the command is:

SELECT c_intercept()+c_slope()*x AS 'Y Forecast' FROM simreg;

This query produces the following output:

Y Forecast
1334.4
1726.3
2118.2
2510.1
2902

If we want to have the forecast side by side with the original Y, we can just add the Y into the query as:

SELECT c_intercept()+c_slope()*x AS 'Y Forecast', y AS 'Y Original' FROM simreg;

This will produce the following output:

Y ForecastY Original
1334.41234
1726.31768
2118.22234
2510.12555
29022800

Likewise, if we want to get the forecast error, we can just modify the query a bit to deduct the 'Y Original' with the 'Y Forecast' as follows:

SELECT c_intercept()+c_slope()*x AS 'Y Forecast', y AS 'Y Original', (c_intercept()+c_slope()*x) - y AS 'Y Error' FROM simreg;

This will produce the following output:

Y ForecastY OriginalY Error
1334.41234100.40000000000009
1726.31768-41.700000000000045
2118.22234-115.80000000000018
2510.12555-44.90000000000009
29022800102


D.  Calculating the Mean Absolute Difference

We can calculate the Mean Absolute Difference (MAD) as a means to compare the "Simple Regression Method" with other forecasting methods.  The SQL command to get the MAD is:

SELECT avg(abs((c_intercept()+c_slope()*x) - y)) AS 'MAD' FROM simreg;

Which produces the output:

MAD
80.96000000000008


E.  Calculating the Root Mean Square Error

As an alternative the MAD, we can also get the Root Mean Square Error (RMSE) of the method.  The SQL command is:

SELECT SQRT(AVG(POWER((c_intercept()+c_slope()*x - y),2))) AS 'RMSE' FROM simreg;

Which produces the output:

RMSE
86.77407446927921


F.  Coefficient of Determination

The Coefficient of Determination, also commonly referred to as R-Squared, is the percentage of the variation in the dependent variable (Y) that can be explained by the independent variable (X).  The higher percentage and closer the value to 1, the better.  To be precise, R-Squared is the Regression Sum of Squares (RSS) over the Total Sum of Squares (TSS).

To calculate R-Squared, the following SQL functions needs to be created to facilitate the query.

a) Mean of X function

DELIMITER |
CREATE FUNCTION c_avex() RETURNS DOUBLE
BEGIN
DECLARE myres DOUBLE;
SELECT avg(x) INTO myres FROM simreg;
return myres;
END |
DELIMITER ;

b) Mean of Y function

DELIMITER |
CREATE FUNCTION c_avey() RETURNS DOUBLE
BEGIN
DECLARE myres DOUBLE;
SELECT avg(y) INTO myres FROM simreg;
return myres;
END |
DELIMITER ;

c)  Regression Sum of Squares (RSS) function

DELIMITER |
CREATE FUNCTION c_rss() RETURNS DOUBLE
BEGIN
DECLARE myres DOUBLE;
SELECT sum(power(((c_intercept()+c_slope()*x) - c_avey()),2)) INTO myres FROM simreg;
return myres;
END |
DELIMITER ;

d)  Total Sum of Squares (TSS) function

DELIMITER |
CREATE FUNCTION c_tss() RETURNS DOUBLE
BEGIN
DECLARE myres DOUBLE;
SELECT  sum(power(y-c_avey(),2)) INTO myres FROM simreg;
return myres;
END |
DELIMITER ;


e)  R-Squared function

DELIMITER |
CREATE FUNCTION c_r_squared() RETURNS DOUBLE
BEGIN
DECLARE myres DOUBLE;
SELECT c_rss()/c_tss() INTO myres; 
return myres;
END |
DELIMITER ;

After creating all these SQL functions, R-Squared can be calculated by issuing the following command:

SELECT c_r_squared() AS 'R-Squared';

This produces the following output:

R-Squared
0.9760733246803284


G.  Correlation Coefficient

The Correlation Coefficient is designated in Statistics by the symbol R and this is for a good reason.  It means that if we have already the Coefficient of Determination, which is designated by R-Squared, we can readily derive R by just squaring the value of the R-Squared.  Thus, we can create the following function in SQL to get R:

DELIMITER |
CREATE FUNCTION c_r() RETURNS DOUBLE
BEGIN
DECLARE myres DOUBLE;
SELECT sqrt( c_r_squared() ) INTO myres; 
return myres;
END |
DELIMITER ;


After creating the c_r() function, we can get the Correlation Coefficient by issuing the command:

SELECT c_r() AS 'Correlation';

This produces the output:

Correlation
0.9879642448615398


H.  Calculating the F Statistic

The F Statistic is the difference between the regression mean square (RMS) and the error mean square (EMS).  Since the RMS is equal to the regression sum of square (RSS), we just need to create a function to derive the EMS as follows:

DELIMITER |
CREATE FUNCTION c_ems() RETURNS DOUBLE
BEGIN
DECLARE myres DOUBLE;
SELECT sum(power(y-(c_intercept()+c_slope()*x),2)) / (count(x)-2) INTO myres FROM simreg; 
return myres;
END |
DELIMITER ;

After this, the function that will create the F Statistic can be created as:


DELIMITER |
CREATE FUNCTION c_f_stat() RETURNS DOUBLE
BEGIN
DECLARE myres DOUBLE;
SELECT c_rss()/c_ems() INTO myres; 
return myres;
END |
DELIMITER ;

Then, the following command to calculate the F Statistic can be invoke:

SELECT c_f_stat() AS 'F Statistic';

This produces the following output;

F Statistic
122.38319782621953


I.  The Student's t Statistic

In a simple regression equation, the relation between t and F statistic is that the square of the t Statistic is equal to the F Statistic.  Therefore, if we got the F, the t can be derived by just getting the square root of F.  This is undertaken by the following function:

DELIMITER |
CREATE FUNCTION c_t_stat() RETURNS DOUBLE
BEGIN
DECLARE myres DOUBLE;
SELECT sqrt(c_rss()/c_ems()) INTO myres; 
return myres;
END |
DELIMITER ;

The Student's t Statistic can be calculated by issuing the following command:

SELECT c_t_stat() AS 't Statistic';

This produces the following output:

t Statistic
11.062693967846148


J.  Putting it all together

To put it all together, we can create the procedure:

DELIMITER |
CREATE PROCEDURE c_simple_regression()
BEGIN
SELECT c_intercept() as 'Intercept',c_slope() as 'Slope',c_t_stat() as 't Stat',c_r_squared() as 'R Squared',c_r() as 'Correlation', c_f_stat() as 'F Stat';
END |
DELIMITER ;

and the simple regression statistics can be calculated by issuing the command:

CALL c_simple_regression();

which produces the output:

InterceptSlopet StatR SquaredCorrelationF Stat
942.5391.911.0626930.9760730.987964122.383197


Note:

The above-mentioned procedure has been tested in MariaDB 10.3 database.  My guess is, it should also work with MySQL, PostgreSQL and other SQL databases with little modification and with some differences in precision mathematics.



Monday, April 6, 2020

Bringing to Life Dead VRLA/Gel Battery

VRLA (Valve Regulated Lead Acid)/Gel Type batteries can be reasonably  brought to life from zero volts to full voltage capacity at 13.1v for a 12v rated battery by charging the battery continuously for approximately 1 1/2 weeks.   Rocket (Made in China) is the brand of VRLA/Gel battery I have used.  My own test was undertaken on 4 VRLA/Gel battery I own.  The charging voltage tested is 6 amps at 15v using automatic charger manufactured by Panther Philippines.

Unfortunately, the same procedure undertaken on a Motolite (Made in the Philippines) Solar Battery, a flooded deep cycle  battery, does not work.  At most, the Motolite battery only registers between 9v to 10v, after 2 weeks (Tested on 2 Motolite Batteries).

Notes on Lead Acid Battery

1.  Positive and Negative:  Lead Plates
2.  Separator:  Polyolefin
3.  Electrolyte:  Sulfuric Acid
4.  Charging time (Single Cell):  24 hrs at 5v (Creates Lead Dioxide in the Positive Plate
5.  Voltage Reading: 1.8 (Based on this; it will need 7 cells to create 12.6v or 8 cells which is preferred to create 14.4v for home solar use)

Note:
-  The battery cell still got very high internal resistance, most probably on the use of solid Polyolefin separator; a separate test will be undertaken to determine the cause of very high internal resistance