Wednesday, May 29, 2019

How to Remove Empty Lines from File in linux?

Method 1 – Using sed

Sed is an stream editor. We can easily remove all blank lines using sed command. Use one of
following sed command to remove blank lines from file. For example main.txt is your original file
from which you need to remove blank lines.


Below command will remove all blank line and save content in second file out.txt. It will not affect
the original file.


# sed '/^$/d' main.txt > out.txt
Now if you want to make changes in original file using -i switch sed command.


# sed -i '/^$/d' main.txt
-i ( edit files in place ) Used for make changes in same file.


Method 2 – Using perl

Instead of sed, you can also use perl (a programming languege) to remove blank lines. Use the below
example command to remove blank lines from main.txt file.


# perl -i -n -e "print if /S/" main.txt


Method 3 – Using awk

Also you can use AWK command line tool to remove blank lines from a file. For example use below
command.

# awk 'NF > 0' main.txt > out.txt

Monday, May 27, 2019

How to reset root MySQL password on Ubuntu 18.04 Bionic Beaver Linux?

After Installing MySQL on Ubutnu 18.04, Fix “Access denied for user ‘root’@’localhost'” Error?


How to reset root MySQL password on Ubuntu 18.04 Bionic Beaver Linux?

Recently, I came across an issue when installing mysql-server on Ubuntu 18.04. What I found is that
after I installed mysql-server using my standard approach of sudo apt install mysql-server, and after
running mysql_secure_installation, my root user was denied access to MySql when trying to access
with mysql -u root -p. I would always get the following error:


ERROR 1698 (28000): Access denied for user 'root'@'localhost'


Fixing Access denied for user ‘root’@’localhost’ Ubuntu
After hours of trying to come up with the right solution, this is what I did to fix:


Stop MySQL Server from running:


sudo service mysql stop


We need to create a directory for MySQL to store access socket files so that we may use the mysqld
command. Without this directory, the command will fail.


sudo mkdir -p /var/run/mysqld
sudo chown mysql:mysql /var/run/mysqld


Manually start MySQL using the following command:


sudo /usr/sbin/mysqld --skip-grant-tables --skip-networking &


The command we just ran will allow us to now access MySQL without having a password. For what
ever reason, when MySQL was installed, the password we installed it with did not get applied to to
the root user. Now you can manually change your password using the following `mysql` commands



Log in to MySql without a password:


mysql -u root


Flush Privileges to apply the pending privileges to the root user created during the installation process:


mysql> FLUSH PRIVILEGES;


Update the password of the root user using the three following commands:


mysql> USE mysql;
mysql> UPDATE user SET authentication_string=PASSWORD("{PASSWORD_HERE}") WHERE User='root';


mysql> UPDATE user SET plugin="mysql_native_password" WHERE User='root';


End you MySQL session:


mysql> quit


Now that you’ve successfully reset your password, you will want to terminate mysqld:


sudo pkill mysqld


Restart your MySQL Server:

sudo service mysql start

Saturday, May 11, 2019

How do I list all IRQS currently used under Linux?



An interrupt request (IRQ) is a hardware signal sent to the processor instructing it to suspend its
current activity and handle some external event, such as a keyboard input or a mouse movement. In
x86 based computer systems, IRQs are numbered from 0 to 15. Newer computers, including x86-64
systems, provide more than these 16 interrupts (usually 24). Some interrupts are reserved for
specific purposes, such as the keyboard and the real-time clock; others have common uses but may
be reassigned; and some are left available for extra devices that may be added to the system.
Here is a list of the IRQs and their common purposes in the x86 system:

irq list and purpose
There is a file called /proc/interrupts. The proc file system is a pseudo file system which is used as an
interface to kernel data structures. It is commonly mounted at /proc.

This is used to record the number of interrupts per each IRQ on (at least) the i386 architecture. Very
easy to read formatting, done in ASCII.

Display /proc/interrupts

Use cat or less command:

$ cat /proc/interrupts

Output:


Sunday, May 5, 2019

How to Install GDB?


There are two ways you can install GDB on your linux machine.

1. Install pre-built gdb binaries from verified distribution resources
You can install gdb on Debian-based linux distro (e.g. Ubuntu, Mint, etc) by following command.
$ sudo apt-get update
$ sudo apt-get install gdb

2. Download source code of GDB, compile it and install.
Follow below mentioned steps to compile GDB from scratch and install it.
Step-1: Download source code.
You can download source code of all release from http://ftp.gnu.org/gnu/gdb/

Step-2: Extract it
$ tar -xvzf gdb-8.0.tar.gz

Step-3: Configure and Compile it.
$ cd gdb-8.0
gdb-8.0$ ./configure
gdb-8.0$ make
This step will take a bit of time. So you can sit back and have cup of beer for a while.
Once it is completed, you can locate gdb binary located at gdb-8.0/gdb/gdb

Step-4: Install GDB.
$ make install
By default this will install gdb binaries in /usr/local/bin and libs in /usr/local/lib
Congratulation, you have successfully compiled and installed GDB.

Once you installed GDB, you can print GDB version to test whether it is installed correctly.

$ gdb --version

How do I kill all the processes in MySQL show processlist?

How do I kill a running query in mysql ?



We can kill the processes with the help of the ‘kill’ command. However, you need to
kill those processes one by one, since MySQL does not have any massive kill command.


To check how many processes exist, use ‘show processlist’


The following is the output.


mysql> show processlist;
+------+--------+--------------------+----------+---------+------+-------+------------------+
| Id   | User  | Host            | db | Command | Time | State | Info             |
+------+--------+--------------------+----------+---------+------+-------+------------------+
|   41 | zabbix | localhost          | zabbixdb | Sleep | 2 |   | NULL |
|   48 | zabbix | localhost          | zabbixdb | Sleep | 0 |   | NULL |
|   50 | zabbix | localhost          | zabbixdb | Sleep | 29 |   | NULL |
|   55 | zabbix | localhost          | zabbixdb | Sleep | 0 |   | NULL |
|   58 | zabbix | localhost          | zabbixdb | Sleep | 1 |   | NULL |
|   62 | zabbix | localhost          | zabbixdb | Sleep | 57 |   | NULL |
| 5280 | root   | 192.168.109.202:58608 | zabbixdb | Query   | 0 | NULL | show processlist |
+------+--------+--------------------+----------+---------+------+-------+------------------+
7 rows in set (0.00 sec)


To kill a process which have been active for more than 10 seconds, the following is the query. Here, we are killing a process with Id “41”


mysql> select concat('kill ',41,';')
  -> from information_schema.processlist
  -> where TIME > 10;


The following is the output.


mysql> select concat('kill ',41,';')  from information_schema.processlist where TIME > 10;
+------------------------+
| concat('kill ',41,';') |
+------------------------+
| kill 41;               |
| kill 41;               |
| kill 41;               |
| kill 41;               |
+------------------------+
4 rows in set (0.00 sec)
Method 2:


kill <thread_id>;


mysql> show processlist;
+------+--------+--------------------+----------+---------+------+-------+------------------+
| Id   | User  | Host            | db | Command | Time | State | Info             |
+------+--------+--------------------+----------+---------+------+-------+------------------+
|   48 | zabbix | localhost          | zabbixdb | Sleep | 3 |   | NULL |
|   50 | zabbix | localhost          | zabbixdb | Sleep | 23 |   | NULL |
|   55 | zabbix | localhost          | zabbixdb | Sleep | 4 |   | NULL |
|   58 | zabbix | localhost          | zabbixdb | Sleep | 55 |   | NULL |
|   62 | zabbix | localhost          | zabbixdb | Sleep | 51 |   | NULL |
| 5280 | root   | 192.168.109.202:58608 | zabbixdb | Query   | 0 | NULL | show processlist |
| 5355 | zabbix | localhost          | zabbixdb | Sleep | 26 | | NULL             |
+------+--------+--------------------+----------+---------+------+-------+------------------+
7 rows in set (0.00 sec)


mysql> kill 5355  ;


Query OK, 0 rows affected (0.00 sec)



you can still try the following MySQL query to kill all the processes.


mysql -e "show full processlist;" -ss | awk '{print "KILL "$1";"}'



Wednesday, May 1, 2019

How to copy files with rsync over SSH?



Install rsync


If the command is not included by default inside the server configuration we can easily add it
using the default package manager:


Cent Or Redhat:

sudo yum install rsync


Debian/Ubuntu:

sudo apt-get install rsync


Copy a file from local server to remote one:

rsync -v -e ssh /home/localuser/testfile.txt remoteuser@X.X.X.X:/home/remoteuser/transfer


In the above example we will copy a file called testfile.txt from the current server to the remote
one and will place it inside the folder /home/remoteuser/transfer.


The output should be similar to the following one:

sent X bytes  received X bytes  X.X bytes/sec
total size is X  speedup is X.X

If the remote server is configured to work with non-default SSH port (other than 22) we can specify that inside the -e option:

rsync -v -e "ssh -p2222" /home/localuser/testfile.txt remoteuser@X.X.X.X:~/transfer
Again the testfile.txt will be copied inside the /home/remoteuser/transfer folder situated on the remote server.


Copy a file from remote server into a local folder:

rsync -v -e ssh remoteuser@X.X.X.X:/home/remoteuser/transfer/testfile.txt /home/localuser/


In the above example we will copy a file called testfile.txt from the remote server inside a local folder called /home/localuser/.


Synchronize local folder on remote server:


rsync -r -a -v -e ssh --delete /home/localuser/testfolder    remoteuser@X.X.X.X:/home/remoteuser/testfolder


Synchronize folder from the remote server on the local server:

rsync -r -a -v -e ssh --delete remoteuser@X.X.X.X:/home/remoteuser/testfolder /home/localuser/testfolder

Here is a list of some of the most common rsync options:

--delete - delete files that don't exist on sender (system)
-v - verbose (-vv will provide more detailed information)
-e "ssh options" - specify the ssh as remote shell
-a - archive mode - it preserves permissions (owners, groups), times, symbolic links, and devices
-r - recurse into directories
-z - compress file data during transfer
--exclude 'foldername' – excludes the corresponding folder from transfer
-P – show progress during transfer