In the world of Linux, if you don’t play around with commands, you’d be too embarrassed to even greet your peers. At the same time, servers are normally placed remotely, and people generally log in via SSH; remote desktop users are a rare sight. This article covers commonly used commands on Linux servers.
1. Check Local Time
- $date
Since there are so many time zones on Earth, checking the current time is never a bad idea. For instance, if the server has an issue, you can check the time zone to see if it’s the administrator’s working hours, making it more efficient to send them an email or give them a call.
2. View Currently Logged-in Users
- $who
This command can tell you when the computer was started and who is logged in.
- $whoami
If you forget who you are, you can use this command.
3. Check Computer Memory Usage
- $free
This command tells you detailed information about the computer’s current memory, including total memory, used memory, and free memory.
4. Check CPU Information
- $lscpu
View current CPU information, including clock speed and number of cores.
Of course, Linux physical configuration files are all located under the /proc/ directory. You can also view computer hardware information using the following two commands.
- $cat /proc/cpuinfo
- $cat /proc/meminfo
5. View Process Resource Usage Ranking
- $top
This command prints out current memory usage and the top ten processes ranked by CPU usage. If you want to sort by memory usage instead, you can press Shift + F, then use the up and down arrow keys to select the column you want to sort by, and press the spacebar to select or deselect the current column.
6. View Current Directory
- $pwd
- $ls -l
The first command prints the current directory. When you are constantly switching directories or get lost in the Linux kingdom, you can print the current directory with this command.
The second command lists detailed information for all files in the current directory. Of course, ls has many other useful parameters; just ask man for them.
7. Calculate Total Resources Used by a Specific User
- $ps aux | awk '/username/ {sum += $4} END {print sum}'
This command is a collaboration of several commands. First, `ps aux` prints out all processes, which is then piped as input to awk. Who is awk? Head over to CoolShell, where you can appreciate the boundless charm of awk. The awk parameter here means: match processes for the user ‘username’, sum up the content of the fourth column of the matched processes. The fourth column is the memory usage size. This way, you can calculate the total memory used by a specific user. Of course, you can also calculate the memory usage of a specific application.
8. Find the Location of a File
- $locate file
This command is mainly for finding files scattered across the vast Linux file system. For example, if I am developing a MySQL application and need the libmysql.a file but forgot where this static file is placed, locate will list the absolute paths of all files with that name at the fastest speed.
9. Edit Files
- $vi
When logging into a server remotely, vi is an indispensable editor, not only because it’s a standard feature on all Linux systems, but more so for its powerful commands like search, location, deletion, and copy. Precisely because of its absolute advantage in single-file editing, vi is my most frequently used tool. I don’t want to ramble on about its detailed usage here, as CoolShell already has a detailed description.
10. Process Management
- $./server &
If you’ve logged in via SSH, running processes in the background is very necessary. After all, you can’t log in to SSH ten times just to start ten services—that would be too silly. The command above runs the executable file named ‘server’ in the current directory in the background.
- $kill
kill is used to delete a process. Actually, saying ‘delete’ is not entirely correct; it’s more about sending a signal to a process. To see what signals are available, you can use
`kill -l` to list all signal names. Of course, you often see numbers being sent, like 9 or 19. This is because the names listed by `kill -l` are macros defined in the file /usr/include/x86_64-linux-gnu/bits/signum.h. So, if your process is in the background and you want to use Ctrl-Z to suspend it rather than directly killing it with Ctrl-C, looking at the signal names from `kill -l` will make things clear.
There are a vast number of Linux commands, but the most basic ones still need to be remembered. Of course, it’s not about rote memorization, but using some small tricks, understanding the commands, and practicing them. I believe mastering the common commands is still quite easy. Here, I have only listed a small portion of commands, specifically those used more frequently when deploying game servers. Behind these commands lies a lot of computer knowledge, such as signals, foreground and background process execution, and regular expressions. When will you ever finish learning all this knowledge? I can only say that learning is a boundless sea, and another point is to choose your own direction.