The Ultimate Guide to Linux Commands for Cloud Engineers

Introduction

Hey there, all the Linux enthusiasts!

Linux command line
Linux

Ready to uncover the secrets of the Linux command line?

Today, we’re taking a fun-filled journey into the land of ‘sudo‘ – your ultimate helper for doing special stuff on your Linux computer. Then we will unlock the world of package managers followed by navigating through the file permissions and ownership commands in Linux.

No need to stress if it sounds a bit techy – will guide you through it all.

Let’s kick things off!

Sudo

In Linux, ‘sudo‘ stands for “superuser do,” and it’s your golden ticket to gain administrative powers without logging in as the almighty superuser, ‘root‘.

With great power comes great responsibility!

However, great power is not needed all the time while using the Linux environment.

Let’s understand this way.

Consider that your Linux computer is a mighty kingdom, and you’re the ruler of that kingdom.

Now, sometimes, you need to do really important stuff that only the main ruler can do. But you don’t want to be the main ruler all the time because that could be risky. So, you have a special magic word called “sudo.”

When you say “sudo,” it’s like asking for permission to do something very powerful, almost like borrowing the main ruler’s powers for a moment. It’s like saying, “Hey, computer, can I do this important thing just for a little while?”

Now, the Linux computer is smart. It knows that not everyone should have these superpowers, so it might ask you for your secret password before letting you use “sudo.”

Once you say “sudo” and give your secret password, the computer says, “Okay, you can use some of the main ruler’s powers for this one task.” You do your important thing, and then the computer goes back to normal.

So, “sudo” is like your special key to unlock powerful abilities, but you have to use it carefully. Just like you wouldn’t give your secret superhero powers to anyone, you use “sudo” only when you need to do something really special on your computer. And that’s how the magic of “sudo” works!

So be cautious and use it wisely!

The Syntax of ‘sudo’

Before we jump into the exciting examples, let’s understand how to use ‘sudo‘ with its basic syntax.

sudo [options] command

Where,

sudo: The command itself.

[options]: These are extra settings you can use to modify how ‘sudo‘ behaves. We call it ‘flags’ also.

command: The actual command you want to run with superuser privileges.

Here are some commonly used options with sudo:

-u: Executes the command as the specified user instead of the superuser (root).

Example: sudo -u john ls

This command runs the ls command as the user “john” with elevated privileges. It allows the user “john” to perform actions that require administrative access.

-s or --shell: Runs a shell with root privileges.

Example: sudo -s

This command opens a shell with root privileges, allowing the user to execute multiple commands as the superuser. It is useful when performing a series of administrative tasks.

-i: Simulates an initial login environment, setting the environment variables accordingly.

Example: sudo -i

This command simulates an initial login environment, providing the user with a shell that has the same environment variables as the root user. It is useful when you need to work in an environment that requires root privileges.

-l or --list: Lists the commands or permissions available to the user.

Example: sudo -l

This command lists the commands or permissions that are available to the user. It helps the user understand what actions they can perform with elevated privileges.

-k: Forces the user to enter their password again before executing the command.

Example: sudo -k apt-get update

This command forces the user to enter their password again before running the apt-get update command. It adds an extra layer of security to prevent unauthorized use of the sudo command.

-v: Updates the sudo timestamp, extending the password timeout period.

Example: sudo -v

This command updates the sudo timestamp, extending the period before the user is required to enter their password again. It is useful when you need to keep the elevated privileges active for an extended period.

Remember, ‘sudo‘ isn’t just a word, it’s a tool with immense power. Always double-check your commands before pressing Enter and only use ‘sudo‘ when necessary.

Now that you are the ruler of your Linux Kingdom

So as the legend goes, every kingdom has a rule book. So every Linux environment has a rule book which is called ‘sudoers’ file.

It’s like a list of who’s allowed to use sudo and what they’re allowed to do. Only the computer king, in our case this king is called the root user, can edit this rule book. But wait, there’s more! The king can also choose certain trustworthy knights, in our case these knights are the users, to help rule the kingdom. These knights are listed in the sudoers file, giving them the right to use sudo.

Great…I hope you are now able to relate to this explanation.

Once you have decided which users should be included in the sudoers file with the respective permissions, you will make the necessary changes in this file to reflect your decisions.

Each entry in the sudoers file specifies who is allowed to use the super power and what they’re allowed to do means which commands they are allowed to use.

Let’s break it down this way.

The format of assigning permissions is as below.

username ALL=(ALL:ALL) command

User Names: In the ‘sudoers‘ file, you list the usernames of your chosen users.

Permissions: For each user, you define what they’re allowed to do by using the sudo command. It’s like giving specific tasks or privileges. You can say they’re allowed to be used sudo for anything or only for certain tasks.

The format can be understood as follows.

username: This is where you write the name of the user you’re granting sudo powers to

ALL=(ALL:ALL): This is the part that determines the scope of their powers. It’s like saying, “They can use sudo anywhere, for anything.”

command: The specific task they’re allowed to perform using sudo.

Let’s see this with an example

Example 1:alice ALL=(ALL:ALL) ALL

In this example, Alice is the chosen user. The entry ALL=(ALL:ALL) indicates that Alice has the power to use sudo anywhere (ALL for all hosts) and for any user (ALL for all users). The last ALL denotes that she can use sudo for any command. This is like making Alice a universal guardian with the ability to perform any task using sudo

Example 2:alex ALL=(ALL:ALL) /usr/bin/apt-get

“alex” is the username.

The first “ALL” means Alex can use sudo on any computer.

The second “ALL” means Alex can become any user on the computer.

The third “ALL” means Alex can use sudo anytime.

"/usr/bin/apt-get" is the specific command. Alex can only use sudo to run the apt-get command for installing software.

So, this command says, “Hey, Alex, you have the power to use sudo anywhere, become anyone, and use it anytime, but only for installing software using ‘apt-get‘.”

Example 3:

carol host1=(user1:user1) /bin/echo

In this unique scenario, Carol has been granted sudo powers on a specific host (machine) called host1. Her superpower is further narrowed down to a specific user, user1, for both the source and destination users. Carol’s sole command privilege is /bin/echo. This setup offers a finely tuned level of control, allowing Carol to use sudo only for the echo command on a specific host and user.

And that’s how you shape sudo powers with the sudoers file! Whether it’s granting universal access, focusing on specific commands, or exercising intricate control, the sudoers file empowers you to tailor superpowers according to your realm’s needs.

Package management

In Linux, package management refers to the process of installing, managing, and updating software packages on your computer.

The two of the most popular package managers in Linux are YUM and APT.

Before we dive into the details of these package managers, let me explain how exactly the whole package management thing works in Linux.

Let’s use an example this way.

Consider I am throwing a pizza party. I want to have all sorts of delicious pizzas with different toppings. Now, going to the market to buy all the ingredients for each pizza would be a hassle, right?

Here’s where package management comes to my rescue!

Wishlist:

I will first start with making a Pizza Wishlist. I will make a list of all the pizzas I want for my party – Margherita, Pepperoni, Veggie Supreme, and Hawaiian.

The Supermarket (Repository):

Instead of going store to store, I have a special supermarket called a repository that has all the pizza ingredients. Each ingredient is like a software package.

My Shopping Assistant (Package Manager):

I have a shopping assistant called a package manager who knows exactly where to find each ingredient (package) in the supermarket (repository).

Fetching the Ingredients:

Then I told my shopping assistant (package manager) about my wishlist. It goes to the supermarket, grabs all the ingredients I need for each pizza, and brings them to my kitchen.

Assembling the Pizzas (Dependencies):

Now some pizzas need specific ingredients. For example, Veggie Supreme needs mushrooms, peppers, and olives. My shopping assistant makes sure to grab all the right ingredients for each pizza, just like how a package manager manages dependencies in software.

Cooking and Updates:

I start cooking your pizzas (installing the software). And guess what? If I find out there’s a better recipe for Margherita or new, fresher toppings, my shopping assistant can update those ingredients for me (software updates).

Tidying Up (Removal):

After the party, I might want to clear out some leftover ingredients. My shopping assistant can also help me get rid of those extras (uninstalling software).

So, Package management in Linux works just like my pizza party.

I have a wishlist of software I want (packages).

My package manager fetches all the software from a special place (repository).

It ensures everything I need for each software to work is there (dependencies).

I can update software to get the latest and tastiest versions.

And if I don’t need something anymore, I can remove it to keep things neat.

So the two most popular package managers are YUM and APT.

Well, let me define the term ‘Package Manager’ at this juncture for you.

Package manager helps you find, download, install, and manage software programs (also called packages) that you want to use on your Linux system. Instead of searching the internet for each program and manually installing it, you can use the package manager to handle all the heavy lifting for you.

For example, if you want to install the popular text editor called Visual Studio Code – VSCode on your Linux system to write code. Instead of going to a website, downloading files, and figuring out dependencies, you will use a package manager.

Linux distributions, such as Ubuntu, Fedora, and Debian, have their own package management systems. The most commonly used package managers are apt (used in Ubuntu and Debian-based distributions) and yum (used in Fedora and Red Hat-based distributions).

Now suppose you are using the Ubuntu Linux system, so you will use the APT package manager. Let’s see what are the steps to install the VSCode text editor.

You will start by opening the command line prompt and then typing the following command.$ sudo apt-get install code

Here,

sudo gives you administrative privileges,

apt-get is the package manager command,

install is the action you want to perform,

code is the name of the package you want, in this case, VSCode.

The package manager connects to a repository – an online store of software that contains VSCode and all its required files.

The package manager downloads VSCode and any other necessary files, making sure to also install any libraries or components that VSCode depends on.

Once everything is downloaded and set up, you can now simply type code in your terminal to launch the Visual Studio Code text editor.

Later on, when there’s an update for VSCode, you can use the package manager to easily update it by using the following commands.$ sudo apt-get update

This command updates the repository information.$ sudo apt-get upgrade code

This command upgrades the VSCode package to the latest version available in the repository.

In this way, the package manager simplifies the process of finding, installing, and updating software on your Linux system. It takes care of the technical details, ensuring that the software works properly and is integrated into your system seamlessly.

I hope you are able to relate to the working of a package manager now.

Let us also discuss the YUM package manager with an example.

YUM stands for Yellowdog Updater Modified and is a package manager used primarily in Red Hat-based Linux distributions, like CentOS and Fedora. It helps you install, update, and manage software packages on your system.

Suppose you are using a Fedora Linux system and you want to install a web server software called “Apache” so you can host a website. YUM will help you do that.

Go to your command line terminal and type the following command.$ sudo yum install httpd

Here,

sudo gives you administrative privileges.

yum is the package manager command.

install is the action you want to perform.

httpd is the name of the package you want, in our case, it is the software for the Apache web server.

Then YUM checks its repository for the package named “httpd.”

YUM downloads the Apache package and any necessary files, such as configuration files and dependencies.

YUM configures your Fedora Linux system to properly set up and integrate the Apache web server.

Once the installation is complete, you will start the Apache web server using the following command.$ sudo systemctl start httpd

The systemctl command is used to manage system services and start httpd tells it to start the Apache service.

You can also enable the Apache service to start automatically when your computer boots up:$ sudo systemctl enable httpd

This ensures that the web server is always available.

Now you can open a web browser and access your website using the server’s IP address.

So, isn’t it an easy way to get the software packages of your choice through this package management in Linux?

Well, that’s how package management is helpful. It helps us with,

a. Easy Software Installation: Package managers make installing software as simple as typing a command. You don’t need to hunt for files or worry about complex installation steps.

b. Efficient Updates: Package managers keep your software up to date with just a few commands. You don’t need to manually find updates and download them.

c. Dependency Management: Package managers automatically handle software dependencies, ensuring that all required components are installed correctly.

d. Centralized Repository: They provide access to a central repository of software, which makes it easy to discover and install a wide range of applications.

e. Security: Package managers ensure that software packages come from trusted sources, reducing the risk of malware or compromised software.

f. Version Control: You can easily manage and switch between different versions of software if needed.

g. System Integration: Package managers integrate software into your system properly, maintaining compatibility and reducing conflicts.

File Permissions and Ownership

As you read this blog, a huge amount of data and information is being made, agree?

Millions of messages are exchanged every second, financial transactions are crisscrossing the borders, videos, images, buy and sell all happening constantly and continuously.

The sheer magnitude of this ceaseless data creation is both breathtaking and humbling.

In the middle of all this fascinating information, a quiet protector is making sure data is secure.

And that’s where the concept of file permissions and ownership comes into play in Linux systems.

File permissions and ownership in Linux work together to control who can do what with files and directories. For all the files and directories in your Linux system, file permissions are the locks that determine who can access and manipulate those files while ownership designates who the rightful owner of each file or directory is.

File permissions set rules for three main actions: reading (viewing the content of a file), writing (making changes to a file), and executing (running a file as a program). These permissions can be granted separately for three types of users: the file’s owner, members of the file’s group, and everyone else.

Ownership, on the other hand, assigns a specific user as the owner of a file or directory. This owner has special privileges, such as the ability to change permissions, modify the file’s content, or even delete it. The owner can also delegate certain permissions to the group associated with the file. Groups allow multiple users to share access to files while still maintaining some level of control.

File permissions and ownership work together and create a structured framework that allows users to collaborate, restrict unauthorized access, and maintain the integrity of data, contributing to the stability and reliability of the entire Linux system.

So..let’s learn all the commands that help us to achieve this level of security for our Linux system.

The permissions are represented by a series of letters or symbols.

Let’s use the command. Execute this command in your home directory.$ ls -l

OUTPUT-rw-r--r-- 1 user group 4096 Jun 1 10:30 myfile.txt

The first character indicates the file type (‘-‘ for a regular file), if it is ‘d’..it means it is a directory. The next three characters (‘rw-‘) represent the permissions for the owner, the next three (‘r–‘) represent the permissions for the group, and the final three (‘r–‘) represent the permissions for other users.

What does an Owner mean here?

A user is the owner of the file or directory. An “owner” refers to the user account that has primary control and authority over a file or directory. Every file in the system belongs to precisely one user name, which is also called the file owner.

What does a Group mean here?

A group can contain multiple users. A “group” refers to a collection of user accounts that share certain permissions and access rights to files and directories. By grouping users together, Linux provides a way to manage access to resources more efficiently and securely. Users who belong to that group will have the same Linux group permissions to access the file. Every file is associated with one group name, which is called the group owner.

What does Other mean here?

Other ownership means that the user did not create the file and does not belong to a user group that could own the file. This category includes all users who are not the owner and not members of the group associated with the file. It encompasses all other users on the system who are not part of the specific file’s owner or group.

h. Uninstallation: Removing software is clean and efficient with package managers, as they track installed files and configurations.

i. Consistency: Package managers help maintain a consistent and organized system environment.

j. Time Savings: They save you time by automating complex processes and reducing manual work.

In essence, package managers make software management on Linux more convenient, secure, and efficient.

Linux File Permissions: Absolute and Symbolic Mode

Linux has two modes to configure permissions.

  1. Absolute mode: Uses numbers to set the permissions. This mode is commonly used.
  2. Symbolic mode: It is a combination of letters and symbols to add permissions or to remove any set of permissions.

Now..in case you want to modify file permissions, you can use the chmod command followed by the permission code and the file or directory you want to change.

Let’s see how.

The chmod command in Linux is used to change the permissions of files and directories. It allows you to modify the read, write, and execute permissions for the owner, group, and other users

Let’s learn To change permissions using numeric mode or absolute mode.

You can represent the permissions using numeric values. Each permission has a corresponding numeric value:

  • Read (r) = 4
  • Write (w) = 2
  • Execute (x) = 1

To change the permissions of a file named “myfile.txt” to read and write for the owner, read-only for the group, and no access for others, you can use the following command:chmod 640 myfile.txt

Here, “6” represents the permission for the owner (4+2), “4” represents the permission for the group (read-only), and “0” represents no access for others.

Let’s learn to change permissions using symbolic mode.

Symbolic mode allows you to modify permissions using symbols and operators. The symbols used are:

We will follow this format for symbolic mode.chmod [options] [permissions] filename

Here,

chmod: The command itself.

[options]: Optional flags that modify how the chmod command behaves.

permissions: Specifies the new permissions you want to apply to the file(s) or directory(ies). When we use the symbolic mode, this permission will consist of the following format.

[who] [operator] [permissions]

Here,

[who]: Represents the category of users you want to modify: u for owner, g for group, and o for others.

[operator]: Specifies the action you want to perform: + to add permissions, - to remove permissions, or = to set permissions explicitly.

[permissions]: Indicates the permissions you want to add, remove, or set using the symbols rw, and x.

Let me write down a few examples.

Example 1: To give the owner write and execute permissions on a file.chmod u+wx filename

Example 2: To remove read and write permissions from the group for a directory.chmod g-rw directoryname

Example 3: To set read and execute permissions for others on a script.chmod o=rx scriptname

Changing File Permissions with chmod

Let’s explore a few options that we can use with the chmod command.

Adding Permissions (+):

You have a file called mydata.txt, and you want to allow everyone to read it.$ chmod +r mydata.txt

You’re telling Linux to add (+) read permission (r) for everyone to the file mydata.txt. Now, anyone can read the contents of the file.

Removing Permissions (-):

Now, let’s say you want to stop others from changing your notes.txt file.$ chmod go-w notes.txt

So with this command, you can change the permissions of notes.txt to remove (-) write permission (w) for the group (g) and others (o). They can’t change the notes anymore.

Setting Exact Permissions (=):

Now, you want only you to be able to use a file named game.$ chmod u=x game

So with this command, you can change the permissions of the game folder to set (=) execute permission (x) only for you (u means user/owner).

Using Different Options (-c and -v):

Now, you want to see what’s happening as you change permissions for a folder named photos.$ chmod -cv 755 photos

So with this command, you can change the permissions of photos and show (-c) a message for each change. Also, with (-v) option the command will tell you what it is doing. In this command, you are giving read, write, and execute permissions to the owner, and read and execute permissions to the group and others.

All right! So chmod is the first command we learned in this file permissions category.

Our next command is chown.

Changing Ownership with chown

The chown command in Linux is used to change the ownership of files and directories. It allows you to assign a new owner and group to a file or directory.

The syntax of chown command is.chown [options] new_owner:new_group file(s)

Let’s learn to change ownership of a file.

To change the owner of a file named “myfile.txt” to a user named “newuser,” you can use the following command:$ chown newuser myfile.txt

This command will change the owner of “myfile.txt” to “newuser,” keeping the group ownership unchanged.

Let’s learn to change ownership of a directory.

To change the owner of a directory named “mydir” to a user named “newuser” and the group ownership to a group named “newgroup,” you will use.$ chown newuser:newgroup mydir

This command will change both the owner and group ownership of “mydir.”

Let’s learn to change ownership recursively.

You can use the -R option to change ownership recursively for directories and their contents. For example, to change the owner and group ownership of a directory named “mydir” and all files and subdirectories within it to a user named “newuser” and a group named “newgroup,” you can use the following command:$ chown -R newuser:newgroup mydir

The -R option ensures that the ownership changes are applied recursively.

Let’s learn to specify ownership using numeric values.

Instead of using usernames and group names, you can also specify ownership using numeric user and group IDs (UID and GID). For example, to change the owner and group ownership of a file named “myfile.txt” using their respective IDs, you can use the following command.$ chown 1001:1002 myfile.txt

This command will change the owner to the user with UID 1001 and the group ownership to the group with GID 1002.

Remember, using the chown the command is like a transfer of power. Just like giving someone a key to a room, you’re letting them have control over files and folders. Use it wisely, and always double-check before you change ownership, especially for important files and system areas.

Changing Group Ownership with chgrp

The chgrp command in Linux is used to change the group ownership of files and directories. It allows you to assign a new group to a file or directory.

The syntax of chgrp the command is,chgrp [options] new_group file(s)

Here,

  • chgrp: This is the command itself.
  • [options]: These are optional settings you can use to control how the command works.
  • new_group: This is the name of the group you want to change to. It’s like telling Linux which group your files should belong to.
  • file(s): This is the name of the file or files you want to give the new group to. So it’s like telling Linux to change the group of these files to a new group called new_group.

Let’s understand the chgrp command with some examples and the options that we can use with it.

To change group ownership of a file named “myfile.txt” to a group named “newgroup,” you can use the following command.$ chgrp newgroup myfile.txt

This command will change the group ownership of “myfile.txt” to “newgroup,” keeping the owner unchanged.

To change the group for many files (-R).

Let’s say you have a folder named photos with tons of pictures. You want them all to be part of the memories group.$ chgrp -R memories photos

With this command, you can change the group of the photos folder and all its contents (using -R) to memories. Now, all the pictures inside belong to the memories group.

Conclusion

And there you have it, amazing commands of Linux! We explored the details of sudo commands, learned the package managers, and delved deep into the file permissions and ownership commands. Think about it – a simple command like ‘sudo’ can get you the power of the superuser. But remember, with great power comes great care.

File permissions grant access only to those who possess the right permissions. Ownership designates the authorized user to bear the responsibility and privilege of accessing the files.

May your Linux journey be filled with discovery, learning, and above all, the thrill of mastering the art of using Linux commands effortlessly.

Leave a Comment

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