create account

Twenty things in Linux are supposed to be known to every system administrator by aser1111

View this thread on: hive.blogpeakd.comecency.com
· @aser1111 ·
$10.37
Twenty things in Linux are supposed to be known to every system administrator
it is essential for any developer or engineer to learn some basic system management commands, especially as new tools and environments are constantly invading our world. Certain commands and packages can help developers organize, manage, troubleshoot and try to fix applications, . 
Whether you're a new developer or just want to manage your applications, the 20 commands explained in this guide will help you better understand your applications or give you the ability to describe problems you have in your work for system administrators to fix them. For example, why does the application work locally but not on the remote host? These commands are implemented within the Linux environment and the computer system, which contains both bare metal environment and containers.

![](https://steemitimages.com/DQmfN7mLbkgr1vEAkkisEPS3kLYcVp6wYDej6KW7pSrw9h8/image.png)

1. curl
Curl is a tool for transferring files using a URL link via the command line; it also checks the connection of applications with a service, in other words, determining whether your application can access a particular service, such as a database, or checking whether the service itself works Right. 
Suppose, for example, that your application shows an HTTP 500 error indicating that it is unable to access the MongoDB database.

$ curl -I -s myapplication: 5000
HTTP / 1.0 500 INTERNAL SERVER ERROR
Option -I (or -head) shows only the http header information, and the -s option (or -silent) hides the progress details or error messages. In this case, you can lose the endpoint of your database from your local computer as follows:

$ curl -I -s database: 27017
HTTP / 1.0 200 OK
No problem, so where is the problem ?! Make sure your app can access other places in addition to the database from the host application.

$ curl -I -s https://opensource.com
HTTP/1.1 200 OK
No problem either; try now to access the database from the host application; your application uses the host name of the database so try it first.

$ curl database:27017
curl: (6) Couldn't resolve host 'database'
This indicates that your application can not identify the server's server address because it is either unavailable or the host (the container or virtual machine) does not 
have a server name to identify the host name.

2. python -m json.tool / jq
After executing curl, you will find that its output is difficult to read. Sometimes you need to print outputs in a beautiful, coordinated and readable format to search for specific inputs. This is provided by the JSON data format, which is smooth, easy to read and understand and used to transfer data between programming languages ​​or between web and server applications. The JSON library, built into Python, This is done by using the python -m json.tool command after converting the output to it.

$ 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"]}
Did you understand any of the previous outputs? We will use JSON in the following command to send the previous output to Python.

$ 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"
}
Have you noticed the difference ?! Outputs can also be shown in an advanced way similar to using the previous command to convert the output to the jq tool, which contains several options to obtain specific values ​​from the JSON input; you can install jd from this link .

$ 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
The ls command is used to show a list of files in a folder, used by system administrators and developers; it can also be used to find a particular file in addition to displaying its permissions. 
In the following example, we tried to run the myapp file, but we could not because of the permissions issue; when we checked the permissions through the ls -l command, we found that the file is valid for read and write only.

$ ./myapp
bash: ./myapp: Permission denied
$ ls -l myapp
-rw-r--r--. 1 root root 33 Jul 21 18:36 myapp
4. tail
The tail command shows the last part of the file; you do not often need to read all the file or record lines while searching for a problem or malfunction, but the most recent data may be the most important. You can, for example, use the tail command to check and see what happens in the logs when you create an HTTP request.

[root@localhost ~]# tail -f /var/log/httpd/access_log
::1 - - [21/Jul/2017:18:46:58 +0000] “GET / HTTP/1.1” 403 4897 “-” “curl/7.29.0”
::1 - - [21/Jul/2017:18:47:00 +0000] “GET / HTTP/1.1” 403 4897 “-” “curl/7.29.0”
::1 - - [21/Jul/2017:18:47:02 +0000] “GET / HTTP/1.1” 403 4897 “-” “curl/7.29.0”
::1 - - [21/Jul/2017:18:47:04 +0000] “GET / HTTP/1.1” 403 4897 “-” “curl/7.29.0”
::1 - - [21/Jul/2017:18:47:06 +0000] “GET / HTTP/1.1” 403 4897 “-” “curl/7.29.0”
::1 - - [21/Jul/2017:18:47:08 +0000] “GET / HTTP/1.1” 403 4897 “-” “curl/7.29.0”
::1 - - [21/Jul/2017:18:47:10 +0000] “GET / HTTP/1.1” 403 4897 “-” “curl/7.29.0”
::1 - - [21/Jul/2017:18:47:12 +0000] “GET / HTTP/1.1” 403 4897 “-” “curl/7.29.0”
::1 - - [21/Jul/2017:18:47:14 +0000] “GET / HTTP/1.1” 403 4897 “-” “curl/7.29.0”
::1 - - [21/Jul/2017:18:47:16 +0000] “GET / HTTP/1.1” 403 4897 “-” “curl/7.29.0”
The -f option (or -follow) shows the newly inserted log or file lines, which is clear from the previous example. A specified number of lines can be shown instead of following and updating file updates, such as showing the last 100 lines of a file through the -n option.

$ tail -n 100 /var/log/httpd/access_log
5. cat
This command merges or prints files into the standard output stream and is used to check the contents of files. 
In the following example, make sure that the Flask framework, a minimized framework for the development of small and medium Web applications, is included in the list on which the Python Flask application is based.

$ cat requirements.txt
flask
flask_pymongo
6. grep
The grep tool is a powerful tool for matching text styles within files; if you are looking for a particular text style within a file or in the output of another command, you can use this tool to search for and find this style. 
For example, suppose you want to make sure that the Apache Tomcat server is running, you will be unable to read many lines to search for it so the output can be sent to the grep command to isolate the lines it points to.

$ cat tomcat.log | grep org.apache.catalina.startup.Catalina.start
01-Jul-2017 18:03:47.542 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in 681 ms
7. ps
The ps command shows the status of the processes currently running in the system; it is used to determine which applications are running or to verify an operation. If you want to check the functionality of the Tomcat web server, use the ps command with the following options to obtain the process number for this server.

$ 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
To avoid reading a lot of lines and operations, send the previous output to the grep tool as follows:

$ ps -ef | grep tomcat
root         1     0  1 18:55 ?        00:00:02 /docker-java-home/jre/bi
8. env
The env command lets you view the environment variables that have been tuned for a particular terminal; it may be useful to look for problems and malfunctions to make sure that there is no wrong variable that prevents your application from starting. 
We will use the env command in the following example to check the environment variables set for the applications.

$ env
PYTHON_PIP_VERSION=9.0.1
HOME=/root
DB_NAME=test
PATH=/usr/local/bin:/usr/local/sbin
LANG=C.UTF-8
PYTHON_VERSION=3.4.6
PWD=/
DB_URI=mongodb://database:27017/test
Note that the application uses Python 3 and contains an environment variable to connect to the MongoDB database.

9. top
The top command prints a list of operations that are performed in the system in a more interactive way than the ps command. It arranges operations by activity, and the list occurs every three seconds. This tool can determine the amount of memory and processor consumed per process. A common situation can occur when an application is running, it stops working and then it is frozen. In this case, you must first check the error that the application is showing, which may be a memory error.

$ tail myapp.log
Traceback (most recent call last):
MemoryError
Is memory really not enough or is there an error? To verify this, we use the top command to determine the amount of memory and processor consumed by the application. When executed, we notice that Python uses a large percentage of the processor and memory, and what we expected was true.

01_example_top.png

While running the top tool, press C to see the full command location and link each process to a specific application.

02_example_top.png

It turns out that the application "memeater.py" consumes most of the memory. When memory is depleted as a result of consuming an application, the system terminates this application or process and sends an out-of-memory error (OOM).

10. netstat
The function of the netstat command is to display the status of the network, the ports used, and the connections received. This performance is not built into Linux so you need to install it through the net-tools package . You may encounter an error - if you are a developer - indicates that the port is reserved or the address is currently used ... etc. The netstat command can be used to analyze and examine the faults to try to fix them. The following example demonstrates that Apache uses port 80 on the machine when you perform the following command:

03_example_netstat.png

Where option t refers to TCP and option u to UDP and l to listening and n to numeric.

11. ip address
The ip address command displays the interfaces and IP addresses of the host applications or verifies the IP address of the container; if it is not present or not working, you can install it via the iproute2 package .

If your connection is connected, for example, with two networks, ip address shows the interface connected to each network; this command can also be used to obtain the IP address of the host device. The following example shows that the IP address of the Web container on the eth0 interface is 172.17.0.2:

01_example_ipaddr_0.png

12. lsof
The lsof option is used to list a list of open files and link them to applications; this may not exist in some Linux distributions so the lsof package is installed.

There is a famous saying: "Any interaction with the system in Linux is treated as a file," so if an application writes to a file or creates a connection on the network, the lsof command will be considered a file.

This command is also used similarly to the netstat command in the portlet view. For example, if you want to verify that port 80 is currently in use or not, use the lsof command with the -i option, which gives a list of open files associated with a network connection. The following image shows an Apache server (httpd) listening on port 80; the same command can be reused by adding the -p option with the process number to specify the original name of the operation and its location on the device. This is particularly useful with Apache.

02_example_lsof.png

13. df
The df command is used to show the disk space that is used to fix storage problems. When you run an application on a container, you may receive an error indicating that the space available is low, and then the df command can be used to make sure that the error is correct and that there is really no space available as the system administrator is responsible for managing and optimizing the disk space. The image shows disk space for all existing file systems.

03_example_df.png

The -h (or -human-readable) option is to show the size in readable form.

14. du
The du command is used to obtain detailed information about the size of files within a folder. If you want to know the size of the records in the / var / log folder, for example, which records are larger, do du with the -h option to make the file size and the -s option to display the total size.

$ 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 / log / messages
We find that the anaconda folder is the largest of the volumes in / var / log. This command can be used in conjunction with the df command to learn how to distribute the space used on files and what are the large files.

15. id
The task of the id command is to display information about the identity of a user, a specific group, the current user, and the group to which he or she belongs.

In the following example, we used the Vagrant tool - a tool to operate in a virtual environment - to test and isolate the application in a development environment. After accessing the Vagrant box, we attempted to install an Apache server, but the system did not allow execution, , So we use the id command to identify the current user ID.

$ yum -y install httpd 
Loaded plugins: fastestmirror 
You need to be root to perform this command. 
$ id 
uid = 1000 (vagrant) gid = 1000 (vagrant) groups = 1000 (vagrant) context = unconfined_u: unconfined_r: unconfined_t: s0-s0: c0.c1023
To execute the previous command, you must select a user with higher privileges.

16. chmod
Sometimes when you run an application for the first time on your computer, you may encounter the "Permission denied" error message, so make sure the permissions are using the ls command.

$ ls -l 
total 4 
-rw-rw-r--. 1 vagrant vagrant 34 Jul 11 ​​02:17 test.sh
Note from the example that the user does not have permission to execute (no x) to run the application; we then use the chmod command to change the permissions and enable the user to run the application by typing the following command:

$ chmod + x test.sh 
[vagrant @ localhost ~] $ ls -l 
total 4 
-rwxrwxr-x. 1 vagrant vagrant 34 Jul 11 ​​02:17 test.sh
Note that the Execute (Existence x) permission exists under File Permissions and if we now try to run it, the preceding error message will not appear.

17. dig / nslookup
Helps the DNS to convert the URL to an IP address, but did not prevent this address , there will be a problem in communication. Suppose you want to access your database (mydatabase) from your application, but you can not. If you see a "can not resolve" error message, try using the dig tool to search for DNS or nslookup to query domain name servers to resolve this problem. And why the application was unable to query the database domain name.

$ nslookup mydatabase 
Server: 10.0.2.3 
Address: 10.0.2.3 

** server can not find mydatabase: NXDOMAIN
We tried using the nslookup command but the same error message appeared, which did not find a URL for the database domain name.

If you try dig it will get the same result.

$ dig mydatabase 

; << >> DiG 9.9.4-RedHat-9.9.4-50.el7_3.1 << >> mydatabase
;;; global options: + cmd
;;; connection timed out; no servers could be reached
There are many possibilities to explain why this error is not within the subject of this article; contact your system administrator if you can not resolve the problem to help you find the cause of the problem and resolve it. If these are not on your computer, install the BIND Utilities package .

18. iptables
The iptables is a firewall exists in the majority of Linux distributions and its function is to allow or block network data traffic; may prevent the firewall specific applications from the receipt or transmission of data over the network; if one of the applications you have difficulty accessing a service or a particular site may be This firewall is the reason why data traffic is prevented from accessing that service or location.

Let's say, for example, that applications are not able to access opensource.com, we first make sure to contact the curl tool:

$ curl -vvv opensource.com 
* About to connect () to opensource.com port 80 (# 0) 
* Trying 54.204.39.132 ... 
* Connection timed out 
* Failed connect to opensource.com:80; Connection timed out
* Closing connection 0 
curl: (7) Failed connect to opensource.com:80; Connection timed out
Failed to connect to the site after the timeout; there may be a connection to that site so we use iptables with the -S option to view the firewall rules.

$ iptables -S 
-P INPUT DROP 
-P FORWARD DROP 
-P OUTPUT DROP 
-A INPUT -p tcp -m tcp --dport 22 -j ACCEPT 
-A INPUT -i eth0 -p udp -m udp --sport 53 -j ACCEPT 
-A OUTPUT -p tcp -m tcp --sport 22 -j ACCEPT 
-A OUTPUT -o eth0 -p udp -m udp --dport 53 -j ACCEPT
It is clear that the default settings for the first three rules are to "ignore" the sent, received, and forwarded network data, while the other rules are "allow" to pass data through the SSH and DNS servers. In this case consult the System Administrator if there is a need to add or modify a rule to allow traffic to the previous site. If the host you are using is a local environment or a local test, use iptables to allow the passage of non-harmful data.

19. sestatus
There is an access control mechanism on some Linux distributions called Security Enhanced Linux, or called SELinux , and it is enabled (enforcing); this mechanism provides access to currently running processes with minimal powers, These processes are harmful and thus prevent access to important files on the system. An application may sometimes need access to a file but can not display an error message; make sure SELinux prevents this application from accessing the tail command with grep to search for the "denied" message in the / var / log / audit log. SELinux may not be active from the start so it is useful to first verify this through the sestatus command.

$ sestatus 
SELinux status: enabled 
SELinuxfs mount: / sys / fs / selinux 
SELinux root directory: / etc / selinux 
Loaded policy name: targeted 
Current mode: enforcing 
Mode from config file: enforcing 
Policy MLS status: enabled 
Policy deny_unknown status: allowed 
Max kernel policy version: 28

From the previous message, SELinux is enabled; the system administrator can help you determine whether the application has access to the files it needs. In addition, it is always useful to always update SELinux on your local development environment to become more lenient.

20. history
In conclusion, and after executing a lot of commands, whether to try or fix some problems, we have to point to history, which is almost shell-free; the task of this is to print all the commands that you used since the start of your use of chance. Its usefulness is to record the commands used to fix a problem and to remember or implement it.

If we do this, all the commands you learned and tried will appear in this article:

$ history 
	1 clear 
	2 df -h 
	3 du
If you want to execute one of these commands you do not need to retype it, just use the code! Followed by the number of the order you would like to implement again.

04_example_history.png

Conclusion
I've identified a set of basic commands that will increase your troubleshooting experience, and that many system administrators use to solve and fix the problems they face. Learning and understanding these commands gives you the ability to communicate with system administrators to solve the problems you will face in the future.
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , and 70 others
properties (23)
authoraser1111
permlinktwenty-things-in-linux-are-supposed-to-be-known-to-every-system-administrator
categoryubuntu
json_metadata{"tags":["ubuntu","steemthat","brograming","utopian-io"],"image":["https://steemitimages.com/DQmfN7mLbkgr1vEAkkisEPS3kLYcVp6wYDej6KW7pSrw9h8/image.png"],"links":["https://opensource.com"],"app":"steemit/0.1","format":"markdown"}
created2018-04-20 21:32:06
last_update2018-04-20 21:32:06
depth0
children2
last_payout2018-04-27 21:32:06
cashout_time1969-12-31 23:59:59
total_payout_value8.074 HBD
curator_payout_value2.298 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length20,213
author_reputation3,224,691,028,416
root_title"Twenty things in Linux are supposed to be known to every system administrator"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id51,195,863
net_rshares1,793,575,203,682
author_curate_reward""
vote details (134)
@postpromoter ·
re-aser1111-twenty-things-in-linux-are-supposed-to-be-known-to-every-system-administrator-20180423t234619423z
You got a 1.33% upvote from @postpromoter courtesy of @aser1111!

Want to promote your posts too? Check out the [Steem Bot Tracker website](https://steembottracker.com) for more info. If you would like to support the development of @postpromoter and the bot tracker please [vote for @yabapmatt for witness!](https://v2.steemconnect.com/sign/account-witness-vote?witness=yabapmatt&approve=1)
properties (22)
authorpostpromoter
permlinkre-aser1111-twenty-things-in-linux-are-supposed-to-be-known-to-every-system-administrator-20180423t234619423z
categoryubuntu
json_metadata{"app":"postpromoter/1.9.1"}
created2018-04-23 23:46:21
last_update2018-04-23 23:46:21
depth1
children0
last_payout2018-04-30 23:46:21
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length391
author_reputation12,722,616,650,811
root_title"Twenty things in Linux are supposed to be known to every system administrator"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id51,764,137
net_rshares0
@steembottrackerr ·
<center>https://steemitimages.com/200x200/https://s-media-cache-ak0.pinimg.com/originals/81/28/3c/81283c6aed7bdb5b9f8ad73b8ce62c2f.jpg</center>
---
<center>Hello @aser1111 , Congratulations ✅ . Your content began to appear in the hot section.
I am the information account of "SteemBotTracker" site.
</center>
---
<center>
Your Informations
Total SBD: 5.211
Total STEEM: 0.024
</center>
---
<center>
I recommend to increase this;
You can make "Resteem" and advertise to the followers of the whale accounts.
"Resteem Bot" for you;
✅ The most profitable Resteem Whale @hottopic  has 18.500 Followers + 5200 Sp + Upvote with min +45 accounts. 
</center>
---
<center>
You can purchase "upvote" by bid bots.
"Upvote Bot"
✅ The most profitable whale in the last round. @buildawhale
</center>
---
<center>
I'm taking this message once. You need to use the #steembottrackerr tag for more information.
Those who "upvote" this interpretation will be awarded a "UpVote" prize of 100 Sbd per week per person.
I am a bot, I can not answer the comment. I hope I could help. Good luck. Sorry if I disturbed you.
</center>
properties (22)
authorsteembottrackerr
permlink20180424t194729568z
categoryubuntu
json_metadata{"tags":["advice"],"app":"steemjs/test"}
created2018-04-24 19:47:36
last_update2018-04-24 19:47:36
depth1
children0
last_payout2018-05-01 19:47:36
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length1,134
author_reputation-1,493,369,324,060
root_title"Twenty things in Linux are supposed to be known to every system administrator"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id51,926,474
net_rshares0