Java程序員必會常用Linux速查手冊


目錄

  1. 系統服務管理
  2. 文件管理
  3. 查看日志
  4. 壓縮與解壓
  5. 磁盤和網絡管理
  6. 防火牆
  7. ftp操作
  8. 軟件的安裝與管理
  9. 其他

系統服務管理

systemctl

輸出系統中各個服務的狀態:

systemctl list-units --type=service

查看服務的運行狀態:

systemctl status firewalld

關閉服務:

systemctl stop firewalld

啟動服務:

systemctl start firewalld

重新啟動服務(不管當前服務是啟動還是關閉):

systemctl restart firewalld

重新載入配置信息而不中斷服務:

systemctl reload firewalld

禁止服務開機自啟動:

systemctl disable firewalld

設置服務開機自啟動:

systemctl enable firewalld

文件管理

查找文件
(根據名稱查找/目錄下的filename.txt文件)

find / -name filename.txt

查看文件,包含隱藏文件

ls -al

列出當前目錄(/)下的所有文件:
ls

ls -l /

獲取目前所在工作目錄的絕對路徑

pwd

改變當前工作目錄:cd

cd /usr/local

顯示或修改系統時間與日期;date

date '+%Y-%m-%d %H:%M:%S'

用於設置用戶密碼:passwd

passwd root

改變用戶身份(切換到超級用戶):su

su -username

用於清除屏幕信息

clear

顯示指定命令的幫助信息:man

man ls

查詢系統處於什么運行級別:who

who -r

顯示目前登錄到系統的用戶:

who -buT

顯示系統內存狀態(單位MB):free

free -m

顯示系統進程運行動態:ps

ps -ef

查看sshd進程的運行動態:

ps -ef | grep sshd

查看即時活躍的進程,類似Windows的任務管理器

top

創建目錄

mkdir

復制文件包括其子文件到自定目錄

cp -r sourceFolder targetFolder

刪除目錄(此目錄是空目錄)

rmdir deleteEmptyFolder

刪除文件包括其子文件

rm -rf deleteFile

刪除文件:rm

rm text.txt

移動文件

mv /temp/movefile /targetFolder

移動或覆蓋文件:mv

mv oldNameFile.md newNameFile.md

修改文件權限(file.java的權限-rwxrwxrwx,r表示讀、w表示寫、x表示可執行)

chmod 777 file.java

用於文件過長時分頁查看文件內容:more
每頁10行查看boot.log文件

more -c -10 /var/log/boot.log

查看Linux啟動日志文件文件,並標明行號:cat

cat -Ab /var/log/boot.log

創建text.txt文件:touch

touch text.txt

啟動Vi編輯器

vi filename

1)進入編輯模式

shift+i

2)退出編輯模式

esc-->shift+:

3)保存退出

wq

4)強制退出

q

查看日志

查看文件頭10行

head -n 10 example.txt

查看文件尾10行

tail -n 10 example.txt

查看日志文件(這個命令會自動顯示新增內容,屏幕只顯示10行內容的(可設置))

tail -f exmaple.log

在日志中搜索關鍵字

less server.log

1)如果想從日志第一行開始搜索

less server.log-->/搜索關鍵字-->n查找下一個-->N查找上一個

2)如果想從日志最后一行開始搜索

less server.log-->shitf+g-->?搜索關鍵字-->n查找上一個-->N查找下一個

壓縮與解壓

解壓

unzip FileName.zip
壓縮:
zip -r FileName.zip DirName

將/etc文件夾中的文件歸檔到文件etc.tar(並不會進行壓縮):tar

tar -cvf /mydata/etc.tar /etc

用gzip壓縮文件夾/etc中的文件到文件etc.tar.gz:

tar -zcvf /mydata/etc.tar.gz /etc

用bzip2壓縮文件夾/etc到文件/etc.tar.bz2:

tar -jcvf /mydata/etc.tar.bz2 /etc

分頁查看壓縮包中內容(gzip):

tar -ztvf /mydata/etc.tar.gz |more -c -10

解壓文件到當前目錄(gzip):

tar -zxvf /mydata/etc.tar.gz

磁盤和網絡管理

查看磁盤使用

df -h

查看磁盤使用
free

查看磁盤空間占用情況:

df -hT

dh
查看當前目錄下的文件及文件夾所占大小:

du -h --max-depth=1 ./*

顯示當前網絡接口狀態

ifconfig

查看當前路由信息:netstat

netstat -rn

查看所有有效TCP連接:

netstat -an

查看系統中啟動的監聽服務:

netstat -tulnp

查看系統中某個端口監聽服務:

netstat -ntlp|grep 8080

查看處於連接狀態的系統資源信息:

netstat -atunp

查看是否存在某一個進程

ps -ef|grep java/pid

從網絡上下載文件

wget

防火牆

Linux中有兩種防火牆軟件,ConterOS7.0以上使用的是firewall,ConterOS7.0以下使用的是iptables,本文將分別介紹兩種防火牆軟件的使用。

Firewall

開啟防火牆:

systemctl start firewalld

關閉防火牆:

systemctl stop firewalld

查看防火牆狀態:

systemctl status firewalld

設置開機啟動:

systemctl enable firewalld

禁用開機啟動:

systemctl disable firewalld

重啟防火牆:

firewall-cmd --reload

開放端口(修改后需要重啟防火牆方可生效):

firewall-cmd --zone=public --add-port=8080/tcp --permanent

查看開放的端口:

firewall-cmd --list-ports

關閉端口:

firewall-cmd --zone=public --remove-port=8080/tcp --permanent

Iptables

安裝
由於CenterOS7.0以上版本並沒有預裝Iptables,我們需要自行安裝。
安裝前先關閉firewall防火牆

安裝iptables:

yum install iptables

安裝iptables-services:

yum install iptables-services

開啟防火牆:

systemctl start iptables.service

關閉防火牆:

systemctl stop iptables.service

查看防火牆狀態:

systemctl status iptables.service

設置開機啟動:

systemctl enable iptables.service

禁用開機啟動:

systemctl disable iptables.service

查看filter表的幾條鏈規則(INPUT鏈可以看出開放了哪些端口):

iptables -L -n

查看NAT表的鏈規則:

iptables -t nat -L -n

清除防火牆所有規則:

iptables -F
iptables -X
iptables -Z

給INPUT鏈添加規則(開放8080端口):

iptables -I INPUT -p tcp --dport 8080 -j ACCEPT

查找規則所在行號:

iptables -L INPUT --line-numbers -n

根據行號刪除過濾規則(關閉8080端口):

iptables -D INPUT 1

ftp操作

ftp ip
輸入密碼密碼;
bin將文件轉換成二進制
get 獲取文件名

軟件的安裝與管理

rpm

安裝軟件包:

rpm -ivh nginx-1.12.2-2.el7.x86_64.rpm

模糊搜索軟件包:

rpm -qa | grep nginx

精確查找軟件包:

rpm -qa nginx

查詢軟件包的安裝路徑:

rpm -ql nginx-1.12.2-2.el7.x86_64

查看軟件包的概要信息:

rpm -qi nginx-1.12.2-2.el7.x86_64

驗證軟件包內容和安裝文件是否一致:

rpm -V nginx-1.12.2-2.el7.x86_64

更新軟件包:

rpm -Uvh nginx-1.12.2-2.el7.x86_64

刪除軟件包:

rpm -e nginx-1.12.2-2.el7.x86_64

yum

安裝軟件包:

yum install nginx

檢查可以更新的軟件包:

yum check-update

更新指定的軟件包:

yum update nginx

在資源庫中查找軟件包信息:

yum info nginx*

列出已經安裝的所有軟件包:

yum info installed

列出軟件包名稱:

yum list nginx*

模糊搜索軟件包:

yum search nginx

其他

終止線程(終止線程號位19979的線程)

kill -9 19979

查看線程個數(方便查看程序是否有誤)

ps -Lf 端口號|wc -l

查看網絡的連通性

ping ip

查看ip端口的連通性檢測(防火牆的連通性)

telnet ip 端口-->退出模式 shift+]-->quit

查看本地的ip

ifconfig

查看調度器

crontab -l

編輯調度器

crontab -e

想了解更多面經和開發小技能,歡迎掃描下方的二維碼,持續關注!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM