How to sync datetime in Centos 7 with ntp

This tutorial helps you keep your server synchronized automatically with ntpd service.

First of all, we need update our system:

sudo yum update -y

Next, install ntp:

sudo yum install -y ntp

Next, enable ntpd service run at startup

systemctl enable ntpd

Now check current status of ntpd

systemctl status ntpd

Restart ntpd if need:

systemctl restart ntpd

Check ntpd configuration at:

vi /etc/ntp.conf

Your pre-configured file will be something like this:

For more information about this file, see the man pages
 ntp.conf(5), ntp_acc(5), ntp_auth(5), ntp_clock(5), ntp_misc(5), ntp_mon(5).
 driftfile /var/lib/ntp/drift
 Permit time synchronization with our time source, but do not
 permit the source to query or modify the service on this system.
 restrict default nomodify notrap nopeer noquery
 Permit all access over the loopback interface.  This could
 be tightened as well, but to do so would effect some of
 the administrative functions.
 restrict 127.0.0.1
 restrict ::1
 Hosts on local network are less restricted.
 restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap
 Use public servers from the pool.ntp.org project.
 Please consider joining the pool (http://www.pool.ntp.org/join.html).
 server 0.centos.pool.ntp.org iburst
 server 1.centos.pool.ntp.org iburst
 server 2.centos.pool.ntp.org iburst
 server 3.centos.pool.ntp.org iburst

....

Now check your system date time with command:

date

How to echo current datetime in Linux bash shell

You can get curent datetime in Linux bash shell with commands as following:

var=$(date)
var=`date`

Then in the bash shell file you can print current datetime with echo command like below:

echo "Current date: $now" 

The final example looks like this:

#!/bin/bash
now=`date`
echo "[$now]Setting environment parameter ..." 

How to run scl enable rh-python36 in Linux crontab

Because scl enable rh-python36 create new virtual environment for Python, within this environment, you can use your installed Python 3.6 but this virtual environment not applied automatically while running in a crontab.

So, in order to use Python 3.6 in Linux crontab, you have to run your python files in activated new virtual environment.

My run.sh file:

#!/bin/bash
now=`date`
echo "[$now]Running script now ..."
scl enable rh-python36 "cd /opt/wdir/ && pipenv run python main.py" >> /var/log/logrun.txt
echo "[$now]All done now" 

and my crontab:

* 7 * * * /opt/wdir/run.sh > /var/log/logrun.txt 

Another way is to set scl at the crontab directly:

* 7 * * *  scl enable rh-python36 "cd /opt/wdir/ && pipenv run python main.py" > /var/log/logrun.txt 

More about Linux crontab here.

Happy working!

How to add remote to existing local git repo

When you have a local folder with source code and want to push/pull you code from an existing repository on Github, you’ll need to add remote. This command applied not only Github but most git providers: Bitbucket, Gitlab, …

Syntax:

git remote add origin https://link-to-git-repo

In this case we’ll use default remote name = origin.

What is remote name?

The remote name is a short-hand label for a remote repository. “origin” is the conventional default name for the first remote and is usually where you push to when you don’t specify a remote for git.

You can set up more than one remote for your local repo and you use the remote name when pushing to them.

How to install Python 3.6 in Centos 7 the easy way

First of all, you’ll need to update your system:

sudo yum update -y

The SCL or Software Collections project is an open-source project. Its goal is to let you install and run multiple versions of the same software on the same system. To install Python 3.6 x64, you need to install SCL:

sudo yum install -y centos-release-scl

Now install Python 3.6 x64:

sudo yum install -y rh-python36

Now enable Python 3.6 virtual environment on this machine:

scl enable rh-python36 bash

To enable it permanently add these lines at the bottom of bash profile:

To edit bash_profile.

cd ~ && vi .bash_profile

Add these lines:

#add python36 to the bash permanently
scl enable rh-python36 bash

Run this command to apply immediately:

exec bash

To development Python applications, you will need Development tools sooner or later:

sudo yum groupinstall -y "Development Tools"

To install pipenv we’ll need root privilege:

su - root
sudo yum install -y rh-python36

#Upgrade to new version of pip
pip install --upgrade pip 

#now install pipenv
pip install pipenv 

Now you can switch back to normal user and use pipenv as normal.

How to install ifconfig on CentOS 7/6 Linux

The the network command line tool ifconfig is not installed thus missing by default on CentOS 7/6. You can install with this simple command:

sudo yum install -y net-tools 

ifconfig command usage

This is a very simple command:

# Run the ifconfig command with default input
ifconfig

# Run with -a option
ifconfig -a

How to set password for redis

To set the password for redis server, edit your configuration file, by default, at “/etc/redis/redis.conf”

vi /etc/redis/redis.conf

then search this phrase: “requirepass foobared” in the config file. Then uncomment this line, change foobared to any password you need. For example:

requirepass thisIsTh3l0ngestPas2w0rd?

Now save the config file, restart redis with this command to apply changes:

systemctl restart redis

Check again to make sure redis running ok:

systemctl status redis

How to test connection to Oracle database without installing Oracle client

To test connection from Linux server to Oracle database without installing Oracle client & sqlplus. All you need is java jdk installed and a small jar file as follow steps.

The Oracle JDBC Tester ( https://github.com/aimtiaz11/oracle-jdbc-tester ) is a handy tool we’ll use to test JDBC connection from our server to Oracle database.

First download the jar file and add execute permission to it:

wget https://github.com/aimtiaz11/oracle-jdbc-tester/releases/download/v1.0/jdbc-tester-1.0.jar
chmod +x jdbc-tester-1.0.jar

Execute this command with syntax as follow:

java -jar ./jdbc-tester-1.0.jar SCHEMANAME PASSWORD jdbc:oracle:thin:@ORACLE_IP:ORACLE_PORT/ORACLE_SERVICENAME

For example:

java -jar ./jdbc-tester-1.0.jar USERNAME password123 jdbc:oracle:thin:@172.16.1.55:1521/DBORCL

Then the successfull result like this:

18:54:28.083 [main] INFO Main - arg 0 = USERNAME
18:54:28.086 [main] INFO Main - arg 1 = password123
18:54:28.086 [main] INFO Main - arg 2 = jdbc:oracle:thin:@172.16.1.55:1521/DBORCL
18:54:28.164 [main] INFO Main -  Starting JDBC Connection test *
18:54:28.455 [main] INFO Main - Running SQL query: [select sysdate from dual]
18:54:28.469 [main] INFO Main - Result of sql Query: [2019-06-07 18:19:51.0]
18:54:28.469 [main] INFO Main - JDBC connection test successful!

In case of wrong username/password, the result will like this:

18:56:01.334 [main] INFO Main - arg 0 = USERNAME
18:56:01.337 [main] INFO Main - arg 1 = password1
18:56:01.337 [main] INFO Main - arg 2 = jdbc:oracle:thin:@172.16.1.55:1521/DBORCL
18:56:01.425 [main] INFO Main -  Starting JDBC Connection test *
18:56:01.646 [main] ERROR Main - Exception occurred connecting to database: ORA-01017: invalid username/password; logon denied

How to install, configure & optimize HAProxy on Centos 7/6/5

This post show you how to install, configure & optimize HAProxy on Centos 7/6/5 to make a high performance load balancer with maximum concurrent connection to 10000.

HAProxy is a reliable, high performance TCP/HTTP load balancer. You can read more about HAProxy here at its official website.

Current stable version of HAProxy at the time of this post is 1.9 with this official documentation.

Install HAProxy

To install HAProxy you can use this command:

sudo yum install -y haproxy

To start HAProxy:

sudo systemctl start haproxy

To stop HAProxy:

sudo systemctl stop haproxy

To check HAProxy status:

sudo systemctl status haproxy

To enable HAProxy runs at startup

sudo systemctl enable haproxy

Configure HAProxy

Our network will look like that:

user -----> haproxy:9000 -------->backend1 (172.16.10.20:5000)
                         -------->backend2 (192.168.5.19:6000)

In this topology, HAProxy will listen for user request on port 9000, all request will be forwarded to 2 backends: backend1 (172.16.10.20:5000) and backend2 (192.168.5.19:6000)

By default, HAProxy main configuration file will be at: /etc/haproxy/haproxy.cfg

We need to configure HAProxy to meet our need. First, configure HAProxy frontend listener. Edit frontend listen port in the configuration file main frontend section as follow:

---------------------------------------------------------------------
 main frontend which proxys to the backends
 ---------------------------------------------------------------------
 frontend  main *:9000
  acl url_static path_beg -i /static /images /javascript /stylesheets
  acl url_static path_end -i .jpg .gif .png .css .js
  use_backend static if url_static
 default_backend app

Next we need to modify our backend apps:

---------------------------------------------------------------------
 round robin balancing between the various backends
 ---------------------------------------------------------------------
 backend app
     balance     roundrobin
     server   backend1  172.16.10.20:5000 check
     server   backend2  192.168.5.19:6000 check
     #server  app1 127.0.0.1:5001 check
     #server  app2 127.0.0.1:5002 check
     #server  app3 127.0.0.1:5003 check
     #server  app4 127.0.0.1:5004 check

Before restart HAProxy, we need to validate if our configuration file has something wrong with this command:

 haproxy -c -V -f /etc/haproxy/haproxy.cfg 

in this command:

  • -c : Check
  • -V : Verbose
  • -f : File

If everything goes well, you will see output like this:

Configuration file is valid

Now we can save our configuration file and restart HAProxy to apply changes:

sudo systemctl restart haproxy

Check status of HAProxy:

sudo systemctl status haproxy

Optimize HAProxy

First of all, we need to enable HAProxy statistics page/health check by adding those lines to the end of main configuration file at: /etc/haproxy/haproxy.cfg

---------------------------------------------------------------------
 HAProxy Stats at: 171.244.5.195:8088/haproxy?stats
 --------------------------------------------------------------------
 listen  stats   *:8088
         mode            http
         log             global
        maxconn 10     
        timeout client  100s     
        timeout server  100s     
        timeout connect 100s     
        timeout queue   100s     
        stats enable     
        stats hide-version     
        stats refresh 30s     
        stats show-node     
        stats auth admin:config91231passwd    
        stats uri  /haproxy?stats

Save the config file and restart HAProxy after validating config file as in previous section:

#validate the file
haproxy -c -V -f /etc/haproxy/haproxy.cfg  

#restart HAProxy
sudo systemctl restart haproxy 

I will explain a little about those above lines:

  • listen stats *:8088 -> HAProxy statistic page will be at port 8088
  • maxconn 10 -> maximum concurrent connection for this stats site is 10
  • stats auth admin:config91231passwd -> Authorize this stats site with username = admin and password = config91231passwd

Now you can access statistic website for this HAProxy at: http://haproxy_ip_address:8088/haproxy?stats

The website will look like this:

HAProxy statistics report website
HAProxy statistics report website

This website help you check HAProxy status more easier, faster & realtime.

Next we’ll configure HAProxy to receive more connection by change default max limit of connection.

First change max connection for backend to 5000 each by modify this line in backend section:

  server   backend1  172.16.10.20:5000 check  maxconn 5000
  server   backend2  192.168.5.19:6000 check  maxconn 5000

Next we change the default max connection of frontend at default section:

maxconn                 10000

Save the config file and restart HAProxy after validating config file as in previous section:

#validate the file
haproxy -c -V -f /etc/haproxy/haproxy.cfg  

#restart HAProxy
sudo systemctl restart haproxy