原文地址:https://www.tecmint.com/linux-commands-for-sysadmins/
For someone new to Linux, using it can still feel challenging, even with user-friendly distributions like Ubuntu and Mint.
While these distributions simplify many tasks, some manual configuration is often required, but fully harnessing the power of Linux, understanding basic commands is essential.
In Linux, commands are the primary way to interact with the system and allow users to perform tasks, configure settings, and manage the system efficiently.
This guide introduces 60 essential Linux commands, providing a foundation for beginners and a pathway to becoming a skilled system administrator. These commands cover a wide range of functions, from navigating directories and managing files to performing advanced system operations.
Whether you’re just starting out or aiming to deepen your expertise, these commands will help you unlock the full potential of Linux.
Whether you’re a beginner or an advanced user, mastering basic Linux commands is essential for navigating and controlling the operating system.
Below are some commonly used Linux commands along with their descriptions and examples:
The ls command is used to view the files and directories within a specified directory, it can display both visible and hidden files (files starting with .
are hidden by default).
Additional options can provide detailed information like file permissions, ownership, size, and modification dates.
ls -la
The lsblk command displays information about block devices such as hard drives, partitions, and external drives in a tree-like format.
lsblk
The sha256sum
or md5sum
commands generate and verify cryptographic hash values for files, which ensures the file’s integrity after downloading or transferring.
sha256sum file.tar.gz
The dd command is used for low-level file copying and conversion, which is often employed to create bootable USB drives or clone disk images.
dd if=input.img of=output.img bs=4M status=progress
The uname command provides system information, including the kernel version, system architecture, and hostname.
uname -a
The history command lists previously executed commands, allowing you to recall or reuse them.
history
The sudo command allows users to execute commands with administrative privileges. Use sudo -i
to switch to a root shell.
sudo -i
The mkdir command creates a new directory with the specified name in the current location.
mkdir my_folder
The touch command creates empty files or updates the timestamp of existing ones.
touch my_file.txt
The chmod
command changes file permissions, controlling who can read, write, or execute a file.
chmod 644 my_file.txt
The chown command changes file ownership and group ownership.
sudo chown user:group my_file.txt
The apt command manages software packages on Debian-based systems.
sudo apt update && sudo apt install package_name
The tar command extracts compressed files.
tar -xvzf file.tar.gz
The date and cal
commands display the current date, time, and calendar.
date
cal
The cat command prints the contents of a file to the terminal.
cat my_file.txt
The cp and mv commands copy and move files, respectively.
cp source.txt destination.txt
mv old_name.txt new_name.txt
The pwd command prints the current directory path.
pwd
The cd command navigates between directories.
cd /path/to/directory
Once you’ve mastered the basic Linux commands, it’s time to dive deeper into more advanced commands that will help you manage and optimize your system more efficiently.
The find command is used to search for files and directories within a specified directory or across the entire file system.
find /path/to/directory -name "filename"
The grep command is used to search for specific patterns (keywords) within files, which is highly useful when you want to find specific information inside a file or a set of files.
grep "keyword" filename
The man command displays the manual or help documentation for a specific command, which provides detailed information about how to use a command, its options, and its syntax.
man ls
The ps command shows a snapshot of the current processes running on your system with details like process IDs (PIDs), memory usage, and CPU usage.
ps aux
To list all processes related to Firefox, using grep
to filter the output.
ps aux | grep firefox
The kill command is used to terminate a running process by specifying the process ID (PID) of the process you want to terminate.
kill 1234
The which
command helps locate the path of executables in your system’s PATH by searching through the directories listed in the PATH environment variable and returns the location of the executable.
which python3
The systemctl command is used to manage system services (also known as daemons) on systems using systemd.
systemctl start service_name
systemctl stop service_name
systemctl restart service_name
Aliases are shortcuts for commands, which can save time by reducing the amount of typing and the alias command creates a shortcut, while unalias remove it.
alias shortcut_name='command'
unalias shortcut_name
You can create an alias for a command with a custom name.
alias ll='ls -l'
unalias ll
This creates an alias ll
for the ls -l
command, which lists directory contents in a long format.
The df command shows disk space usage for all mounted file systems by providing information about the total space, used space, and available space.
df -h
The rm command is used to remove files and directories. You can use the -r
option to remove directories and their contents recursively.
rm filename
rm -r directory_name
The echo command is used to print text or the value of a variable to the terminal.
echo "Custom Message"
The passwd
command is used to change passwords for the current user or other users (if you are the root user).
passwd username
The lpq
command shows the status of the printing queue, including any pending or completed print jobs.
lpq
The diff command compares two files line by line and displays the differences between them.
diff file1 file2
The wget command is used to download files from the internet, it supports HTTP, HTTPS, and FTP protocols.
wget https://example.com/file.zip
The mount
command is used to attach a block device (e.g., a hard drive or USB drive) to a directory in the filesystem.
mount /dev/sdX /mnt
To compile and run code in C
, C++
, and Java
, you use the respective compilers and runtimes.
- C:
gcc
is used to compile C programs.c. - C++:
g++
is used to compile C++ programs. - Java:
javac
is used to compile Java programs, and java is used to run them.
To compile and run C
code:
gcc -o outputfile sourcefile.c
./outputfile
To compile and run C++
code:
g++ -o outputfile sourcefile.cpp
./outputfile
To compile and run Java
code:
javac filename.java
java filename
In the last section of this series, we tried to cover the commands that needed to administer a Linux server.
The ifconfig command is used to allow you to set up, manage, and display network interface parameters, which is typically used to assign IP addresses, configure network interfaces, and troubleshoot network issues.
sudo ifconfig eth0 192.168.1.100 netmask 255.255.255.0 up
This command assigns the IP address 192.168.1.100
to the eth0
interface and brings it up.
The netstat command provides detailed information about network connections, routing tables, interface statistics, and more.
netstat -tuln
The curl command is used to transfer data from or to a server. It supports various protocols, including HTTP, FTP, and more. You can use it to fetch data from a web server or test server responses.
curl -I https://www.example.com
The dig command is a DNS lookup utility that provides detailed information about DNS records, which is commonly used for troubleshooting DNS issues.
dig example.com
The uptime command shows how long the system has been running, the number of users logged in, and the system load averages for the past 1, 5, and 15 minutes.
uptime
The wall
command is used to send messages to all users currently logged into the system.
echo "System Maintenance in 10 minutes" | wall
The write
command allows you to send a message to another logged-in user.
write username
You can combine multiple commands using pipes (|)
to pass the output of one command to another.
ps aux | grep firefox
The top command displays a dynamic, real-time view of system processes, including CPU and memory usage.
top
The mkfs.ext4
command is used to create an ext4 file system on a partition.
sudo mkfs.ext4 /dev/sdb1
In Linux, there are several text editors available for editing files directly from the command line. Among the most popular are vi, emacs, and nano.
To edit files with vi
:
vi filename.txt
To edit files with emacs
:
emacs myfile.txt
To edit files with nano
:
nano myfile.txt
The rsync command is used to copy files and directories. With the --progress
option, it shows a progress bar.
rsync -avh --progress source_directory/ destination_directory/
The free command shows the amount of free and used memory in the system.
free -h
The mysqldump command is used to create backups of MySQL databases.
mysqldump -u root -p database_name > backup.sql
The openssl
command can generate a random password.
openssl rand -base64 12
The cat
command can be used to concatenate two text files into one.
cat file1.txt file2.txt > mergedfile.txt
The lsof command lists all open files and the processes that opened them.
lsof
These commands are fundamental tools for system administrators and users to interact with Linux systems efficiently.