Advanced Linux Commands For Administrator Management

Advanced Linux Commands For Administrator Management

Managing of different user and users group, various search command, granting permission, understanding various process running in your system

ยท

8 min read

The last time I have we have learned about DevOps terminology and some of the most used basic commands in Linux. If you have not checked it out do take a look at the blog here. because this blog is going to be covering Linux in an advanced section

Connecting to Linux shell to different end system

One of the best features of Linux is that the shell can be connected remotely to different end systems. To enable it we use SSH (Secured Shell Hosting).

I am going to connect my EC2 instance from the cloud to my local system running on WSL. Before Connecting the machine, I need to have the Pair Key which I have created and download using creating my instance. You can follow up the steps for SSH below

  1. Create a key pair

  1. Download it to your local system

  2. Locate to PEM file and Run the below command

     chmod 400 my-linux-server.pem #provides only read access to user
     ssh -i "my-linux-server.pem" ubuntu@ec2-107-23-107-217.compute-1.amazonaws.com
    
  3. Successfully connected ๐Ÿ™Œ

    Now we have connected to our EC2 instance shell from our local system

    After being connected, we can start to do tweaks and change to the machine as per our wish and requirement ๐Ÿง.

Creating a user & Adding them to a group

The key feature of Linux is multiuser and to get most of the benefit from these, we are going to use the user group and useradd commands. When working in a team each team has its own requirement and use case and to meet their requirement the group is been used. Hence it will be easier to manage and update any permission from the user

Let's have a Look ๐Ÿ‘€, How to achieve the above task by doing a hands-on session ๐Ÿ’ป

  • To create a user group, follow the below command

       sudo useradd younus-devops -m #add user with directory present in home
    

    Hence you will be getting the output as the above image. Now the question arise, how do I switch to user younus-devops, hence setup the password of user

       sudo passwd younus-devops # sets password of user: younus-devops
       su younus-devops #change current user to younus-devops
    

    you will be getting output same as above image shown

  • To add multiple users or append the already existing user to a particular group, use the below command

    First we will create a group and later append user in it

      sudo groupadd devops #create user group devops
      sudo gpasswd -a younus-devops devops #append younus-devops to group devops
      sudo gpasswd -M younus-devops,younus-dev devops #add multiple user on the go
    

To view our newly create group and user we need to view the follow bellow command

sudo cat /etc/passwd #check the user info
sudo cat /etc/group #check the group and user add into it

Understanding about "sudo" user

The sudoers file is a file Linux and Unix administrators use to allocate system rights to system users.

sudo vim /etc/sudoers #used to view admin permission

If we see 'sudo' is a super do user which has the same properties like the root user in the present in the Linux.

We can add a new user to the same file to add up same permission like the root user

Searching a Pattern in Linux

Grep, find & awk are some of the famous commands to find text or pattern in a given file or system

Grep

grep stands for Global Regular Expression Pattern. It is used to search a pattern or group of words from a given text file. It is mostly used in finding the exact words in a file.

To search for a string in a file, use the following command:

#Syntax for grep command
grep "string" filename
#Syntax to find recursively in the directory
grep -r "string" directory

Example of Grep command

grep -i "devops" test.txt #used to search devops with ignore case sensitive

Find

find the command is used to search for files or directories in a specified location based on various criteria such as name, size, type, and time modified.

#To find all text files (ending with .txt) within the current directory and its subdirectories, use the following command:
find . -type f -name "*.txt"

#To find files modified within 24 hours, use the following command:
find . -type f -mtime -1

Example of find from above example

AWK

awk stands for Aho, Weinberger, and Kernighan who created the language. awk is a versatile command-line tool for text processing, which can perform various operations like searching, finding and replacing, filtering, and more.

awk features user-defined functions, multiple input streams, TCP/IP networking access, and a rich set of regular expressions.

#To print a specific field (i.e, word or column) from a file, use the following command:
awk '{print $1}' filename

#To print lines starting with a particular character, use the following command:
awk '/^DevOps/' filename

Example of awk using the above command

#Mainly AWK uses for tracing file
awk '{print $3}' tracefile.txt

awk '{print $4}' tracefile.txt

awk '/GET/{print $4}' tracefile.txt

These are some basic examples of grep, find, and awk commands in Linux. There are many more options available for these commands, and you can find them in the respective manuals (man grep, man find, man awk)

Grant Permission to File or Groups

Give Permission to files or directories is been critical, especially when working within a large organization hence to manage the permission of the File and Group we are going to use the chmod, chown, ACL

chmod

chmod stands for "change mode" and is used to change the permission modes on files and directories. There are three basic permission modes: read, write and execute, represented by the letters r, w, and x, respectively.

chmod Cheatsheet : r/linux

To give read, write and execute permission to the owner and group members of a file, use the following command:

Tip: Just Remember read,write,execute number for giving absolute file permission

4 => Read Permisssion

2 => Write Permission

1 => Execute Permisssion

0 => No permission

#To give read, write and execute permission to the owner and group members of a file, use the following command:
chmod 770 filename

Here, 7 refers to read, write, and execute permission to the owner, 7 refers to read, write, and execute permission to the group members, and 0 refers to no permission to others.

If you run the above commands you will be getting corresponding output as follows

chown

chown is short for "change owner" and is used to change the owner or group of a file or directory.

#To change the owner of a file to user1, use the following command:
sudo chown user1 filename

#To change both the owner and group of a file at the same time, use the following command:
sudo chown user1:user1group filename

ACL - access control list

acl stands for "access control list" and allows you to grant permissions to individual users or groups for a specific file or directory beyond the basic permission modes set by chmod.

#To grant read and write permission to user1 on a specific file, use the following command:
setfacl -m u:user1:rw filename

#To grant read and execute permission to group1 on a specific directory, recursively, use the following command:
setfacl -R -m g:group1:rx directoryname

#TO get current permission of the file in a clean manner
#getfacl demo.txt

#sets only read and write permission to devops group
setfacl --modify group:devops:rw demo.txt

These are some examples of how you can use chmod, chown, and acl commands to grant permission to files and directories in Linux. However, it is crucial to use these commands carefully as incorrect usage can expose sensitive files to security threats.

Understanding package manager commands and usage in Linux

systemctl

systemctl is a command-line utility in Linux used to control the state of the systemd system and service manager. The systemctl command can be used to manage system services, start and stop services, manage system targets and check the status of system services.

Here are some commonly used systemctl commands in Linux:

1. Start a service

To start a service, use the following command:

sudo systemctl start service-name

Replace "service-name" with the actual name of the service you want to start.

2. Stop a service

To stop a running service, use the following command:

sudo systemctl stop service-name

Again, replace "service-name" with the actual name of the service you want to stop.

3. Restart a service

To restart a running service, use the following command:

sudo systemctl restart service-name

4. Enable or disable a service at boot

To enable a service to start automatically at boot, use the following command:

sudo systemctl enable service-name

To disable a service from starting automatically at boot, use the following command:

sudo systemctl disable service-name

5. Check the status of a service

To check the status of a running service, use the following command:

sudo systemctl status service-name

This command will show you whether a service is running, stopped, or has encountered any other issues.

6. List all running services

To list all the running services on your system, use the following command:

sudo systemctl list-units --type=service --state=running

These are some common systemctl commands in Linux that can help you manage system services and system targets. In summary, systemctl is an essential command-line utility that users can employ to manage system services and simplify their Linux experience.

Conclusion

Great, Thank you for reading my blog post till the end! We have covered some advanced topics in Linux like connecting to a remote machine using SSH, creating users and adding them to groups, Granting File Permission, searching for patterns using grep, find, and awk commands, and understanding Systemctl and package manager commands This is a critical aspect of managing a Linux system, and it's something that you'll need to know as a DevOps engineer.

I would like to thank Shubham Londhe for the amazing series on Linux. Do check it, if you are a beginner,

giphy.gif

If you find my blog useful or need any suggestions to improve my blog, then Feel Free to connect with me on LinkedIn and Twitter ๐Ÿ™Œ

And Until Next Time
See you in the Cloud โ˜๏ธ

ย