Linux Programmer | RHCE | RHCSA

Search This Blog

Wednesday 24 November 2021

Preserve bash history

 # Maximum number of history lines in memory

export HISTSIZE=50000

# Maximum number of history lines on disk

export HISTFILESIZE=50000
 
# Ignore duplicate lines

export HISTCONTROL=ignoredups:erasedups

# When the shell exits, append to the history file instead of overwriting it

shopt -s histappend

# After each command, append to the history file and reread it

export PROMPT_COMMAND="${PROMPT_COMMAND:+$PROMPT_COMMAND$'n'}history -a; history -c; history -r"
 
 
# Linux command history with date and time.

echo 'export HISTTIMEFORMAT="%d/%m/%y %T "' >> ~/.bash_profile
source ~/.bash_profile

Or set it permenant, add below entry into .bashrc

export HISTTIMEFORMAT="%d/%m/%y %T "

Thursday 18 November 2021

Execute custom script as command.

Create bash script, 

Create file: script.sh

#!/bin/bash 

date echo "testing" 

 

 Apply executable permission.

# chmod 0777 script.sh
 

Move script to /usr/local/bin/

# mv script.sh /usr/local/bin/

 

Now you can execute script from anywhere.

# cd /home/

# script.sh

output:

Thu Nov 18 11:05:41 IST 2021

testing


Friday 12 November 2021

record delete restriction trigger

DELIMITER $$
CREATE TRIGGER trigger_name
BEFORE DELETE
ON table_name
FOR EACH ROW
BEGIN
    if SUBSTRING_INDEX(USER(),'@',1) NOT IN ('root') THEN
        CALL cannot_delete_error;
    END if;
END
$$

Mysql database replication.

mysql replication

Prerequisites

To set up a MySQL master-slave replication, you need to have the following:

    2 VM (Virtual Machine) or VPS (Virtual Private Server) with root access.
    Working Internet.

Steps To Achieve MySQL Master-Slave Replication

For this demo purpose, you will be calling master as root@replication-master and slave as root@replication-slave.

For this demo, let’s assume the IP address for master and slave are as follows:

Master server: 1.2.3.4
Slave server: 11.22.33.44


7 Steps To Achieve MySQL Master-Slave Replication

To start setting up the MySQL master-slave replication, please follow the step-by-step guide provided below:

    Setting Up The Master
    Create A New User For Slave
    Move Data From Master To Slave
    Configure Slave Server
    Import Data Dump
    Start Slave Server
    Test MySQL Master-Slave Replication

1. Setting Up The Master


The first thing you need to accomplish in the replication process is to install and configure the master server. If you have not installed MySQL, then you can install MySQL using the following command:

 root@replication-master:~# sudo apt-get update
 root@replication-master:~# sudo apt-get install mysql-server mysql-client -y
 root@replication-master:~# sudo mysql_secure_installation

Once the MySQL installation process is completed, use the following command to edit the MySQL configuration file:

root@replication-master:~# sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf

Next, in the same file, find the line containing bind-address = 127.0.0.1 and replace that IP address with the IP address of your master replication server. So, the line will look like:
bind-address = 1.2.3.4

Next, find the following lines in the file:

server-id = 1
log_bin = /var/log/mysql/mysql-bin.log


You will see that the above lines have been commented, just uncomment these lines and exit the edit interface by clicking CTRL + X. Save the changes and restart the MySQL service for the changes to take effect.

Restart MySQL service using the following command:

root@replication-master:~# sudo service mysql restart


2. Create A New User For Slave

The next step is to create a new user for your slave server. Use the following command to create it:

root@replication-master:~# mysql -uroot -p;
mysql> CREATE USER ‘slave’@’11.22.33.44‘ IDENTIFIED BY ‘SLAVE_PASSWORD‘;
mysql> GRANT REPLICATION SLAVE ON . TO ‘slave’@’11.22.33.44 ‘;
mysql> FLUSH PRIVILEGES;
mysql> FLUSH TABLES WITH READ LOCK;


You will use the following command to know the current status of the master server:

mysql> SHOW MASTER STATUS;


MySQL Master Slave: Create a New User for Slave

This command will also tell the slave to follow the master from this position.
 

3. Move Data From Master To Slave

Now that you have marked the position, you can start moving the data from the master to the slave. You need to create a MySQL dump file to move the data. Use the following command to create the dump file:

root@replication-master:~# mysqldump -u root -p –all-databases 
–master-data > data.sql


To copy the dump file to the slave, use the following command:

scp data.sql root@11.22.33.44


Unlock the tables using the following command:

mysql> UNLOCK TABLES;    


4. Configure Slave Server

Now, all you need to do is configure the slave server and test if replication is working. Ensure MySQL is installed.
Open the configuration file in your slave server and update these lines:

root@replication-slave:~# sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf


In the same way that you did for the master server, you need to bind the IP address and uncomment those two lines for the slave server.
Now, restart the MySQL server using the following command:

root@replication-slave:~# sudo service mysql restart


5. Import Data Dump

Use the following command to import the dump file to the slave server:

root@replication-slave:~# mysql -uroot -p < data.sql


Once the data is imported, you need to stop MySQL in the slave server using the following command:

root@replication-slave:~# mysql -uroot -p;
mysql> STOP SLAVE;


You have finally imported the dump files and updated the master IP address, password, log file name, and position, to enable the master to communicate with the slave without any issues.
 

6. Start Slave Server

Next, use the “Start Slave” command to start operating the slave server.

START SLAVE;

7. Test MySQL Master Slave Replication

To test if your MySQL master slave replication works, just create a database in your master server and see if it is replicated in the slave server. If you can see the database in the slave, then it is working fine.

Create a test database in a master server called ‘sampledb’.

CREATE DATABASE sampledb;


Now login to your slave server and list the databases, and if you see the “sampledb” there, then the master slave replication process is working fine.

Login to your slave server and use the following command to list all databases:

show databases;

Monday 1 November 2021

Create custom plugin in nagios using bash script.

Nagios is an open source computer software application that monitors systems, networks, and infrastructure.

With Nagios, we can monitor host and configure alerts on the services for servers and applications. It sends alert messages to
relevant people through emails when things go wrong and then sends another alert message when things get rectified.
 
Also we can set to send SMS on mobile when things goes wrong.

In this post, make our custom plugin using bash script which is executed on every client machine using nrpe.

First, install the Nagios-plugin and NRPE on the client servers

"""
apt-get install -y nagios-plugins nagios-nrpe-server
"""

Furthermore, assume that we want to monitor the memory of the server. In order to achieve this, write a bash script memory.sh 
in the directory /usr/lib/Nagios/plugins/ as shown below

"""
#!/bin/bash
free -m | awk ‘NR==2{printf "%.2f%%\t\t", $3*100/$2 }’
[/js]
"""

Now, add the script file in the nrpe configuration file (/etc/Nagios/nrpe.cfg) on the client VPS as shown below.

"""
command[memory_bash]=/usr/lib/nagios/plugins/memory.sh -w 85 -c 90
[/js]
"""

Now on the Nagios-core server add the below command to Nagios checks in the commands.cfg file in /etc/nagios3/ directory.

"""
define command{
command_name memory_bash
command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c memory_bash
}
"""

Moving further, define a service in the configuration file to monitor the host as shown below:

"""
define service {
use generic-service
host_name i-085675f30c08376a3
service_description Custom memory checker In Bash
check_command memory_bash
}
"""


Now restart the nrpe server and nagios3 service as below

"""
service nagios3 restart ( on the nagios-core)
service nagios-nrpe-server restart ( on the client server)
"""

And you are good to go!
Your Nagios UI will be able to check the results now. Hope this will helpful and you will yourself be able to create Plugin in Nagios 
using Bash Script.


SSH not working with password after upgrade ubuntu 22.04

Issue: In recent upgrade of ubuntu 22.04 we are not able to login server with SSH password. but when we try to login with key then it allow...