整理linux的一些基礎命令
1. linux目錄與檔案
Windows: 以多根的方式組織檔案 C:\ D:\ E:
Linux: 以單根的方式組織檔案 /
[root@c7-4 /]# ls
bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
[root@c7-4 /]#
根下的目錄決議:
| 目錄 | 定義 |
|---|---|
| root | root用戶的HOME |
| home | 存盤普通用戶家目錄 |
| dev | 設備檔案 /dev/sda,/dev/tty,/dev/pts/1, /dev/zero, /dev/null, /dev/random |
| proc | 虛擬的檔案系統,反映出來的是內核,行程資訊或實時狀態,只存在于記憶體中,大小為0,du -sh /proc |
| bin | 普通用戶使用的命令 |
| sbin | 管理員使用的命令 |
| /usr/bin | 普通用戶使用的應用程式 |
| /usr/sbin | 管理員使用的應用程式 |
| /usr/local | 軟體安裝目錄,相當于C:\Program |
| boot | 存放的系統啟動相關的檔案,例如kernel,grub(引導裝載程式) |
| etc | 組態檔(系統相關如網路/etc/sysconfig/network-scripts) |
| tmp | 臨時檔案(主要是程式產生的臨時資料) |
| /var/log | 日志檔案 |
1.1 關于目錄與檔案的操作方式
目錄管理——創建、復制、洗掉、移動、查看、編輯、壓縮、查找
| 命令 | 結果 |
|---|---|
| ls | 列示當前目錄下的所有內容 |
| cd | 切換目錄,后面加路徑,直接回車是回家,倆點是上級目錄 |
| mkdir | 創建目錄 |
| pwd | 顯示目前的目錄 |
| rmdir | 洗掉一個空的目錄 |
| cp | 復制檔案或目錄 |
| rm | 移除檔案或目錄 |
| mv | 移動檔案與目錄,或修改檔案與目錄的名稱 |
| touch | 創建檔案 |
| cat | 查看檔案內容 |
| less | 按頁翻 空白鍵/[pagedown]/[pageup]/ “/字串”(查找)/n /N /q |
| more | 按頁翻動檔案,空格/回車/q/b/:f/ ”/字串"(查找) |
| head | 查看檔案前n項 head -n |
| tail | 倒敘查看檔案內容 -n 查看幾行, -f 動態查看 |
| nano | 檔案編輯 |
| find | 查找檔案 find / -name $file |
1.2 符號鏈接
軟鏈接只是用鏈接后的檔案作為一個指向源檔案的路徑,鏈接后的檔案是一個新檔案,每個檔案之間的inode不相同,軟連接后的檔案的權限都是777,大小是源檔案的路徑的字符個數,軟連接的源檔案不可以剪切洗掉和重命名
對檔案做符號鏈接 (前面是源檔案 后面是鏈接)
# ln -s file222.txt /boot/ruan.txt
對目錄做符號鏈接
# ln -s /dir1/ /dir2
洗掉目錄軟鏈接
# rm -rf /dir2/ 洗掉目錄下的檔案
# unlink /dir2 洗掉鏈接檔案
硬鏈接是多個檔案名對應到同一個inode;故硬鏈接的檔案和源檔案的inode,權限,屬主,屬組,時間,型別…都一樣
硬鏈接的源檔案可以剪切洗掉重命名
對檔案做硬鏈接(前面是源檔案 后面是鏈接)
# ln /file111.txt /etc/
# ln file111.txt /etc/h-111.txt
# ln /file111.txt /boot/
# ln: 創建硬鏈接"/boot/file222.txt" => "/file222.txt": 無效的跨設備連接
對目錄做硬鏈接
# ln dir111/ /boot/
# ln: "dir111/": 不允許將硬鏈接指向目錄
2 存盤操作
2.1一般存盤操作
| 命令 | 結果 |
|---|---|
| lsblk | 列示當前磁盤設備及掛載情況 |
| fdisk | MBR 磁區表,創建磁區 |
| gdisk | GPT磁區表,創建磁區 |
| mkfs | 格式化:mkfs.ext4 /dev/sdb1 |
| mount | 臨時掛載,mount /dev/sdb1 /mnt,永久掛載寫在/etc/fstab中 |
| umount | 卸載:umount /dev/sdb1 |
| blkid | 查看設備得uuid |
| df | 查看檔案系統使用情況:df -h |
2.2 邏輯卷管理
| 命令 | 創建 | 顯示詳情 | 顯示 | 擴展 | 縮小 | 洗掉 |
|---|---|---|---|---|---|---|
| 物理卷 | pvcreate | pvdisplay | pvs | pvremove | ||
| 卷組 | vgcreate | vgdisplay | vgs | vgextend | vgreduce | vgremove |
| 邏輯卷 | lvcreate | lvdisplay | lvs | lvextend | lvreduce | lvremove |
EXT2/3/4檔案系統擴展:
# resize2fs /dev/vg02/lv02
# df -Th
XFS檔案系統擴展:
# xfs_growfs /dev/vg1/lv2
# df -Th
如果磁盤無法正常使用,請使用如下命令查看具體錯誤:
#dmesg -T | grep -i io
3 用戶管理
3.1 用戶管理操作命令
| 命令 | 引數 | 內容 | 用戶名 |
|---|---|---|---|
| useradd | -u | uid | |
| … | -d | 家目錄 | |
| … | -s | shell型別 /bin/bash登陸或者/sbin/noligin不登陸 | |
| … | -M | 不跟東西,沒有家得意思 | |
| … | -g | 組名,用來指定組 | |
| … | -G | 組名1,組名2 用來指定附加組 |
| 命令 | 引數 | 內容 | 組名 |
|---|---|---|---|
| groupadd | -g | gid | 組名 |
| 命令 | 引數 | 內容 | 組名 |
|---|---|---|---|
| groupdel | -g | gid | 組名 |
| 命令 | 引數 | 內容 | 用戶名 |
|---|---|---|---|
| usermod | -u | uid 修改uid | |
| … | -G | 組名1,組名2 修改附加組,覆寫 | |
| … | -aG | 組名1,組名2 修改附加組,追加 | |
| … | -g | 組名改主組 | |
| … | -s | shell型別 修改shell型別 |
3.2 sudo提權
將普通用戶提升為root
# vim /etc/sudoers
%wheel ALL=(ALL) NOPASSWD: ALL
注釋:
%wheel表示組,用戶需要添加到這個組,作為附加組
lily,沒有%表示用戶,用戶可以直接使用sudo工具
# sudo useradd gougou10
查看用戶id
# id gougou10
# uid=505(gougou10) gid=505(gougou10) 組=505(gougou10)
4. 檔案權限
| 屬主權限 | 屬組權限 | 其他人權限 | 屬主 | 屬組 | 檔案名 |
|---|---|---|---|---|---|
| rwx | rw- | r– | star | hr | file1 |
[root@192 ~]# ll
total 8
lrwxrwxrwx. 1 root root 6 Oct 25 03:31 file11 -> file22
-rw-r--r--. 1 root root 18 Oct 25 03:34 file111.txt
-rw-r--r--. 1 root root 25 Oct 25 03:29 file22
drwxr-xr-x. 2 root root 25 Oct 25 03:38 zhang
4.1 更改檔案得屬主/屬組
==chown:
[root@192 ~]# chown alice.hr file1 //改屬主、屬組
[root@192 ~]# chown alice file1 //只改屬主
[root@192 ~]# chown .hr file1 //只改屬組
[root@192 ~]# chown -R alice.hr dir1
=chgrp:
[root@192 ~]# chgrp it file1 //改檔案屬組
[root@192 ~]# chgrp -R it dir1 //遞回改屬組
4.2 更改檔案得權限
==使用符號
| 命令 | 物件 | 賦值符 | 權限型別 | 檔案 |
|---|---|---|---|---|
| … | u | + | r | file |
| chmod | g | - | w | file |
| … | o | = | x | file |
[root@192 ~]# chmod u+x file1 //屬主增加執行
[root@192 ~]# chmod a=rwx file1 //所有人等于讀寫執行
[root@192 ~]# chmod a=- file1 //所有人沒有權限
[root@192 ~]# chmod ug=rw,o=r file1 //屬主屬組等于讀寫,其他人只讀
[root@192 ~]# ll file1 //以長模式方式查看檔案權限
-rw-rw-r-- 1 star it 17 10-25 16:45 file1 //顯示的結果
== 使用數字
[root@192 ~]# chmod 644 file1
[root@192 ~]# ll file1
-rw-r--r-- 1 star it 17 10-25 16:45 file1
5. 軟體包管理
5.1 使用YUM管理RPM包
自動解決包的依賴關系
==查看可用倉庫
# yum clean all //清空快取及其它文
# yum makecache //重建快取
# yum repolist all //查詢可用的倉庫
==安裝軟體包
# yum -y install httpd vsftpd
# yum -y reinstall httpd //重新安裝
# yum -y update httpd //更新
# yum -y install php
# yum -y update //更新整個系統所有軟體包
# yum -y groupinstall mariadb //安裝軟體包組,一組里面可以包含多個軟體
==查詢(從本地 rpm資料庫 和 yum源 中查詢)
# yum list httpd
# yum list *vnc*
# yum list |grep vnc
# yum list installed
# yum info httpd
==卸載
# yum -y remove mariadb
==history
# yum history
# yum history info 4
# yum history undo 4
擴展查詢:
查找vsftpd.conf屬由哪個包提供?
# yum provides *vsftpd/vsftpd.conf
按照關鍵字搜索,軟體名字中含有chinese或者是描述資訊中含有chinese都會顯示出來
[root@server ~]# yum search chinese
5.2 使用RPM工具管理RPM包
需要手動解決包的依賴關系
==安裝
# rpm -ivh local_path
# rpm -ivh url_path
# rpm -ivh dos2unix-3.1-37.el6.x86_64.rpm 套件名
-i 安裝
-v 顯示程序
-h 顯示百分比
--force //強制安裝軟體包
--nodeps //忽略依賴關系
# rpm -ivh ntfs-3g-2013.1.13-5.el7.x86_64.rpm
error: Failed dependencies:
libc.so.6(GLIBC_2.14)(64bit) is needed by ntfs-3g-2:2013.1.13-5.el7.x86_64
==查詢
(rpm資料庫中記錄的是已經安裝過的包的資訊,需要使用包的全名ntfs-3g)
# rpm -q ntfs-3g //查詢指定包是否安裝
# rpm -qa |grep ntfs
# rpm -qf /usr/bin/ntfs-3g //查詢該檔案屬于哪個rpm包
# rpm -qc ntfs-3g //查詢某個包安裝的組態檔
==卸載
# rpm -e ntfs-3g
# rpm -e ntfs-3g --nodeps
5.3 原始碼包安裝
一、獲得原始碼包途徑
官方網站,可以獲得最新的軟體包
二、安裝原始碼包
準備作業
- 編譯環境如gcc、gcc-c++編譯器、make
- 準備軟體 nginx-1.4.4.tar.gz
部署Nginx
# tar xvf nginx-1.4.4.tar.gz -C /usr/local/src/
# make //將原始碼包編譯成二進制的形式
# make install //安裝編成的二進制的nginx
# /usr/local/nginx/sbin/nginx //啟動nginx服務器
6.打包壓縮
6.1,tar命令
==打包(歸檔),壓縮
c即create創建,f打包后的檔案名
# tar -czf etc1.tar.gz /etc //-z 呼叫gzip工具進行壓縮
# tar -cjf etc2.tar.bz2 /etc //-j 呼叫bzip2工具進行壓縮
# tar -cJf etc3.tar.xz /etc //-J 呼叫xz工具進行壓縮
# ll -h etc*
-rw-r--r--. 1 root root 8.7M 3月 12 00:08 etc1.tar.gz
-rw-r--r--. 1 root root 7.5M 3月 12 00:08 etc2.tar.bz2
-rw-r--r--. 1 root root 4.8M 3月 12 00:09 etc3.tar.xz
==查詢
# file a.txt.gz
a.txt.gz: gzip compressed data, was "a.txt", from Unix, last modified: Wed Nov 8 15:23:10 2017
tar -ztvf etc1.tar.gz //查詢gzip壓縮的檔案
# tar -jtvf etc2.tar.bz2 //查詢bzip2壓縮的檔案
# tar -Jtvf etc3.tar.xz //查詢xz壓縮的檔案
==解壓,解包
# tar -xzvf etc1.tar.gz //x解包
# tar -xvf etc1.tar.gz //無需指定解壓工具,tar會自動判斷
# tar -xvf etc2.tar.bz2 -C /tmp //-C重定向到//tmp目錄
# tar xf etc3.tar.xz
==解壓zip
# yum -y install unzip
# unzip xxx.zip
7. 行程管理
7.1 查看行程
==靜態ps
# ps aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.0 2164 648 ? Ss 08:47 0:00 /usr/lib/systemd/systemd
USER: 運行行程的用戶
PID: 行程ID
%CPU: CPU占用率
%MEM: 記憶體占用率
VSZ: 占用虛擬記憶體,包括程式、代碼、共享庫等所有的
RSS: 占用實際記憶體 駐留記憶體
TTY: 行程運行的終端,?表示不依賴于任何的終端
STAT: 行程狀態
R 運行
S 可中斷睡眠 Sleep
D 不可中斷睡眠
T 停止的行程
Z 僵尸行程
X 死掉的行程
START: 行程的啟動時間
TIME: 行程占用CPU的總時間,單位是minute
COMMAND: 行程檔案,行程名
==動態top(查看、管理行程)
# top
# top -d 1 一秒鐘變化一次
top - 14:22:36 up 4:45, 3 users, load average: 0.00, 0.00, 0.00
Tasks: 206 total, 2 running, 204 sleeping, 0 stopped, 0 zombie
Cpu(s): 1.6%us, 0.7%sy, 0.0%ni, 96.8%id, 0.9%wa, 0.0%hi, 0.0%si, 0.0%st
Mem: 4019424k total, 1327584k used, 2691840k free, 131012k buffers
Swap: 2047992k total, 0k used, 2047992k free, 650880k cached
load average: 0.23, 0.06, 0.02 1分鐘,5分鐘,15分鐘之內系統的平均負載
us:用戶占用的cpu的百分比 sy:內核占用的cpu的百分比 ni 改變過優先級的行程占用的cpu的百分比
id:cpu的空閑時間 wa:cpu的IO等待的時間 hi:硬中斷占用的cpu的百分比 si:軟中斷
st:cpu被偷走的時間
第二部分:行程資訊
命令
h或?獲得幫助
M 按記憶體的使用排序
P 按CPU使用排序
N 以PID的大小排序
R 對排序進行反轉
1 顯示所有CPU的負載
==查看網路行程
ports and services
網路服務是通過socket的方式提供服務的,使用ss命令查詢常用的選項
[root@www ~]# ss --help
-n 不決議服務的名字
-a 顯示所有套接字
-l 顯示監聽(Listen)的埠
-t 顯示tcp的套接字或者是服務
-u 顯示udp的套接字或者是服務
-p 顯示行程的名字
1、Show TCP sockets(LISTEN)
[root@www ~]# ss -ant //查看所有狀態的,包括監聽和建立連接的,此時無需l 選項,
[root@www ~]# ss -ant |grep :22
2、Show UDP sockets(所有狀態)
udp的協議比較少
[root@www ~]# ss -anu
注意:netstat命令的用法和ss的用法相同
==其他查看行程方式
[root@aliyun ~]# pstree -p
[root@aliyun ~]# atop
[root@aliyun ~]# htop
[root@aliyun ~]# pgrep -l sshd
[root@aliyun ~]# lsof file1 顯示檔案或者目錄哪個行程在使用
7.2 行程控制
==shell管理行程
# kill -l //列出所有支持的信號
編號 信號名
1) SIGHUP 重新加載配置
2) SIGINT 鍵盤中斷^C
9) SIGKILL 強制終止,不可忽略,不可攔截
15) SIGTERM 終止(正常退出,干凈)默認信號,可忽略
18) SIGCONT 繼續,不可攔截
19) SIGSTOP 停止,不可忽略
20)SIGTSTP 暫停^Z
==給sshd行程發送1信號
# ps aux |grep sshd
root 1925 0.0 0.0 64116 44 ? Ss Jul25 0:00 /usr/sbin/sshd
# kill -1 1925
# ps aux |grep sshd
root 32701 0.0 0.2 64116 1144 ? Ss 19:27 0:00 /usr/sbin/sshd
==給vim行程發送9,15信號
# touch file1 file2
# tty
/dev/pts/1
# vim file1
# tty
/dev/pts/2
# vim file2
# ps aux |grep vim
root 4362 0.0 0.2 11104 2888 pts/1 S+ 23:02 0:00 vim file1
root 4363 0.1 0.2 11068 2948 pts/2 S+ 23:02 0:00 vim file2
# kill 4362
# kill -9 4363
==給sshd行程發送18,19信號
# ps aux |grep sshd
root 5571 0.0 0.0 64064 1164 ? Ss 09:35 0:00 /usr/sbin/sshd
# kill -STOP 5571
# ps aux |grep sshd
root 5571 0.0 0.0 64064 1164 ? Ts 09:35 0:00 /usr/sbin/sshd
# kill -cont 5571
# ps aux |grep sshd
root 5571 0.0 0.0 64064 1164 ? Ss 09:35 0:00 /usr/sbin/sshd
當sshd行程被終止了,需要使用下面命令把它啟動起來
[root@localhost ~]# systemctl start sshd
start:啟動
stop:停止
status:查看狀態
restart:重新啟動
reload:重新加載,不是所有的服務都支持這個用法的
killall命令
# killall vim //給所有vim行程發送15信號
# pkill -t pts/3 //只是殺掉終端上的行程
# pkill -9 -t pts/3 //連終端都一起殺掉了
8. Linux dmidecode 命令,查看硬體資訊
dmidecode 用于獲取主機的硬體資訊,通常是在不打開計算機機箱的情況下使用該命令來查找硬體詳細資訊,常見用法如下:
==列印所有得硬體資訊
[root@localhost ~]$ dmidecode # 列印所有硬體資訊
[root@localhost ~]$ dmidecode -q # 列印所有硬體資訊,比較簡潔
[root@localhost ~]$ dmidecode | grep 'Product Name' # 以過濾的方式來查看指定的硬體資訊
-t/-type 查看相關型別得硬體資訊
[root@localhost ~]$ dmidecode --type bios # 查看BIOS相關的硬體資訊
[root@localhost ~]$ dmidecode --type system # 查看系統相關的硬體資訊
[root@localhost ~]$ dmidecode --type baseboard # 查看主板相關的硬體資訊
[root@localhost ~]$ dmidecode --type chassis # 查看機箱相關的硬體資訊
[root@localhost ~]$ dmidecode --type processor # 查看處理器相關的硬體資訊
[root@localhost ~]$ dmidecode --type memory # 查看記憶體相關的硬體資訊
等同于
[root@localhost ~]$ dmidecode -t 0 # 查看BIOS相關的硬體資訊
全部型別
Type Information
0 BIOS
1 System
2 Base Board
3 Chassis
4 Processor
5 Memory Controller
6 Memory Module
7 Cache
8 Port Connector
9 System Slots
10 On Board Devices
11 OEM Strings
12 System Configuration Options
13 BIOS Language
14 Group Associations
15 System Event Log
16 Physical Memory Array
17 Memory Device
18 32-bit Memory Error
19 Memory Array Mapped Address
20 Memory Device Mapped Address
21 Built-in Pointing Device
22 Portable Battery
23 System Reset
24 Hardware Security
25 System Power Controls
26 Voltage Probe
27 Cooling Device
28 Temperature Probe
29 Electrical Current Probe
30 Out-of-band Remote Access
31 Boot Integrity Services
32 System Boot
33 64-bit Memory Error
34 Management Device
35 Management Device Component
36 Management Device Threshold Data
37 Memory Channel
38 IPMI Device
39 Power Supply
-s (–string keyword)支持的keyword包括:
[root@localhost ~]$ dmidecode -s system-serial-number
VMware-56 4d 9d 1e a4 f5 ee c7-6e 0e 6e 2b 71 5b ce c7
##可加引數
bios-vendor,
bios-version,
bios-release-date,
system-manufacturer,
system-product-name,
system-version,
system-serial-number,
baseboard-manu-facturer,
baseboard-product-name,
baseboard-version,
baseboard-serial-number,
baseboard-asset-tag,
chassis-manufacturer,
chas-sis-version,
chassis-serial-number,
chassis-asset-tag,
processor-manufacturer,
processor-version.
參考鏈接:
http://www.ttlsa.com/linux/the-linux-dmidecode-command-to-get-the-hardware-information/
https://www.cnblogs.com/pzk7788/p/10314436.html
9. BMC管理
BMC IPMI常用命令
BMC(Baseboard Management Controler)提供了多種通道來和主機通信,進而檢測主機的溫度、風扇轉速、電壓、電源和現場可替代器件,為了便于用戶使用,它提供了非常豐富的命令,下面介紹一下主要的常用命令,
- 遠程電源控制類(除了電源,其他命令也可以遠程)
Ipmitool -I lanplus –H 10.32.228.111 –U username –P Passwordchassis power off
Ipmitool -I lanplus –H 10.32.228.111 –U username –P Passwordchassis power on
Ipmitool -I lanplus –H 10.32.228.111 –U username –P Passwordchassis power reset
Ipmitool -I lanplus –H 10.32.228.111 –U username –P Passwordchassis power cycle
(注意power cycle 和power reset的區別在于前者從掉電到上電有1秒鐘的間隔,而后者是很快上電)
- 讀取系統狀態類
Ipmitool sensor list 顯示系統所有傳感器串列
Ipmitool fru list 顯示系統所有現場可替代器件的串列
Ipmitool sdr list 顯示系統所有SDRRepository設備串列
Ipmitool pef list 顯示系統平臺時間過濾的串列
- 系統日志類
Ipmitool sel elist 顯示所有系統事件日志
Ipmitool sel clear 洗掉所有系統時間日志
Ipmitool sel delete ID 洗掉第ID條SEL
Ipmitool sel time get 顯示當前BMC的時間
Ipmitool sel time set XXX 設定當前BMC的時間
- 啟動設定類
Ipmitool chassis bootdev bios 重啟后停在BIOS 選單
Ipmitool chassis bootdev pxe 重啟后從PXE啟動
- 系統相關的命令
Ipmitool mc info 顯示BMC版本資訊
Ipmitool bmc reset cold BMC 熱啟動
Ipmitool bmc reset warmBMC冷啟動
- 網路介面相關命令
Ipmitool lan print 1 顯示channel1的網路配置資訊
Ipmitool lan set 1 ipaddr 10.32.2.2 設定channel1的IP地址
Ipmitool lan set 1 netmask 255.255.0.0 設定channel1的netmask
Ipmitool lan set 4 defgw ipaddr 255.255.0.254 設定channel4的網關
Ipmitool lan set 2 defgw macaddr <macaddr> 設定channel2的網關mac address
Ipmitool lan set 2 ipsrc dhcp 設定channel2的ip 源在DHCP
Ipmitool lan set 3 ipsrc static 設定channel2的ip是靜態獲得的
- 通道相關命令
Ipmitool channel info 顯示系統默認channel
Ipmitool channel authcap channel-number privilege 修改通道的優先級別
Ipmitool channel getaccess channel-number user-id 讀取用戶在通道上的權限
Ipmitool channel setacccess channel-number user-id callin=on ipmi=on link=onprivilege=5 設定用戶在通道上的權限
- 看門狗相關命令
Ipmitool mc watchdog get 讀取當前看門狗的設定
Ipmitool watchdog off 關掉看門狗
Ipmitool watchdog reset 在最近設定的計數器的基礎上重啟看門狗
- 用戶管理相關命令
Ipmitool user list chan-id 顯示某通道上的所有用戶
Ipmitool set password <user id> <password> 修改某用戶的密碼
Ipmitool disable <user id> 禁止掉某用戶
ipmitool enable <user id> 使能某用戶
ipmitool priv <user id> <privilegelevel> <channel number> 修改某用戶在某通道上的權限
ipmitool test <user id> <16|20> <password> 測驗用戶
參考鏈接:
https://blog.csdn.net/guyan1101/article/details/87275408/
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/202901.html
標籤:其他
下一篇:NVIDIA Jetson Xavier NX從板磚到邊緣計算機——Jetpack4.4適配大疆OSDK4.0.1
