Basic Linux commands


This series is for people who love to enter the world of Linux. With the introduction of microservices, a lot of people started to show interest to learn Linux.


Here we are not covering any advanced topics of Linux. It is for the people who are stepping into the world of Linux. Especially for those who wanted to learn Linux as a stepping stone for Kubernetes.


Most of the system administrators work remotely by accessing the system through ssh or telnet. If you are using a windows desktop to connect the remote system most common method is through putty.


We will see what information we may require once get into the a Linux system.


  • "tty" Reveals the current terminal.

  • "whoami" Reveals the currently logged-in user

  • "who am i" Almost the same as the above command. This command shows the session information. We are discussing more on this later in this module.

  • "su" Switch user. Change the effective user id and group id to that user you given after the "su" command.

Note:- the usage of “-” switches the user along with its environment variables.

  • "who" The command "who" is similar to "who am i". But list the information about all the user sessions in the system.

  • "w" Command shows session information along with the overall server "Load average" and a few other details.

  • last Command shows the historical data of logged in user sessions etc. This command fetches data from "/var/log/wtmp". Don't try to view or edit this file. It stores the data in binary format.

  • "which" Reveals wherein the search path the program is located. In simple words, it shows the full path of the command we gave.

  • "uname" It gives information about the system and kernel.

            • uname -n → Displays the node name/hostname.

            • uname -r → It tells about the current running kernel.

            • uname -a → gives lots of information which we are discussing later.

  • echo Prints the string to the standard output.

    • Example: echo “Hello, welcome to the world of Linux."


Some of the above commands need a bit detailed explanation to understand their proper usage. We will discuss it with the help of some examples.


"whoami" and "who am i"

At first look, both the above commands may look the same in appearance and during execution. If you carefully look into it with the help of some other commands; You will find the actual difference. Before going into it, we will go through another example.


Switch user ( su )

The above command prompt for the password. After the successful authentication, it switches from the current user to the new user.

Syntax

su - < user name >

Things to keep in mind while using this command.

If you are not giving the user name after “su”, it still prompts for the password. It is looking for the “root” user password. Most of the administrators use this method to switch to “root”.

You may find some differences in the working environment while using the “su” and direct login. If you are using "su" alone, then the user environment will not change. It means the path and the other custom variables are still taking from the previous user. To avoid this, use the su a hyphen.

su < user name> switches the user along with its environment variables.

Note: the usage of “-” switches the user along with its environment variables


The actual difference between "whoami" and "who am i."

We were discussing the difference between “whoami" and "who am I”, we will come back to the discussion.


$ who am i

rajith pts/0 2016-02-13 18:58 (:0)

It reveals the current logged in user along with session information.

[rajith@oc8863382021 ~]$ whoami

rajith

It shows the user name alone, but this is not the actual difference.

We will switch from the user "root" and see what is displaying there.

[rajith@oc8863382021 ~]$ su -

Password:

-bash-4.1# who am i

rajith pts/0 2016-02-13 18:58 (:0)

-bash-4.1# whoami

root

-bash-4.1#

Even if the effective user is the root "who am i” still shows the session information.

  • The command “whoami’ shows the effective user.

  • "who am i” still shows the session information.

Hope it is clear. We will see some more useful commands about user logging.


"who" and "w"

The command “who” is the same as "who am i". But "who" list the information about all the login sessions in the system.

The "w" command shows session information along with the overall server "Load average".


Here is the output took from one system almost same time. We will have a look at it.

-bash-4.1# who

rajith tty1 2016-02-13 18:45 (:0)

rajith pts/0 2016-02-13 18:58 (:0)

-bash-4.1# w

22:17:58 up 3:34, 2 users, load average: 0.13, 0.11, 0.09

USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT

rajith tty1 :0 18:45 3:34m 2:44 0.18s pam: gdm-passwo

rajith pts/0 :0 18:58 0.00s 0.24s 0.94s gnome-terminal

-bash-4.1#