Part 3 - Linux Commands for DevOps

ManasiManasi
6 min read

Df, du: The df (disk free) command shows the available disk space used by the file system.

The du (disk usage) command reports the size of a directory tree, including all its contents. Using the -h option will display sizes in a human-readable format, adjusting to the appropriate size units. -k flag indicates Kilobytes.

Adding the -a option will print the size of each file in the directories, too

Running du * will calculate the size of each file individually:

Htop: This command is used to monitor the system’s resources and processes that are running in real time.

TOP: The top command is also used to monitor system resources, but it is simpler than the htop command. It runs continuously. To exit, you can press the 'q' key or 'Ctrl+C'.

Ps: We use ps command to check the unique id behind every process.

ps Command Options:

  • -a: Show processes for all users

  • -u: Display user-oriented format

  • -x: Include processes that do not have a controlling terminal

  • -e: Show information about all processes

  • -f: Display a full listing of processes with additional information

Kill: This command is used to terminate processes manually. This command basically, will send a signal that terminates it.
Syntax: kill -9 <process ID>

TNC: This is the "Test Network Connection" command, commonly used for troubleshooting. It shows diagnostic information for a connection.

Command: tnc <server_name> -port <port>

echo: The echo command performs a simple task: it prints the argument passed to it as output. Be aware that special characters need to be escaped with a backslash \ . $

You can use any valid Bash (or any shell you are using) command and feature here, or you can execute the commands directly.

Traceroute: The traceroute command is a network diagnostic tool that traces the path packets take from a source to a destination over an IP network. It offers useful information about the network route, including the number of hops (routers) between the source and destination, and the round-trip time (RTT) for each hop.

Tracepath: The primary purpose of the ‘tracepath’ command is to trace the network path to a specified destination and identify the MTU of each hop along the path.

chown: Every file or directory in an operating system like Linux or macOS (and UNIX systems in general) has an owner. The owner of a file can do anything with it and can decide what happens to that file. Only the root can change the owner to another user.

chown <owner> <file>

It's quite common to need to change the ownership of a directory, including all the files and subdirectories, flies within it, as well as the files they contain.

You can do so using the -R flag:

chown -R <owner> <file>

Files/directories don't just have an owner, they also have a group. Through this command you can change that simultaneously while you change the owner:

chown <owner>:<group> <file>

You can also just change the group of a file using the chgrp

command: chgrp <group> <filename>

chmod: Every file in the Linux / macOS Operating Systems (and UNIX systems in general) has 3 permissions:

Read, write, execute.

The weird strings you see on each file line, like drwxr xr-x, define the permissions of the file or folder. Let's dissect it.

The first letter indicates the type of file:

  • - means it's a normal file

  • d means it's a directory

  • l means it's a link

Then you have 3 sets of values:

  1. The first set represents the permissions of the owner of the file.

  2. The second set represents the permissions of the members of the group the file is associated to.

  3. The third set represents the permissions of the everyone else.

chmod can be used in 2 ways. The first is using symbolic arguments, the second is using numeric arguments. Let's start with symbols first, which is more intuitive.

You type chmod followed by a space, and a letter:

a: stands for all

u: stands for user

g: stands for group

o: stands for others

Then you type either + or - to add a permission, or to remove it. Then you enter one or more permissions symbols (r,w,x).

All followed by the file or folder name.

Here are some examples:

chmod a+r filename #everyone can now read

chmod a+rw filename #everyone can now read and write

chmod o-rwx filename #others (not the owner, not in read, write and execute )

Numeric arguments are quicker, but I find them hard to remember if you don't use them daily. You use a digit to represent the permissions for each category. The maximum value for a number is 7.

This it's calculated in this way:

1: if has execution permission

2: if has write permission

4: if has read permission

ex: chmod 777 filename

chmod 755 filename

chmod 644 filename

Which: Suppose you have a command you can run because it's in the shell path, but you want to find out where it's located. You can do this using the which command.

whoami: The whoami command displays your login name.

vim: vim is a very popular file editor, especially among programmers, vi in modern systems is just an alias to vim.

You need to know that Vim has two main modes:

  1. Command (or normal) mode

  2. Insert mode (press the i key)

  3. Saving the file: You can do this by pressing : (colon), then w.

  4. You can save and quit by pressing : then w and q (:wq).

passwd: Users in Linux have a password assigned. You can change the password using the passwd command.

nproc: It is a simple Unix command which is used to print the number of processing units available in the system or to the current process.

We use the “–all” option when we want nproc to display the total installed processing units.

export: The export command is used to pass variables to child processes.

Suppose you have a variable TEST defined in this way:

TEST="test", You can print its value using echo $TEST :

export TEST="test" is used to be defined in a bash script.

Sometimes you need to append something to a variable. It's often done with the PATH variable. You use this syntax:

export PATH=$PATH:/new/path

crontab: Cron jobs are tasks scheduled to run at specific times. You might set a command to run every hour, every day, every two weeks, or only on weekends. They are very powerful, especially on servers, for performing maintenance and automation tasks.

The crontab command is used to manage cron jobs.

The first thing you can do is to explore which cron jobs are defined by you:

Run: crontab -e

To edit the cron jobs, and add new ones, To generate a crontab you can use a default editor, which can be vim.

To generate a cron tabwithout any errors you can follow the link:

https://crontab-generator.org/

uname: Calling uname without any options will return the operating system's codename.

The -m option shows the hardware name, and the -p option prints the processor architecture name.

The -s option prints the operating system name, -r prints the release, and -v prints the version:

The -n option displays the network node name.

The -a option displays all the available information.

0
Subscribe to my newsletter

Read articles from Manasi directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Manasi
Manasi

With over3 years of experience in the tech industry, I specialize in bridging the gap between development and operations. My expertise lies in automating workflows, optimizing CI/CD pipelines, and ensuring seamless deployments in cloud environments. 🔧 Skills: Proficient in tools like Docker, Kubernetes, Jenkins, and Terraform Strong background in AWS Platform Scripting in Python and Bash for automation Experienced in configuration management with Ansible and Puppet 🚀 What I Do: I thrive on enhancing collaboration among teams, implementing robust monitoring solutions, and improving system reliability. My goal is to foster a culture of continuous improvement and innovation. 🌐 Let’s Connect: Always eager to share knowledge and explore new technologies. Reach out if you're interested in discussing DevOps best practices, cloud solutions, or just want to chat about the latest trends in tech!