前端學習 linux —— 第一篇
前端學習 linux – 第一篇
本文主要介紹「linux 發行版本」、「cpu 架構」、「Linux 目錄結構」、「vi 和 vim」、「用戶管理」、「文件目錄指令」、「docker 安裝 ubuntu20.04」。
注:筆者使用的是 ubuntu 20.04 桌面版。
linux 發行版本
linux 主要發行版有 ubuntu、redhat、centos等等,都是linux 內核,其他的可能就不同了。不同發行版的很多指令都類似,比如你學會了 ubuntu,在 centos 上也能快速上手。
unix 和 linux 關係
首先有 unix,但它的價格昂貴,不能運行於 x86 的 PC。
有個荷蘭的教授寫了一個叫 MINIX 的操作系統,向學生展示操作系統的內部原理,並將其代碼開源。全世界學計算機的學生都通過鑽研 MINIX 來操作系統,其中有一位芬蘭大學學生,於 1991 年寫出了 Linux 0.01。
Tip:更多介紹請看百度百科
cpu 架構
cpu 架構有 aarch64
、loongarch64
、mips64
、x86_64
(也是AMD64
)
不同的架構可以簡單理解為指令集不同。
Linux 目錄結構
windows 分 C盤、D盤,而 linux 已經幫我們分好了,例如我們在 /
目錄下查看文件信息:
pjl@pjl:/$ ls
bin boot cdrom data dev etc home lib lib32 lib64 libx32 lost+found media mnt opt proc root run sbin snap srv swapfile sys tmp usr var
每個目錄的作用請看下文。
bin
/bin
,存放最經常使用的命令。例如裏面有 whoami
、su
、sudo
等命令
pjl@pjl:/$ ll /bin
lrwxrwxrwx 1 root root 7 12月 7 2021 /bin -> usr/bin/
pjl@pjl:/$ ll usr/bin/ |grep 'whoami'
-rwxr-xr-x 1 root root 39256 9月 5 2019 whoami*
家目錄
/home
,存放普通用戶的主目錄。例如我增加一個用戶 pjl,home目錄下也會增加一個 pjl 的目錄。請看示例:
pjl@pjl:/$ sudo adduser pjl
[sudo] pjl 的密碼:
正在添加用戶"pjl"...
正在添加新組"pjl" (1004)...
正在添加新用戶"pjl" (1004) 到組"pjl"...
創建主目錄"/home/pjl"...
正在從"/etc/skel"複製文件...
新的 密碼:
重新輸入新的 密碼:
passwd:已成功更新密碼
正在改變 pjl 的用戶信息
請輸入新值,或直接敲回車鍵以使用默認值
全名 []:
房間號碼 []:
工作電話 []:
家庭電話 []:
其它 []:
這些信息是否正確? [Y/n] pjl@pjl:/$
home 目錄下增加用戶 pjl 同名的家目錄:
pjl@pjl:/$ ll /home
總用量 32
drwxr-xr-x 8 root root 4096 6月 8 20:22 ./
drwxr-xr-x 23 root root 4096 6月 8 10:48 ../
drwxr-xr-x 2 pjl pjl 4096 6月 8 20:22 pjl/
root
/root
,超級權限者的用戶主目錄。普通用戶不能查看:
pjl@pjl:/$ ll /root
ls: 無法打開目錄 '/root': 權限不夠
切換 root 用戶後查看:
pjl@pjl:/$ su
密碼:
su: 認證失敗
pjl@pjl:/$ su
密碼:
root@pjl:/# ll /root
總用量 2012
drwx------ 28 root root 4096 6月 8 11:18 ./
drwxr-xr-x 23 root root 4096 6月 8 10:48 ../
drwxr-xr-x 2 root root 4096 6月 6 10:52 公共的/
drwxr-xr-x 2 root root 4096 6月 6 10:52 模板/
drwxr-xr-x 2 root root 4096 6月 6 10:52 視頻/
drwxr-xr-x 2 root root 4096 6月 6 10:52 圖片/
drwxr-xr-x 2 root root 4096 6月 6 10:52 文檔/
drwxr-xr-x 2 root root 4096 6月 6 10:52 下載/
drwxr-xr-x 2 root root 4096 6月 6 10:52 音樂/
drwxr-xr-x 2 root root 4096 6月 6 10:52 桌面/
...
lib
/lib
,系統開機所需要的最基本的動態鏈接共享庫,類似 windows 中 DLL 文件。幾乎所有應用程序都需要用到這些共享庫
lost+found
/lost+found
,通常為空,系統非法關機後,就存放一些文件
pjl@pjl:~$ ls /lost+found/
ls: 無法打開目錄 '/lost+found/': 權限不夠
pjl@pjl:~$ sudo ls /lost+found/
[sudo] pjl 的密碼:
etc
/etc
,系統管理所需的配置文件和子目錄。例如 nginx、mysql、python、docker:
pjl@pjl:~$ ls /etc |grep -E 'mysql|nginx|docker|python'
docker
mysql
nginx
python2.7
python3
python3.8
nginx 的配置文件:
pjl@pjl:~$ ls /etc/nginx | grep '.conf'
fastcgi.conf
nginx.conf
usr
/usr
,用戶很多的應用程序和文件都在此,類似 windows 下的 Program Files
:
pjl@pjl:~$ ls /usr
bin games include lib lib32 lib64 libexec libx32 local sbin share src
boot
/boot
,存放啟動 linux 時使用的一些核心文件。包括一些鏈接文件和鏡像文件:
pjl@pjl:~$ ls /boot
config-5.13.0-41-generic efi initrd.img-5.13.0-41-generic initrd.img.old memtest86+_multiboot.bin System.map-5.13.0-48-generic vmlinuz-5.13.0-44-generic
config-5.13.0-44-generic grub initrd.img-5.13.0-44-generic memtest86+.bin System.map-5.13.0-41-generic vmlinuz vmlinuz-5.13.0-48-generic
config-5.13.0-48-generic initrd.img initrd.img-5.13.0-48-generic memtest86+.elf System.map-5.13.0-44-generic vmlinuz-5.13.0-41-generic vmlinuz.old
proc
/proc
,虛擬目錄,它是系統內存的映射,訪問這個目錄獲取系統信息
root@97342c74a502:/# ls /proc
1 cmdline dma ioports kpagecgroup modules schedstat thread-self zoneinfo
10 config.gz driver irq kpagecount mounts self timer_list
160 consoles execdomains kallsyms kpageflags mtrr softirqs tty
acpi cpuinfo filesystems kcore loadavg net stat uptime
buddyinfo crypto fs key-users locks pagetypeinfo swaps version
bus devices interrupts keys meminfo partitions sys vmallocinfo
cgroups diskstats iomem kmsg misc sched_debug sysvipc vmstat
srv
/srv
,service縮寫,存放一些服務啟動之後需要提取的數據
pjl@pjl:~$ ls /srv
ftp
sys
/sys
,這是 linux 2.6 內核的一個很大的變化
pjl@pjl:~$ ls /sys
block bus class dev devices firmware fs hypervisor kernel module power
tmp
/tmp
,存放一些臨時文件
dev
/dev
,類似 windows 的設備管理器,把所有硬件用文件存儲。比如筆者 cpu 有 4 核
pjl@pjl:~$ ls /dev/cpu
0 1 2 3 microcode
media
/media
,linux 會自動識別一些設備,例如 U 盤,識別後將會把該設備掛載到這個目錄下。
mnt
/mnt
,用於讓用戶臨時掛載別的文件系統
opt
/opt
,給主機額外安裝軟件的目錄。例如安裝數據庫,默認為空。筆者安裝了百度網盤
pjl@pjl:~$ ls /opt
baidunetdisk google sogoupinyin teamviewer
usr/local
/usr/local
,另一個給主機額外安裝軟件的目錄。一般通過編譯源碼方式安裝的程序
pjl@pjl:~$ ls /usr/local
bin etc games include lib man rvm sbin share src
var
/var
,存放不斷擴充的東西,習慣將經常修改的目錄放在這裡,例如日誌文件
pjl@pjl:~$ ls /var
backups cache crash lib local lock log mail metrics opt run snap spool tmp www
vi 和 vim
linux 會內置 vi 文本編輯器。vim 是增強版。
編輯器模式
編輯器有三種模式:
- 正常模式
- 插入模式
- 命令行模式
Tip:通過 vim 默認進去的就是正常模式,退出(:q!
)或保存退出(:wq
)屬於命令模式,通過輸入 i
進入編輯模式。
模式切換:
- 正常模式 輸入
i
進入編輯模式 - 編輯模式 輸入
esc
進入正常模式 - 正常模式 輸入
:
或/
進入命令模式 - 命令模式 輸入
esc
進入正常模式
Tip:退出有以下三種:
:wq
保存退出:q
退出。有時退出失敗,提示我們使用強制退出:E37: 已修改但尚未保存 (可用 ! 強制執行)
:q!
強制退出,不保存
快捷鍵
拷貝粘貼
複製一行 yy
,複製 5 行 5yy
,粘貼輸入 p
。需要在正常模式下進行。
在正常模式,光標在第一行,輸入 yy,接着輸入 p:
a
b
c
d
e
文件內容變為:
a
a
b
c
d
e
刪除
刪除當前行 dd
,刪除當前行向下3行,輸入 3dd
即可。需在正常模式下進行。
文件中查找單詞
正常模式輸入 /
進入命令模式,輸入要找的單詞,例如 test,然後輸入回車,接着輸入 n
就能匹配下一個,到底了會提示「已查找到文件結尾,再從開頭繼續查找」,繼續輸入 n
就會從頭開始。
如果需要找其他單詞,輸入 /
,在輸入其他單詞例如 apple,如果要編輯找到的這個 apple,輸入 i
進入編輯模式即可。
行號
命令行下輸入 :set nu
,顯示行號。如下所示:
...
7 # the default umask is set in /etc/profile; for setting the umask
8 # for ssh logins, install and configure the libpam-umask package.
9 #umask 022
Tip:輸入 :set nonu
隱藏行號
文件首行或末行
正常模式下,gg
首行,G
末行。
撤回
比如輸入在某行輸入 aa bb cc
,想撤回,回到正常模式,輸入 u
即可。對刪除的某行(dd)等操作同樣適用
切換到某行
一般模式下輸入行號,例如 99,然後輸入 shift+g
即可
用戶管理
linux 是多用戶,多任務的操作系統。
創建用戶
通過 adduser
創建用戶。請看示例:
root@pjl:/# adduser test11
正在添加用戶"test11"...
正在添加新組"test11" (1007)...
正在添加新用戶"test11" (1007) 到組"test11"...
創建主目錄"/home/test11"...
正在從"/etc/skel"複製文件...
新的 密碼:
重新輸入新的 密碼:
passwd:已成功更新密碼
正在改變 test11 的用戶信息
請輸入新值,或直接敲回車鍵以使用默認值
全名 []:
房間號碼 []:
工作電話 []:
家庭電話 []:
其它 []:
這些信息是否正確? [Y/n]
家目錄下已增加新增用戶 test11:
root@pjl:/# ll /home
總用量 40
drwxr-xr-x 10 root root 4096 6月 14 16:40 ./
drwxr-xr-x 23 root root 4096 6月 8 10:48 ../
drwxr-xr-x 2 test11 test11 4096 6月 14 16:40 test11/
更改用戶密碼
通過 passwd 可以修改用戶密碼。請看示例:
test11@pjl:/$ passwd test11
更改 test11 的密碼。
Current password:
新的 密碼:
重新輸入新的 密碼:
passwd:已成功更新密碼
Tip:遠程工具通過 test11 登錄成功後,默認會進入該用戶的家目錄:
test11@pjl:~$ pwd
/home/test11
test11@pjl:~$
切換用戶
通過 su -
切換用戶。例如切換到 root 用戶:
test10@pjl:~$ su - root
密碼:
root@pjl:~# su - test10
test10@pjl:~$ su - test11
密碼:
su: 認證失敗
test10@pjl:~$ su - test11
密碼:
Tip:權限高的用戶切換到權限低的無需輸入密碼。反之卻需要輸入密碼。
刪除用戶
通過 userdel
可以刪除用戶。比如刪除用戶 test200,但保留家目錄:
root@pjl:/home# userdel test200
root@pjl:/home# ls
test200
通過 -r
刪除用戶,同時刪除其家目錄:
root@pjl:/home# ls
test201 vncuser
root@pjl:/home# userdel -r test201
userdel:test201 信件池 (/var/mail/test201) 未找到
test201 用戶的家目錄已刪除:
root@pjl:/home# ls
vncuser
Tip:比如張三離職,一般情況保留其家目錄,萬一留有重要成果。更多介紹請通過 -h 查看:
root@pjl:/home# userdel -h
用法:userdel [選項] 登錄名
選項:
-f, --force 即使不屬於此用戶,也強制刪除文件
-h, --help 顯示此幫助信息並退出
-r, --remove 刪除主目錄和信件池
-R, --root CHROOT_DIR chroot 到的目錄
-P, --prefix PREFIX_DIR prefix directory where are located the /etc/* files
--extrausers Use the extra users database
-Z, --selinux-user 為用戶刪除所有的 SELinux 用戶映射
查詢用戶信息
通過 id 可查詢用戶信息。請看示例:
root@pjl:/home# id root
用戶id=0(root) 組id=0(root) 組=0(root)
root@pjl:/home# id test10
用戶id=1006(test10) 組id=1006(test10) 組=1006(test10)
root@pjl:/home# id test111
id: 「test111」:無此用戶
whoami
,查詢當前用戶:
root@pjl:/home# whoami
root
Tip:通過 who am i
可以查看什麼時間(2022-06-14 16:46),從哪台機器(192.168.1.225),什麼用戶(test11)登錄這個 linux 終端。
root@pjl:/home# who am i
test11 pts/6 2022-06-14 16:46 (192.168.1.225)
用戶組
用戶組類似角色,方便管理。
增加組、刪除組
增加組使用 groupadd
,刪除組使用 groupde
。請看示例:
# 增加組 agroup
root@pjl:/home# groupadd agroup
# 刪除組 agroup
root@pjl:/home# groupdel agroup
創建用戶並指定所屬組
創建用戶 test30 並放入 agroup 組中:
root@pjl:/home# useradd -g agroup test30
test30 確實已經屬於 agroup 組:
root@pjl:/home# id test30
用戶id=1010(test30) 組id=1010(agroup) 組=1010(agroup)
注:如果創建用戶時不指定組,系統會創建一個與用戶同名的組:
root@pjl:/home# adduser test40
root@pjl:/home# id test40
用戶id=1011(test40) 組id=1011(test40) 組=1011(test40)
切換用戶所屬組
usermod
切換用戶所屬組。例如將 test40 放入 bgroup 組:
root@pjl:/home# groupadd bgroup
root@pjl:/home# usermod -g bgroup test40
root@pjl:/home# id test40
用戶id=1011(test40) 組id=1012(bgroup) 組=1012(bgroup)
用戶和組相關文件
用戶和組相關的有三個文件:
- 用戶的配置文件
- 用戶的口令配置文件
- 組的配置文件
/etc/passwd
用戶的配置文件,記錄用戶的各種信息。請看示例:
test11@pjl:~$ cat /etc/passwd
// 用戶名:口令:用戶id:組id:注釋性描述:家目錄:使用的shell
test40:x:1011:1012:,,,:/home/test40:/bin/bash
...
/etc/shadow
口令配置文件:
root@pjl:/home/test11# cat /etc/shadow
// ! 這裡是密碼
// 19157 等信息都是處理過,看不懂的
test40:!:19157:0:99999:7:::
比如給 test40 設置密碼(比如123456
)
root@pjl:/home/test11# passwd test40
新的 密碼:
重新輸入新的 密碼:
passwd:已成功更新密碼
再次查看 /etc/shadow
,發現密碼加密顯示了:
test40:$6$JF.ed3d.S9RHucx.$13bXH9Ji3P8AGB2tsCnjRI8AFhp301F3HWzquzKCBTQWzS3..D6lvgKTsOLWuY1nwogtBcGdiZp5mGN69Ar7E.:19158:0:99999:7:::
/etc/group
組的配置文件:
root@pjl:/home/test11# cat /etc/group
bgroup:x:1012:
上面我們將 test40 用戶放入 bgroup。這裡的 1012 與用戶配置文件中的組 id 相對應。
幫助指令
man
比如通過 man
查看 ls
命名。請看示例:
root@pjl:/home/test11# man ls
NAME
ls - list directory contents
SYNOPSIS
ls [OPTION]... [FILE]...
DESCRIPTION
List information about the FILEs (the current directory by default). Sort entries alphabetically if none of -cftuvSUX nor --sort is specified.
Mandatory arguments to long options are mandatory for short options too.
-a, --all
do not ignore entries starting with .
...
-l use a long listing format
點擊空格
能翻頁,輸入 q
就能退出:
Tip:筆者使用 mobaxterm 能通過鼠標向上向下滾動。
root@pjl:/home/test11#
ls 較常用的兩個參數, a
顯示所有文件文件。linux 中,隱藏文件以 .
開頭:
root@pjl:/home/test11# ls
root@pjl:/home/test11# ls -a
. .. .bash_history .bash_logout .bashrc .cache .config .local .profile .Xauthority
l
是以列表的形式顯示信息。參數可以組合使用,不要求順序:
root@pjl:/home/test11# ls -l
總用量 0
root@pjl:/home/test11# ls -la
總用量 40
drwxr-xr-x 5 test11 test11 4096 6月 15 18:55 .
drwxr-xr-x 12 root root 4096 6月 14 17:21 ..
-rw------- 1 test11 test11 86 6月 14 17:27 .bash_history
-rw-r--r-- 1 test11 test11 220 6月 14 16:40 .bash_logout
-rw-r--r-- 1 test11 test11 3771 6月 14 16:40 .bashrc
drwx------ 4 test11 test11 4096 6月 14 16:46 .cache
drwx------ 4 test11 test11 4096 6月 14 16:46 .config
drwxr-xr-x 3 test11 test11 4096 6月 14 16:46 .local
-rw-r--r-- 1 test11 test11 807 6月 14 16:40 .profile
-rw------- 1 test11 test11 108 6月 15 18:55 .Xauthority
help
help 能查看 shell 內置命令。例如 cd
,請看示例:
root@pjl:/home/test11# help cd
cd: cd [-L|[-P [-e]] [-@]] [目錄]
改變 shell 工作目錄。
改變當前目錄至 DIR 目錄。默認的 DIR 目錄是 shell 變量 HOME
的值。
變量 CDPATH 定義了含有 DIR 的目錄的搜索路徑,其中不同的目錄名稱由冒號 (:)分隔。
一個空的目錄名稱表示當前目錄。如果要切換到的 DIR 由斜杠 (/) 開頭,則 CDPATH
不會用上變量。
如果路徑找不到,並且 shell 選項 `cdable_vars' 被設定,則參數詞被假定為一個
變量名。如果該變量有值,則它的值被當作 DIR 目錄。
選項:
-L 強制跟隨符號鏈接: 在處理 `..' 之後解析 DIR 中的符號鏈接。
-P 使用物理目錄結構而不跟隨符號鏈接: 在處理 `..' 之前解析 DIR 中的符號鏈接。
-e 如果使用了 -P 參數,但不能成功確定當前工作目錄時,返回非零的返回值。
-@ 在支持拓展屬性的系統上,將一個有這些屬性的文件當作有文件屬性的目錄。
默認情況下跟隨符號鏈接,如同指定 `-L'。
`..' 使用移除向前相鄰目錄名成員直到 DIR 開始或一個斜杠的方式處理。
退出狀態:
如果目錄改變,或在使用 -P 選項時 $PWD 修改成功時返回 0,否則非零。
注:man 查看 cd 失敗;help 查看 ls 也失敗。請看示例:
root@pjl:/home/test11# man cd
沒有 cd 的手冊頁條目
root@pjl:/home/test11# help ls
bash: help: 沒有與「ls」匹配的幫助主題。嘗試使用「help help」、「man -k ls」或「info ls」。
pwd
用這兩個幫助指令都可以。
info
如果覺得 ls --help
不詳細,可以使用 info ls
:
root@97342c74a502:/# info ls
Next: dir invocation, Up: Directory listing
10.1 'ls': List directory contents
==================================
The 'ls' program lists information about files (of any type, including
directories). Options and file arguments can be intermixed arbitrarily,
as usual.
...
文件目錄指令
pwd
pwd
,查看當前目錄所在的絕對路徑。請看示例:
root@pjl:/home/test11# pwd
/home/test11
cd
cd ~
回到家目錄。請看示例
// 目前在 tmp
test11@pjl:~/tmp$ pwd
/home/test11/tmp
test11@pjl:~/tmp$ cd ~
// 已回到test11 用戶的家目錄
test11@pjl:~$ pwd
/home/test11
cd ..
返回上一層目錄。
mkdir
mkdir
,創建目錄,默認創建一級目錄,多級目錄需要使用 -p
參數。例如創建 e、f、g 三個目錄:
test11@pjl:/tmp$ mkdir -p e/f/g
test11@pjl:/tmp$ cd e/f/g/
test11@pjl:/tmp/e/f/g$
rmdir
rmdir
,刪除空目錄。非空目錄可以使用 rm -rf
。請看示例:
test11@pjl:~/tmp$ rmdir e
rmdir: 刪除 'e' 失敗: 目錄非空
test11@pjl:~/tmp$ rm -rf e
通過 rm --help
知道:r 表示遞歸,f 指強制刪除
。
rm 也能刪除文件,或同時刪除非空目錄 a 和 a.txt 文件。請看示例:
test11@pjl:~/tmp$ rm -rf a a.txt
touch
touch
創建一個空文件。
test11@pjl:~/tmp$ touch b.txt
test11@pjl:~/tmp$ ls
b.txt
cp
cp
拷貝文件或目錄。例如將 b.txt 拷貝到 文件夾 c 中:
# 創建多級目錄
test11@pjl:~/tmp$ mkdir -p a/b/c
# 將 b.txt 拷貝到文件夾 c 中
test11@pjl:~/tmp$ cp b.txt a/b/c
test11@pjl:~/tmp$ ls a/b/c
b.txt
通過參數 -r
將非空目錄 a 整個拷貝到上層目錄。請看示例:
# 拷貝非空目錄失敗
test11@pjl:~/tmp$ cp a ../
cp: 未指定 -r;略過目錄'a'
test11@pjl:~/tmp$ cp a -r ../
test11@pjl:~/tmp$ ls ../
a tmp
多次執行拷貝,未提示是否需要覆蓋。
test11@pjl:~/tmp$ cp a -r ../
Tip:筆者是ubuntu20.04,並且也修改了 a/b/c/b.txt 這個文件。
mv
mv
移動目錄或文件,也可以重命名。請看示例:
test11@pjl:~$ ls
a tmp
# 將 a 重命名為 a2
test11@pjl:~$ mv a a2
test11@pjl:~$ ls
a2 tmp
移動並重新命名。將 b.txt 移動到上一層目錄,並重命名:
test11@pjl:~/tmp$ mv b.txt ../b-rename.txt
test11@pjl:~/tmp$ ls ../
b-rename.txt tmp
移動 a 文件夾,並重命名:
test11@pjl:~/tmp$ mv a ../a-rename
test11@pjl:~/tmp$ cd ../a-rename/b
# 檢驗:a中有b
test11@pjl:~/a-rename/b$
cat
cat
查看文件內容。只讀,更安全。請看示例:
# n 顯示行號
test11@pjl:~/a-rename/b$ cat -n /etc/profile
...
10 fi
11 else
12 if [ "`id -u`" -eq 0 ]; then
13 PS1='# '
14 else
15 PS1='$ '
16 fi
17 fi
可配合 more
使用:
test11@pjl:~/a-rename/b$ cat -n /etc/profile | more
1 # /etc/profile: system-wide .profile file for the Bourne shell (sh(1))
2 # and Bourne compatible shells (bash(1), ksh(1), ash(1), ...).
3
4 if [ "${PS1-}" ]; then
5 if [ "${BASH-}" ] && [ "$BASH" != "/bin/sh" ]; then
6 # The file bash.bashrc already sets the default PS1.
7 # PS1='\h:\w\$ '
8 if [ -f /etc/bash.bashrc ]; then
9 . /etc/bash.bashrc
10 fi
11 else
12 if [ "`id -u`" -eq 0 ]; then
13 PS1='# '
14 else
15 PS1='$ '
16 fi
17 fi
18 fi
19
20 if [ -d /etc/profile.d ]; then
21 for i in /etc/profile.d/*.sh; do
22 if [ -r $i ]; then
23 . $i
--更多--
more
more
,適合屏幕查看的文件閱讀輸出工具。基於 vi
編輯器的文本過濾器,以全屏幕的方式顯示文件內容。可以配合管道使用,也可以單獨使用。
例如查看文件:
test11@pjl:~/a-rename/b$ more /etc/profile
# /etc/profile: system-wide .profile file for the Bourne shell (sh(1))
# and Bourne compatible shells (bash(1), ksh(1), ash(1), ...).
if [ "${PS1-}" ]; then
if [ "${BASH-}" ] && [ "$BASH" != "/bin/sh" ]; then
# The file bash.bashrc already sets the default PS1.
# PS1='\h:\w\$ '
if [ -f /etc/bash.bashrc ]; then
. /etc/bash.bashrc
fi
else
if [ "`id -u`" -eq 0 ]; then
PS1='# '
else
PS1='$ '
fi
fi
fi
if [ -d /etc/profile.d ]; then
for i in /etc/profile.d/*.sh; do
if [ -r $i ]; then
. $i
--更多--(77%)
內置許多快捷鍵:
q
– 退出 more空格
– 向下翻頁回車
– 向下一行ctrl + f
– 向下一屏ctrl + b
– 向上一屏=
– 輸出當前行號:f
– 輸出文件名和當前行號
less
less
,功能與 more
類似,功能比 more 更強大。less 顯示文件內容時,不是一次將整個文件加載之後才顯示,對顯示大型文件有較好的效率。
內置許多快捷鍵:
q
– 退出空格
– 向下翻頁page down
– 向下翻頁page up
– 向上翻頁/字符串
– 向下搜索字符串。n
向下查找;N
向上查找。
echo
echo
,輸出內容到控制台。請看示例:
# 輸出字符串
root@97342c74a502:/# echo 'hello world'
hello world
# 輸出內置變量
root@97342c74a502:/# echo $SHELL
/bin/bash
head
head
,用於顯示文件開頭部分,默認前 10 行。請看示例:
test11@pjl:~/a-rename/b$ head /etc/profile
# /etc/profile: system-wide .profile file for the Bourne shell (sh(1))
# and Bourne compatible shells (bash(1), ksh(1), ash(1), ...).
if [ "${PS1-}" ]; then
if [ "${BASH-}" ] && [ "$BASH" != "/bin/sh" ]; then
# The file bash.bashrc already sets the default PS1.
# PS1='\h:\w\$ '
if [ -f /etc/bash.bashrc ]; then
. /etc/bash.bashrc
fi
假如只要顯示前 5 行,可以使用參數 n
。請看示例:
test11@pjl:~/a-rename/b$ head /etc/profile -n 5
# /etc/profile: system-wide .profile file for the Bourne shell (sh(1))
# and Bourne compatible shells (bash(1), ksh(1), ash(1), ...).
if [ "${PS1-}" ]; then
if [ "${BASH-}" ] && [ "$BASH" != "/bin/sh" ]; then
tail
tail
, 與 head 對應,默認顯示文件末尾 10 行。也能指定顯示末尾 5 行。
test11@pjl:~/a-rename/b$ tail /etc/profile
if [ -r $i ]; then
. $i
fi
done
unset i
fi
export ANDROID_HOME=/home/pjl/software/android-studio-2021.1.1.22-linux/android-studio/bin
export PATH=$PATH:$ANDROID_HOME
tail -f filename
,隨文件增長即時輸出新增數據,也就是能監聽文件。請看示例:
# 新建文件 a.txt
test11@pjl:~/a-rename/b$ touch a.txt
# 監聽文件輸出。此刻終端不會結束,一直監聽着
test11@pjl:~/a-rename/b$ tail -f a.txt
另開一個終端,將字符串(apple
)輸出(或重定向)到 a.txt 文件中:
test11@pjl:~/a-rename/b$ echo 'apple' > a.txt
立刻監聽到並輸出:
test11@pjl:~/a-rename/b$ tail -f a.txt
apple
> 和 >>
>
輸出重定向。例如將 pwd 結果輸出到 a.txt,這是一個覆蓋操作:
test11@pjl:~/tmp$ touch a.txt
test11@pjl:~/tmp$ pwd > a.txt
test11@pjl:~/tmp$ cat a.txt
/home/test11/tmp
test11@pjl:~/tmp$ ls > a.txt
test11@pjl:~/tmp$ cat a.txt
a.txt
>>
則是追加。例如將 pwd 的結果追加到 a.txt 文件中:
test11@pjl:~/tmp$ pwd >> a.txt
test11@pjl:~/tmp$ cat a.txt
a.txt
/home/test11/tmp
ln
ln -s
創建軟鏈接,類似 windows 下快捷鍵。
語法:
ln -s 文件或目錄 軟連接名
例如給 a 創建一個軟鏈接 ln-a:
test11@pjl:~/tmp$ ln -s a ln-a
test11@pjl:~/tmp$ ll
總用量 16
drwxrwxr-x 3 test11 test11 4096 6月 17 14:22 ./
drwxr-xr-x 8 test11 test11 4096 6月 17 14:13 ../
drwxrwxr-x 3 test11 test11 4096 6月 17 14:22 a/
lrwxrwxrwx 1 test11 test11 1 6月 17 14:22 ln-a -> a/
刪除軟連接可以使用 rm
,類似刪除一個文件:
test11@pjl:~/tmp$ ls
a ln-a
# 刪除軟鏈接
test11@pjl:~/tmp$ rm ln-a
# 軟鏈接已被刪除
test11@pjl:~/tmp$ ls
a
Tip:為什麼要加 -s
,通過 ln --help
有這麼一段信息:-s, --symbolic 創建符號鏈接而非硬鏈接
,也就是說加上 s
就是創建軟鏈接。
history
history
查看已執行過的歷史命令,也可以執行歷史命令。
查看所有歷史命令:
$ history
...
170 ls
171 cd ..
172 ls
173 rm ln-a
174 ls
175 history
176 history |more
177 ls
178 history
查看最近 5 條歷史命令:
test11@pjl:~/tmp$ history 5
176 history |more
177 ls
178 history
179 ls
180 history 5
執行第 177(這裡是 ls
命名) 條歷史命令:
test11@pjl:~/tmp$ !177
ls
a a.txt
運行級別
常用的運行級別有 3
和 5
:
- 0 關機
- 3 多用戶有網絡服務
- 5 多用戶圖形
- 6 系統重啟
可以通過 init
來切換運行級別,例如 init 0
等於關機。
Tip:可以通過 --help
查看 init
命名。
root@pjl:/home/test11# init --help
init [OPTIONS...] COMMAND
Send control commands to the init daemon.
Commands:
0 Power-off the machine
6 Reboot the machine
2, 3, 4, 5 Start runlevelX.target unit
1, s, S Enter rescue mode
q, Q Reload init daemon configuration
u, U Reexecute init daemon
Options:
--help Show this help
--no-wall Don't send wall message before halt/power-off/reboot
See the telinit(8) man page for details.
當前運行級別是圖形化。請看示例:
root@pjl:/home/test11# systemctl get-default
graphical.target
比如將運行級別設置為 3:
root@pjl:/home/test11# systemctl set-default multi-user.target
Created symlink /etc/systemd/system/default.target → /lib/systemd/system/multi-user.target.
root@pjl:/home/test11# systemctl get-default
multi-user.target
注:如果這麼做,重啟(reboot
)後直接進入級別 3,能夠節省資源。
docker 安裝 ubuntu20.04
筆者不想安裝虛擬機,為圖方便,直接在筆記本中安裝 ubuntu。
筆者已經在 win10 中安裝 Docker Desktop。
Tip: 有關 docker 的介紹請看 初步認識 docker。
下載鏡像:
$ docker pull ubuntu:22.04
創建一個名為 ubuntu-test 的容器:
PS C:\Users\78614> docker run -itd --name ubuntu-test ubuntu:20.04
通過 exec 命令進入 ubuntu 容器,指定通過 bash 進行交互:
PS C:\Users\78614> docker exec -it ubuntu-test /bin/bash
root@5dbf1344541e:/# whoami
root
安裝 vi
docker 下的 ubuntu 鏡像沒有 vi 命令。
root@5dbf1344541e:/# vi
bash: vi: command not found
root@5dbf1344541e:/# vim
bash: vim: command not found
如果需要 vi ,我們需要安裝它。
據網友介紹:
apt-get
/dpkg
:適用於Debian、Ubuntu等平台rpm/yum
:適用於 Redhat、CentOS、Suse等平台zypper
:適合於Suse平台
筆者依次輸入 rpm
、yum
、apt-get
,發現 apt-get
生效:
root@9b385d9791f6:~# rpm
bash: rpm: command not found
root@9b385d9791f6:~# yum
bash: yum: command not found
root@9b385d9791f6:~# apt-get
apt 2.0.8 (amd64)
Usage: apt-get [options] command
apt-get [options] install|remove pkg1 [pkg2 ...]
apt-get [options] source pkg1 [pkg2 ...]
通過 --help
查看 apt-get
:
root@5dbf1344541e:/# apt-get --help
apt 2.0.8 (amd64)
Usage: apt-get [options] command
apt-get [options] install|remove pkg1 [pkg2 ...]
apt-get [options] source pkg1 [pkg2 ...]
apt-get is a command line interface for retrieval of packages
and information about them from authenticated sources and
for installation, upgrade and removal of packages together
with their dependencies.
Most used commands:
update - Retrieve new lists of packages
upgrade - Perform an upgrade
install - Install new packages (pkg is libc6 not libc6.deb)
reinstall - Reinstall packages (pkg is libc6 not libc6.deb)
remove - Remove packages
purge - Remove packages and config files
autoremove - Remove automatically all unused packages
dist-upgrade - Distribution upgrade, see apt-get(8)
dselect-upgrade - Follow dselect selections
build-dep - Configure build-dependencies for source packages
satisfy - Satisfy dependency strings
clean - Erase downloaded archive files
autoclean - Erase old downloaded archive files
check - Verify that there are no broken dependencies
source - Download source archives
download - Download the binary package into the current directory
changelog - Download and display the changelog for the given package
See apt-get(8) for more information about the available commands.
Configuration options and syntax is detailed in apt.conf(5).
Information about how to configure sources can be found in sources.list(5).
Package and version choices can be expressed via apt_preferences(5).
Security details are available in apt-secure(8).
This APT has Super Cow Powers.
使用 apt-get
安裝 vi,報錯:
root@9b385d9791f6:~# apt-get install vi
Reading package lists... Done
Building dependency tree
Reading state information... Done
E: Unable to locate package vi
通過apt-get update
檢索新的包列表:
root@9b385d9791f6:~# apt-get update
Get:1 //archive.ubuntu.com/ubuntu focal InRelease [265 kB]
Get:2 //security.ubuntu.com/ubuntu focal-security InRelease [114 kB]
Get:3 //security.ubuntu.com/ubuntu focal-security/restricted amd64 Packages [1324 kB]
Get:4 //archive.ubuntu.com/ubuntu focal-updates InRelease [114 kB]
Get:5 //archive.ubuntu.com/ubuntu focal-backports InRelease [108 kB]
Get:6 //archive.ubuntu.com/ubuntu focal/multiverse amd64 Packages [177 kB]
Get:7 //archive.ubuntu.com/ubuntu focal/universe amd64 Packages [11.3 MB]
Get:8 //archive.ubuntu.com/ubuntu focal/restricted amd64 Packages [33.4 kB]
Get:9 //archive.ubuntu.com/ubuntu focal/main amd64 Packages [1275 kB]
Get:10 //archive.ubuntu.com/ubuntu focal-updates/multiverse amd64 Packages [30.3 kB]
Get:11 //archive.ubuntu.com/ubuntu focal-updates/restricted amd64 Packages [1404 kB]
Get:12 //archive.ubuntu.com/ubuntu focal-updates/universe amd64 Packages [1170 kB]
Get:13 //archive.ubuntu.com/ubuntu focal-updates/main amd64 Packages [2407 kB]
Get:14 //archive.ubuntu.com/ubuntu focal-backports/main amd64 Packages [54.2 kB]
Get:15 //archive.ubuntu.com/ubuntu focal-backports/universe amd64 Packages [27.1 kB]
Get:16 //security.ubuntu.com/ubuntu focal-security/main amd64 Packages [1974 kB]
91% [16 Packages 710 kB/1974 kB 36%]
...
Reading package lists... 80%
Reading package lists... Done
再次通過 apt-get 安裝 vi,依然失敗:
root@9b385d9791f6:~# apt-get install vi
Reading package lists... Done
Building dependency tree
Reading state information... Done
E: Unable to locate package vi
嘗試安裝 vim,成功:
root@9b385d9791f6:~# apt-get install vim
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
alsa-topology-conf alsa-ucm-conf file libasound2 libasound2-data libcanberra0 libexpat1 libgpm2 libltdl7 libmagic-mgc libmagic1 libmpdec2 libogg0 libpython3.8 libpython3.8-minimal libpython3.8-stdlib
libreadline8 libsqlite3-0 libssl1.1 libtdb1 libvorbis0a libvorbisfile3 mime-support readline-common sound-theme-freedesktop vim-common vim-runtime xxd xz-utils
Suggested packages:
libasound2-plugins alsa-utils libcanberra-gtk0 libcanberra-pulse gpm readline-doc ctags vim-doc vim-scripts
The following NEW packages will be installed:
alsa-topology-conf alsa-ucm-conf file libasound2 libasound2-data libcanberra0 libexpat1 libgpm2 libltdl7 libmagic-mgc libmagic1 libmpdec2 libogg0 libpython3.8 libpython3.8-minimal libpython3.8-stdlib
libreadline8 libsqlite3-0 libssl1.1 libtdb1 libvorbis0a libvorbisfile3 mime-support readline-common sound-theme-freedesktop vim vim-common vim-runtime xxd xz-utils
0 upgraded, 30 newly installed, 0 to remove and 7 not upgraded.
Need to get 14.9 MB of archives.
After this operation, 70.6 MB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 //archive.ubuntu.com/ubuntu focal/main amd64 libmagic-mgc amd64 1:5.38-4 [218 kB]
Get:2 //archive.ubuntu.com/ubuntu focal/main amd64 libmagic1 amd64 1:5.38-4 [75.9 kB]
Get:3 //archive.ubuntu.com/ubuntu focal/main amd64 file amd64 1:5.38-4 [23.3 kB]
Get:4 //archive.ubuntu.com/ubuntu focal-updates/main amd64 libexpat1 amd64 2.2.9-1ubuntu0.4 [74.4 kB]
Get:5 //archive.ubuntu.com/ubuntu focal/main amd64 libmpdec2 amd64 2.4.2-3 [81.1 kB]
Get:6 //archive.ubuntu.com/ubuntu focal-updates/main amd64 libssl1.1 amd64 1.1.1f-1ubuntu2.13 [1321 kB]
Get:7 //archive.ubuntu.com/ubuntu focal-updates/main amd64 libpython3.8-minimal amd64 3.8.10-0ubuntu1~20.04.4 [717 kB]
Get:8 //archive.ubuntu.com/ubuntu focal/main amd64 mime-support all 3.64ubuntu1 [30.6 kB]
Get:9 //archive.ubuntu.com/ubuntu focal/main amd64 readline-common all 8.0-4 [53.5 kB]
Get:10 //archive.ubuntu.com/ubuntu focal/main amd64 libreadline8 amd64 8.0-4 [131 kB]
Get:11 //archive.ubuntu.com/ubuntu focal-updates/main amd64 libsqlite3-0 amd64 3.31.1-4ubuntu0.3 [549 kB]
22% [11 libsqlite3-0 158 kB/549 kB
...
update-alternatives: warning: skip creation of /usr/share/man/man1/editor.1.gz because associated file /usr/share/man/man1/vim.1.gz (of link group editor) doesn't exist
Processing triggers for libc-bin (2.31-0ubuntu9.9) ...
命令行輸入 vi 或 vim,發現 vi 和 vim 都已安裝成功。
注:vi 和 vim 只在這個容器中安裝,如果通過 unbuntu 鏡像運行另一個容器,需要重新安裝 vi。我們可以通過停止和啟動這個安裝好 vi 的容器,反覆使用它即可。