Linux commands used by Devops Engineers in performing day to day tasks


Below are the list of Linux Commands with Syntax and Real time usage used by Devops Engineer in handling day to day tasks.

1. File and Directory Management

ls – List directory contents.

? Syntax: ls [options] [directory]

? Example: ls -l /var/log # Lists files in the /var/log directory with detailed info

cd – Change directory.

? Syntax: cd [directory]

? Example: cd /etc/nginx # Change to the /etc/nginx directory

cp – Copy files or directories.

? Syntax: cp [source] [destination]

? Example: cp file.txt /tmp/backup.txt # Copies file.txt to /tmp/ as backup.txt

mv – Move or rename files or directories.

? Syntax: mv [source] [destination]

? Example:

mv oldname.txt newname.txt # Renames oldname.txt to newname.txt

mv /tmp/file.txt /var/www/ # Moves file.txt to /var/www/

rm – Remove files or directories.

? Syntax: rm [options] [file or directory]

? Example:

rm file.txt # Deletes file.txt

rm -rf /var/tmp/* # Recursively and forcefully delete all files in /var/tmp

find – Search for files or directories.

? Syntax: find [path] [options] [expression]

? Example: find /var/log -name "*.log" # Finds all log files in /var/log

mkdir – Create directories.

? Syntax: mkdir [directory]

? Example: mkdir /var/www/myproject # Creates the /var/www/myproject directory

chmod – Change file or directory permissions.

? Syntax: chmod [permissions] [file or directory]

? Example: chmod 755 /var/www/html # Set permissions to read, write, and execute for the owner, and read and execute for others

chown – Change ownership of files or directories.

? Syntax: chown [owner:group] [file or directory]

? Example: chown -R user:group /var/www/html # Changes ownership of /var/www/html to the user and group recursively

2. System Monitoring and Performance Analysis

top – Display Linux tasks and system resource usage.

? Syntax: top

? Example: top # Shows an interactive list of processes and resource usage in real-time

htop – A more user-friendly and interactive version of top.

? Syntax: htop

? Example: htop # Starts htop with better UI and keyboard controls

ps aux – Display information about running processes.

? Syntax: ps aux

? Example: ps aux | grep nginx # Filters out the list of processes to show only those related to nginx

df -h – Show disk space usage.

? Syntax: df -h

? Example: df -h # Displays disk space usage in a human-readable format

du -sh – Display disk usage of a file or directory.

? Syntax: du -sh [file or directory]

? Example: du -sh /var/log # Shows the size of the /var/log directory in a human-readable format

free -m – Display memory usage.

? Syntax: free -m

? Example: free -m # Displays memory usage in MB

vmstat – Report virtual memory statistics.

? Syntax: vmstat [interval]

? Example: vmstat 5 # Displays system performance every 5 seconds

iostat – Report CPU and I/O statistics.

? Syntax: iostat

? Example: iostat # Shows CPU and I/O statistics for storage devices

3. Networking and Connectivity

ping – Test network connectivity to a host.

? Syntax: ping [host]

? Example: ping google.com # Sends ICMP echo requests to Google and reports latency

ifconfig – Configure or view network interfaces (deprecated, use ip addr instead).

? Syntax: ifconfig [interface]

? Example: ifconfig # Displays the status of network interfaces

ip addr – Display or manage IP addresses and network interfaces.

? Syntax: ip addr [command]

? Example: ip addr show # Shows detailed information about network interfaces

netstat – Display network connections, routing tables, interface statistics (deprecated, use ss instead).

? Syntax: netstat [options]

? Example: netstat -tuln # Displays listening TCP/UDP ports

ss – Display network sockets.

? Syntax: ss [options]

? Example: ss -tuln # Displays listening TCP/UDP ports and statistics

traceroute – Trace the path of packets to a network host.

? Syntax: traceroute [host]

? Example: traceroute google.com # Displays the route packets take to Google

nslookup – Query DNS to obtain domain name or IP address mapping.

? Syntax: nslookup [host]

? Example: nslookup example.com # Resolves the IP address for example.com

dig – Perform DNS queries.

? Syntax: dig [domain]

? Example: dig example.com # Queries DNS for detailed information about example.com

curl – Transfer data from or to a server.

? Syntax: curl [options] [URL]

? Example: curl -I ? https://example.com # Sends a request to example.com and returns HTTP headers

scp – Securely copy files between hosts over SSH.

? Syntax: scp [source] [destination]

? Example: scp file.txt user@remotehost:/path/to/destination # Copies file.txt to a remote host via

SSH

ssh – Securely log into a remote server.

? Syntax: ssh [user]@[host]

? Example: ssh user@remotehost # Logs in to the remotehost via SSH

4. User and Group Management

useradd – Add a new user.

? Syntax: useradd [options] [username]

? Example: sudo useradd -m devops_user # Creates a new user named devops_user with a home directory

passwd – Change or set a user password.

? Syntax: passwd [username]

? Example: sudo passwd devops_user # Sets the password for devops_user

usermod – Modify a user account.

? Syntax: usermod [options] [username]

? Example: sudo usermod -aG sudo devops_user # Adds devops_user to the sudo group

groupadd – Add a new group.

? Syntax: groupadd [groupname]

? Example: sudo groupadd devops # Creates a new group called devops

groups – Show which groups a user belongs to.

? Syntax: groups [username]

? Example: groups devops_user # Shows all groups devops_user belongs to

5. Package Management

apt-get – Manage packages on Debian/Ubuntu systems.

? Syntax: apt-get [command] [package]

? Example:

sudo apt-get update # Updates package lists

sudo apt-get install nginx # Installs the nginx web server

yum – Manage packages on RedHat/CentOS systems.

? Syntax: yum [command] [package]

? Example: sudo yum install httpd # Installs Apache web server on CentOS

rpm – Manage RPM packages.

? Syntax: rpm [options] [package]

? Example: rpm -ivh package.rpm # Installs a package from an RPM file

dpkg – Manage Debian packages.

? Syntax: dpkg [options] [package]

? Example: sudo dpkg -i package.deb # Installs a package from a .deb file

6. Service and Process Management

systemctl – Manage services on systemd-based systems.

? Syntax: systemctl [command] [service]

? Example:

sudo systemctl start nginx # Starts the nginx service

sudo systemctl status nginx # Checks the status of nginx

sudo systemctl enable nginx # Enables nginx to start on boot

service – Start, stop, or restart services on older systems.

? Syntax: service [service] [command]

? Example: sudo service apache2 restart # Restarts the Apache service

7. Log Management and Troubleshooting

tail -f – View the last lines of a file in real-time.

? Syntax: tail -f [file]

? Example: tail -f /var/log/syslog # Continuously outputs new lines added to syslog

cat – Display the contents of a file.

? Syntax: cat [file]

? Example: cat /var/log/nginx/error.log # Displays the contents of the nginx error log

grep – Search for patterns in files or output.

? Syntax: grep [pattern] [file]

? Example: grep "ERROR" /var/log/syslog # Searches for the word ERROR in syslog

journalctl – Query systemd logs.

? Syntax: journalctl [options]

? Example: sudo journalctl -xe # Shows systemd logs for troubleshooting

8. Automation and Scripting

cron – Schedule tasks to run periodically.

? Syntax: crontab [options]

? Example: crontab -e # Opens the cron file for editing

bash – Run shell scripts.

? Syntax: bash [script]

? Example: bash deploy_script.sh # Executes the deploy_script.sh script

chmod +x – Make a script executable.

? Syntax: chmod +x [script]

? Example: chmod +x backup.sh # Makes backup.sh executable

9. Disk and Filesystem Management

fdisk -l – List disk partitions.

? Syntax: fdisk -l

? Example: sudo fdisk -l # Lists all disk partitions

mount – Mount a filesystem.

? Syntax: mount [device] [directory]

? Example: sudo mount /dev/sda1 /mnt # Mounts the /dev/sda1 partition to /mnt

umount – Unmount a filesystem.

? Syntax: umount [device or directory]

? Example: sudo umount /mnt # Unmounts the filesystem mounted on /mnt

10. Backup and Restore

tar – Archive files into a tarball.

? Syntax: tar [options] [archive] [file/directory]

? Example: tar -czvf backup.tar.gz /var/www # Creates a compressed backup of /var/www

rsync – Sync files between local and remote directories.

? Syntax: rsync [options] [source] [destination]

? Example: rsync -avz /var/www user@remote:/backup/ # Syncs the /var/www directory to a remote backup

11. Security and Access Control

iptables – Configure firewall rules.

? Syntax: iptables [options]

? Example: sudo iptables -L # Lists current firewall rules

ufw – Uncomplicated Firewall, a simpler interface for iptables.

? Syntax: ufw [command]

? Example:

sudo ufw allow 22 # Allows SSH (port 22) through the firewall

sudo ufw enable # Enables the firewall

Detailed Hands-on explanation of Linux Commands used by Devops Engineers in Day to day tasks.

Basic Linux Commands

? #pwd → it shows the present working directory

? #ls → it shows available files and directory list in the present working directory.

? #uname → it shows the name of the kernel (OS)

? #uname -r → it shows version of the kernel

? #cd → it use for change directory

? #clear → it use for clear screen

? #whoami → it show currently login user name

? #history → it show list of previously used commands

? #date → it show time and date

Create file or directory

1. For create single directory

#mkdir Prabhu

2. For create multiple directory

#mkdir dev qa test

3. For create directory path (directory inside directory)

#mkdir -p /dev/qa/test/devops

4. For create number of directory

#mkdir /student{1..10}

Create file

? Touch command is used to create empty file, we can’t write data in a file, can’t edit or save file.

Touch:

1. Create single file with touch command

#touch notes

2. Create multiple file

#touch python java react

3. Create number of files

#touch books{1..10}

For Copy & Paste

? cp : cp command is used for copy and paste file or directory

? Syntax:

#cp <option> <source> <destination>

? Options

-r for recursive

-v for verbose

-f for forcefully

1. For copy file

#cp - rvf /root/anaconda-ks.cfg /home

2. For copy all data which start form D alphabet

#cp -rvf /root/D* /home

3. For remove file & directory

1. For delete file or directory

#rm -rvf /india/pune

4. For move or rename file & directory

1. For move file or directory

#mv /home/prabhu/root/Desktop

2. For rename file or directory

#mv dev devops

User Management

1. For create user account

#useradd prabhu

2. For check user account properties

#grep prabhu/etc/passwd

prabhu:1001:1001: /home/prabhu:/bin/bash

3. For create user account password

#passwd prabhu

4. For check user password properties

#grep prabhu /etc/shadow

prabhu:@s$!1bc:18002:0:99999:7: : :

5. For switch user account

#su prabhu

6. For logout from user account

#exit

Or

Press "ctrl+d" key

For Delete user account

#userdel prabhu

For change user Login name

#usermod -l devops prabhu

Group Management

? A group is a collection of user accounts that is very use full to administrators for managing and applying permission to a number of users.

1. For add Group account

#groupadd ibmgrp

2. For check group account property

#grep ibmgrp /etc/group

Ibmgrp:x:1001:ajay, vijay, sachin

3. For check group admin property

#grep ibmgrp /etc/gshadow

Ibmgrp:!::

4. For Delete group Account

#groupdel ibmgrp

5. For add single member in group

#gpasswd -a ajay ibmgrp

6. For add multiple member in group

#gpasswd -M rahul, virat , rohit ibmgrp

7. For remove group member

#gpasswd -d virat ibmgrp

8. For make group admin

#gpasswd -A sachin ibmgrp

Linux File System Permission

Type of File Permission

? Basic Permission

? Special Permission

? Access Control List (ACL) Permission

For check file permission

#ls -l /notes.txt

-rw-r-r- . 1 root root 0 Oct 15 14:59 /notes.txt

? Permission

? Link

? Owner

? Group owner

? Size of file

? Date & time of file creation

? Name of file

For check directory permission

#ls -ld /dev

Permission in details

Type of File Permission

? Basic Permission

? Special Permission

? Access Control List (ACL) Permission

Permission Group

Permission Description

? Owner (u) → Permissions used for the owner of the file

? Group (g) → Permissions used by members of the group

? Other (o) → Permissions used by all other users

Permission Set


Permission with numeric value & Symbol

#chmod 751 /prabhu

For change permission

? For add read permission to owner

#chmod u+r /notes.txt

? For add read write permission to group

#chmod g+rw /notes.txt

? For remove read permission to others

#chmod o-r /notes.txt

For change ownership

Syntax:

#chown <user name> <file/directory name>

Ex: #chown ajay /notes.txt

For change group ownership

Syntax:

#chgrp <group name> <file/directory name>

Ex: #chgrp ibmgrp /notes.txt

Set permission with a numeric value

? r (read) = 4

? w (write) = 2

? x (execute) = 1

For set permission with a numeric value

#chmod 751 /prabhu

Access Control List (ACL)

? Access control list (ACL) provides an additional, more flexible permission mechanism for file systems.

? Access control list is a service which is used for providing special permission to specific users and group to particular directories and file

Use of ACL

? Think of a scenario in which a particular user is not a member of group created by you but still you want to give some read or write access, how can you do it without making user a member of group, here comes in picture Access Control Lists, ACL helps us to do this trick.

For check ACL Permission

Syntax:

#getfacl <name of file or directory>

Ex. #getfacl /devops

For set ACL permission to user

#setfacl -m u:prabhu:rwx /devops

For remove ACL permission of user

#setfacl -x u:prabhu: /devops

For set ACL permission to Group

#setfacl -m g:prabhugrp:rwx /devops

For remove ACL permission of group

#setfacl -x g:prabhugrp: /devops

For remove all ACL permissions

#setfacl -b /devops

Regular Expressions, What are Regular Expressions?

? Regular expressions are special characters which help search data, matching complex patterns.

GREP (Global Regular Expression Print)

? The grep filter searches a file for a particular pattern of characters, and displays all lines that contain that pattern.

1. Search a word (string in a file)

#grep root /etc/passwd

2. Search a string in multiple files

#grep root /etc/passwd /etc/group

3. Search a string insensitive in file

#grep -i Root /etc/passwd

4. Search a string in all files recursively

#grep -r root/

5. Inverting the string match

#grep -v root /etc/passwd

6. Displaying the string match total line no

#grep -c root /etc/passwd

7. Display the file names that match the string

#grep -l root /etc/passwd/etc/shadow

8. Display the file names that do not contain the string

#grep -L root /etc/passwd /etc/shadow

9. Displaying the string match line with number

#grep -n root /etc/passwd

10. Display the lines that start with a string

#grep ^root /etc/passwd

11. Display the lines that end with a string

#grep /bin/bash$ /etc/passwd

12. Search and redirect output in a new file

#grep /bin/bash$ /etc/passwd

Find

? The Linux Find Command is one of the most important and much used command in Linux systems.

? Find command used to search and locate the list of files and directories based on conditions you specify for files that match the arguments.

? Find can be used in a variety of conditions like you can find files by permissions, users, groups, file type, date, size, and other possible criteria

1. Find files under Home directory

#find /home -name prabhu.txt

2. Find files with suid permission

#find / -perm 4755

3. Find files with guid permission

#find / -perm 2644

4. Find files with sticky bit permission

#find / -perm 1755

5. Using Find command based on users

#find / -user root

6. Using Find command based on groups

#find / -group prabhugrp

7. Search the file with less than 10MB in a folder

#find /tmp -size -10M

8. Search the file with more than 10MB in a folder

#find /tmp -size +10M

WC (Word Count)

? The wc command is use for the count word and line numbers.

1. Count number of lines

#wc -l /etc/passwd

2. Count number of words

#wc -w /etc/passwd

Head

? Head command is used for to display top line of the file.

1. Display top 10 line of the file

#head /etc/passwd

2. Display top specific no line of the file

#head -n 15 /etc/passwd

tail

? Tail command is used for to display the bottom line of the file.

1. Display bottom 10 line of the file

#tail /etc/passwd

2. Display bottom specific line of the file

#tail -n 5 /etc/passwd

Archive File in Linux

? Archiving is the process of combining multiple files and directories (same or different sizes) into one file. Archive process is very useful for backup and compression size of data in Linux.

What is Tar

? The Linux “tar” stands for tape archive, which is used by large number of Linux/Unix system administrators to compress size or drives backup. For create archive tar used some compression algorithms Such as gzip,bz2 and xz

Tar command syntax

#tar <options> <files>

Commonly used options

? c -for create

? x -for extract

? v -for verbose

? f -for forcefully

? t -for test

? z -for gzip

? j -for bz2

? J -for xz

? C -for specific destination

1. To create a tar archive file

#tar -cvf /mnt/backup.tar /var

2. To show file size in human-readable format

#du -sh /var

#du -sh /mnt/backup.tar

3. To extract a tar archive file on the default location

#tar -xvf /mnt/backup.tar

4. To extract a tar archive file on the specific location

#tar -xvf /mnt/backup.tar -C /root/Desktop/

5. To create a tar archive file with compress in size (gzip)

#tar -cvzf /mnt/backup.tar.gz /var

6. To extract a tar archive file with compress in size (gzip)

#tar -xvzf /mnt/backup.tar.gz

7. To create a tar archive file with compress in size (bzip2/bz2)

#tar -cvjf /mnt/backup.tar.bz2 /var

8. To extract a tar archive file with compress in size (bzip2/bz2)

#tar -xvjf /mnt/backup.tar.bz2

9. To create a tar archive file with compress in size (xz)

#tar -cvJf /mnt/backup.tar.xz /var

10. To extract a tar archive file with compress in size (xz)

#tar -xvJf /mnt/backup.tar.xz


Job Automation

? Job automation allows us to perform tasks automatically in OS by using tools.

? This feature is very useful for the administrator to assign tasks to OS whenever he is not present or performing daily basis work.

Two type of job automation

1. at—command is used to execute a job only one time.

2. crontab—Crontab command is used to execute a job multiple times.

? To a set job with at command

#date

#at 8:10 AM

at>useradd prabhu

at>

Ctrl+d (write & quit)

? To show pending a job

#atq

? To remove a job

#atrm 2

? To restrict user from accessing at

#vim /etc/at.deny

prabhu (add here user name)

:wq (write & quit)

? To start crond service

#systemctl start crond

? To enable crond service (Permanent on)

#systemctl enable crond

? For set cron jobs

#crontab -e

? To show cron jobs of the current user

#crontab -l

? To remove cron jobs

#crontab -r

Or

? Go to the crontab file and remove the job line

#crontab -e

? To the set cron job to other users

#crontab -u prabhu -e

? To show cron job, other user

#crontab -u prabhu -l

? To restrict user from crond service

#vim /etc/cron.deny

Sudo Command

? sudo (“superuser do”, or “switch user do”) allows a user with proper permissions to execute a command as another user, such as the superuser.

? sudo allows a permitted user to execute a command as another user, according to specifications in the /etc/sudoers file.

Provide sudo privilege to the user

? For edit configuration file:

# vim /etc/sudoers

root ALL=(ALL) ALL

amir ALL=(ALL) ALL (add this line appro. 101 lines)

:wq

Provide sudo privilege to the group

? For edit configuration file:

# vim /etc/sudoers

%punegrp ALL=(ALL) ALL (line number 108)

:wq

? By default, all members of punegrp group got sudo privileges

Wheel group

? Wheel is a system group that by default has sudo privileges, if we add any member to that group then that user got sudo privilege

#grep wheel /etc/group

#useradd prabhu

#passwd prabhu

#gpasswd –a prabhu wheel

By default, all members of the wheel group got sudo privileges

Sudo without password

? For edit configuration file:

# vim /etc/sudoers

amir ALL=(ALL) NOPASSWD: ALL

%punegrp ALL=(ALL) NOPASSWD: ALL

:wq

Managing networking based on Red Hat Enterprise Linux

To show ip address

#ifconfig

Or,

#ip addr

Configure networking with nmcli

? Network Manager is a daemon that monitors and manages network settings.

? nmcli command used to manage networking

Manage IP configuration

? For Show all list of connection

#nmcli con show

? To show a active connection

#nmcli con show --active

? To show a specific connection

#nmcli con show "citynet"

? To show the device status

#nmcli dev status

? To create a new connection with nmcli

#nmcli con add con-name "citynet" ifname ens33 type ethernet

ipv4.add 192.168.0.2/24 gw4 192.168.0.1 ipv4.dns 191.168.0.1

connection.autoconnect yes ipv4.method manual

? To show the device status

#nmcli con mod “citynet” ipv4.add 192.168.0.100

#nmcli con mod “citynet” gw4 192.168.0.254

#nmcli con mod “citynet” ipv4.dns 192.168.0.254

#nmcli con down citynet

#nmcli con up citynet

#nmcli con show—active

#nmcli con show citynet

? To activate new connection

#nmcli con up "citynet"

#nmcli con show-active

? To deactivate connection

#nmcli con down citynet

? To start a new connection and stop the old connection

#nmcli connection modify "citynet" connection.autoconnect no

#nmcli connection modify "pune" connection.autoconnect yes

? To remove existing connection

#nmcli con delete citynet

? To set hostname

#hostnamectl set-hostname it.citynet.com

? To show hostname

#hostname

? Configuring networking with nmtui

#nmtui

IP address configuration files

? All created connections with nmcli and nmtui by default are stored in the following file

#cd /etc/NetworkManager/system-connections/

#ls

Note : We can also modify the connection using the above connection file, but it is not recommended.

If update the file, restart NetworkManager service to update IP-configuration

#systemctl restart NetworkManager


RahulSagar Kulkarni

RTL WellsFargo IOS | Objective-C | Swift | SwiftUI

4 个月

Very informative

回复
Dr. NITIN KULKARNI.

Offshore Structural Engineer at TechnipFMC

5 个月

Very informative

回复

要查看或添加评论,请登录

Prabhuraj Kulkarni的更多文章

  • Kubernetes Architecture

    Kubernetes Architecture

    Kubernetes Architecture Explained This document explains the key components that make up the architecture of a…

  • Docker Fundamentals

    Docker Fundamentals

    What is a container ? A container is a standard unit of software that packages up code and all its dependencies so the…

  • Linux File System Hierarchy with real time examples

    Linux File System Hierarchy with real time examples

    The Linux File System Hierarchy is an organized structure that defines where specific types of files should be located…

  • Linux Architecture with real time examples

    Linux Architecture with real time examples

    Linux is at the heart of modern computing, especially in DevOps, cloud computing, and server management. Its robust and…

社区洞察

其他会员也浏览了