Linux系統作web類項目服務器的首先系統,在從事web項目相關的開發、測試、運維等工作時,常用的Linux命令屬於基本技能。
本文將常見的Linux命令進行分類、彙總、說明,各命令較少涉及參數的使用,如需詳細瞭解某個命令可以通過:
1.官網:https://www.linux.org/
2.參考:Linux常用命令大全
3.終端輸入:man 命令 或: 命令 --help # 可查看幫助信息
Linux常用命令分類:
系統狀態顯示:系統、內存、CPU、時間、聯網等,服務器常規的信息查看命令
系統工作命令:關機、重啓、進程、下載、用戶管理等,服務器工作相關的命令
目錄文件操作:切換目錄、新建文件夾/文件、複製粘貼、剪切、刪除、查找、備份、更改權限、製作軟連接等
文件內容操作:內容查看、編輯文件內容,簡要介紹強大的vim編輯器
磁盤相關操作:磁盤的查看、掛載、卸載等
壓縮解壓操作:文件壓縮、解壓處理
軟件安裝卸載:使用常用的包管理工具進行軟件的安裝、更新/升級、卸載等
其他與web項目相關的命令:數據庫、反向代理等
一、系統狀態顯示命令
1、當前系統信息顯示:
[ec2-user@ip-172-31-34-34 /]$ echo Hello,world! # 先來慣例,echo:打印命令
Hello,world!
[ec2-user@ip-172-31-34-34 ~]$ arch # 顯示內核架構 x86_64 [ec2-user@ip-172-31-34-34 ~]$ uname # 顯示內核信息 Linux [ec2-user@ip-172-31-34-34 ~]$ uname -m # 顯示內核架構 x86_64 [ec2-user@ip-172-31-34-34 ~]$ uname -r # 顯示內核版本 4.14.77-70.59.amzn1.x86_64 [ec2-user@ip-172-31-34-34 ~]$ uname -a # 內核具體信息 Linux ip-172-31-34-34 4.14.77-70.59.amzn1.x86_64 #1 SMP Mon Nov 12 22:02:45 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux [ec2-user@ip-172-31-34-34 ~]$ cat /proc/version # 系統版本 Linux version 4.14.77-70.59.amzn1.x86_64 (mockbuild@gobi-build-64001) (gcc version 7.2.1 20170915 (Red Hat 7.2.1-2) (GCC)) #1 SMP Mon Nov 12 22:02:45 UTC 2018 [ec2-user@ip-172-31-34-34 ~]$ cat /etc/issue # 系統發行版信息 Amazon Linux AMI release 2018.03 Kernel \r on an \m
2、查看內存:
[ec2-user@ip-172-31-34-34 ~]$ free -m # 參數m是指按照MB統計 可換成 b k m g
total used free shared buffers cached
Mem: 985 883 102 2 20 91
-/+ buffers/cache: 770 214
Swap: 0 0 0
3、查看CPU情況:
[ec2-user@ip-172-31-34-34 ~]$ lscpu
Architecture: x86_64
CPU op-mode(s): 32-bit, 64-bit
Byte Order: Little Endian
CPU(s): 1
On-line CPU(s) list: 0
Thread(s) per core: 1
Core(s) per socket: 1
Socket(s): 1
NUMA node(s): 1
Vendor ID: GenuineIntel
CPU family: 6
Model: 63
Model name: Intel(R) Xeon(R) CPU E5-2676 v3 @ 2.40GHz
Stepping: 2
CPU MHz: 2400.110
BogoMIPS: 4800.11
Hypervisor vendor: Xen
Virtualization type: full
L1d cache: 32K
L1i cache: 32K
L2 cache: 256K
L3 cache: 30720K
NUMA node0 CPU(s): 0
[ec2-user@ip-172-31-34-34 ~]$ getconf LONG_BIT # 位數
64
[ec2-user@ip-172-31-34-34 ~]$ uptime # 查看負載信息
11:19:29 up 23 days, 8:17, 1 user, load average: 0.00, 0.00, 0.00
4、當前系統日期和時間
[ec2-user@ip-172-31-34-34 ~]$ date # 當前是UTC時區,修改時區可通過tzselect命令,或者直接修改/etc/sysconfig/clock配置文件,重啓後生效
Thu Mar 21 10:03:55 UTC 2019 # 修改時間通過運行 date date 041217002007.00 然後執行clock -w 修改的時間格式是:月日時分年.秒
[ec2-user@ip-172-31-34-34 ~]$ cal 2019 # 顯示2019年日曆
2019
January February March
Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa
1 2 3 4 5 1 2 1 2
6 7 8 9 10 11 12 3 4 5 6 7 8 9 3 4 5 6 7 8 9
13 14 15 16 17 18 19 10 11 12 13 14 15 16 10 11 12 13 14 15 16
20 21 22 23 24 25 26 17 18 19 20 21 22 23 17 18 19 20 21 22 23
27 28 29 30 31 24 25 26 27 28 24 25 26 27 28 29 30
31
5、聯網情況
[ec2-user@ip-172-31-34-34 ~]$ ifconfig
eth0 Link encap:Ethernet HWaddr 06:3D:35:50:67:14
inet addr:172.31.**.** Bcast:172.31.88.*** Mask:255.255.240.0 # 顯示本地ip地址
inet6 addr: fe80::ggg:35ff:fe50:6714/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:9001 Metric:1
RX packets:1016816 errors:0 dropped:0 overruns:0 frame:0
TX packets:996573 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:94653851 (90.2 MiB) TX bytes:917494558 (874.9 MiB)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:220729 errors:0 dropped:0 overruns:0 frame:0
TX packets:220729 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:131542337 (125.4 MiB) TX bytes:131542337 (125.4 MiB)
二、系統工作命令
1、關機、重啓、註銷登錄:
[ec2-user@ip-172-31-34-34 ~]$ init . # 或者 init 0 關機
[ec2-user@ip-172-31-34-34 ~]$ shutdow # 關機
[ec2-user@ip-172-31-34-34 ~]$ poweroff # 關機
[ec2-user@ip-172-31-34-34 ~]$ shutdow -h hours:minutes & # 定時關機
[ec2-user@ip-172-31-34-34 ~]$ shutdow -c # 取消定時關機
[ec2-user@ip-172-31-34-34 ~]$ shutdow -r now # 重啓
[ec2-user@ip-172-31-34-34 ~]$ reboot # 重啓
[ec2-user@ip-172-31-34-34 ~]$ logout # 註銷登錄
2、用戶管理:
[ec2-user@ip-172-31-34-34 ~]$ who # 顯示當前用戶信息
ec2-user pts/0 Mar 21 09:01 (223.71.81.68)
[ec2-user@ip-172-31-34-34 ~]$ groups # 查看當前用戶的組內成員,關於用戶組的操作不做介紹
ec2-user wheel
[ec2-user@ip-172-31-34-34 ~]$ hostname # 主機名
ip-172-31-34-34
[ec2-user@ip-172-31-34-34 ~]$ w # 顯示當前活動的用戶的具體信息
10:28:12 up 23 days, 7:26, 1 user, load average: 0.00, 0.00, 0.00
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
ec2-user pts/0 223.71.81.68 09:01 1.00s 0.06s 0.00s w
[ec2-user@ip-172-31-34-34 ~]$ passwd 密碼 # 修改當前用戶的登錄密碼
[ec2-user@ip-172-31-34-34 ~]$ passwd user1 密碼 # 修改user1的登錄密碼 只允許root用戶修改
[ec2-user@ip-172-31-34-34 ~]$ last # 顯示登錄記錄
ec2-user pts/0 223.71.81.68 Thu Mar 21 09:01 still logged in
ec2-user pts/0 223.71.81.68 Mon Mar 18 11:09 - 11:09 (00:00)
3、進程管理:
[ec2-user@ip-172-31-34-34 ~]$ ps aux # ps 是常用的進程顯示命令,通常和管道符(|)一起查詢某個進程信息,ps參數很多,請自行參考:man ps
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.2 19692 2372 ? Ss Feb26 0:02 /sbin/init
root 2 0.0 0.0 0 0 ? S Feb26 0:00 [kthreadd]
root 3 0.0 0.0 0 0 ? I Feb26 0:00 [kworker/0:0]
[ec2-user@ip-172-31-34-34 ~]$ ps aux|grep nginx # 查看Nginx進程
root 9548 0.0 0.2 59952 2528 ? Ss Feb28 0:00 nginx: master process /usr/sbin/nginx
[ec2-user@ip-172-31-34-34 ~]$ top # top 命令可以動態監視本都進程和系統負載信息 屬於強化版的‘win系統任務管理器’
top - 10:38:48 up 23 days, 7:37, 1 user, load average: 0.00, 0.00, 0.00
Tasks: 97 total, 1 running, 72 sleeping, 0 stopped, 0 zombie
Cpu(s): 0.0%us, 0.0%sy, 0.0%ni,100.0%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
Mem: 1009432k total, 906100k used, 103332k free, 21796k buffers
Swap: 0k total, 0k used, 0k free, 94576k cached
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1 root 20 0 19692 2372 2048 S 0.0 0.2 0:02.81 init
2 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kthreadd
3 root 20 0 0 0 0 I 0.0 0.0 0:00.00 kworker/0:0
[ec2-user@ip-172-31-34-34 ~]$ pidof sshd # 查看指定進程的 進程號(PID)
9916 9914 9722 9720 2577
[ec2-user@ip-172-31-34-34 ~]$ kill # 結束進程命令 命令格式: kill 進程號(PID)
kill: usage: kill [-s sigspec | -n signum | -sigspec] pid | jobspec ... or kill -l [sigspec] # kill 參數
[ec2-user@ip-172-31-34-34 ~]$ kill -9 9916 # 常用 -9 這個參數,來強制結束某一進程
[ec2-user@ip-172-31-34-34 ~]$ kill -l # 這個命令列出所有數字含義,其中9是指忽略程序的等待情況(比如有的進程需要釋放內存,釋放的時間太長,導致kill阻塞),直接停止
1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL 5) SIGTRAP
6) SIGABRT 7) SIGBUS 8) SIGFPE 9) SIGKILL 10) SIGUSR1
11) SIGSEGV 12) SIGUSR2 13) SIGPIPE 14) SIGALRM 15) SIGTERM
16) SIGSTKFLT 17) SIGCHLD 18) SIGCONT 19) SIGSTOP 20) SIGTSTP
21) SIGTTIN 22) SIGTTOU 23) SIGURG 24) SIGXCPU 25) SIGXFSZ
26) SIGVTALRM 27) SIGPROF 28) SIGWINCH 29) SIGIO 30) SIGPWR
31) SIGSYS 34) SIGRTMIN 35) SIGRTMIN+1 36) SIGRTMIN+2 37) SIGRTMIN+3
38) SIGRTMIN+4 39) SIGRTMIN+5 40) SIGRTMIN+6 41) SIGRTMIN+7 42) SIGRTMIN+8
43) SIGRTMIN+9 44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12 47) SIGRTMIN+13
48) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14 51) SIGRTMAX-13 52) SIGRTMAX-12
53) SIGRTMAX-11 54) SIGRTMAX-10 55) SIGRTMAX-9 56) SIGRTMAX-8 57) SIGRTMAX-7
58) SIGRTMAX-6 59) SIGRTMAX-5 60) SIGRTMAX-4 61) SIGRTMAX-3 62) SIGRTMAX-2
63) SIGRTMAX-1 64) SIGRTMAX
[ec2-user@ip-172-31-34-34 ~]$ killall httpd # 結束整個進程樹 命令格式: killall 進程名
4、文件下載:
[ec2-user@ip-172-31-34-34 ~]$ wget 下載鏈接 # 下載文件,常用於源碼包的下載
三、目錄文件操作
1、路徑、目錄操作:
[ec2-user@ip-172-31-34-34 ~]$ pwd # 顯示當前路徑
/home/ec2-user
[ec2-user@ip-172-31-34-34 ~]$ cd / # 切換到指定目錄,命令格式: cd 絕對路徑或者相對路徑 其中 cd .. 返回上層目錄,cd - 返回上次目錄
[ec2-user@ip-172-31-34-34 /]$ ls # 顯示當前文件夾下的文件夾和文件,不含隱藏文件
bin cgroup etc lib local media opt root sbin srv tmp var
boot dev home lib64 lost+found mnt proc run selinux sys usr
[ec2-user@ip-172-31-34-34 /]$ ls -al # ls 的參數很多 常用的有-a 顯示所有文件夾和文件,含隱藏的 -l 顯示具體信息
total 116
dr-xr-xr-x 25 root root 4096 Feb 26 03:02 .
dr-xr-xr-x 25 root root 4096 Feb 26 03:02 ..
-rw-r--r-- 1 root root 0 Feb 26 03:01 .autofsck
-rw-r--r-- 1 root root 0 Nov 20 05:50 .autorelabel
dr-xr-xr-x 2 root root 4096 Nov 16 23:06 bin
dr-xr-xr-x 4 root root 4096 Nov 16 23:07 boot
drwxr-xr-x 2 root root 4096 Feb 28 2014 cgroup
2、新建、複製粘貼、剪切、刪除操作:(操作文件時,必須是完整的文件名+文件格式,如:file.txt)
[ec2-user@ip-172-31-34-34 ~]$ mkdir aaa # 創建文件夾
[ec2-user@ip-172-31-34-34 ~]$ touch aaa.txt # 新建文件,指定文件格式,touch命令很強大,可以修改文件的創建、更新等時間
[ec2-user@ip-172-31-34-34 ~]$ ls
aaa aaa.txt
[ec2-user@ip-172-31-34-34 ~]$ cp 路徑1/file1 路徑2/file2 # 複製文件夾或文件,將文件1複製到文件2,注意:兩個文件(夾)的名字可以不一樣,但是內容是一樣的
[ec2-user@ip-172-31-34-34 ~]$ mv 路徑1/file1 路徑2/file2 # 移動文件夾或文件,將文件1移動文件2
[ec2-user@ip-172-31-34-34 ~]$ mv file1 file2 # 如果是同一個路徑,則實現了重命名的功能
[ec2-user@ip-172-31-34-34 ~]$ rm 文件名 # 刪除一個文件,rm 常用參數:-r 遞歸刪除,刪除文件夾使用 -f 強制刪除
[ec2-user@ip-172-31-34-34 ~]$ rm -rf 文件夾 # 刪除一個文件夾,常用-rf
3、文件查找:
[ec2-user@ip-172-31-34-34 ~]$ find 路徑 -name 文件名 # 其中路徑常用 / 表示從根目錄開始查找,文件名可以是文件全稱,也可使用通配符
[ec2-user@ip-172-31-34-34 ~]$ which mysql # 常用於顯示二進制的可執行程序文件的路徑
/usr/bin/mysql
[ec2-user@ip-172-31-34-34 ~]$ whereis mysql # 顯示二進制文件的命令文件、源碼等相關信息的路徑
mysql: /usr/bin/mysql /usr/include/mysql /usr/share/mysql /usr/share/man/man1/mysql.1.gz
4、文件備份,強大的dd命令:
[ec2-user@ip-172-31-34-34 ~]$ dd if=文件 of=文件 count=1 bs=200M # 命令解釋:if輸入的文件名 of輸出的文件名 bs設置每個塊大小 count設置總塊數
# 1、有個神奇的設備文件 /dev/zero,不佔具體內存空間,但是可以提供無窮的數據,常用於創建隨意大小的空文件,如:
dd if=/dev/zero of=/var/xx_file.txt count=1 bs=10M
# 2、dd命令常用於製作系統鏡像,如
dd if=/dev/cdrom of=linux_64.iso
5、文件權限管理:
[ec2-user@ip-172-31-34-34 /]$ ls -al # 使用ls可顯示文件的權限
dr-xr-xr-x 4 root root 4096 Nov 16 23:07 boot
drwxr-xr-x 2 root root 4096 Feb 28 2014 cgroup
[ec2-user@ip-172-31-34-34 ~]$ chmod [參數] 權限 文件或文件夾 # 權限可以使用r w x,也可使用數字表示,如修改文件權限,需要使用-R參數,表示遞歸
[ec2-user@ip-172-31-34-34 ~]$ chmod -R 777 aaa # 將aaa文件夾權限改爲 所有人、全部權限
四、文件內容操作:
內容查看、編輯文件內容,簡要介紹強大的vim編輯器
1、文件內容查看:
[ec2-user@ip-172-31-34-34 online]$ cat test.py # cat 命令常用於查看小文件的內容
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
__author__ = "問道編程"
__date__ = "2018-06-04 16:59"
def application(venv, start_response):
start_response('200 OK', [('Content-Type','text/html')])
return [b"Hello World from uWSGI"]
[ec2-user@ip-172-31-34-34 online]$ more uswgi.log # more 常用於大文件的查看,可使用空格鍵或回車向下翻頁,有百分比提示
chdir() to /var/www/online
your processes number limit is 3867
your memory page size is 4096 bytes
detected max file descriptor number: 1024
lock engine: pthread robust mutexes
thunder lock: disabled (you can enable it with --thunder-lock)
--More--(0%)
[ec2-user@ip-172-31-34-34 online]$ head -n 5 uswgi.log # head 常用於查看文件的前幾行,命令格式:head -n 行數 文件名
*** Starting uWSGI 2.0.17 (64bit) on [Tue Jun 12 07:54:51 2018] ***
compiled with version: 5.5.0 on 12 June 2018 06:58:29
os: Linux-4.14.33-51.37.amzn1.x86_64 #1 SMP Thu May 3 20:07:43 UTC 2018
nodename: ip-172-31-40-62
machine: x86_64
[ec2-user@ip-172-31-34-34 online]$ tail -f uswgi.log # tail 常用於查看文件的尾部,用法同head,但是tail有個強大的功能,tail -f 可以查看動態日誌文件,可持續刷新
[ec2-user@ip-172-31-34-34 online]$ cat test.py | tr [a-z] [A-Z] # 將文件中的小寫全部轉換爲大寫,tr命令通常和|一起用,格式是 tr 原字符 目標字符
[ec2-user@ip-172-31-34-34 online]$ wc test.py # 統計文件的 行數、字數、字節數,可選參數有 -l -w -c,單獨顯示行數、字數、字節數
8 25 245 test.py
[ec2-user@ip-172-31-34-34 online]$ stat test.py # 查看文件具體信息
File: 'test.py'
Size: 245 Blocks: 8 IO Block: 4096 regular file
Device: ca01h/51713d Inode: 656710 Links: 1
Access: (0777/-rwxrwxrwx) Uid: ( 500/ec2-user) Gid: ( 500/ec2-user)
Access: 2018-11-27 07:59:36.000000000 +0000
Modify: 2018-06-12 05:40:12.000000000 +0000
Change: 2018-11-27 08:41:48.066246190 +0000
Birth: -
2、使用vim編輯文件內容:
後續更新、、、、
參考資料:https://blog.csdn.net/qq_31278903/article/details/83146031