Saiyam Pathak avatar
By Saiyam Pathak
Field CTO

Description

Welcome to Civo Academy on Linux commands, a vital foundation for mastering Kubernetes. This lesson will not only familiarize you with the basics of Linux commands but also provide you with practical examples and use cases that will prove invaluable throughout your Kubernetes journey.


Before delving into Docker and Kubernetes, it's crucial to understand some basic Linux commands. These commands form the backbone of your interaction with Linux-based systems and will be used extensively throughout your Kubernetes learning journey.

Essential Linux Commands

The first command to know is the ls command, which lists information about the files and directories in your current directory. For more detailed information, use ls -ltr.

To create a directory, use the kdir command. You can switch to this new directory using the cd command. To create a file in the directory, use the touch command.

To add content to the file, use the vi command. Press I to enter INSERT mode, then press Escape to save the file, followed by :wq!, and hit Enter. To view the file's contents, use the cat command. The output will be printed on the console.

To clear the screen, use the clear command. To copy the file to a previous directory, use the cp command. To switch to the previous directory, use thecd .. command.

Changing File Permissions

To change the permission of a file, use the chmod 400 command, which sets the file to read-only mode.

Making a Binary File Executable

Binary files are a type of file that can be executed or run as a program in your computer. They are essential in Linux as they contain compiled code or scripts that can be run directly by the computer's operating system.

We can apply several modes to a file in Linux, but the two most important ones are the read-only mode (400) and the executable mode.

First, we need to have the executable downloaded for the executable mode. We use the curl command to transfer the data from and to a server. The curl command to get the kubectl binary downloaded to our system is:

bash curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl

After downloading, you can see that kubectl is downloaded, but it's not executable. We'll run the command chmod +x to make it executable.

bash chmod +x kubectl

Now, you can see it is in the executable format. We can run it with ./ and it's running.

bash ./kubectl

Extracting Compressed Files

To extract a compressed file, we use a command called tar, which is an archive utility, along with a few options. The tar command allows you to extract the contents of a compressed file, revealing the original files and directories.

For instance, if we have a TAR file, we can extract it using the tar command as follows:

bash tar -xvf file.tar

In this command, -xvf is an option that tells tar to extract (`-x`) the file, verbosely list the files processed (`-v`), and specifies the file name (`-f`). After running this command, you can see the directory has been extracted.

Viewing and Setting Environment Variables in Linux

In Linux, environment variables are dynamic-named values that can affect the way running processes will behave on a computer. They are part of the environment in which a process runs.

To view all the environment variables that are already set, you can use the env command. This command will list all the environment variables in your current shell session.

If you want to set a new environment variable, you can use the export command followed by the variable name and its value. For example, to set a new environment variable named demo with the value training, you would use the following command:

bash export demo=training

After setting the new environment variable, you can verify that it has been added by running the `env` command again. You should see `demo=training` appear in the list of environment variables.

To use the value stored in an environment variable, you can use the `echo` command followed by the variable name, preceded by a dollar sign. For example, to print the value of the `demo` variable, you would use the following command:

bash echo $demo

The du command is a disk utility command that provides information about the disk space used by files and directories. By default, the sizes are reported in blocks, which might not be easy to read. To get the a human-readable format, you can use the -h option:

bash du -h

If you want a summary of the total size of a directory, you can use the -s option:

bash du -sh

This command will provide a summary of the total size of the directory in a human-readable format.

Network Connectivity Commands

Network connectivity is a crucial Linux aspect. Various commands allow users to test and troubleshoot network connections, ensuring smooth and efficient communication between devices.

The ping command is a fundamental network connectivity tool. By using ping www.google.com, you can test the connection to Google's servers. If you want to send only one packet, you can use ping -c 1 www.google.com.

To monitor network connections and view statistics, the netstat command comes into play. Running netstat will display all active network connections. If you want to filter the results to only show TCP connections, you can use netstat -at. For UDP connections, use netstat -au.

The nslookup command, short for name server lookup, provides DNS records, including the area code and more. To use this command, you can type nslookup www.google.com. This command will return the Domain Name System (DNS) records for www.google.com, providing valuable information about the domain.

These commands provide valuable insights into your network's status and allow for efficient problem-solving when issues arise.

Process status commands

The ps command, short for process status, is a powerful tool in Linux that provides information about the currently running processes, including their process identification numbers (PIDs). You can use ps -ef to display detailed information about all the running processes.

One of the primary uses of the PID is to terminate unwanted processes. If there's a process you wish to terminate, you can use the `kill` command along with the PID of the process.

Tail commands

The tail command in Linux is used to print the last part of files. For instance, if you have a file named demo with some content, you can use tail demo to print out the last ten lines of that file.

If you want to limit the output to a specific number of lines, you can use the -n option, followed by the number of lines you wish to print. For example, tail -n 5 demo will print the last five lines of the file.

Linux Ctl Commands

Ctl commands in Linux are powerful tools that allow you to control services, view system logs, and monitor system utilization. These commands include systemctl, journalctl, top, and htop.

The systemctl command is used to control the systemd system and service manager. It allows you to start, stop, and view the status of services. For instance, if you have Docker installed, you can check its status using systemctl status docker. If it's inactive, you can start it using systemctl start docker. Running the status command again will show that Docker is now running.

journalctl is another ctl command that is used to view the systemd logs. Running journalctl will display the systemd logs, which can be helpful for debugging purposes. For instance, viewing the kubelet logs can provide valuable insights when troubleshooting Kubernetes.

The top command monitors the system's CPU and memory utilization. It provides a dynamic real-time view of the running system. However, the htop command provides a more user-friendly and visually appealing interface while displaying the same information.

These commands provide valuable insights into your system's status and allow for efficient problem-solving when issues arise.

Don't stop now, check out your next lesson