In this tutorial, we’ll cover 20 of the most essential Linux commands that every system administrator should know. These commands are the building blocks of many common tasks, and with a good understanding of them, you’ll be able to accomplish a lot on your Linux servers.
What is a Command?
A Linux command is a set of instructions or operations that can be executed in the command-line interface (CLI) of a Linux operating system. Linux commands are used to perform various tasks on a Linux system, such as managing files and directories, managing system processes, configuring the system, and much more. Linux commands are typed into the terminal and can be executed by pressing the enter key. There are hundreds of Linux commands available, and each command has its own set of options and arguments that can be used to customize its behavior.
What is Command Line Interface (CLI)?
The Command-Line Interface (CLI), also known as the command-line shell, is a means of interacting with a computer’s operating system by typing commands into a terminal or console window. The CLI provides users with a text-based interface that allows them to navigate the file system, run programs, and perform various other tasks by entering commands into the terminal. Unlike graphical user interfaces (GUIs), which use a mouse and visual elements to interact with the system, the CLI relies solely on text-based commands, making it a powerful and flexible tool for performing advanced system administration tasks and automation.
1. `ls`
Command (List Files and Directories)
The ls command is used to list the files in a directory. By default, ls will show you the files in the current directory, but you can specify a different directory by including its path as an argument.
Here are a few examples of how to use the ls command:
- Show the files in the current directory
ls
- Show the files in the /etc directory
ls /etc
- Show the files in the current directory, including hidden files
ls -a
- Show the files in the current directory, including hidden files and details about each file
ls -al
2. `cd`
Command (Change Directory)
The cd command is used to change the current working directory. When you open a terminal window, you start in your home directory. The cd command lets you navigate to other directories on your file system.
Here are a few examples of how to use the cd command:
- Change to your home directory
cd ~
- Change to the
/etc
directorycd /etc
- Change to the parent directory of the current directory
cd ..
3. `pwd`
Command (Print Current Working Directory)
The pwd command is used to print the current working directory. This command is useful for determining your current location on the file system.
Here’s an example of how to use the pwd command:
- Print the current working directory
pwd
- Show the physical path of the directory in case of a symbolic link:
pwd -P
4. `cat`
Command (Read and Print File Content)
The cat command is used to concatenate and display the contents of one or more files. This command is useful for quickly viewing the contents of a file.
Here are a few examples of how to use the cat command:
- Show the contents of a file
cat file.txt
- Concatenate and display the contents of two files
cat file1.txt file2.txt
- Display the contents of a file, one page at a time
cat file.txt | less
5. `grep`
Command (Pattern Searching)
The grep command is used to search for a pattern in one or more files. This command is useful for finding specific information in large files.
Here are a few examples of how to use the grep command:
- Search for a pattern in a file
grep pattern file.txt
- Search for a pattern in multiple files
grep pattern file1.txt file2.txt
- Search for a pattern in a file, and show the line numbers of the matches
grep -n pattern file.txt
- Search for a pattern in a file, and show only the matches, not the entire lines
grep -o pattern file.txt
6. `tail`
Command (Print last lines from content)
The tail command is used to display the last few lines of a file. This command is useful for monitoring log files and other text files.
Here are a few examples of how to use the tail command:
- Show the last 10 lines of a file
tail file.txt
- Show the last 20 lines of a file
tail -n 20 file.txt
- Continuously display the end of a file as it grows
tail -f file.txt
7. `head`
Command (Print start lines from content)
The head command is used to display the first few lines of a file. This command is useful for quickly reviewing the contents of a file.
Here are a few examples of how to use the `head` command:
- Show the first 10 lines of a file
head file.txt
- Show the first 20 lines of a file
head -n 20 file.txt
8. `cp`
Command (Copy Files)
The cp command is used to copy files and directories. This command is useful for making backups of important files and for copying files between directories.
Here are a few examples of how to use the `cp` command:
- Copy a file
cp file.txt file_copy.txt
- Copy a directory and its contents
cp -R dir1 dir2
- Copy a file, and preserve its permissions and timestamps
cp -p file.txt file_copy.txt
9. `mv`
Command (Move or rename files)
The mv command is used to move or rename files and directories. This command is useful for reorganizing your files and directories.
Here are a few examples of how to use the `mv` command:
- Move a file
mv file.txt dir1/
- Rename a file
mv file.txt file_renamed.txt
10. `rm`
Command (Delete files)
The rm command is used to delete files and directories. This command is useful for freeing up space on your file system and for removing unneeded files.
Here are a few examples of how to use the `rm` command:
- Delete a file
rm file.txt
- Delete a directory and its contents
rm -r dir1
11. `chmod`
Command (Change Permissions)
The chmod command is used to change the permissions of files and directories. This command is useful for controlling who can read, write, and execute files on your system.
Here are a few examples of how to use the `chmod` command:
- Give the owner of a file read and write permissions
chmod u+rw file.txt
- Give everyone execute permissions on a file
chmod +x file.txt
- Set the permissions of a file to 644
chmod 644 file.txt
12. `chown`
Command (Change ownership)
The chown command is used to change the owner of a file or directory. This command is useful for changing the ownership of files and directories so that they match the permissions on your system.
Here are a few examples of how to use the `chown` command:
- Change the owner of a file to user1
chown user1 file.txt
- Change the owner and group of a file to user1 and group1
chown user1:group1 file.txt
13. `df`
Command (Check free disk)
The df command is used to display information about disk space usage on your system. This command is useful for monitoring the available space on your file system.
Here are a few examples of how to use the `df` command:
- Show disk space usage for all file systems
df
- Show disk space usage in a human-readable format.
df -h
14. `du`
Command (Check disk uses)
The du command is used to estimate the space used by a file or directory. This command is useful for finding out which files or directories are using the most space on your file system.
Here are a few examples of how to use the du command:
- Show the size of a directory and its contents
du -sh dir1
- Show the size of a directory and its contents, including hidden files
du -sh dir1 --exclude=".*"
- Show the size of a directory, sorted by size
du -sh dir1 | sort -hr
15. `top`
Command (Display running processes)
The top command is used to display information about the processes running on your system. This command is useful for monitoring the performance of your system and identifying processes that are using too much CPU or memory.
Here are a few examples of how to use the top command:
- Show the top processes on your system
top
- Show only processes owned by a specific user
top -u user1
- Show the full command line for each process
top -c
16. `kill`
Command (Terminate processes)
The kill command is used to send a signal to a process, causing it to terminate. This command is useful for ending processes that have become unresponsive or that you no longer need.
Here are a few examples of how to use the kill command:
- End a process with a specific PID
kill 12345
- End a process gracefully using the INT signal
kill -INT 12345
- End a process immediately using the KILL signal
kill -KILL 12345
17. `ssh`
Command (Remote shell access)
The ssh command is used to securely log in to a remote system. This command is useful for remotely managing servers and other systems.
Here are a few examples of how to use the ssh command:
- Login to a remote system as user1
ssh [email protected]
- Login to a remote system as user1 and run a command
ssh [email protected] "ls -l"
- Login to a remote system using a key pair for authentication
ssh -i /path/to/private/key [email protected]
18. `scp`
Command (Remote copy)
The scp command is used to securely copy files between systems. This command is useful for copying files to and from remote systems.
Here are a few examples of how to use the scp command:
- Copy a file from the local system to a remote system
scp file.txt [email protected]:~/
- Copy a file from a remote system to the local system
scp [email protected]:file.txt .
- Copy a directory and its contents from the local system to a remote system
scp -r dir1 [email protected]:~/
19. `sudo`
Command (Priviledged access)
The sudo command is used to run a command as another user, typically the root user. This command is useful for performing administrative tasks on a system.
Here are a few examples of how to use the sudo command:
- Run a command as the root user
sudo command
- Run a command as the root user and keep the environment variables from your current user
sudo -E command
- Run a command as another user
sudo -u user2 command
- Run a graphical application as the root user
sudo -i gedit
- Run a command and keep the terminal open after the command completes
sudo -b command
20. `nano`
Command (Command line editor)
The nano command is a text editor that is commonly used in Linux systems. This command is useful for editing configuration files and other text files on a system.
Here are a few examples of how to use the `nano` command:
- Open a file for editing
nano file.txt
- Press
Ctrl + O
to save the changes to a file - Press
Ctrl + X
to exit nano - Press
Ctrl + W
to search for text in a file
Conclusion
In conclusion, the 20 commands listed above are some of the most essential Linux commands for system administrators. It is important to become familiar with these commands and how to use them effectively, as they will help you perform many common administrative tasks on your system. Whether you are a seasoned system administrator or just starting out, these commands are a great place to start.