Essential Linux Commands for DevOps: Part 2

Mastering Key Linux Commands for Efficient DevOps Workflow

Essential Linux commands for DevOps include ifconfig, hostname, yum, service, echo, user and group management, softlink/hardlink, archive, chmod, chown, and chgrp. Mastering these commands enhances productivity in managing Linux systems, file management, user/group management, and package management.

Basic Commands

To know the network details of the machine -> ifconfig

To know the hostname -> hostname

To only know the IP address -> hostname -i

To know the current os-release -> cat /etc/os-release

To know whether a package is installed or not -> which <package-name>

To check whether you are a root user or not -> whoami

To sort the file contents alphabetically -> sort <file-name>

To find a phrase in a file -> grep <phrase> <file-name/file-path>

To download a file from a URL -> wget <url>

yum

(Yellowdog Updater Modified)

This command helps to install, remove or update any packages installed in Linux.

  1. To install a package called 'tree' -> yum install tree -y (To skip yes/no questions put -y. This will put yes in every question.)

  2. To update any package -> yum update <package-name>

  3. To remove any package -> yum remove <package-name>

  4. To see the list of installed packages -> yum list installed

  5. To see the list of available packages -> yum list available

  6. To see both installed and available packages -> yum list all

  7. To downgrade a package to an earlier version -> yum downgrade <package-name>

  8. To update all the installed packages -> yum update

service

  1. To start a particular service -> service <service-name> start (Eg. service httpd start )(This will start the apache server.)

  2. To stop a particular service -> service <service-name> stop

  3. To start the service as soon as you boot the machine -> chkconfig <service-name> on

    Eg. chkconfig httpd on (This will configure the httpd service to start when you boot the machine)

  4. To stop the service from automatically starting when you boot the machine -> chkconfig <service-name> off

echo

  1. To display a message -> echo <msg> (Eg. echo hello)

  2. To create a file and store the message -> echo <msg> > <file-name> (Eg. echo helloworld > program) (This will override everything in the file if it was previously created.)

  3. To append a message to an existing file -> echo <msg> >> <file-name> (Eg. echo world >> file1)

  4. To empty a file -> echo > <file-name>

Users and Groups

  1. To add a user -> useradd <user-name> (If any user is created for the first time a group with that user name is by default created.)

  2. To view all users -> cat /etc/passwd

  3. To create a group -> groupadd <group-name>

  4. To view all groups -> cat /etc/group

  5. To add a single user to a group -> gpasswd -a <user-name> <group-name> (This does not override the existing group members list.)

  6. To add multiple users to a group -> gpasswd -M <user-name>,<user-name> <group-name> (This will overwrite the existing group members list.)

To create a soft link of a file -> ln -s <file-name> <soft-link name> (This soft link acts as a shortcut for the original file. Making some changes in the soft link file will also be reflected in the original file. This file is of no use if you delete the original file.)

To create a hard link of a file -> ln <file-name> <hardlink-name> (This acts as a backup file. Any changes in the original file will also be reflected in the hard link file.)

Archive

To archive a file or folder -> tar -cvf <archive-name> <file/folder> (c- create, v- verbose, f- forcefully) (It has an extension of filename.tar)

To compress a tar file -> gzip <tar-file-name> (It has an extension of filename.tar.gz)

To uncompress a file -> gunzip <compressed-zip-file-name) (Eg. gunzip file.tar.tz)

To extract a tar file -> tar -xvf <archive-file-name> (x- extract)

What does this below line represent when you do ls -l?

-/l/dFile/Link(soft link)/Directory
rwxThis represents the permissions root/owner users have.
r-xThis represents the permissions of the group.
r-xThis represents the permissions of others.
rootThe owner of the file.
root (2nd)Whatever group this file is currently present in.

There are three types of modes in Linux

  1. Read (Value = 4) (Represented by r)

  2. Write (Value = 2) (Represented by w)

  3. Execute (Value = 1) (Represented by x)

If the root has the permission of rwx i.e. the permission can be represented by 7 (4+2+1)

if the root has the permission of --x i.e. the permission can be represented by 1(0+0+1)

Similarly for groups and other users.

Access ModeActions on FileActions on Directory
r - 4To display the contentTo view the contents
w - 2To modify the contentTo create or remove
x - 1To execute itTo enter inside the directory

chmod

To change the permissions this command is used.

For eg. chmod 777 file1

This will set the permissions of owner/root, groups and others to 7 (i.e. read+write+execute (rwx))

Representation

  • u = user/owner

  • g = group

  • o = others

chmod 700 dir1

  • Owner/root will have full permission 7 (4+2+1)(rwx)

  • Groups will have no permission 0 (---)

  • Others will have no permission 0 (---)

chmod 755 file1

  • Owner/root will have full permission 7 (4+2+1)(rwx)

  • Groups will only have read and execute permissions 5 (4+0+1)(r-x)

  • Others will only have read and execute permissions 5 (4+0+1)(r-x)

We can also directly set the permissions -> chmod g=r,o=rw dir1 (root's permission will remain unchanged.)

We can also do like this -> chmod u+w,g-w,o+r file1

  • Add write permission to user/owner/root.

  • Remove write permission for groups.

  • Add read permission for others.

chown

This command is used to change the owner of a file/directory.

This will change the owner to the specified username -> chown <user-name> <file/directory name>

chgrp

This command is used to change the group of a file/directory.

This will change the group to the specified group name -> chgrp <group-name> <file/directory name>

Conclusion

In conclusion, understanding and mastering these essential Linux commands for DevOps will enable you to efficiently manage and maintain Linux systems, perform various tasks such as file management, user and group management, and package management, and enhance your overall productivity in a DevOps environment.