Redirection_UserManagement_In_Linux

  ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ Redirection, grep, pipe, etc..

What you will Learn:

  • Redirection commands​​ (override, append text)

  • grep command, grep​​ -r, grep​​ -nr

  • printenv

  • Modify PATH variable

  • User management​​ (/etc/passwd)

  • /etc/shadow

  • adduser command

  • useradd and passwd command

  • groups command

  • Switch to root user without giving root password

  • Add a user to sudo group

Redirection commands

The ‘echo’ command prints the same text in the output that is present within the double quotes, see below. Now, instead of printing the output on the screen, you can redirect the output to a file​​ 

We use single arrow > (like seen above and below) to override the contents

If you want to append the contents, you can use double arrow >>

grep command

‘grep’ command can be used to search for a specific pattern in a file and then display the output​​ line​​ matching that​​ pattern.​​ 

The​​ piping​​ is​​ used to chain commands, syntax below.​​ 

command1​​ | another command2

Below, we​​ have​​ redirected the standard output​​ of command1​​ into the standard input of another command2. Notice that the output shows the line ​​ ​​ that has the matching pattern

We can also use the below grep command to get the same results

grep –r ‘pattern’ .​​ will search the desired pattern in each and every file​​ (starting from root directory​​ /). So below, as we can see, we have found the desired pattern​​ in a single file ‘hello.txt’​​ 

Let us add some text to testfile4.txt

Now let us search for the ‘test’ pattern, we find it at 4 places

The​​ -n switch will also tell you the line number on which the​​ pattern is matching (see the green​​ coloured​​ numbers in the output)

printenv

‘printenv’ command can be used to print all the environment variables

You can grep the value of any environment variable like shown below

Similarly

You can also use ‘echo’ command to print the value of any environment variable

Modify PATH variable

Let us suppose we want to add some path to the PATH variable shown above. Let’s say we want to add /home path to the PATH variable. To do that, we simply use export command as seen below

Now you can see that /home is appended

This is how you can update the PATH variable.

User management

As a DevOps Engineer, you might be responsible for allocating appropriate permissions to users. That is why user management is important.

Every user in linux OS will have a ‘user id’

  • Zero (0) is reserved for root user​​ (superuser, administrator);​​ 

  • 1-999 is reserved for system users;

  • 1000 and above is reserved for normal users​​ (local users)

All the usernames reside in /etc/passwd file, see below. In the below snapshot: root, daemon, bin etc are all users. The first digit denotes the user id and second denotes group id. Also notice the home directories shown​​ below


​​ 

Notice above that root user has user id as 0 and normal user has user id as 1000
.​​ 

/etc/shadow

All the encrypted passwords are stored in /etc/shadow file. To view this file, you have to​​ switch user​​ as root

Once you have viewed the above file, make sure to exit out of the root user session

You can use Ctrl+L to clear the screen or you can use ‘clear’ command as well

adduser command

‘adduser’ command is used to create a new user in linux. You have to first become the superuser to add a new user. The advantage with this command is that, thus command will also ask us to setup a password for the new user

Notice below that there is now an entry for this new user in the /etc/passwd file

Similarly there is an entry in /etc/shadow file

useradd and passwd command

As you can see below, when you create a user using ‘useradd’ command, you have to separately execute ‘passwd’ command to setup the password

groups command

A group is a collection of users. As you can see below, testuser1 belongs to testuser2 group and so on

Switch to root user without giving root password

If you have logged in and logged out at least once as root user, you can use ‘sudo’ command to switch to root user without giving the root password. As you can see below, we are now the root user!

Add a user to sudo group

You can execute ‘usermod’ command with 2 switches: -a (to append to a group) and -G (for group). So below we are adding the testuser1 to sudo group. The ‘groups’ command now shows that the testuser1 belongs to 2 groups: testuser1 and sudo

After adding a user to sudo group, you have to logout once

Next let us switch user to testuser1. Notice below that we can execute the sudo commands

As testuser1, we can also open the shadow file

Thank you for reading!

Share On

Share on facebook
Share on twitter
Share on linkedin
Share on whatsapp
Share on tumblr
Share on email

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top

Lifetime Membership Club

LIFETIME MEMBERSHIP BIG SALE - ALL LIVE COURES JUST - 7000 RS/ 99 USD
Attend All Live courses in just 7000 rs / $99 - offer ends 1st Nov 2024