5 Linux commands every sysadmin needs to know
Specific commands and packages can help developers organize, troubleshoot, and optimize applications, and provide valuable information when problems occur.
Whether you are a new developer or want to manage your own applications, the following basic sysadmin commands can help you better understand your applications. They can also help you describe problems for troubleshooting like why an application might work locally but not on a remote server. These commands apply to Linux development environments, containers, virtual machines (VMs), and bare metal.
1. curl
curl passes a URL. Use this command to test the application endpoint or connect to the upstream service endpoint. curl is useful in determining if an application can reach another service, such as a database, or check if a service is working properly.
For example, your application gets an HTTP 500 error stating that it cannot access the MongoDB database:
$ curl -I -s myapplication:5000 HTTP/1.0 500 INTERNAL SERVER ERROR
Let's check the database endpoint from the local desktop:
$ curl -I -s database:27017 HTTP/1.0 200 OK
So what could be the problem? Check if your application can reach places other than the database from the application server:
$ curl -I -s https://opensource.com HTTP/1.1 200 OK
Everything seems fine. Now try to access the database from the application server. Your application is using the hostname of the database, so first try:
$ curl database:27017 curl: (6) Couldn't resolve host 'database'
This indicates that your application cannot resolve the database, either because the database URL is unavailable or the host (container or virtual machine) does not have a nameserver that it can use to resolve hostnames.
2. python -m json.tool / jq
After executing the curl command, the output of the API call can be hard to read. Sometimes you want to output JSON so that it's easy to read to find a specific item. Python has a built-in JSON library that can help with this. You use python -m json.tool to indent and organize the JSON. To use Python's JSON module, pass the output of the JSON file to the python -m json.tool command .
$ cat test.json {"title":"Person","type":"object","properties":{"firstName":{"type":"string"},"lastName":{"type":"string"},"age":{"description":"Age in years","type":"integer","minimum":0}},"required":["firstName","lastName"]}
To use the Python library, pipe the output to Python with the -m (module) option.
$ cat test.json | python -m json.tool { "properties": { "age": { "description": "Age in years", "minimum": 0, "type": "integer" }, "firstName": { "type": "string" }, "lastName": { "type": "string" } }, "required": [ "firstName", "lastName" ], "title": "Person", "type": "object" }
For more advanced JSON parsing you can install jq. jq provides several options for extracting specific values from the JSON input. To output the output so it looks like the Python module above, just apply jq to the output.
$ cat test.json | jq { "title": "Person", "type": "object", "properties": { "firstName": { "type": "string" }, "lastName": { "type": "string" }, "age": { "description": "Age in years", "type": "integer", "minimum": 0 } }, "required": [ "firstName", "lastName" ] }
3. ls
ls lists files in a directory. Sysadmins (system administrators) and developers use this command quite often. In the container space, this command can help determine the directory and file of the container image. Besides looking up files, ls can help you check your permissions. In the example below, you cannot run myapp due to permission issue. When checking the permissions with ls -l , you realize that the permissions don't have the "x" in -rw-r - r-- , only read and write.
$ ./myapp bash: ./myapp: Permission denied $ ls -l myapp -rw-r--r--. 1 root root 33 Jul 21 18:36 myapp
4. tail
tail displays the last part of the file. You usually don't need every log line to troubleshoot. Instead, you want to check what the logs say about the most recent request to the app. For example, you can use tail to check what happens in the log when you make a request to your Apache HTTP server.
The -f option means "follow", outputs the log lines as they are written to the file. For example there is a background script that accesses the endpoint every few seconds and logs this request. Instead of monitoring the log in real time, you can also use tail to view the last 100 lines of the file with the -n option .
$ tail -n 100 /var/log/httpd/access_log
5. cat
cat concatenates and prints files. You can ask cat to check the contents of file dependencies or to confirm the version of the application that you have created locally.
$ cat requirements.txt flask flask_pymongo
The example above checks if your Python Flask application has Flask listed as a dependency.
Basic commands can enhance troubleshooting, when it is necessary to determine why an application works in one development environment but not in another. Many sysadmins have taken advantage of these commands to debug many problems with the system. Understanding some of these helpful troubleshooting commands can help you communicate with your sysadmin and resolve issues with your application.
You should read it
- How to Run Linux Commands on Windows with WSL 2
- Guide to network operation for Linux users: 11 commands to know
- 14 interesting Linux commands in Terminal
- 15 Tar commands should try in Linux
- Useful commands in Unix / Linux
- Basic Linux commands everyone needs to know
- Top 10 examples of Netstat commands on Linux
- Kali Linux commands from AZ and commonly used commands
- How to run 2 or more Terminal commands at the same time on Linux
- 10 most useful Linux commands
- How to run Linux commands when starting Windows Subsystem for Linux on Windows 10
- How to schedule Linux commands with 'at'