alpine 编译c语言,Alpine Linux配置使用技巧

Alpine Linux是一个面向安全应用的轻量级Linux发行版。它采用了musl libc和busybox以减小系统的体积和运行时资源消耗,同时还提供了自己的包管理工具apk。

Alpine Linux的内核都打了grsecurity/PaX补丁,并且所有的程序都编译为Position Independent Executables (PIE) 以增强系统的安全性。

Alpine Linux的优势和问题

优势Alpine Linux的Docker镜像特点是轻巧(大小只有5M)且有完整的包管理工具(APK)。

Docker官方镜像可能会用Alpine Linux替换Ubuntu。

问题Alpine Linux使用了musl,可能和其他Linux发行版使用的glibc实现会有些不同。

musl实现的DNS服务不会使用resolv.conf文件中的search和domain两个配置,通过DNS来进行服务发现时需要注意。

Alpine Linux 包管理

apk包管理命令

Alpine使用apk进行包管理,通过apk --help命令查看完整的包管理命令,下面列举常用命令:update:从远程镜像源中更新本地镜像源索引

update命令会从各个镜像源列表下载APKINDEX.tar.gz并存储到本地缓存,一般在/var/cache/apk/(Alpine在该目录下)、 /var/lib/apk/ 、/etc/apk/cache/下。$ apk updateadd:安装PACKAGES并自动解决依赖关系

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 vimupgrade:升级当前已安装的软件包

upgrade命令升级系统已安装的所以软件包(一般包括内核),当然也可指定仅升级部分软件包(通过-u或--upgrade选择指定)。$ apk update    #更新最新本地镜像源

$ apk upgrade   #升级软件

$ apk add --upgrade busybox   #指定升级部分软件包search:搜索软件包

search命令搜索可用软件包,-v参数输出描述内容,支出通配符,-d或—description参数指定通过软件包描述查询。$ apk search            #查找所以可用软件包

$ apk search -v         #查找所以可用软件包及其描述内容

$ 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中是当前Alpine官方提供的镜像源(Alpine安装的时候系统自动选择最佳镜像源)国内镜像源

如何配置软件源

以中科大源为例:在/etc/apk/repositories文件中加入对应源地址就行了,一行一个地址。$ 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系统

Alpine Linux使用的是Gentoo一样的OpenRCinit系统.

以下命令可用于管理init系统rc-update

rc-update主要用于不同运行级增加或者删除服务。

rc-update语法格式Usage: rc-update [options] add  [...]

or: rc-update [options] del  [...]

or: rc-update [options] [show [...]]

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 #增加一个服务

$ rc-update del docker boot #删除一个服务rc-status

rc-status 主要用于运行级的状态管理。

rc-status语法格式Usage: rc-status [options] ...

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  #检查默认运行级别的状态

$ rc-status -a #检查所有运行级别的状态rc-service

rc-service主用于管理服务的状态

rc-service语法格式Usage: rc-service [options] [-i]  ...

or: rc-service [options] -e 

or: rc-service [options] -l

or: rc-service [options] -r 

Options: [ e:ilr:INChqVv ]

-e, --exists                 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                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 #启动一个服务。

$ rc-service sshd stop  #停止一个服务。

$ rc-service sshd restart  #重启一个服务。openrc

openrc主要用于管理不同的运行级。

openrc语法格式Usage: openrc [options] []

Options: [ a:no:s:SChqVv ]

-n, --no-stop                     do not stop any services

-o, --override               override the next runlevel to change into

when leaving single user or boot runlevels

-s, --service                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  #重启系统,类似于shutdown -r now。

$ halt     #关机,类似于shutdown -h now。

$ poweroff #关机

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值