Tuesday, June 25, 2019

id command examples in linux ?

How do I find out the user and group’s names and numeric IDs of the current user or any users on
my server? How can I display and effective IDs on the system using command line options? In Linux,
how do I find a user’s UID or GID?

To find a user’s UID (user ID) or GID (group ID) and other information in Linux/Unix-like operating
systems, use the id command.

This command is useful to find out the following information:
  • Get User name and real user ID
  • Find a specific user’s UID
  • List out all the groups a user belongs to
  • Show the UID and all groups associated with a user
  • Display security context of the current user
  • Effective Linux or Unix user name and effective user ID (UID)
  • Name of effective Linux or Unix user’s group and effective group ID (GID)

Displays the system identifications of a specified user.

id command syntax
The basic syntax is:

id [UserNameHere]
id [options]
id [options] [UserNameHere]

 id --help
Usage: id [OPTION]... [USERNAME]
Print user and group information for the specified USERNAME,
or (when USERNAME omitted) for the current user.

  -a              ignore, for compatibility with other versions
  -Z, --context   print only the security context of the current user
  -g, --group     print only the effective group ID
  -G, --groups    print all group IDs
  -n, --name      print a name instead of a number, for -ugG
  -r, --real      print the real ID instead of the effective ID, with -ugG
  -u, --user      print only the effective user ID
      --help     display this help and exit
      --version  output version information and exit

Display your own UID and GID
Type the command:

Sample outputs:

uid=1000(dev) gid=1000(dev) groups=1000(dev),4(adm),24(cdrom),27(sudo)

How do I find a specific user’s UID?

In this example, find a dev user’s UID, type:
id -u {UserNameHere}
id -u dev

Sample output:

id -u dev

How do I find a specific user’s GID?

In this example, find a dev user’s GID, run:
id -g {UserNameHere}
id -g dev

Sample output:

id -g dev

How do I see the UID and all groups associated with a user name?

In this example, find the UID and all groups associated with a user called ‘root’, enter:
id {UserNameHere}
id root

Sample output:

id root
uid=0(root) gid=0(root) groups=0(root)

Find out all the groups a user belongs to  !!

In this example, display the UID and all groups associated (secondary groups) with a user called ‘dev’, run:
id -G {UserNameHere}
id -G dev

Sample output:

id -G dev
1000 4 24 27 30 46 108 124 142

How do I display real ID instead of the effective ID for specified user?

You can show the real ID for the -g, -G and -u options instead of the effective ID by passing the -r option:

id -r -g {UserNameHere}
id -r -u {UserNameHere}

### [NOTE]  -r and -G only works on Linux

id -r -G {UserNameHere}

id -r -u dev

Sample output:
id -r -u dev

Determining root privileges in a script

Linux and Unix sysadmin relates shell scripts must be run by root user. The following shell script
shows how to determining root privileges in a script:


## if root user not running this script, die with a message on screen ##
if [ $(id -u -r) -ne 0 ]
        echo "Requires root privileges. Please re-run using sudo."
        exit 1

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

      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).

      -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
$ sudo chattr +i -V test.txt

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


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


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

# 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:


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


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