Photo by Gabriel Heinzer on Unsplash
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.
To install a package called 'tree' -> yum install tree -y (To skip yes/no questions put -y. This will put yes in every question.)
To update any package -> yum update <package-name>
To remove any package -> yum remove <package-name>
To see the list of installed packages -> yum list installed
To see the list of available packages -> yum list available
To see both installed and available packages -> yum list all
To downgrade a package to an earlier version -> yum downgrade <package-name>
To update all the installed packages -> yum update
service
To start a particular service -> service <service-name> start (Eg. service httpd start )(This will start the apache server.)
To stop a particular service -> service <service-name> stop
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)
To stop the service from automatically starting when you boot the machine -> chkconfig <service-name> off
echo
To display a message -> echo <msg> (Eg. echo hello)
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.)
To append a message to an existing file -> echo <msg> >> <file-name> (Eg. echo world >> file1)
To empty a file -> echo > <file-name>
Users and Groups
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.)
To view all users -> cat /etc/passwd
To create a group -> groupadd <group-name>
To view all groups -> cat /etc/group
To add a single user to a group -> gpasswd -a <user-name> <group-name> (This does not override the existing group members list.)
To add multiple users to a group -> gpasswd -M <user-name>,<user-name> <group-name> (This will overwrite the existing group members list.)
Softlink/Hardlink
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/d | File/Link(soft link)/Directory |
rwx | This represents the permissions root/owner users have. |
r-x | This represents the permissions of the group. |
r-x | This represents the permissions of others. |
root | The owner of the file. |
root (2nd) | Whatever group this file is currently present in. |
There are three types of modes in Linux
Read (Value = 4) (Represented by r)
Write (Value = 2) (Represented by w)
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 Mode | Actions on File | Actions on Directory |
r - 4 | To display the content | To view the contents |
w - 2 | To modify the content | To create or remove |
x - 1 | To execute it | To 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.