Tuesday, June 11, 2019

How to Make File undeletable Even By Root in Linux ?

On Unix-like operating systems including Linux, root is the account or user name that by default can
modify all directories and files on a system. In this article, we’ll show how to make directories or
files unremovable even by the root user in Linux.

To make a file undeletable by any system user, including root, you need to make it unmodifiable
using using the chattr command. This command changes file attributes on a Linux file system.

How to Make File Undeletable in Linux

The command below makes test.txt file immutable (or undeletable). This implies that the file can’t
be modified in any way: it can’t be deleted or renamed. You can’t even create a link to it and no data
can be written to the file as well.

Note that you need superuser privileges to set or remove this attribute, using the sudo command:

chattr - change file attributes on a Linux file system

DESCRIPTION
      chattr changes the file attributes on a Linux file system.

      The format of a symbolic mode is +-=[acdeijstuACDST].

      The operator `+' causes the selected attributes to be added to the existing attributes of the files;
`-' causes them to be removed; and `=' causes
      them to be the only attributes that the files have.

      The letters `acdeijstuACDST' select the new attributes for the files: append only (a), compressed
(c), no dump (d), extent  format (e), immutable (i), data  journalling (j),  secure deletion (s), no
tail-merging (t), undeletable (u), no atime
updates (A), no copy on write (C), synchronous
      directory updates (D), synchronous updates (S), and top of directory hierarchy (T).

      The following attributes are read-only, and may be listed by lsattr(1) but not modified by chattr:
huge file (h), compression  error (E), indexed
      directory (I), compression raw access (X), and compressed dirty file (Z).

OPTIONS
      -R Recursively change attributes of directories and their contents.

      -V Be verbose with chattr's output and print the program version.

      -f Suppress most error messages.

      -v version
             Set the file's version/generation number.



$ sudo chattr +i test.txt
OR
$ sudo chattr +i -V test.txt

Output:
sudo chattr +i -V test.txt
chattr 1.42.9 (4-Feb-2014)
Flags of test.txt set as ----i--------e--



To view attributes of a file, use the lsattr command as shown.

$ lsattr test.txt

Output:

lsattr test.txt
----i--------e-- test.txt




Now try to remove the immutable file, both as a normal user and as a root.

$ rm test.txt
$ sudo rm test.txt

Output:

sudo rm -rf test.txt
rm: cannot remove ‘test.txt’: Operation not permitted

use -i sign to remove the attribute

sudo chattr -i -V test.txt
chattr 1.42.9 (4-Feb-2014)
Flags of test.txt set as -------------e--

Now we can delete the file.

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