Linux is being an open-source operating system that offers several important features that make it a popular choice for many users.
In this article, we will cover Linux advanced topics like Sudo Privileges, Service Management, Package Management, Remote Access using SSH, File Transfer, and Text Processing.
What is Process?
The process is an Instance of a particular executable running. An application may have multiple processes running simultaneously.
$ ps
--------------------------->Check running processes.
What is Daemon?
A Computer program constantly running in the background that triggers an action when receiving certain input. Examples of daemons:
cron: a daemon that schedules and runs a command on a predetermined schedule.
sshd: a daemon that provides secure shell (SSH) access to a Linux system.
syslogd: a daemon that collects and stores system logs from various sources.
What is Service?
Service is a program that runs in the background and provides specific functionality to other programs or users. Services can be thought of as a type of daemon that provides specific functionality to other parts of the system. Examples of Services:
Apache: a web server service that serves web pages to clients over HTTP.
MySQL: a database service that provides data storage and retrieval services to applications.
Nginx: a web server and reverse proxy service that serves web pages and load balances incoming requests.
Service statuses- Active, Inactive, Enabled, Disabled
What is Systemctl?
Systemctl is a command-line utility used in Linux-based operating systems to control and manage system services and daemons.
With systemctl, you can start, stop, restart, enable, disable, or query the status of a particular service or daemon on your system.
Syntax: systemctl [options] [service/daemon_name]
Options: start, stop, restart, enable, disable, status
systemctl start cron
------------->Starts the cron service
systemctl is-enabled sshd
------------->Determine sshd service is enabled
systemctl is-enabled cron
------------->Determine cron service is enabled
What is Sudo?
sudo
is a command used in Unix and Unix-based operating systems like Linux. This command is often used when a normal user needs to execute a command that requires root privileges.
By using sudo
, the user can temporarily elevate their privileges and execute the command with root permissions.
sudo
\= superuser + do
Provide sudo privilege to a user->
Edit the configuration file /etc/sudoers, Just add the below line.
vim /etc/sudoers
root ALL=(ALL:ALL) ALL
user1 ALL=(ALL) ALL
[Add this line]
:wq
Provide sudo privilege to a group->
Edit the configuration file /etc/sudoers, Just add the below line.
vim /etc/sudoers
root ALL=(ALL:ALL) ALL
%krishiva ALL=(ALL) ALL
[Add this line]
:wq
All members of krishiva group got the sudo privileges
Wheel Group ->
A wheel is a system group that by default has sudo privileges, adding any user in this group gets sudo privileges.
grep wheel /etc/group
------------->Check if the group wheel exists.
gpasswd -a chaitu wheel
------------->Add user to the wheel group.
User chaitu
got sudo privileges just by being a member of the wheel group.
Use sudo commands without getting asked for the password->
Edit the configuration file /etc/sudoers
vim /etc/sudoers
chaitu ALL=(ALL) NOPASSWD:ALL
%wheel ALL=(ALL) NOPASSWD: ALL
Now for using package update commands user chaitu
won't get asked to provide the password.
Package Management
Package in Linux means a compressed file archive containing all the files that come with a particular application.
Package Architecture:
Name_version.release architecture extension
For example: apache2_2.4.41-4ubuntu1.1_amd64.deb
Package Installation
Standalone installation:
A standalone installation involves installing the operating system directly from a local source such as a CD or USB drive.
rpm [option] [package_name]
[options]:
i
------------->for installv
------------->for verboseh
------------->for hashese
------------->for eraseq
------------->for queryNetwork installation:
The network installation process is carried out remotely, and the system files are downloaded from the network location to the target machine.
yum or apt-get [option] [package_name]
[options]:
install, remove, upgrade, groupinstall, groupremove, grouplist, clean all, list.
Remote Access in Linux
Remote access in Linux can be achieved through Secure Shell (SSH). SSH is a protocol used to securely access remote computers (Linux/unix-like servers) using CLI. SSH has default port 22 and is a secure and popular way of remote access in Linux.
ssh -i ["ssh_private_key.pem"] [username@public_ip_address]
Remote File Transfer
To transfer files from a local machine to a remote Linux server and vice-versa, we can use a variety of tools including:
scp
SCP (Secure Copy) CLI tool for transferring files from a local to a remote server or vice versa. SCP uses SSH protocol.
- Local to remote
scp -i ["ssh_key.pem"] [filename] [root@<ip_address>:/location]
------>[ for file ]
scp -i ["ssh_key.pem"] -r [foldername] [root@<ip_address>:/location]
->[ for direct. ]
- Remote to local
scp -i ["ssh_key.pem"] [root@<ip_address>:/mnt/file.txt] [/location]
------>[ for file ]
scp -i ["ssh_key.pem"] -r [root@<ip_address>:/mnt/folder/] [/location]
->[ for direct. ]
rsync
'rsync' is a CLI tool to copy and synchronize files and directories remotely as well as locally in Linux/Unix.
Remote to local
rsync -rvh -e "ssh -i [ssh.pem]" [root@<ip_address>:/filename] [/location]
Local to remote
rsync -rvh -e "ssh -i [ssh.pem]" [/filename] [root@<ip_address>:/location]
Text processing commands:
grep
(global regular expression print)'grep' is a command-line utility in Unix-based operating systems that searches for a specific pattern of characters in a file or files.
( Regular Expressions = Special characters which help search data, matching complex patterns )
Syntax:
grep [option] [pattern] [files]
[options]:
-i
------------->case insensitive-r,-R
---------->search recursively-v
------------->invert match-l
------------->list files that match the pattern-L
------------->list files that Do not match the pattern-n
------------->prefix each line of output with the line number-A num
--------->print num lines after matching-B num
--------->print num lines before matching
Commands | Description |
| Search for pattern inside file |
| Search for pattern inside multiple files |
| Search for case-insensitive word |
| Check for pattern recursively in all files/folders |
| Inverting string match |
| Displaying string match total lines count |
| Display filenames having the pattern |
| Display filenames that are not having the pattern |
| File with pattern and its count |
| Lines that start with the pattern |
| Lines that end with the pattern |
| Search pattern recursively and stores output inside the new file |
| Use of OR to check either of two patterns presence |
| Use of AND to check both two patterns presence |
find
The 'find' command is used to search and locate the list of files and directories based on the conditions you provide. Conditions can be permissions, users, groups, file types, date, size and other possible criteria.
Syntax:
find [ path ] [ condition ] [ attribute ]
Commands
Description
find [ path ] -name [filename]
Find files with a filename
find [ path ] -perm 644
Find files with the given permission
find [ path ] -perm 4755
Find files with suid permissions
find [ path ] -perm 2644
Find files with guid permissions
find [ path ] -perm 1755
Find files with sticky bit permissions
find [ path ] -user root
Find files with user root
find [ path ] -user chaitu
Find files with user chaitu
find [ path ] -group root
Find files with group root
find [ path ] -group chaitu
Find files with group chaitu
find [ path ] -size -10MB
Find files with filesize less than 10MB
find [ path ] -size +10MB
Find files with filesize more than 10MB
sed
'sed' is a command-line utility for performing text transformations on a file or a stream of text. It can be used to search for specific patterns of text and replace them with new text, delete lines that match a certain pattern or modify the content of a file in various ways.
Syntax:
sed [OPTIONS] [SCRIPT] [FILE]
Substitute text in a file:
sed 's/pattern/replacement/g' [file]
This command replaces every occurrence of the pattern with the replacement from the file text. The
g flag
at the end of the command tellssed
to replace all occurrences of the pattern, rather than just the first one on each line.Delete lines that match a pattern:
sed '/pattern/d' [file]
This command deletes every line from the file that contains pattern.
Modify the contents of a file:
sed 's/\bapple\b/orange/g' [file] > [new_file]
This command replaces every occurrence of the word
apple
infile
with the wordorange
, and writes the modified text tofile
. The\b
symbols are word boundaries that ensuresed
only matches the exact wordapple
, rather than parts of other words that contain the letters "apple
".awk
'awk' is a powerful text processing tool in Linux that allows you to manipulate and analyze text files.
awk
reads the contents of the file line by line and splits it into columns.Syntax:
awk 'pattern {action}' [input_file] > [ output_file]
Let's take an example to learn:
cat > employee.txt
ajay manager account 45000
sunil clerk account 25000
varun manager sales 50000
amit manager account 47000
sunil peon sales 13000
satvik director purchase 80000
if {actions} is used to print then {print $*}
$0
------------->prints the entire line$1, $2, $3
----->prints 1, 2, and 3 columnsNR
------------->no of lines recordedNF
------------->no of fields or columns in a row$ awk '/manager/ {print}' employee.txt
ajay manager account 45000
varun manager sales 50000
amit manager account 47000
$ awk '/manager/ {print NR, $1, $4}' employee.txt
1 ajay 45000
3 varun 50000
4 amit 47000
$ awk '/manager/ {print NR, $1, $4, ("Columns=" NF)}' employee.txt
1 ajay 45000 Columns=4
3 varun 50000 Columns=4
4 amit 47000 Columns=4
$ date | awk '{print "Date:" $3 "-" $2 "-" $6}'
Date: 29-Mar-2023
xargs
xargs is a command which can be used to build and execute commands from standard input.
Importance :
Some commands likegrep
can accept input as parameters, but some commands accepts arguments, this is place where xargs came into picture.Syntax :
xargs [options] [command]
[options]
-a file
----->read items from file instead of standard input
-p
----->prompt the user about whether to run each command line and read a line from the terminal.
-r
----->If the standard input does not contain any nonblanks, do not run the command
-x
-----> exit if the size is exceeded.Let's take an example to learn:
Here we are passing two files with
*.txt
extension as an argument tosed
command to replace a pattern of characters throughxargs
command.$ sudo find /var/log -type f -name "*.log" | xargs grep -i "CRON.*Script.sh" > Report.txt
Above command helps to search the history about cronjob run by user with the specific name of the script. And you can generate a report for that event.
Summary:
In this blog, we covered some Linux advanced features like Sudo Privileges, Service Management, Package Management, Remote Access (SSH), File Transfer, and Text Processing in Linux. I hope this blog has been informative and helpful to you.
Stay tuned for my next blog on "Linux Shell Scripting". I will keep sharing my learnings and knowledge here with you.
Let's learn together! I appreciate any comments or suggestions you may have to improve my Linux blog.
Thank you,
Chaitannyaa Gaikwad