5 Linux commands every sysadmin needs to know

In a world full of new tools and diverse development environments, learning some basic sysadmin commands is practically essential for any developer or engineer. 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.

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 to 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.

See more:  8 best Linux distros for pentest

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 is 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 JSON. To use Python’s JSON module, pass the output of the JSON file to the command python -m json.tool.

$ 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 option -m (module).

$ 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 that 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 permissions with ls -l, you realize that the permissions are not available “x” in -rw-rr–, are read and write only.

$ ./myapp
bash: ./myapp: Permission denied
$ ls -l myapp
-rw-r--r--. 1 root root 33 Jul 21 18:36 myapp

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 application. For example, you can use tail to check what happens in the log when you make a request to your Apache HTTP server.

See more:  How to Install and Use Ansible on Debian 10
Use tail -f to monitor Apache HTTP logs and see requests as they happen
Use tail -f to monitor Apache HTTP logs and see requests as they happen

Option -f 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 option -n.

$ tail -n 100 /var/log/httpd/access_log

cat concatenates and prints files. You can ask cat to check the contents of file dependencies or to confirm the version of the application 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.

Source link: 5 Linux commands every sysadmin needs to know
– https://techtipsnreview.com/

, ,

Leave a Reply

Your email address will not be published. Required fields are marked *