【轉(zhuǎn)】Alpine Linux 配置使用技巧

https://www.hi-linux.com/posts/64839.html

Alpine Linux是一個面向安全應(yīng)用的輕量級Linux發(fā)行版。它采用了musl libc和busybox以減小系統(tǒng)的體積和運行時資源消耗,同時還提供了自己的包管理工具apk。

Alpine Linux的內(nèi)核都打了grsecurity/PaX補丁,并且所有的程序都編譯為Position Independent Executables (PIE) 以增強系統(tǒng)的安全性。

Alpine Linux的優(yōu)勢和問題

優(yōu)勢

  • Alpine Linux的Docker鏡像特點是輕巧(大小只有5M)且有完整的包管理工具(APK)。
  • Docker官方鏡像可能會用Alpine Linux替換Ubuntu。

問題

  • Alpine Linux使用了musl,可能和其他Linux發(fā)行版使用的glibc實現(xiàn)會有些不同。

Alpine Linux 包管理

apk包管理命令

Alpine使用apk進行包管理,通過apk --help命令查看完整的包管理命令,下面列舉常用命令:

  • update:從遠程鏡像源中更新本地鏡像源索引

update命令會從各個鏡像源列表下載APKINDEX.tar.gz并存儲到本地緩存,一般在/var/cache/apk/(Alpine在該目錄下)、 /var/lib/apk/ 、/etc/apk/cache/下。

$ apk update
  • add:安裝PACKAGES并自動解決依賴關(guān)系

add命令從倉庫中安裝最新軟件包,并自動安裝必須的依賴包,也可以從第三方倉庫添加軟件包。

$ apk add openssh openntp vim
$ apk add --no-cache mysql-client
$ apk add docker --update-cache --repository http://mirrors.ustc.edu.cn/alpine/v3.4/main/ --allow-untrusted

安裝指定版本軟件包

$ apk add asterisk=1.6.0.21-r0
$ apk add 'asterisk<1.6.1'
$ apk add 'asterisk>1.6.1'
  • del:卸載并刪除PACKAGES
$ apk del openssh openntp vim
  • upgrade:升級當(dāng)前已安裝的軟件包

upgrade命令升級系統(tǒng)已安裝的所以軟件包(一般包括內(nèi)核),當(dāng)然也可指定僅升級部分軟件包(通過-u或–upgrade選擇指定)。

$ apk update    #更新最新本地鏡像源
$ apk upgrade   #升級軟件
$ apk add --upgrade busybox   #指定升級部分軟件包
  • search:搜索軟件包

search命令搜索可用軟件包,-v參數(shù)輸出描述內(nèi)容,支出通配符,-d或–description參數(shù)指定通過軟件包描述查詢。

$ apk search            #查找所以可用軟件包
$ apk search -v         #查找所以可用軟件包及其描述內(nèi)容
$ apk search -v 'acf*'  #通過軟件包名稱查找軟件包
$ apk search -v -d 'docker'   #通過描述文件查找特定的軟件包
  • info:列出PACKAGES或鏡像源的詳細信息

info命令用于顯示軟件包的信息。

$ apk info              #列出所有已安裝的軟件包
$ apk info -a zlib       #顯示完整的軟件包信息
$ apk info --who-owns /sbin/lbu #顯示指定文件屬于的包

鏡像源配置

MIRRORS.txt中是當(dāng)前Alpine官方提供的鏡像源(Alpine安裝的時候系統(tǒng)自動選擇最佳鏡像源)

  • 國內(nèi)鏡像源

清華TUNA鏡像源:https://mirror.tuna.tsinghua.edu.cn/alpine/
中科大鏡像源:http://mirrors.ustc.edu.cn/alpine/
阿里云鏡像源:http://mirrors.aliyun.com/alpine/

如何配置軟件源

以中科大源為例:在/etc/apk/repositories文件中加入對應(yīng)源地址就行了,一行一個地址。

$ vi /etc/apk/repositories
# /media/cdrom/apks
http://mirrors.ustc.edu.cn/alpine/v3.5/main
http://mirrors.ustc.edu.cn/alpine/v3.5/community

Alpine Linux init系統(tǒng)

Alpine Linux使用的是Gentoo一樣的OpenRCinit系統(tǒng).

以下命令可用于管理init系統(tǒng)

  • rc-update

rc-update主要用于不同運行級增加或者刪除服務(wù)。

rc-update語法格式

Usage: rc-update [options] add <service> [<runlevel>...]
 or: rc-update [options] del <service> [<runlevel>...]
 or: rc-update [options] [show [<runlevel>...]]

Options: [ asuChqVv ]
 -a, --all                         Process all runlevels
 -s, --stack                       Stack a runlevel instead of a service
 -u, --update                      Force an update of the dependency tree
 -h, --help                        Display this help output
 -C, --nocolor                     Disable color output
 -V, --version                     Display software version
 -v, --verbose                     Run verbosely
 -q, --quiet                       Run quietly (repeat to suppress errors)

使用實例

$ rc-update add docker boot #增加一個服務(wù)
$ rc-update del docker boot #刪除一個服務(wù)
  • rc-status

rc-status 主要用于運行級的狀態(tài)管理。

rc-status語法格式

Usage: rc-status [options] <runlevel>...
 or: rc-status [options] [-a | -c | -l | -r | -s | -u]

Options: [ aclrsuChqVv ]
 -a, --all                         Show services from all run levels
 -c, --crashed                     Show crashed services
 -l, --list                        Show list of run levels
 -r, --runlevel                    Show the name of the current runlevel
 -s, --servicelist                 Show service list
 -u, --unused                      Show services not assigned to any runlevel
 -h, --help                        Display this help output
 -C, --nocolor                     Disable color output
 -V, --version                     Display software version
 -v, --verbose                     Run verbosely
 -q, --quiet                       Run quietly (repeat to suppress errors)

使用實例

$ rc-status  #檢查默認運行級別的狀態(tài)
$ rc-status -a #檢查所有運行級別的狀態(tài)
  • rc-service

rc-service主用于管理服務(wù)的狀態(tài)

rc-service語法格式

Usage: rc-service [options] [-i] <service> <cmd>...
 or: rc-service [options] -e <service>
 or: rc-service [options] -l
 or: rc-service [options] -r <service>

Options: [ e:ilr:INChqVv ]
 -e, --exists <arg>                tests if the service exists or not
 -i, --ifexists                    if the service exists then run the command
 -I, --ifinactive                  if the service is inactive then run the command
 -N, --ifnotstarted                if the service is not started then run the command
 -l, --list                        list all available services
 -r, --resolve <arg>               resolve the service name to an init script
 -h, --help                        Display this help output
 -C, --nocolor                     Disable color output
 -V, --version                     Display software version
 -v, --verbose                     Run verbosely
 -q, --quiet                       Run quietly (repeat to suppress errors)

使用實例

$ rc-service sshd start #啟動一個服務(wù)。
$ rc-service sshd stop  #停止一個服務(wù)。
$ rc-service sshd restart  #重啟一個服務(wù)。
  • openrc

openrc主要用于管理不同的運行級。

openrc語法格式

Usage: openrc [options] [<runlevel>]

Options: [ a:no:s:SChqVv ]
 -n, --no-stop                     do not stop any services
 -o, --override <arg>              override the next runlevel to change into
 when leaving single user or boot runlevels
 -s, --service <arg>               runs the service specified with the rest
 of the arguments
 -S, --sys                         output the RC system type, if any
 -h, --help                        Display this help output
 -C, --nocolor                     Disable color output
 -V, --version                     Display software version
 -v, --verbose                     Run verbosely
 -q, --quiet                       Run quietly (repeat to suppress errors)

Alpine Linux可用的運行級

  • default
  • sysinit
  • boot
  • single
  • reboot
  • shutdown

使用實例

$ openrc single #更改為single運行級
  • 其它指令
$ reboot  #重啟系統(tǒng),類似于shutdown -r now。
$ halt     #關(guān)機,類似于shutdown -h now。
$ poweroff #關(guān)機

參考文檔

http://www.google.com
http://t.cn/RizgdLy
https://wiki.alpinelinux.org/wiki/Alpine_Linux_Init_System
https://wiki.alpinelinux.org/wiki/Alpine_Linux_package_management

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容