Administrators typically rely on a command-line interface (CLI) when managing a Linux remote system (such as a virtual private server). Numerous Linux distros feature a graphical user interface (GUI), which some users may find more helpful than a CLI. However, entering commands tends to be smoother and more flexible via a CLI.
For instance, you can use one command to replace certain entries across several files with a CLI, but that takes longer when using a GUI. As Linux has lots of commands for different tasks, though, it can be difficult to utilize Bash (Bourne Again Shell) shell.
That’s why we’ve created the following list of 60 need-to-know Linux commands for efficient virtual private server management.
- Managing User and Permissions
- Managing Files and Directories
- Commands for Processing and Searching Text
- Network Management and Problem Solving Commands
- Miscellaneous commands
- System Management and Information Commands
- More Hints for Using Linux Commands
- Conclusion
Managing Users And Permissions
1. su command
You can use the su command to run a Linux shell program as another user, which is helpful for connecting through SSH when the root user is deactivated. The basic syntax is:su [options] [username [argument]]
With no argument or option, su command runs via root and informs the user to utilize the sudo privileges on a temporary basis. You can try the following options:
- -s — allows you to define a different shell environment to run.
- -p — maintains the same shell setting, with HOME, LOGNAME, SHELL, and USER.
- -l — enables you to change users by running a login script. You’ll need to put the user password in too.
root@srv:/# su user1
$ whoami
user1
2. sudo command
The superuser do (sudo) command is one of Linux’s most basic, and lets you run a command with root or admin permissions. The syntax is:sudo (command)
Terminal will ask you for the root password when you use a sudo command. You can add various options, including:
- -g — execute commands as a particular ID or group name.
- -h — run host commands.
- -k — the timestamp file becomes invalidated.
3. chown command
You can use chown to adjust the ownership of a directory or file to that of a certain username. The syntax is:chown [option] owner [:group] file(s)
If you wanted to name linuxmaster10 as the owner of a file, you could enter
chown linuxmaster10 filename.txt
4. useradd and userdel commands
The useradd command is used with the passwd command (see below) to set up a new user account and modify its password. Here’s how it looks:useradd [option] username
passwd username
However, useradd and passwd both need sudo privileges, so bear that in mind before you use them.
You can take advantage of the userdel command followed by the relevant username to remove that user. For example:
userdel user10
5. chmod command
The chmod command is utilized for adjusting permissions for files or directories. The syntax is:chmod [option] [permission] [file_name]
All files in Linux have a specific user class: owner, group member, or others. There are also three permissions: read, write, and execute.
If you, as the owner, plan to give each user all permissions, you could enter the following command:
chmod -rwxrwxrwx note.txt
Managing Files and Directories
These Linux commands are used for managing files and directories.6. ls command
If you want to view a list of all files and directories, enter the ls command. The syntax is:ls [/directory/folder/path]
It looks like this:
root@srv:/# ls / directory/folder/path file1.txt
When you remove the path, the ls command displays the content of the directory currently working. However, you can change it with one of the following options:
- -a — displays every file, as well as those that are hidden.
- -R — shows a list of all files within subdirectories.
- -lh — transforms sizes into formats that are readable, including TB and GB.
7. cd command
The cd command allows you to browse files and directories within your Linux system. The following syntax must be run with sudo privileges:cd /directory/folder/path
Here’s how it looks:
root@srv:/# cd /directory/folder/path
root@srv:/directory/folder/path#
You need the directory name or complete path depending on your present location. So, you would remove /username from /username/directory/folder if you were inside it at the time.
You’ll return to the home folder if you take the arguments out. Various shortcuts are available for simple navigation, including:
- – cd .. — jump up one directory.
- cd ~[username] — jumps to the home directory of a different user.
- cd- — jump back to the previous directory.
8. pwd command
If you want to check out the path of the directory currently working, you can use the pwd command. Here’s its syntax:pwd [option]
You can use two pwd command options:
- -L or — logical — prints environment-variable content (e.g. symbolic links).
- -P or -physical — supplies the actual path of the currently working directory.
root@srv:/directory/folder/path# pwd
/directory/folder/path
9. rmdir command
You can delete a whole Linux directory with this command, but only if you have sudo privileges within the parent directory. The syntax is:rmdir [option] directory_name
However, the rmdir command will deliver an error when there’s a subdirectory in the folder. You should use the -p option to forcibly delete a directory that isn’t empty.
10. mkdir command
The mkdir command enables you to make directories and define their permissions, but only if you have the authorization to create a new folder within the parent directory. Here’s the syntax:mkdir [option] [directory_name]
If you want to make a folder inside a directory, the path should be the command parameter (such as mkdirfilms/videos) will set up a videos folder within films. You can also use the following mkdir command options:
- -m — establishes folder permissions (such as read and write).
- -p — sets up a directory between a pair of previously created folders.
- -v — print a message for every existing directory.
root@srv:/# mkdir -v new-folder
mkdir: created directory ‘new-folder’
11. rm command
You can use this command to delete directory files on a permanent basis. The syntax is as follows:rm [filename1] [filename2] [filename3]
Tweak the amount of files to suit your needs when writing the command. Make sure you have the ‘write’ directory permission if you run into any errors.
You can add these options to adjust the rm command:
- -r — used for recursive deletion of directories and files.
- -f — enable removal of files without making a confirmation.
- -i — prompt confirmation before you proceed with a deletion.
12. cp command
The cp command is used for copying directories or files (and the content they contain) from your present location to a different one. You may use this in various scenarios. For example, if you wanted to copy a file from the present directory to a different folder, you would define both the target path and file’s name:cp filename.txt /home/username/Documents
Want to copy the content of one file to a different one in the same directory? You’ll need the destination file and the source:
cp filename1.txt filename2.txt
When copying several files to a single directory, use the destination path and names of the relevant files:
cp filename1.txt filename2.txt filename3.txt /home/username/Documents
For copying a whole directory, pass the -R flag followed by the destination directory and the source:
cp -R /home/username/Documents /home/username/Documentsbackup
13. mv command
The mv command is suitable for renaming or relocating directories and files. When relocating files or directories, input the name of the file and the directory it’s going to:mv filename.txt /home/username/Documents
Additionally, if you want to change the name of a Linux file with this command, use:
mv old_filename.txt new_filename.txt
14. file command
With the file command, you can check file types (e.g. binary or image). The syntax is:file filename.txt
Need to check several files at the same time? No problem: list them one by one or, for files within the same directory, use their path. You can show further detail by adding the -k option, while the -i option displays the MIME type for the file.
15. touch command
You can enter the touch command to generate a single blank file in a designated directory path. The syntax is:touch [option] /home/directory/path/file.txt
However, if you want to set up the item in the current folder, just leave the path out. The touch command is also suitable for creating and changing timestamps within a Linux command line.
16. tar command
The tar command is used to place several items into a single TAR file for archiving. The format is close to ZIP.tar [options] [archive_file] [target file or directory]
If you wanted to set up a new newarchive.tar archive in a specific directory (such as /home/user/Documents), you could enter:
tar -cvzf newarchive.tar /home/user/Documents
17. zip and unzip commands
The zip command enables compressing items into one ZIP file with the best ratio of compression. The syntax is:zip [options] zipfile file1 file2….
So, for instance, the following command compresses note.txt into archive.zip within the directory working currently:
zip archive.zip note.txt
To extract the file after it’s compressed, use the unzip command:
unzip [option] file_name.zip
Commands for Processing and Searching Text
The following Linux commands are used for text processing and searching:18. cat command
concatenate (abbreviated to cat) is a commonplace command in Linux, used for listing, writing, and combining content of files to the standard output. The syntax is:cat filename.txt
The cat command can be used in different ways:
- tac file.txt — presents content in a reverse order.
- cat > filen.txt — generates a new file.
- Cat file1.txt file2.txt > file3.txt — combines file1.txt and file2.txt, storing the resulting output into filename3.txt.
19. nano, vi, and jed commands
With Linux, you can modify files with vi, nano, jed — popular text editors. The majority of distros feature vi and nano, but you’ll need to manually install jed if you would rather use that instead. The command syntax is virtually identical for each editor:nano filename
vi filename
jed filename
Each of these text editors will generate a target file if one hasn’t been made yet. Generally, nano is best for modifying text files fast, while jed and vi are recommended for both programming and scripting.
20. sed command
With the sed command, you can locate, delete, or replace file patterns without relying on a text editor. The syntax is:sed [option] ‘script’ input_file
Inside the script, there is the searched regular expression pattern, subcommands, and the replacement string. To change matching patterns, use the s subcommand. To delete them, go with the d subcommand instead.
When ending the command, you should define the file carrying the pattern to be changed. For example, the following command switches green in colors.txt and hue.txt to yellow:
sed 's/green/yellow' colors.txt hue.txt
21. head command
With this command, you can print the initial 10 lines of a text file or piped data in your CLI. The syntax is:head [option] [file]
If you were looking to check out the starting 10 lines of note.txt within the current directory, you could type:
head note.txt
There are multiple options for the head command, including:
- -q — deactivates headers that define the name of the file.
- -n — modifies the amount of lines to be printed (e.g. head -n 8 displays the first 8 only).
- -c — prints the first customized number of bytes within a file.
22. grep command
Globular regular expression (AKA grep) is a command used to locate a word within a file. As it prints every line with the matching strings, this can be very helpful for filtering massive log files in a more efficient way. If you were looking for lines featuring mentions of ‘pink’ in notepad.txt, you would use:grep pink notepad.txt
23. awk command
With the awk command, you can scan a file’s regular expression patterns or manipulate data that matches. The syntax is as follows:awk '/regex pattern/{action}' input_file.txt
This may be used for various actions, including output expressions (e.g. print) or mathematical operations. The $n notation is also present, referring to one of the current line’s fields.
If you need to add several actions, you should list them in their order of execution with semicolons to separate each one. This may take the following form when a command carries statements of a mathematical, conditional, and output nature:
awk -F':' '{ total += $2; students[$1] = $2 } END { average = total / length(students); print "Average:", average; print "Above average:"; for (student in students) if (students[student] > average) print student }' score.txt
24. tail command
This command presents a file’s final 10 lines. It’s helpful when assessing new data and errors. Here’s the syntax:tail [option] [file]
For instance, you can show the final 10 lines of colors.txt with this command:
tail -n colors.txt
25. cut command
This command is used to find specific parts of a file and print them as Terminal outputs. The syntax is:cut [option] [file]
However, rather than using a file, you can utilize data from standard input instead if you want to. These options can be used to define how a line is sectioned by the command:
- -b — cuts the line by the byte size stated.
- -f — picks a particular field.
- -d — uses delimiters to section lines.
- -c — uses specified characters to section lines.
cut -d',' -f3-5 list.txt
26. sort command
With the sort command, you can reorder lines within a file in a particular way. This doesn’t actually change the file, and the result is printed as Terminal outputs. Use the following syntax:sort [option] [file]
The sort command orders the lines alphabetically as standard. However, you can change the arrangement method with the following options:
- -r — reverse the sorting order (from ascending to descending).
- -o — directs command outputs to a different file.
- -k — rearranges data in a particular field.
- -n — arranges a file in a numerical way.
27. tee command
Using the tee command allows you to write input to Terminal’s output and files. The syntax is:command | tee [option] file1
With the below command, Google is pinged and the output is printed in Terminal, ping_result.txt, and the 19092024.txt file:
ping google.com | tee ping_result.txt 19092024.txt
28. diff command
This command is used to compare the content of two separate files then output the differences found. You can modify a program without having to change the code when you use the diff command. The general syntax is:diff [option] file1 file2
The following options can be used:
- -u — displays the output with all redundant data removed.
- -i — sets the diff command to be case insensitive.
- -c — presents, in a context form, two files’ differences.
29. find command
The find command looks for files in a particular directory. Here’s the syntax:find [option] [path] [expression]
So, you can use this command to look for file5.txt in the directory folder and its subdirectories:
find /home -name file5.txt
However, with the path removed, the find command will look for the current working directory. The following command can also be used to locate directories:
find ./ -type d -name directoryname
30. locate command
With the locate command, you can locate a file within the database system. Use the -i option to deactivate case sensitivity, and enter an asterisk to search for content using several keywords. If you were looking for files with the words work and log in them, no matter what the case of their characters, you could use:locate -i work*log
Network Management and Problem Solving Commands
Here are Linux commands for troubleshooting and network management:31. wget command
The wget command is used for downloading internet files via FTP, HTTP, or HTTPS protocols. The syntax is:wget [option] url]
If you wanted to download the most up-to-date WordPress release, you could enter:
wget https://wordpress.org/latest.zip
32. curl command
You can utilize this command for migrating data from one server to another. Most commonly, the curl command is used to retrieve content from a page on a website to your system using the URL. Here’s how it looks:curl [option] URL
- -X — modifies the standard HTTP GET method.
- -o (or -O) — download a file from a particular URL.
- -F — uploads a file to a particular location.
- -H — delivers a custom header to the specified URL.
33. ping command
Among Linux users, ping is an incredibly popular command. You can utilize this to discover if a specific server or network can be reached. That’s great when facing problems with connectivity. The syntax is as follows:ping [option] [hostname_or_IP_address]
When checking Bing’s connection and response time, you could input:
ping bing.com
34. rsync command
With the resync command, you can sync folders or files between two locations. This is helpful for making sure that the content is the same in both. The syntax is:rsync [options] source destination
When a folder is the source or destination, you should input the directory path in this way: /home/directory/path. When syncing a remote server, enter the IP address and hostname (such as [email protected]).
There are a number of options with the resync command:
- -v — displays visual details about the file that has been transferred.
- -z — compresses files that have been transferred to make them smaller.
- -a — activates archive mode for preservation of certain attributes (such as file permissions and dates).
35. scp command
The scp command is used to copy files and directories from system to system, via a network, in a secure way. The syntax is:scp [option] [source username@IP]:/[directory and file name] [destination username@IP]:/[destination directory]
Take out the IP address and hostname for a local system. You can change the copying behavior with these options:
- -l — restricts the bandwidth for the scp command.
- -P — modifies the port for copying (it’s 22 as standard).
- -C — make data smaller, through compression, after transferral.
36. netstat command
With the netstat command, you can view the network information for your system (such as sockets). The syntax is:netstat [option]
The following options can be used to change the data shown:
- -t — displays TCP connections.
- -a — shows sockets that are closed and listening.
- -r — shows routing tables.
- -u — presents all UDP connections.
- -i — displays network interface details.
- -c — outputs network information on a continuous basis, which is helpful if you want to monitor it in real-time.
- -p — shows the name and process IDs for programs.
37. iconfig command
Use the iconfig command to view and set up the network interface for your system (the same as the ip command in the latest Linux distros). The syntax is:ifconfig [interface] [option]
If you run this command without arguments, it presents information on each of your system’s network interfaces. When you need to see a particular interface, enter its name as an argument with no option.
However, these options can be used for different tasks:
- Up and down — activates and deactivates network interfaces.
- -s — summarizes all network interfaces and their setup (placed in front of the name of an interface).
- netmask — defines the subnet mask that should be used with an IPv4 address.
- inet and inet6 — allocates an IPv4 or IPv6 address to an interface.
38. nslookup command
This command is for querying DNS servers to discover the domain linked with a particular IP address and vice versa. The syntax is:nslookup [options] domain-or-ip [server]
The command will utilize your system’s or ISP’s standard resolver unless you state which DNS server to use instead. The nslookup command has a number of options including:
- -retry= — repeats a query a particular number of times before it fails.
- -type= — queries certain details, such as an MX record.
- -debug — activates the debug mode to give you additional information on a query.
- -port= — establishes the DNS server’s port number for a query.
39. traceroute command
Use the traceroute command to track the path of a packet while it migrates to a different host via a network. This provides details about the routers and the length of time the process takes. The syntax is:traceroute [option] destination
The hostname, IP address, or domain can all be used for the destination. For deeper monitoring of packets, add these options to the command:
- -n — allows for faster tracing by stopping the command from resolving IP addresses to hostnames.
- -l — modifies the standard UDP packets to UCMP.
- -w — specifies the number of seconds that must pass before a timeout occurs.
- -m — defines the maximum hops for every packet involved.
Miscellaneous commands
Here is a list of Linux commands with a variety of functions:40. history command
The history command shows you a list of all commands you have executed in the past. The benefit of this command is that you can reuse others without needing to write them again. How do you use it? Just input the following with sudo privileges:history [option]
If you want to go back to a particular utility, input an exclamation mark and the number for the specific command as it appears on the list. That might look like this:
!180
You can use various options with this, including:
- -d offset — deletes the history item at the offset point on the list.
- -c — wipes the history list.
- -a — adds to history lines.
41. dig command
The domain information groper (or dig for short) command collects a domain’s DNS data. This is a more versatile command than nslookup (covered previously). The syntax is:dig [option] target [query_type]
Switch ‘target’ in this example for the relevant domain name. The dig command also displays A record type as standard, so you need to modify ‘query_type’ to check a particular record type or use ANY to query them all.
If you want to do a reverse DNS lookup, add the -x option and make the ID address the target.
42. echo command
With the echo command, you can present a text line as a standard output. Here’s the syntax:echo [option] [string]
echo command has various options, including:
- -n — shows the output but omits the trailing newline.
- -e — enables interpretation of these two escapes:
- /b — takes out the spaces between texts.
- /c — creates no additional output.
43. man command
The man command is used to access a user manual for all utilities of Linux Terminal. This contains nine sections, and includes names, options, and descriptions. The sections are:- System calls
- Games
- Library calls
- Executable programs or shell commands
- File formats and conventions
- Special files
- Miscellaneous
- System administration commands
- Kernel routines
man [option] [section_number] command_name
Terminal presents the complete user manual when you only make the command name the parameter. For instance, if you were to enter man 2 mv, you would view section 2 of the command manual for mv command.
44. cal command
If you want to output a calendar in Linux Terminal, the cal command will do that for you. The calendar presents the present date by default unless you specify a particular month and year you want to view. The syntax is:cal [option] [month] [year]
With the cal command, months are represented by their number (from 1 to 12). To change the command output, use these options:
- -1 — outputs the calendar in one line.
- -m — begins the calendar with Monday, not Sunday.
- -3 displays the previous, present, and forthcoming month.
- -A and -B — shows a specific range of months before and after the present one.
45. ln command
The ln command is used to streamline system management by linking files or directories. The syntax is:ln [option] [source] [destination]
Using the ln command will set up a target directory or file, which is then linked to the relevant source. This generates a hard link: the new item and the data block both link back to the source.
46. alias and unalias commands
You can input the alias command to tell the shell to swap one string with a different one. This lets you set up a shortcut for a specific file name, text, or program. The syntax is:alias name=string
So, for instance, the echo command alias might be a simple ‘e’:
alias e=‘echo’
There is no output with this command. If you want to see the alias connected to a specific command, use this:
alias command_name
Utilize the unalias command to delete an alias:
unalias [alias_name]
47. apt-get command
The apt-get command is used to handle the Advanced Package Tool (APT) libraries in the Debian-based Linux (as with Ubuntu). You’ll need the root or sudo privileges for this, though. Using the apt-get command allows you to update, install, remove, and generally manage software. The basic syntax is:apt-get [options] [command]
The most popular apt-get commands include:
- upgrade — installs the most up-to-date version of previously installed packages.
- update — syncs package files from sources.
- check — updates the package cache and assess broken dependencies.
Linux System Management and Information Commands
The following Linux commands cover system management and information queries.48. du command
The du command is helpful for checking the amount of storage that a specific file or directory consumes. Make sure you’re specific about directory paths with du command. For instance:du /home/user/Pictures
You can use various options with the du command, including:
- -m — displays file and information folder in megabytes (MB).
- -s — shows the overall size of a specific folder.
- -n — reveals the date of the most recent adjustment to the files and folders shown.
- -k — shows information in kilobytes (KB).
49. df command
The df command lets you check out the amount of disk space that a Linux system uses, in both kilobytes and percentages. The syntax is:df [options] [file]
The command shows details on all file systems mounted unless you specify a particular item. Options suitable for the df command are:
- -k — prints the system usage of a file in kilobytes.
- -m — shows file system usage in megabytes.
- -T — displays the type of a file system in a new column.
50. ps command
Use the ps command to generate an overview of every system process running. If you execute it with no argument or option, it will show a list of processes running in the shell with this information:- Terminal type (TTY)
- Process ID (PID)
- Command to start the process (CMD)
- CPU usage time for a specific process (TIME)
- -u username — displays processes linked to a particular user.
- -A — lists every running process.
- -T — shows every process connected to the present shell session.
51. top command
You can utilize the top command to see the condition of a system and its running processes (e.g resource usage). This command is helpful for discovering which processes consume resources the most, so you can deactivate them as necessary. If you want to use the top command, just type the following word into your CLI:top
52. htop command
If you’re looking to monitor server processes and system resources, use the htop command. It’s different to the top command in that it utilizes visual indication and mouse functions. Here’s the syntax:htop [options]
This command can be used with the following options:
- -h — shows the help message with exits.
- -c — activates monochrome mode.
- -d — shows delays between updates (using tenths of a second).
53. hostname command
Use this command to view the hostname for your system. The syntax is:hostname [option]
While you can use this command with no options, you may want to run it with one of these:
- -A — displays the Fully Qualified Domain Name (FQDN) for the machine.
- -i — outputs the IP address of the machine.
- -a — shows the alias of the hostname.
54. uname command
The unix name or uname command prints details related to your machine, such as the Linux kernel and system name. Here’s the syntax:uname [option]
You can run this command with no option, you may add one of these options to adjust it:
- -s — outputs kernel name.
- -a — prints all system details.
- -n — presents the node hostname for the system.
55. watch command
With the watch command, you can run a specific utility on a continuous basis, at an interval of your choosing. The results may be printed as a standard output. The syntax is:watch [option] command
The watch command is helpful to monitor alterations in command output. You can utilize these options to change the way it behaves:
- -t — deactivates the header that carries the time interval, timestamp, hostname, and command itself.
- -d — shows how the command executions differ.
- -n — modifies the standard interval of two seconds.
56. time command
The time command is used to measure the execution time of commands. The syntax is:time [commandname]
If you want to measure a command series, the commands should be separated with two ampersands or semicolons. As an example, let’s measure the execution times of the watch, ps, and pwd commands:
time watch /home/directory/path; ps bashscript.sh; pwd +x bashscript.sh
57. systemctl command
You can use the systemctl command for managing services that you have installed on your Linux machine. Here’s the syntax for the systemctl command:systemctl [commandname] [service_name]
However, if you want to use the systemctl command, you’ll need root privilege first. There are a number of use cases for this command, such as starting or ending a particular service. You may also use it to view the dependencies and current status of a service.
Please note: the systemctl command is available when the Systemd init system is present in Linux distros.
58. shutdown command
If you want to switch off your Linux system or restart it again at a particular time, you can take advantage of the shutdown command. The syntax for this is:shutdown [option] [time] “message”
The time format used for the shutdown command can vary: you may use 24 hours or, for example, +20 if you want to initiate the process 20 minutes from now. Users who are logged into the system will receive a notification (the “message”) about the shutdown once it’s put into place.
However, you can restart the system instead of shutting it down — use the -r option to do that. But if you want to cancel a system restart once it’s scheduled, use the -c option with the command.
59. kill command
The kill command enables you to end a program that stops responding. You can use its identification number (PID), which you can check with this:ps ux
Use the following syntax to end the program:
kill [signal_option] pid
The two most widely used signals for program termination are SIGKILL and SIGTERM, though there are more than 60 in total. SIGTERM is the default one and allows the program to save its current progress ahead of its termination, while SIGKILL force-stops programs and removes any progress that is unsaved.
60. jobs command
Using this command, you can view the active processes of a shell with their statuses. This is available in the following shells only: bash, ksh, tcsh, and csh. The syntax is:jobs [options] jobID
Type jobs in Terminal, with no arguments, when you need to see the status of jobs in the current shell. If there are no jobs running on your Linux system, the jobs command will return an empty output. You can also utilize these options:
- -p — shows process IDs.
- -l — displays process IDs and the relevant details about them.
- -n — lists jobs that have undergone status changes since the most recent notification.
More Hints for Using Linux Commands
Looking for ways to make system management more efficient? Check out these Linux commands:- To close Terminal, just enter the exit
- Tap the Tab button after you input a command, with no argument, to autofill.
- Use the -help option to display a command’s total usage.
- Press Ctrl+C to end a command that’s currently running.
- Input clear to wipe the Terminal screen.
- Press Ctrl+z if you want to put a currently working command on pause.
- To jump to the end of a line, press Ctrl+E.
- To jump to the start of a line, press Ctrl+A.
- Use double ampersands or semicolons to keep several commands separate.
No comment yet, add your voice below!