Organize some basic system administration commands for Linux as a memo
=========================================================================
To view a file or edit a file:
=========================================================================
View files, folders, and read and write execution permissions in the current directory
$ ll
Output Description: (Take mnt/this record as an example)
First column (drwxrwxr-x) identifies file type and file permissions
The second column (5) identifies the number of file links
third column (root) identifies the file owner
Fourth column (bme319-users) identifies the group in which the file resides
The fifth column (4096) indicates the file length (size)
Sixth to eighth column (3 20:54) file Last updated
The Nineth column represents the file name
The first character of the first column represents the file type:
D |
Directory |
- |
File |
L |
Link |
S |
Socket |
P |
Named pipe |
B |
Block device |
C |
Character device |
The first column of 2~10 characters is a group of three characters, each representing the permissions of the file owner, the group in which the file resides, and other users:
RWX (read, write, execute) appears to have permissions,-means no permissions, and mnt/, for example, indicates that the file owner root has read and write execution permissions, that the group Bme319-users has read and write execution permissions, and that other users have the reading and execution rights, and no write permission. It is necessary to note that the user needs to have Execute permission to access the folder (or directory)
=========================================================================
View the last part of the file:
Tail
View the last 10 lines of the file:
Tail Mongod.log
View the last 20 lines of the file:
Tail Mongod.log
When the file has new content, print it out:
tail -F Mongod.log
Ideal for viewing log output when executing an application
=========================================================================
View or merge Files
Cat
To view files:
Cat Mongod.log
Callout File line number:
Cat -N mongod.log
A blank line above two lines shows only one line:
Cat -S Mongod.log
Merging files:
Add a line number to the Log1.log and enter it into the Log2.log:
Cat -N log1.log log2.log
Enter the contents of the Log1.log and Log2.log in addition to the blank line plus the uplink number into the Log3.log:
Cat -B log1.log log2.log Log3.log
=========================================================================
Edit File
sudo vim/etc/mongod.conf
After entering the edit page, you can edit the file using some of the usage of vim:
I enter edit insert mode, click ESC to return a character after vimx delete cursor: Q exit: Wq save and exit: Q! Force exit (do not save)
For specific other editing methods, please see VIM usage
=========================================================================
Permission-Related:
To change file or directory permissions:
chmod
The Read permission assignment for the 4,write permission is assigned to the 2,execute permission assignment to 1, and when the permission is changed, the permissions that are required are added together, with a total of 3 numbers representing the owner, all groups, and other users. For example: For the file mongod.conf, the owner has read and write execution permissions, all groups have reading and execute permissions, and other users have only Read permissions, then use the following command:
chmod 754 mongod.conf
For the directory, you want to make changes to its subdirectories, just add the-R operation:
chmod 755 /mnt
=========================================================================
To change the owner of a file or directory:
Chown (change owner)
Change the owner of the file mongod.conf to bme319
chown bme319 mongod.conf
You can also use the-R operation:
chown -R bme319/mnt
=========================================================================
Change all groups of files or directories:
chgrp (Change group)
Similar to the previous usage:
Chgrpchgrp -R bme319/mnt
=========================================================================
Curl Command:
This is a very useful command for Web development.
View Web page source code:
$ Curl Www.sina.com
Auto Jump (some URLs are auto-jump):
$ curl-l www.sina.com
Can jump to www.sina.com.cn
Display HTTP response header information:
I uppercase only display header information, lowercase display header information and page code
$ curl-i www.sina.com
To display the HTTP communication process:
$ curl-v www.sina.com
Or a more detailed communication process:
$ curl–trace output.txt www.sina.com
Or
$ curl–trace-ascii output.txt www.sina.com
Send form information:
GET Request:
$ Curl Example.com/form.cgi?data=xxx
POST request:
$ curl-x post–data "Data=xxx" example.com/form.cgi
If you need to encode your data as a form:
$ curl-x Post–data-urlencode "date1" example.com/form.cgi
Note that the-x parameter is required when the get is used directly with the curl [url] and the-x parameter can specify any HTTP verb:
$ curl-x DELETE www.example.com
If the domain requires HTTP authentication, you can use the--user parameter:
$ curl–user Name:password example.com
Or just enter the user name, then you will be prompted to enter the password:
$ curl–user Name example.com
If you need to impersonate a client device, you need to use the User agent field, and the server may return different data formats based on this field. For example, the user agent for IPhone4 is:
mozilla/5.0 (IPhone; U CPU iPhone os 4_0 like Mac os X; En-US) applewebkit/532.9 (khtml, like Gecko) version/4.0. 5 mobile/8a293 safari/6531.22. 7
Then use curl to simulate this:
$ curl–user-agent "[user agent]" [URL]
To set up a proxy:
$ curl-x proxyserver.test.com:1111 http://google.com
FTP server upload or download files:
List files or directories:
FTP://ftpserver/dir/
Download file:
FTP://ftpserver/dir/file.html
Upload file Myfile.log to server:
FTP://ftpserver
Upload Multiple files simultaneously:
FTP://ftpserver
From the table, enter the get content to the server specified file:
FTP://Ftpserver/myfile.log
Upload or download (save) files:
Download the file locally and name it myfile.html
$ Curl-o myfile.html http://example.com/file.html
Download the file locally and do not rename it:
$ Curl-o http://example.com/file.html
can also use > for steering output
Get multiple files at the same time:
$ Curl-o Url1-o URL2
The-c option allows you to use the breakpoint continuation feature on large files:
To end the process before the download is complete:
$ Curl-o http://example.com/file.html
By adding the-C operation to continue to download the file, the downloaded file will not be re-downloaded:
$ curl-c-O http://example.com/file.html
Upload file:
$ curl–form "[email protected]" http://Example.com/dir
=========================================================================
Python-m Json.tool
With curl, the output of the API calls is poorly readable, the JSON-formatted data is stacked on one line, and if you want to format it as a newline, you can use python-m Json.tool to indent and organize the JSON. You need to use the pipeline mechanism to write the output of the JSON file as input to the Python-m json.tool command line.
$ curl http:///example.com/api/getjson/| python-m json.tool
=========================================================================
Grep
grep can search for text using a specific pattern match, including regular expressions. If you look for a specific pattern in the output of another command, grep highlights the related rows. You can use this command to search for log files and specific processes, and so on:
Cat grep 27017"
=========================================================================
Ps
PS can view the various status information of a process, the-e action indicates that all are listed, and the-F option indicates the details, so:
PS -ef
To check all processes running on the server. It is more common to find information about a particular process that can be used with grep:
PS -ef | Gref "MONGO"
=========================================================================
Env
Env is used to list all environment variables and assign values to them. During troubleshooting, you may want to check if there are errors in the environment variables that prevent the application from starting. Check the environment variables set on the program host:
Env
=========================================================================
Top
Top is used to display information and resource usage for each process in the system, similar to the Task Manager for Windows. Use this command to determine which processes are running, and how much memory and CPU they have.
Press the C key to view a detailed start command for the process that starts the red line:
=========================================================================
Netstat
The netstat is used to display network status information. This command displays the network port you are using and its incoming connections. Similarly, it is commonly associated with grep:
grep 27017
Operator Description:
-T-u (TCP, UDP) shows only the results of the two protocols;-p show PID or program name -l only show listening port -N does not resolve domain name (IP only)
=========================================================================
IP Address
Used to display the host interface and IP address of the application. To view the IP address of an interface, you can use show. To view the native IP address, use the following command:
$ IP Address show eth0
If you need to see the addresses of all containers and hosts, you can use the following command:
Ifconfig
=========================================================================
Lsof
Lsof (list open files) is used to list the files that are open by the current system. In a Linux system, almost any interaction with the system is treated as a file. Therefore, if the application writes to a file or makes a network connection, lsof will map the interaction to a file. Similar to netstat, you can use lsof to check listening ports. It is important to note that root permissions may be required to view all processes, especially when the process is not running by the current user.
sudo lsof-i tcp:27017
Operator-I selects all Ipv4/ipv6 processes. If selected based on process ID (PID):
sudo 3235
You can view all the information for the process.
=========================================================================
Df
Use the DF command to display idle disk space (display free) to troubleshoot disk space issues. To make the results more readable, use the-H operation:
DF -H
You can get disk space information, including used and unused, and so on.
=========================================================================
Du
is also used to view the space used, but unlike the DF command, the du command is a view of the space used by the file and directory disks, and you can use the du command to get more details about which files use disk space in the directory. For example, to get the total size available with-s operation, plus-H can make the results more readable:
sudo du -sh /mnt/*
The above command is to get all directories or file sizes under the/mnt directory.
=========================================================================
Id
This command can check the current user identity, including UID (user ID), GID (group ID), groups information. How to use:
Id
Or
ID user_name
=========================================================================
Dig/nslookup
The dig command is a common domain name query tool that you can use to test whether your domain Name system is working properly. Domain Name server (DNS) helps resolve URLs to a set of application servers. However, you will find that some URLs cannot be parsed, which can lead to application connectivity issues.
=========================================================================
Iptables
Iptables is used to block or allow traffic on a Linux host for IP packet filter management, similar to a network firewall. This tool can prevent some applications from receiving or sending requests. More specifically, if an application is having difficulty accessing another endpoint, it may have been iptables denied traffic to access that endpoint. To view iptables rules:
sudo iptables-s
=========================================================================
Sestatus
SELinux (a Linux security module) is typically used on enterprise-management application hosts. SELinux provides minimal access to processes running on the host, preventing potentially malicious processes from accessing important files on the system. In some cases, an application needs to access a specific file, but an error may occur. To check if SELinux is blocking the application, use tail and grep to find information about "denied" (denied) in/var/log/audit logging. Otherwise, use Sestatus to check if SELinux was started.
=========================================================================
History
When you use a lot of commands for testing and debugging, you may forget useful commands. Each shell has a variant of the history command. It displays the history of the commands used since the beginning of the session. You can use history to record commands that are used to troubleshoot applications. The history command is used to display a specified number of command commands, to read the directory from the historical command file to the history command buffer, and to write the directory in the history command buffer to the command file.
$ history
This command can be used to view a list of history commands, and use! Specify the sequence number of the command to execute the corresponding command:
$ ! 288
Executes the command with the ordinal 288 in the history command list.
=========================================================================
Reference:
Https://mp.weixin.qq.com/s/pEwNyYPfZ18h5vUxA_TQsg
Http://www.ruanyifeng.com/blog/2011/09/curl.html
Linux commands commonly used by system administrators