1 Linux安裝與基礎
1.1 Linux哲學思想
-
一切皆文件
-
單一用途的程序,小型
-
鏈接程序,共同完成複雜任務
-
配置數據存儲在文本中
-
避免令人困惑的用戶界面
1.1.1 Linux操作系統
完整的類Unix操作系統
linux內核+GNU工具=Linux/GNU
如:CentOS,Ubuntu,Android
1.1.2 Linux內核版本組成
-
主版本號
-
次版本號
-
末版本號
-
打包版本號
- 廠商版本號
1.2 Linux發行版
-
slackware: SUSE Linux Enterprise Server(SLES),OpenSure桌面
-
Debian:Ubuntu,deepin,mint
-
redhat: RHEL:ReadHat Enterprise linux,每18個月發行一個新版本
CentOS:Community Enterprise Operating System 兼容RHEL的格式
中標麒麟: 中標軟件
Fedora:每6個月發行新版本
-
Alpine: 一個面向安全的輕型Linux發行版。不同於通常Linux發行版,Alpine採用了musllibc和busybox以減少系統的體積和運行時資源消耗,但功能上比busybox又完善的多,只有5M 左右大小
-
ArchLinux: 輕量簡潔,遵循K.I.S.S原則(keep it simple and stupid),Manjaro
-
Gentoo:極致性能,不提供傳統意義的安裝程序,下載源代碼在本機上編譯安裝軟件
-
LFS: Linux From scratch 自制Linux,只是一個說明書
- Android: kerner+busybox(工具集)+ java虛擬機
Linux發行版排名:https://distrowactch.com/dwres.php?resoure=popularity.
Linux分支參考網站: http://futurist.se/gldt/
2 Linux系統安裝
2.1 安裝CentOS
開始安裝
選擇語言
選擇時區
選擇手動分區
添加掛載點
接收自定義分區
配置網絡
關閉KDUMP
配置好以後開始安裝過程
配置root密碼
創建一個可登陸用戶並設置密碼
安裝完成重啓
測試lee新用戶可以正常登陸
3 Linux常用命令
3.1 設置主機名
hostname NAME
範例
[root@centos7 ~]#hostname nc-dx-k8s-node1-10-10.teach.org
注意:主機名不要使用下劃線
3.2 區別指定的命令是內部命令還是外部命令
type COMMAND
範例:查看是否存在對應內部和外部命令
[root@centos7 ~]#type -a echo
echo is a shell builtin
echo is /usr/bin/echo
3.2.1 shell中可執行的兩類命令
- 內部命令:由shell自帶的,而且通過某命令形式提供
- 外部命令:在文件系統路徑下有對應的可執行程序文件
3.3 命令別名
常用的較長命令,可以將其定義爲較短的別名,以方便執行。
顯示當前shell進程所有可用的命令別名
alias
定義別名NAME,其相當於執行命令VALUE
範例:
[root@centos7 ~]#alias ipnet='/etc/sysconfig/network-scripts/'
[root@centos7 ~]#alias
alias cp='cp -i'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias ipnet='/etc/sysconfig/network-scripts/'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias mv='mv -i'
alias rm='rm -i'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
撤銷別名:unalias
3.4 查看系統版本
3.4.1 查看內核版本
[root@centos7 ~]#uname -r
3.10.0-1160.el7.x86_64
3.4.2 查看操作系統發行版本
[root@centos7 ~]#cat /etc/redhat-release
CentOS Linux release 7.9.2009 (Core)
[root@centos7 ~]#cat /etc/os-release
NAME="CentOS Linux"
VERSION="7 (Core)"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="7"
PRETTY_NAME="CentOS Linux 7 (Core)"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:centos:centos:7"
HOME_URL="https://www.centos.org/"
BUG_REPORT_URL="https://bugs.centos.org/"
CENTOS_MANTISBT_PROJECT="CentOS-7"
CENTOS_MANTISBT_PROJECT_VERSION="7"
REDHAT_SUPPORT_PRODUCT="centos"
REDHAT_SUPPORT_PRODUCT_VERSION="7"
3.5 查看硬件信息
3.5.1 查看cpu
[root@centos7 ~]#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有幾個線程
Core(s) per socket: 1 #每個槽位有1個core
Socket(s): 1 #服務器面板上有1個cpu槽位
NUMA node(s): 1 #nodes的數量
Vendor ID: AuthenticAMD
CPU family: 23
Model: 24
Model name: AMD Ryzen 5 3500U with Radeon Vega Mobile Gfx
Stepping: 1
CPU MHz: 2096.064
BogoMIPS: 4192.12
Hypervisor vendor: VMware
Virtualization type: full
L1d cache: 32K
L1i cache: 64K
L2 cache: 512K
L3 cache: 4096K
NUMA node0 CPU(s): 0 #對應的core
3.5.2 查看內存大小
[root@centos7 ~]#free -h
total used free shared buff/cache available
Mem: 972M 162M 375M 7.6M 434M 652M
Swap: 1.9G 264K 1.9G
[root@centos7 ~]#cat /proc/meminfo
MemTotal: 995676 kB
MemFree: 384444 kB
MemAvailable: 668456 kB
Buffers: 4176 kB
Cached: 389996 kB
SwapCached: 40 kB
Active: 231732 kB
Inactive: 203896 kB
Active(anon): 26888 kB
Inactive(anon): 22308 kB
Active(file): 204844 kB
Inactive(file): 181588 kB
Unevictable: 0 kB
Mlocked: 0 kB
SwapTotal: 1952764 kB
SwapFree: 1952500 kB
Dirty: 4 kB
Writeback: 0 kB
AnonPages: 41448 kB
Mapped: 29228 kB
Shmem: 7740 kB
Slab: 91984 kB
SReclaimable: 50580 kB
SUnreclaim: 41404 kB
KernelStack: 3920 kB
PageTables: 3640 kB
NFS_Unstable: 0 kB
Bounce: 0 kB
WritebackTmp: 0 kB
CommitLimit: 2450600 kB
Committed_AS: 234380 kB
VmallocTotal: 34359738367 kB
VmallocUsed: 178416 kB
VmallocChunk: 34359310332 kB
Percpu: 22528 kB
HardwareCorrupted: 0 kB
AnonHugePages: 8192 kB
CmaTotal: 0 kB
CmaFree: 0 kB
HugePages_Total: 0
HugePages_Free: 0
HugePages_Rsvd: 0
HugePages_Surp: 0
Hugepagesize: 2048 kB
DirectMap4k: 63360 kB
DirectMap2M: 985088 kB
DirectMap1G: 0 kB
[root@centos7 ~]#
3.5.3 查看硬盤和分區情況
[root@centos7 ~]#blkid
/dev/sda1: UUID="30988762-b535-4145-8e94-4f17e1c7c617" TYPE="xfs"
/dev/sda2: UUID="f720d1f7-ad53-443f-8a7f-b7838dc1f580" TYPE="xfs"
/dev/sda3: UUID="97d22c9f-9550-46fb-9fed-0bb874de83f9" TYPE="swap"
/dev/sda5: UUID="18731b4a-c59e-40f3-bf05-10d656e54e26" TYPE="xfs"
/dev/sr0: UUID="2020-11-02-15-15-23-00" LABEL="CentOS 7 x86_64" TYPE="iso9660" PTTYPE="dos"
[root@centos7 ~]#lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 200G 0 disk
├─sda1 8:1 0 953M 0 part /boot
├─sda2 8:2 0 100G 0 part /
├─sda3 8:3 0 1.9G 0 part [SWAP]
├─sda4 8:4 0 1K 0 part
└─sda5 8:5 0 46.6G 0 part /data
sr0 11:0 1 9.5G 0 rom
[root@centos7 ~]#cat /proc/partitions
major minor #blocks name
8 0 209715200 sda
8 1 975872 sda1
8 2 104857600 sda2
8 3 1952768 sda3
8 4 1 sda4
8 5 48827392 sda5
11 0 9961472 sr0
3.6 日期和時間
Linux的兩種時間
系統時間:有Linux內核通過CPU的工作頻率進行的
硬件時鐘:主板
命令: date 顯示和設置系統時間
選項:
- date 顯示時間
[root@centos7 ~]#date
Tue Jan 26 23:47:57 CST 2021
- %Y 顯示年 2021
[root@centos7 ~]#date +%Y
2021
- %m 顯示月 01
[root@centos7 ~]#date +%m
01
- %d 顯示日 26
[root@centos7 ~]#date +%d
26
- %F 顯示年月日
[root@centos7 ~]#date +%F
2021-01-26
- %H 顯示小時 24小時制 (00~23)
[root@centos7 ~]#date +%H
23
- %M 顯示分鐘 (00~59)
[root@centos7 ~]#date
Fri Jan 29 19:57:17 CST 2021
[root@centos7 ~]#date +%M
57
- %S 顯示分秒(00~59)
- %w 顯示周 (0~6)
[root@centos7 ~]#date +%w
5
[root@centos7 ~]#date
Fri Jan 29 20:03:54 CST 2021
- %s 顯示1月1號 至今 多少秒
[root@centos7 ~]#date +%s
1611921876
- %T 顯示時間 時:分:秒
[root@centos7 ~]#date +%T
20:05:28
3.7 信息輸出 echo
echo命令看將後面跟的字符進行輸出
語法:
echo [-neE] 字符串
選項:
- -E (默認)不支持\解釋轉義功能
- -n 不換行
- -e 啓用\字符的轉義解釋功能
顯示變量
echo "$VAR_NAME" #用變量值替換,弱引用
[root@centos7 ~]#echo "$HOSTNAME"
centos7.9-server.rog
echo '$VAR_NAME' #變量不會替換,強引用
[root@centos7 ~]#echo '$HOSTNAME'
$HOSTNAME
啓用-e命令選項,出現以下字符,則特別加以處理,不會將它當成一般字符輸出
- \a 發出警告聲
- \b 退格鍵
- \c 最後不加上換行符
- \e escape,相當於\033
- \n 換行且光標移至行首
- \r 回車,即光標移至行首,但不換行
- \t 插入tab
- \\ 插入\字符
- \0nnn 插入nnn(八進制) 所代表的ASCII字符
- \xHH 插入HH(十六進制) 所代表的ASCII字符 (man 7 ascii)
[root@centos7 ~]#echo -e '\033[43;33;1;5mCentos\e[0m'
Centos
[root@centos7 ~]#echo \$PATH
$PATH
[root@centos7 ~]#echo \
>
[root@centos7 ~]#echo \\
\
[root@centos7 ~]#ehco \\\
>
3.8 screen
利用screen可以實現會話管理,如:新建會話,共享會話等,Centos7來自於base源,Centos8來自於epel源。
安裝screen
[root@centos7 ~]#yum -y install screen
前提:連接的是同一網絡中的同一臺機器
screen命令常見用法:
-
創建新screen會話
screen -S [SESSION]
-
加入screen會話
screen -x [SESSION]
-
退出並關閉screen會話
exit
-
剝離當前screen會話
Ctrl+a,b
-
顯示所有已經打開的screen會話
screen -ls
-
恢復某screen會話
screen -r [SESSION]
[root@centos7 ~]#screen -S
[screen is terminating]
[root@centos7 ~]#screen -ls
There is a screen on:
46322.top (Attached)
1 Socket in /var/run/screen/S-root.
[root@centos7 ~]#screen -x top
[screen is terminating]
3.9 關機和重啓
關機:
- halt
- poweroff
- init 0
重啓:
-
reboot
-f:強制,不調用shutdown
-p:切斷電源
- init 6
關機或重啓:shutdown
[root@centos7 ~]#shutdown --help
shutdown [OPTIONS...] [TIME] [WALL...]
Shut down the system.
--help Show this help #顯示幫助
-H --halt Halt the machine #關機
-P --poweroff Power-off the machine #關機
-r --reboot Reboot the machine #重啓
-h Equivalent to --poweroff, overridden by --halt #關機
-k Don't halt/power-off/reboot, just send warnings #發出警告
--no-wall Don't send wall message before halt/power-off/reboot #關機或重啓不發送wall消息
-c Cancel a pending shutdown #取消掛起的關機
TIME:無指定,默認相當於+1(Centos7)
now: 立刻,相當於+0
+#:相對時間法,幾分鐘之後;例 +3
hh:mm: 絕對時間法,指明具體時間
[root@centos7 ~]#shutdown +10
Shutdown scheduled for Fri 2021-01-29 21:31:54 CST, use 'shutdown -c' to cancel.
[root@centos7 ~]#
Broadcast message from [email protected] (Fri 2021-01-29 21:21:54 CST):
The system is going down for power-off at Fri 2021-01-29 21:31:54 CST!
^C
[root@centos7 ~]#shutdown -c
Broadcast message from [email protected] (Fri 2021-01-29 21:22:00 CST):
The system shutdown has been cancelled at Fri 2021-01-29 21:23:00 CST!
[root@centos7 ~]#
4 history 命令相關shell變量
-
HISTSIZE #命令歷史記錄的條數
-
HISTFILE #指定命令歷史文件,默認爲~/.bash_history
-
HISTFILESIZE #命令歷史文件記錄歷史的條數
-
HISTTMIEFORMAT #顯示時間,例: HISTTIMEFORMAT=" %F %T"
-
HISTIGNORE #指定在歷史中不記錄的命令,例如: HISTIGNORE="str1:str2*...",忽略strl命令,str2開頭的歷史
-
HISTCONTROL #控制命令歷史記錄方式,可設置值:
ignoredups: 默認,忽略重複的命令,連續且相同認爲重複
ignorespace:忽略空白開頭的命令
ignoreboth:相當於ignoredups和ignorespace組合
erasedups:刪除重複命令
範例:
修改~/.bash_profile,在最後一行增加HISTTIMEFORMAT設置,看到執行命令的時間
[root@centos7 ~]#echo 'export HISTTIMEFORMAT="%F %T"' >> ~/.bash_profile [root@centos7 ~]#cat ~/.bash_profile # .bash_profile # Get the aliases and functions if [ -f ~/.bashrc ]; then . ~/.bashrc fi # User specific environment and startup programs PATH=$PATH:$HOME/bin export PATH export HISTTIMEFORMAT="%F %T" [root@centos7 ~]#history 1 2021-01-29 22:12:04history
4.1 history
選項:
- -c 清空歷史命令
- -d offset 刪除歷史中第offset個命令
- n 顯示最近的n條命令
- -a 追加本次會話新執行的歷史命令列表到歷史文件
-
-r 讀歷史文件附加到歷史列表
- -w 保存歷史列表到指定的歷史文件
- -n 讀取歷史文件中未讀過的行到歷史列表
- -p 展開歷史參數成多行,但不存在歷史列表中
- -s 展開歷史參數成一行,附件在歷史列表後
[root@centos7 ~]#history -c
[root@centos7 ~]#history
1 2021-01-29 22:18:47history
[root@centos7 ~]#history -r
[root@centos7 ~]#history 1
511 2021-01-29 22:19:03history 1