DEV Community

Cover image for Linux Commands — Devops Prerequisite 1
Aaditya Kediyal
Aaditya Kediyal

Posted on

Linux Commands — Devops Prerequisite 1

Here are the some commands with explanation which are important to know before starting your DevOps journey.

1. System Info Commands

hostname — shows the name of the system host.

➜ ~ hostname
localhost
Enter fullscreen mode Exit fullscreen mode

hostid — shows the host id of the system assigned by the OS.

➜ ~ hostid
0a123456
Enter fullscreen mode Exit fullscreen mode

date — shows the current date and time in UTC format.


➜ ~ date
Wed Jan 19 12:34:56 UTC 2024
Enter fullscreen mode Exit fullscreen mode

uptime — shows the elapsed time duration since the machine logged in.


➜ ~ uptime
12:34:56 up 1 day, 3:45, 2 users, load average: 0.25, 0.20, 0.18
Enter fullscreen mode Exit fullscreen mode

uname — unix name.

➜ ~ uname
Linux
Enter fullscreen mode Exit fullscreen mode

clear — clears the screen.

➜ ~ clear

history — lists all the commands executed until now.

➜ ~ history
1 ls
2 cd Documents
3 nano file.txt
4 gcc program.c -o program
5 ./program
6 history
Enter fullscreen mode Exit fullscreen mode

sudo — Super User Do.


➜ ~ sudo su — USERNAME
Enter fullscreen mode Exit fullscreen mode

echo $? — shows the exit status of the last executed command (0 — success, 1–255 — error/failure).

➜ ~ echo $?
127

shutdown -r now — restart the machine immediately (-r restart).

➜ ~ sudo shutdown -r now
Broadcast message from user@hostname
(/dev/pts/0) at 12:34 …

The system is going down for reboot NOW!
Enter fullscreen mode Exit fullscreen mode

printenv — displays all the environment variables of the Linux system.

➜ ~ printenv
TERM=xterm-256color
SHELL=/bin/bash
USER=your_username
…
Enter fullscreen mode Exit fullscreen mode

last — shows previous logins in the Linux system.

➜ ~ last
root pts/0 Wed Jan 19 12:34 still logged in
reboot system boot 5.4.0–96-generic Sat Jun 1 12:33 still running
Enter fullscreen mode Exit fullscreen mode

*systemctl * — System Control: Manage system services using systemd.

➜ ~ systemctl status sshd
● sshd.service — OpenBSD Secure Shell server
Loaded: loaded (/lib/systemd/system/sshd.service; enabled; vendor preset: enabled)
Active: active (running) since Sat 2024–06–01 12:34:56 UTC; 3h ago
Docs: man:sshd(8)
man:sshd_config(5)
Process: 1234 ExecStartPre=/usr/sbin/sshd -t (code=exited, status=0/SUCCESS)
Main PID: 5678 (sshd)
Tasks: 1 (limit: 1234)
Memory: 2.3M
CPU: 12ms
CGroup: /system.slice/sshd.service
└─5678 /usr/sbin/sshd -D

Jan 19 12:34:56 hostname systemd[1]: Starting OpenBSD Secure Shell server…
Jan 19 12:34:56 hostname sshd[5678]: Server listening on 0.0.0.0 port 22.
Jan 19 12:34:56 hostname sshd[5678]: Server listening on :: port 22.
Jan 19 12:34:56 hostname systemd[1]: Started OpenBSD Secure Shell server.

Enter fullscreen mode Exit fullscreen mode

2. File Commands

touch — creates an empty file or updates timestamp of the existing file.

touch <fileName> — creates a single empty file.
touch <file1> <file2> — creates file1, file2 empty files.

Enter fullscreen mode Exit fullscreen mode

cat — concatenates and displays the contents of files.

cat <fileName> — displays the contents of the file.
cat > <fileName> — creates a new file, allows to input content interactively and redirects inputted content to the created file (> redirection operator).

Enter fullscreen mode Exit fullscreen mode

➜ ~ head -n 5 help.txt

  1. Commands shortcut ….
  2. huddle — Connect to Syncup Call

tail — displays the last 10 lines of the file by default.

tail -F — displays contents of the file in real-time even when the file is rotated or replaced (used for log file monitoring).

➜ ~ tail -F mySystem.logs
echo “I love DevOps”
echo “Best Linux commands”
….

less — used to view large files (log files) in a paginated manner.

rm — remove command.

r

m <fileName> — removes the file.
rm -r <dirName> — removes files & folders of directory recursively (-r recursive).
rm -rf <dirName> — force remove the files & folders of directory recursively (-f force).
Example: rm -r ./test

Enter fullscreen mode Exit fullscreen mode

cp — copy command.

cp <source> <destination> — copy the files and folders from source to destination.
cp -r <dir1> <dir2> — copy dir1 directory to dir2 directory recursively (-r recursive).
Example: cp -r ./sourceDir ./destiDir

Enter fullscreen mode Exit fullscreen mode

File Permission Commands

ls -l — shows the permissions of the file.

*ls -ld *- shows the permissions of the directory.

chmod — changes mode/permissions of the file.

chmod -R — changes mode/permissions of the directory recursively.

chown — changes the user ownership of a file.

chown : — changes the user & group ownerships of a file.

chgrp — updates the group name for file/directory.

getfacl — shows the file/directory access control list.
**
setfacl -m u::rwx **- modifies the current acl of the file/directory.

setfacl -x u:: — removes the acl permissions for the file/directory.

setfacl -m g::rwx — modifies the group acls for the file/directory.

setfacl -x g:: — removes the group acl permissions for the file/directory.
File Permission Octal Numbers

read (r) — 4, write (w)- 2, execute (x) — 1

Sum the numbers to generate an octal number for setting permissions on a file or directory.

User Management Commands

ac — Total connect time for all users or specified users.

The ac command reads the /var/log/wtmp file, which contains binary data about every login, logout, system event, and current status on the system. It gets its data from the wtmp file.
Display total login time of a specific user.
ac john
Display total login time for each user.
ac -p
Display total login time for each day.
ac -d
Display total login time for the current day.
ac -d -p
Display login time from a specific log file.
ac -f /var/log/wtmp

*useradd *- Creates a user account.

** useradd ** — Creates user account without home & mail spool directories.
Example: useradd bot
useradd -m — Creates user account with home & mail spool directories.
Example: useradd -m bot

*passwd *- The system generates a password for the user and then stores it in the /etc/shadow file.

*userdel *- Deletes User Account.

** userdel ** — deletes the user from the system.
userdel -r — deletes the user from the system along with home and mail spool directories.
Example: userdel -r bot
**
/etc/passwd** — Stores information about user accounts.

*cat /etc/passwd *- displays the complete list of users on that machine.

*/etc/shadow *- stores the password for users in an encrypted format.

cat /etc/shadow — displays the complete list of user passwords on that machine.

su — substitute user.

su — switches to the user mentioned.
exit — to logout from that user.
Example: su — ram

usermod — modify user.

usermod -aG — adds the user to another group (-aG append the user to the group without removing from other groups).
Example: usermod -aG mygroup ram

chsh — change shell.

chsh -s /bin/bash — changes the shell to bash for the user.
chsh -s /bin/sh — changes the shell to sh for the user.
Example: chsh -s /bin/sh ubuntu

3. Group Management Commands

groupadd — creates the group.

groupdel — delete the group.

*/etc/group *- stores the information of the groups.

cat /etc/group — displays the complete list of groups on that machine.

gpasswd — creates a password for the group.

** gpasswd -a ** — adds the user to the group.
** gpasswd -d ** — removes the user from the group.
gpasswd -M ,, — adds multiple users to the group and removes the existing ones of the group.

4. Searching Commands

find — Search for files/directories based on their names.

locate — Search for files/directories based on their names.

*locate *- locates the file/directory and displays the path.
Example: locate crazy.txt
**

  1. GREP Command — Global Regular Expression Print**

grep — used to find text patterns within files.
** grep -i ** — used to find text patterns within the file ignoring the case (-i ignore case).
grep -v — used to find non matching lines of text patterns (-v invert-match).
grep -l — used to display the matching string file names.
Example: grep -l welcome crazy.txt
There are additional commands related to grep.
egrep (or grep -E)
fgrep (or grep -F)
zgrep (for compressed files)
zegrep (or zgrep -E for compressed files)
bzgrep (for compressed files)
ack-grep (Ack)

5. Hardware Infomation Commands

*free -h *- Display system memory information in human-readable format (-h).

*df -h *- It displays the disk space usage of mounted file systems.

du — Disk usage.

** du -h **- Display disk usage information in human-readable format.

6. Connection To Remote System

ssh — Secure Shell: Connect to a remote server securely.
Example: ssh user@remote_host

scp — Securely Copy Files: Copy files between local and remote systems using SSH.
Example: scp file.txt user@remote_host:/path

rsync — Remote Sync: Synchronize files and directories between systems.
Example: rsync -avz local_folder/ user@remote_host:remote_folder/
Network Commands

nc — Simple tcp proxy, network daemon testing

Example: nc -vz google.com 443

ping — tests the reachability & responsiveness of the remote host.

Example: ping google.com -c 2 (-c pings 2 times)

dig — Shows DNS information of the domain.

Example: dig medium.com

wget - Used to retrieve/download files from the internet.

*curl *- client URL.

curl — Used to retrieve/download files from the internet.

ifconfig — Display available network interfaces.

*ip addr *- Display and manipulate network interface info.

curl ifconfig.me — Shows the public ip address of the machine.
**
netstat -antp**- shows all tcp open ports (-a all, t-tcp, n-active, p protocol).

traceroute — traces the route using packets from source to destination host.
Process Information Commands

ps — Process status.

ps — Displays the currently running process.
ps -u - Displays the process of the username
ps -ef — Displays all the processes of the system.

*top *- Shows the real-time, dynamic view of the running processes of a system.

kill — Gracefully terminates the process pid(-9 forcefull).
**
pgrep ** — Shows process id of processes based on name/other criteria.

bg — background, sends the process to the background & continues execution without interruption.

fg — foreground, brings the process to the foreground and makes it an active process.

nohup — no hangup, runs command/script in the background even after the terminal is closed or the user logs out.

Example: nohup ./script.sh

& — Using in last of command runs in background, allowing you to continue using the terminal while the command runs asynchronously.

Example: ./script.sh &

7. Archiving File Commands

*tar *- tape archive.

tar -cvf — creates the tar file with the fileName for the directory mentioned (-c create, -v verbose, -f output file name).
tar -xvf -C — puts the extracted files into the destination directory (-x extract, -v verbose, -f source tar file name, -C change the folder and download to destination dir).

8. Ubuntu Package related Commands

apt — Package Manager for Debian-based Linux distributions Eg: Ubuntu.

apt — Anewer version of the package manager with colorized output, progress bar and additional functions.
apt-get — Older version and basic package manager.

apt update — Updates the package list.

apt list — installed — Lists all the installed packages.

apt list — installed — shows the package name if it’s installed.

apt show — shows information about a package mentioned.

apt search — searches and shows the list of packages.
**
apt install ** — installs the required package.

apt remove — removes the required package.

apt purge — removes the required package along with its config files.

Note: For other package manager just replace “apt” with other package manager

9. Directory Commands

pwd — shows the present working directory (abbr. Print Working Directory).

cd — change directory.

cd .. — changes to its parent directory (i.e) one level up.
cd — change to the directory mentioned.
cd ~ or cd — changes to the currently logged in user’s home directory.
cd ../.. — changes the directory two levels up.
cd — — changes to the last working directory.

*mkdir *- make directory.

mkdir — creates the directory.
mkdir -p — creates directory with its parent directories if it does not exists (-p parent).

ls — lists the files & folders of the directory you are in.

ls -a — lists all files & folders along with hidden files (-a all).
ls -al — lists all files & folders along with hidden files in a formatted manner (-l long listing format).

10. Misc Commands

** man** — Displays the manual page for a specific command. Provides detailed information and usage instructions.
** sed - Edits a stream of text by substituting occurrences of a pattern with another.
** awk *- A powerful programming language for text processing.
*
wc *-(Word Count)
*
ln *-(Create Links):
*
stat **- shows detailed information about the file or directory.
**cron
— system daemon for managing scheduled tasks.
** crontab -Used to create, edit, and manage cron jobs.
*tree *- Representation of files and directories of a specific directory.
** echo “sample text” | grep text
— The output of the first command is passed as an input to the second command using the pipe (|) symbol.
ls -l | tee file.txt — Redirects the list to the file.txt and simultaneously displays it in the terminal.
** echo “sample text” > ** — Write the content to the file mentioned by overwriting the existing content (> redirection operator).
** echo “new sample text” >> ** — Appends the contents to the file mentioned without overwriting the existing content (>> redirection operation).

Conclusion

DevOps experts frequently use a core set of Linux commands to manage systems, automate workflows, and maintain seamless infrastructure operations. These commands are fundamental for DevOps activities and find applications in diverse areas, including system administration and deployment automation.

Top comments (0)