In a booming world with new tools and diverse development environments, learning some basic sysadmin commands is a must for any developer or engineer. Specific commands and packages can help developers organize, troubleshoot, and optimize their applications and – when something goes wrong – provide valuable trilogy information to operators and systems.
In this article I want to share some useful commands that I often use when operating with a Linux server.
1. history
Imagine when you manipulate and use a lot of commands and make the server error and now you do not know which command you have used the wrong command, the history is the command that will help you. The history command will list the commands that you have used.
1 2 3 4 5 | $ history 1 clear 2 df -h 3 du |
Also when you want to reuse a certain statement without having to retype or use ! Number . If you want to use df -h
, type ! 2 .
2. chmod
Unlike other operating systems like Windows .. when you manipulate a file on Linux many times you will get a permission denied error, which means you are not having the right to execute with file or file. there. Use the chmod command
1 2 | chmod + w test.sh |
w: is the permission you want to assign to the file (for example: w is write permission, r: read permission ..)
3. du
To retrieve more detailed information about which files are using disk space in the directory, you can use the du command. For example, if you want to find out which logs take up the most space in / var / log, you can use du with the -h (human readable) option and the -s option for the aggregate. ruler.
1 2 3 4 5 6 7 8 9 | $ du -sh / var / log / * 1.8M / var / log / anaconda 384K / var / log / Audit 4.0K / var / log / boot.log 0 / var / log / chrony 4.0K / var / log / cron 4.0K / var / log / maillog 64K / var /nhật ký / tin nhắn |
The example above shows the largest directory in / var / log is / var / log / Audit
4. df
You can use df (show free disk space) to troubleshoot disk space. When you run your application on a container coordinator, you may receive an error message indicating a lack of free space on the container server. Although disk space must be managed and optimized by sysadmin, you can use df to find out available space in the directory and confirm if you are really running out of space.
1 2 3 4 5 6 7 8 9 | $ df -h Kích thước hệ thống tệp đã sử dụng Thời gian sử dụng % Được gắn trên devtmpfs 7.7G 0 7.7G 0 % / dev / dev / mapper / RHEL-Root 50G 16G 35G 31 % / / dev / nvme0n1p2 3.0G 246M 2.8G 9 % / boot / dev / mapper / RHEL-Home 100G 88G 13G 88 % / home / dev /nvme0n1p1 200M 9,4M 191M 5 % / boot / efi / dev / sdb1 114G 55G 54G 51 % / run / media / tux / red |
The -h option prints out information in human-readable format. By default, as in the example df gives results for everything in the root directory, but you can also limit the results by providing a directory as part of your command (such as df -h / home).
5. ip
The subcommand address (or ip a for short) displays your application server’s interfaces and IP addresses. You use the ip address to verify the host or host’s IP address. For example, when your container is attached to two networks, the ip address can show which interface connects to which network. For simple testing, you can always use the ip address command to get the server’s IP address. The example below shows that the web layer container has an IP address of 172.17.0.2 on the eth0 interface.
1 2 3 4 5 6 | $ ip address show eth0 3 : eth0: < BROADCAST, MULTICAST, UP, LOWER_UP > mtu 1500 qdisc noqueue state UP group default qlen 1000 link / ether d4: 3b: 04: 9e: b2: c2 brd ff: ff: ff: ff : ff: ff inet 10.1.1.3 / 27 brd 10.1.1.31 phạm vi động toàn cầu noprefixroute eth0 valid_lft 52072 giây ưu tiên_lft 52072 giây |
6. tail
For developers who have to debug, viewing the log of a file is not uncommon. However, we cannot sit down to read every line in the log. Sometimes just want to see the latest log recorded in the file. The tail – f statement will help us with this.
The -f option indicates the “follow” option, which outputs the log lines as they are written to the file. An example has a background script that hits the endpoint every few seconds and logs the request. Instead of tracking the log in real time, you can also use the extension to see the last 100 lines of the file with the -n option.
7. ls
A legendary command that everyone who uses Linux has probably used it. ls lists the files in a directory. Sysadmins and its developers issue this command quite often. In the container space, this command can help locate your container image files and folders. Besides looking up your files, ls can help you check your permissions. In the example below, you cannot run myapp due to a permission issue. When you check the permissions using ls -l , you realize that the permissions don’t have “x” in -rw-r – r–, can only be read and written.
1 2 3 4 5 | $. / myapp bash:. / myapp: Quyền bị từ chối $ ls -l myapp -rw-r - r--. 1 root root 33 Jul 21 18 : 36 myapp |
8. curl
This command is extremely useful when we want to test if we can connect to another url (end-point). For example, imagine your application gets an HTTP 500 error saying it cannot access the MongoDB database:
1 2 3 | $ curl -I -s myapplication: 5000 HTTP / 1.0 500 LỖI MÁY CHỦ NỘI BỘ |
1 2 3 | $ curl -I -s https: // opensource.com HTTP / 1.1 200 OK |
9. grep
The grep command is used to search for text. It searches for certain files for lines containing matches that match given strings or words. This is one of the most useful commands on Linux and Unix-like systems. Let us see how to use grep on Linux or Unix-like systems.
1 2 3 4 5 | Tìm kiếm bất kỳ dòng nào có chứa từ trong tên tệp trên Linux: grep 'word' filename Thực hiện tìm kiếm không phân biệt chữ hoa chữ thường cho từ 'bar' trong Linux và Unix: grep -i 'bar' file1 Tìm kiếm tất cả các tệp trong thư mục hiện tại và trong tất cả các thư mục con của nó trong Linux để tìm từ 'httpd'grep -R 'httpd' . Tìm kiếm và hiển thị tổng số lần chuỗi 'nixcraft' xuất hiện trong tệp có tên frontpage.md: grep -c 'nixcraft' frontpage.md |
10. ps
The ps command, part of the procps-ng package, provides commands that are useful for investigating a process ID, displaying the status of a running process. Use this command to identify a running application or to validate an expected process. For example, if you want to test a running Tomcat web server, you use ps with its options to get Tomcat’s process ID.
1 2 3 4 5 6 | $ ps -ef UID PID PPID C STIME TTY TIME CMD root 1 0 2 18 : 55 ? 00:00:02 / docker-java-home / jre / bi root 59 0 0 18 : 55 pts / 0 00:00:00 / bin / sh root 75 59 0 18 : 57 pts / 0 00:00:00 ps -ef |
11. pwd
When you want to check the current path in the folder you are working in, use the pwd command. Use the pwd command to find out the path of the current working directory (directory) you are in. This command will return an absolute (complete) path, which is essentially the path of all directories starting with a forward slash (/). An example of an absolute path is / home / username.
1 2 3 | $ pwd /home/nguyen.thanh.tungm |
12. cat
cat is one of the most frequently used commands in Linux. It is used to list the contents of a file on standard output (sdout). To run this command, type cat, followed by the file name and its extension. For example: cat file.txt.
1 2 | cat test.txt |
13. kill
If you have an unresponsive program, you can end it manually using the kill command. It sends a certain signal to the malfunctioning app and instructs the app to end itself.
There are sixty-four signals in total that you can use, but people typically only use two:
SIGTERM (15) – asks a program to stop running and give it some time to save all of its progress. If you do not specify a signal when entering a kill command, this signal will be used. SIGKILL (9) – forces programs to stop immediately. Unsaved progress will be lost. Besides knowing the signals, you also need to know the process code (PID) of the program you want to kill. If you don’t know the PID, just run the ps ux command.
After knowing the signal you want to use and the PID of the program, enter the following syntax:
1 2 | kill 9 PID |
14. top
As a terminal equivalent to the Task Manager in Windows, the top command displays a list of running processes and how much CPU each process uses. It is very useful for monitoring system resource usage, especially knowing which processes need to be terminated as it consumes too many resources.
15. cp
Use the cp command to copy files from the current directory to another. For example, the command cp scenery.jpg / home / username / Pictures will create a copy of scenery.jpg (from your current directory) into the Pictures folder.
Summary
Basic Linux commands help users perform tasks easily and efficiently. It may take a while to remember some of the basic commands, but nothing is impossible if you practice a lot.
The article is traced from: