Most use Linux commands and Online tools for DevOps Engineer

In my day-to-day task, these are the most used commands and tools related to Linux and DevOps Configuration

Basics

SSH (Secure Shell) is a cryptographic network protocol that allows secure remote login and command execution on remote machines

# Connects to a remote SSH server. The basic syntax is:
# it will also promt for password , if your server is allowed password based auth
ssh username@hostname
ssh usama@192.168.100.1
# Key base authentication
ssh -i /path/to/private_file  usama@192.168.100.1
# ssh-copy-id: Installs your public key on the remote server, allowing passwordless SSH authentication.
ssh-copy-id username@hostname

NOTE: In case you are accessing a remote machine in AWS anywhere sometime it asks for private file permission must be 400,

To change file permissions in Linux, you can use the chmod command. The chmod the command allows you to modify the read, write, and execute permissions for the owner, group, and others on a file or directory. Here is the basic syntax

chmod options permissions filename
# To give read and write permissions to the owner, and read-only permissions to the group and others:
chmod u=rw,g=r,o=r filename
# To give execute permission to the owner, group, and others
chmod a+x filename
# To remove write permissions for the group and others:
chmod g-w,o-w filename
# To set permissions recursively on a directory and its contents:
chmod -R 755 /vaw/www/site_name

Here are some commonly used options:

  • u: Set permissions for the owner (user) of the file.

  • g: Set permissions for the group associated with the file.

  • o: Set permissions for others (everyone else).

  • a: Set permissions for all (equivalent to specifying ugo together).

  • +: Add the specified permissions.

  • -: Remove the specified permissions.

  • \=: Set the specified permissions and remove all other permissions.

 cd directory_name

Additional details:

  • cd .. Move to the parent directory.

  • cd ~ Move to the home directory.

  • cd - Move to the previous directory.

chown Change the ownership of files and directories

$ chown user1:group1 file.txt

Additional options:

  • -R: Recursively change ownership for files and directories within a directory.

Example with option:

$ chown -R user1:group1 directory/

grep: Search for a specific pattern in files

grep "error" log.txt

Additional options:

  • -i Ignore the case when searching.

  • -r Recursively search files in directories

find: Search for files and directories.

find /home -name "*.txt"
/home/user/file1.txt
/home/user/directory1/file2.txt

additional options:

  • -type: Filter files by type (e.g., -type f for regular files).

  • -size: Filter files by size

ssh: Connect to a remote server using the Secure Shell (SSH) protocol.

 ssh user@hostname
ssh usama@192.168.100.143
# In case of private key authentication
ssh -i path-to-private-key usama@192.168.100.143

Additional options:

  • -p: Specify a different port number.

  • -i: Use a different identity file (private key) for authentication.

Copying files

SCP: Securely copy files between local and remote systems.

scp file.txt user@hostname:/remote/directory
#   copy a hidden folders/files from local to remote using scp

Additional options:

  • -r: Recursively copy directories.

  • -P: Specify a different port number.

rsync is used for synchronizing files and directories between local and remote systems

rsync source_file destination_file
rsync -r source_directory/ destination_directory
# -r option tells rsync to copy directories recursively.
rsync -avz source_file_or_directory user@remote_host:destination_directory
# -a preserves file permissions, ownership, timestamps, etc.
# -v enables verbose output.
# -z compresses data during transfer, reducing network usage.
rsync -avz --delete source_directory/ destination_directory

# --delete option removes files in the destination that are not present in the sourcersync -avz --exclude 'file.txt' source_directory/ destination_directory
rsync -avz --exclude 'file.txt' source_directory/ destination_directory
#--exclude option allows excluding specific files or directories from the transfer.
# Perform a dry run without actually copying files:
rsync -avz --dry-run source_directory/ destination_directory

you can use a PEM file (also known as a private key file) rsync for secure authentication when connecting to a remote server. Here's an example of how to use a PEM file with rsync

rsync -avz -e 'ssh -i /path/to/private_key.pem' source_directory/ user@remote_host:destination_directory

Data and File Management

less command is a pager utility in Linux that allows you to view text files or the output of other commands one page at a time. It provides a more convenient way to navigate through large amounts of text compared to scrolling in a terminal window.

less filename
  • Up/Down Arrow keys: Scroll one line at a time.

  • Page Up/Page Down keys: Scroll one page at a time.

  • Spacebar: Scroll one page down.

  • B: Scroll one page up.

  • G: Go to the beginning (first line) of the file.

  • Shift+G: Go to the end (last line) of the file.

  • /pattern: Search forward for a specific pattern. Press n to find the next occurrence.

  • ?pattern: Search backward for a specific pattern. Press n to find the previous occurrence.

tail: Display the last lines of a file.

$ tail -n 10 log.txt
# use -f to follow the file for real time data

head: Display the first lines of a file.

$ head -n 5 log.txt

wget: Download files from the web.

wget https://example.com/file.txt
# Mirror a website:
wget -r -np https://example.com

we will save a file using a different name with the help of -O option:
wget -O wordpress-install.zip https://wordpress.org/latest.zip

Additional options:

  • -O: Save the downloaded file with a different name.

  • -P: Specify a different directory to save the file.

sed command is a powerful text stream editor in Linux that can perform various operations on text files, such as search, replace, insert, delete, and more. Here are some real-world use cases for the sed command

# Search and Replace, update all occurrences of a word in a configuration file
sed 's/old_word/new_word/g' config.txt
Delete Lines: You can use sed to delete specific lines from a file based on a pattern or line number. For example, remove all lines containing a certain word from a log file
sed '/pattern/d' logfile.txt

tar: Archive files and directories into a tarball.

$ tar -czvf archive.tar.gz file1.txt directory1

The basic syntax to zip a file is shown below:

zip [OPTION] file.zip file
#  to zip a single file run the following command
zip file1.zip file1.txt
# To zip multiple files, run the following command:

zip files.zip file1.txt file2.txt file3.txt
# to view the content of the files.zip file, run the following command:
zipinfo files.zip

# zip a directory, run the following command:
zip -r direcory.zip direcory
# Create a Password Protected Zip File
zip -r -e teachers.zip teachers

unzip option

unzip file.zip
# to unzip a zipped file named files.zip, run the following command:
unzip files.zip
#  extract to a particular destination folder,
unzip file.zip -d destination_folder
# extract to a directory with the same name as the zip in your current working directory
unzip file.zip

Monitoring

Checking disk usage:

df -h

Monitoring disk usage by directory:

du -sh directory

Checking system uptime:

uptime

User Administration

useradd: Create a new user account.

$ useradd -m -s /bin/bash john

usermod: Modify user account attributes.

$ usermod -aG sudo john
  • Example: usermod -aG groupname username (Add a user to a group)

  • Example: usermod -c "New User Name" username (Change the user's full name)

  • Example: usermod -L username (Lock a user account)

  • Example: usermod -U username (Unlock a user account)

passwd: Change the password for a user account.

passwd john

Network Administration

ifconfig is used to initialize an interface, configure it with an IP address, and enable or disable it. It is also used to display the route and the network interface

ifconfig
# Addition
ifconfig [interface-name]

Monitoring network connections and statistics:

netstat -tuln

Check if a specific port is open on a remote host using netcat

nc -zv remote_host.com 80

Display network connections and their processes

netstat -tulnpe

Show all listening ports on the system

netstat -tuln | grep 'LISTEN'

Use lsof to find the process ID (PID) associated with port 3001:

lsof -i :3001
kill <PID>
# -9 mean forcefully
kill -9 <PID>

Bonus Command

# for getting IP address from within terminal
curl ident.me
# incase your ISP assign you ipv6 to you
curl -4 ident.me

Tools

SSH client and terminal how it should be in 2023 https://termius.com/

DNS Checker: https://dnschecker.org/

Check open ports: https://www.yougetsignal.com/tools/open-ports/

https://www.ipaddressguide.com/cidr

Performance Test for URLS for Testing CDN: https://tools.keycdn.com/performance

https://www.uptrends.com/tools/cdn-performance-check

The quick and simple editor for cron schedule expressions by https://crontab.guru/

Install Any command in any flavor https://command-not-found.com/

Write down your favorite Tools in the comment

Did you find this article valuable?

Support Beyond DevOps by becoming a sponsor. Any amount is appreciated!