📍Introduction
📚 In this 2-part blog series, we'll explore some more advanced Linux commands! After learning and mastering basic Linux commands and getting introduced to advanced Linux in the previous blog we are now ready to learn some advanced Linux commands. By the end, you'll be familiar with piping, redirection, sudoers file, some administrative commands, etc 💪. Get ready to unlock the potential of Linux commands and join me on this exciting Linux journey! 🚀
📍Piping
Piping is used to run multiple commands at a time. Using the operand |
, it is the key located above the Enter key on your keyboard. |
is called a pipe.
Syntax: command1 | command2
In the above syntax, the command1
's output will act as an input to the command on the right-hand side i.e. command2
.
Example:
#ls -l | grep script
:
The list of files inside the current directory from ls -l
has gone as input to the command grep script
. For this, we can use a pipe |
to concatenate two or more commands.
📍Redirection
Input/Output redirection:
There are many times when you don't want to see the output of any process running or after executing a command. Since DevOps engineers like to automate tasks and run the process in the background with as minimal manual interactions as possible this redirection process can help you to achieve that.
These redirections will be very helpful while writing Shell scripts.
✔Redirection symbols:
>
: It is used to redirect the output of a command.#command > file path
: Thecommand
's output will be redirected to the file to which thefile path
is provided.Example:
#echo "I am studying redirection" > /tmp/test.txt
: If test.txt is not present it will create the file and if the file already exists it will overwrite the content of the file with the command's output./tmp
directory is where we can store files or directories temporarily.#apt-get install tree > /temp/tree_install.txt
:But, if the other output is redirected to the same file then it will overwrite its content. Like this:
>> or 1>>
: It is also used to redirect the output of a command but instead of overwriting the content of the file provided, it will append the command's output to the existing content of the file.Example:
#echo "This sentence is appended to the existing content" >> /tmp/test.txt
:But if any error occurs while executing the command it will show it on the screen to redirect such an error we use
2>>
.2>>
: It is used to redirect errors while executing the command to a file.Example:
#echooo 2>> /tmp/error.log
:Therefore, >> or 1>> is used to redirect the standard output of a command, and 2>> is used to redirect errors while executing a command.
&>>
: It will redirect any kind of output to the standard output as well as errors.Example:
Try trying to redirect the error using
>> or 1>>
and to redirect standard output using2>>
It will not be redirected.
The open part of the redirection symbol is for input and the tapering/pointed part is for the output.
📍Sudo
As we know, only the root user has all the privileges to perform any kind of operation or administration. If a normal user wants to execute commands which only the root user is allowed to execute then the normal user will have to use sudo
to temporarily grant root user privileges or switch to the root user.
It is harmful if normal users can execute root user commands. Therefore, not all users are given the privilege to use this sudo command and switch to the root user. To give normal users the privilege to use the sudo
command, and switch to the root user, we have to add the users in the sudoers
file.
Let's take an example:
I have 3 users ubuntu
, aryan
, and lokesh
as you can see in the image below and I have also set the password for each one of them.
Now, I can switch to the root
user from ubuntu
user and I can also execute root
user commands using sudo
.
But, when I switch to aryan
user, I am not able to execute the sudo
command nor I can switch to the root user. It throws me an error called aryan is not the sudoers file
:
This sudoers
file is located inside the etc
directory /etc/sudoers
:
It is a read-only file for the root user, and for the root group, no permission is given to others. Therefore, to edit this file we use the command #visudo
from root user.
Traditionally, visudo
opens the /etc/sudoers
file with the vi
text editor. Ubuntu, however, has configured visudo
to use the nano
text editor instead.
If you would like to change it back to vi
, issue the following command:
#sudo update-alternatives --config editor
and then select option 3 for vim editor.
#visudo
: This command will open the sudoers file in write mode.
Then do, :se nu
to set line numbers and go to line number 44th. Copy this line and write the user you want to give the privileges to.
NOPASSWD
: This is to ask for no password while switching to the root user and executing the sudo command.
If while editing this sudoers file there is any syntax error then it will give the prompt to edit it again for that type e (to edit)
:
Important: Be careful while editing the sudoers file If you save the file with the syntax error then you will not be able to execute the sudo command and you will also not have the root password (for security reasons) and you will be stuck.
Now the aryan
user can execute sudo commands and switch to the root user.
📍Software Management/package manager
The most popularly used Linux distros in the IT industry are:
These distros are different based on how they are packaged.
RPM (Red Hat Package Manager) based: Centos, Oracle Linux, RHEL (Red Hat Enterprise Linux)
Debian-based: Ubuntu, Kali Linux
We will be looking into software management in Ubuntu:
✔APT- Advanced Packaging Tool
In Ubuntu, apt
command is used to download any software or upgrade existing software, or update the package list index or service. This apt command makes software installation easy by downloading the necessary dependencies if they are not present in the system for the software.
Only the root user and user who can execute the sudo
command can install or remove the packages. Let's see some apt commands:
#sudo apt install package_name -y
: To install a package -y is to not ask any questions during the installation.#sudo apt remove package_name
: To remove the package installed.--purge
: This--purge
option toapt remove
will remove the package configuration files as well.
#sudo apt update
: To update the local package index with the latest changes made in the repositories.The package index is essentially a database of available packages from the repositories defined in the
/etc/apt/sources.list
file and in the/etc/apt/sources.list.d
directory.#sudo apt upgrade
: To upgrade installed packages from the package repository which was updated using#sudo apt update
. This is done so that the packages in our system are up-to-date. You should always do#sudo apt update
and then#sudo apt upgrade
.
📍Systemctl - Services
There are many services that we will use as DevOps engineers, to administer those services is very important and every DevOps engineer should know.
#systemctl
: This command helps us to manage control or administer a service.
Syntax: #systemctl subcommand service
#systemctl
is a utility that can be used to know about the services using subcommand
.
List of subcommands:
#systemctl status service
: Used to check the status of the service- active or inactive.As you can see, after the output is displayed it does not go to the command line so to exit from this output press the
q
key.sshd
is a service for ssh login, we can connect to the shell using ssh because thissshd
service is active, if it was inactive we could not have been able to connect to the shell.#systemctl start service
: To start the service. Give sudo for normal users.#systemctl stop service
: To stop the service gracefully i.e. it will stop the process as well as its child processes.Sometimes, services refuse to stop gracefully for that use:
#systemctl kill service
: It will stop/kill the service but it will not stop the child processes running.#systemctl enable service
: To enable the service. It means when the system will be restarted or rebooted then this service will automatically start and run if the service is enabled.#systemctl disable service
: To disable the service.
But there are many subcommands to remember them is very difficult, for that use the trick #systemctl
+ Tab
key 2 times. Type #systemctl
with 'single' space and press the Tab
key 2 times.
📍Conclusion
Thank you for reading this blog! 📖 We've reached the end of this Advance Linux Commands blog. You are now familiar with some Linux system administration commands as well. Keep practicing these commands, and get familiar with them. 💻 Master Linux system administration and keep learning. Stay tuned for more valuable content coming your way!
If you enjoyed this blog and found it helpful, please give it a like 👍, share it with your friends, share your thoughts, and give me some valuable feedback.😇 Don't forget to follow me for more such blogs! 🌟