[Linux服务器] Linux系统管理:虚拟机Alpine Linux安装

1424 0
Honkers 2025-3-31 11:51:59 | 显示全部楼层 |阅读模式

目录

一、理论

1.Alpine Linux

二、实验

1.Alpine Linux安装

三、问题

1.Alpine Linux 缺少VIM命令

2.Alpine Linux SSH连接不上

3.Alpine Linux IP配置

四、总结


 

 

一、理论

1.Alpine Linux

(1)概念

Alpine 操作系统是一个面向安全的轻型 Linux 发行版。它不同于通常 Linux 发行版,Alpine 采用了 musl libc 和 busybox 以减小系统的体积和运行时资源消耗,但功能上比 busybox 又完善的多,因此得到开源社区越来越多的青睐。在保持瘦身的同时,Alpine 还提供了自己的包管理工具 apk,可以通过 https://pkgs.alpinelinux.org/packages 网站上查询包信息,也可以直接通过 apk 命令直接查询和安装各种软件。

Alpine 由非商业组织维护的,支持广泛场景的 Linux发行版,它特别为资深/重度Linux用户而优化,关注安全,性能和资源效能。Alpine 镜像可以适用于更多常用场景,并且是一个优秀的可以适用于生产的基础系统/环境。
 

(2)下载

进入Alpine官网,下载Alpine Linux的ISO镜像,Alpine Linux提供了专门的虚拟机版本。

(3)比较

下面是系统安装完成后,并手动使用自带软件管理器,安装vim软件后的体积对比图。

 (4)安装方法

安装方法参考官方的文档:
Install Alpine on VMware Workstation - Alpine Linux

(5)Alpine Linux源管理

国内源简介:

  1. 清华大学:https://mirror.tuna.tsinghua.edu.cn/alpine/
  2. 阿里云:https://mirrors.aliyun.com/alpine/
  3. 中科大:http://mirrors.ustc.edu.cn/alpine/
  4. 网易:http://mirrors.163.com/
复制代码

配置:

中科大的帮助http://mirrors.ustc.edu.cn/help/alpine.html
一般情况下,将 /etc/apk/repositories 文件中 Alpine 默认的源地址 http://dl-cdn.alpinelinux.org/ 替换为 http://mirrors.ustc.edu.cn/ 即可。
 

  1. sudo vim /etc/apk/repositories
复制代码

(6) Alpine Linux 包管理

  1. 1.简介
  2. Alpine使用apk进行包管理,下面介绍常用命令
  3. 2.apk update
  4. apk update #更新最新镜像源列表
  5. 3.apk search
  6. apk search #查找所以可用软件包
  7. apk search -v #查找所以可用软件包及其描述内容
  8. apk search -v 'acf*' #通过软件包名称查找软件包apk search -v -d ‘docker’ #通过描述文件查找特定的软件包
  9. 4.apk add
  10. apk add openssh #安装一个软件
  11. apk add openssh openntp vim #安装多个软件
  12. apk add --no-cache mysql-client #不使用本地镜像源缓存,相当于先执行update,再执行add
  13. 5.apk info
  14. apk info #列出所有已安装的软件包
  15. apk info -a zlib #显示完整的软件包信息
  16. apk info --who-owns /sbin/lbu #显示指定文件属于的包
  17. 6.apk upgrade
  18. apk upgrade #升级所有软件
  19. apk upgrade openssh #升级指定软件
  20. apk upgrade openssh openntp vim #升级多个软件
  21. apk add --upgrade busybox #指定升级部分软件包
  22. 7.apk del
  23. apk del openssh #删除一个软件
复制代码

(7)  Alpine Linux服务管理

  1. 1.简介
  2. alpine没有使用fedora的systemctl来进行服务管理,使用的是RC系列命令
  3. 未安装openrc时需执行以下命令
  4. sudo apk add --no-cache openrc
  5. 2.rc-update
  6. rc-update主要用于不同运行级增加或者删除服务。
  7. alpine:~# rc-update --help
  8. Usage: rc-update [options] add <service> [<runlevel>...]
  9. or: rc-update [options] del <service> [<runlevel>...]
  10. or: rc-update [options] [show [<runlevel>...]]
  11. Options: [ asuChqVv ]
  12. -a, --all Process all runlevels
  13. -s, --stack Stack a runlevel instead of a service
  14. -u, --update Force an update of the dependency tree
  15. -h, --help Display this help output
  16. -C, --nocolor Disable color output
  17. -V, --version Display software version
  18. -v, --verbose Run verbosely
  19. -q, --quiet Run quietly (repeat to suppress errors)
  20. 3.rc-status
  21. rc-status 主要用于运行级的状态管理。
  22. alpine:~# rc-status --help
  23. Usage: rc-status [options] <runlevel>...
  24. or: rc-status [options] [-a | -c | -l | -m | -r | -s | -u]
  25. Options: [ aclmrsuChqVv ]
  26. -a, --all Show services from all run levels
  27. -c, --crashed Show crashed services
  28. -l, --list Show list of run levels
  29. -m, --manual Show manually started services
  30. -r, --runlevel Show the name of the current runlevel
  31. -s, --servicelist Show service list
  32. -u, --unused Show services not assigned to any runlevel
  33. -h, --help Display this help output
  34. -C, --nocolor Disable color output
  35. -V, --version Display software version
  36. -v, --verbose Run verbosely
  37. -q, --quiet Run quietly (repeat to suppress errors)
  38. 4.rc-service
  39. rc-service主用于管理服务的状态
  40. alpine:~# rc-service --help
  41. Usage: rc-service [options] [-i] <service> <cmd>...
  42. or: rc-service [options] -e <service>
  43. or: rc-service [options] -l
  44. or: rc-service [options] -r <service>
  45. Options: [ ce:ilr:INChqVv ]
  46. -e, --exists <arg> tests if the service exists or not
  47. -c, --ifcrashed if the service is crashed then run the command
  48. -i, --ifexists if the service exists then run the command
  49. -I, --ifinactive if the service is inactive then run the command
  50. -N, --ifnotstarted if the service is not started then run the command
  51. -l, --list list all available services
  52. -r, --resolve <arg> resolve the service name to an init script
  53. -h, --help Display this help output
  54. -C, --nocolor Disable color output
  55. -V, --version Display software version
  56. -v, --verbose Run verbosely
  57. -q, --quiet Run quietly (repeat to suppress errors)
  58. 5.openrc
  59. openrc主要用于管理不同的运行级。
  60. alpine:~# openrc --help
  61. Usage: openrc [options] [<runlevel>]
  62. Options: [ a:no:s:SChqVv ]
  63. -n, --no-stop do not stop any services
  64. -o, --override <arg> override the next runlevel to change into
  65. when leaving single user or boot runlevels
  66. -s, --service <arg> runs the service specified with the rest
  67. of the arguments
  68. -S, --sys output the RC system type, if any
  69. -h, --help Display this help output
  70. -C, --nocolor Disable color output
  71. -V, --version Display software version
  72. -v, --verbose Run verbosely
  73. -q, --quiet Run quietly (repeat to suppress errors)
  74. 6.常用的RC系列命令
  75. 1.增加服务到系统启动时运行,下例为docker
  76. rc-update add docker boot
  77. 2.重启网络服务
  78. rc-service networking restart
  79. 3.列出所有服务
  80. rc-status -a
  81. 服务开机自启
  82. Alpine Linux 的 开机自启目录在/etc/local.d下,这个目录用于放置我们需要在本地服务启动或停止后执行的脚本。
复制代码

 

 

二、实验

1.Alpine Linux安装

(1)新建虚拟机

新建一台虚拟机,类型选Linux,

 版本选Linux 3.x / 4.x (64-bit),

 虚拟机名称

虚拟磁盘8G

自定义硬件, 内存一般1G

完成

 

 引导

 首次启动时使用root这个用户名登录,不需要密码

 登录成功后,执行setup-alpine进入安装向导,向导会询问你一系列的问题,然后等待你的输入。

 备注:如果途中操作有失误,可以按Ctrl + c 退出向导,再重新开始向导。

下面是每个问题的具体含义及操作步骤。

  1. Select keyboard layout [none]:
  2. 选择键盘布局,系统默认给你选了none,直接回车
  3. Enter system hostname (short form, e.g. 'foo') [localhost]
  4. 选择虚拟机host名称,系统给你选了localhost,没有特殊需要的话直接回车
  5. Which one do you want to initialize? (or '?' or 'done') [eth0]
  6. 选择要不要设置网卡,系统给你选了需要初始化eth0那张网卡,直接回车
  7. Ip address for eth0? (or 'dhcp', 'none', '?') [dhcp]
  8. 输入eth0网卡的IP地址,或者直接通过dhcp协议自动获取IP,系统给你选了dhcp,直接回车
  9. Do you want to do any manual network configuration? [no]
  10. 还需要手工修改网卡的其他配置吗?系统给你选了no,直接回车
  11. Changeing password for root
  12. New password:
  13. 安装完成后,就不能像现在这样不用密码就登录了,系统提示你输入root帐号的密码。
  14. Retype Password:
  15. 确认一遍密码
  16. Which timezone are you in? ('?' for list) [UTC]
  17. 输入时区,国内可以输入"Asia/Shanghai"
  18. HTTP/FTP pxory URL?
  19. 需要使用HTTP代理连接网络吗?一般不需要,可以直接回车
  20. Enter mirror number (1-21) or URL to add (or r/f/e/done) [f]
  21. Alpine Linux自带的包管理器需要联网下载软件包
复制代码

每个步骤直接按回车默认选择

 

 

 此时屏幕上会打出多个镜像站的URL,但都是国外的

 

这里推荐使用国内镜像:这时候按下e,编辑镜像配置文件,把国内的镜像地址填进去:

  1. https://mirrors.ustc.edu.cn/alpine/latest-stable/community/
  2. https://mirrors.ustc.edu.cn/alpine/latest-stable/main/
  3. https://mirrors.aliyun.com/alpine/latest-stable/community/
  4. https://mirrors.aliyun.com/alpine/latest-stable/main/
复制代码

 

 

退出编辑后向导会检查地址是否可用,没问题的话会继续下面的步骤。 

  1. Which SSH server? ('openssh', 'dropbear' or 'none') [openssh]
  2. SSH服务器(用于登录Linux)有两种可供选择,默认选择openssh,回车
  3. Which NTP client to run? ('busybox', 'openntpd', 'chorony' or 'none') [chrony]
  4. 选择NTP客户端(用来调整系统时钟)的类型,保持默认的chrony即可,回车
  5. Available disks are:
  6. sda (8.6 GB ATA VBOX HARDDISK )
  7. Which disk(s) would you like to use? (or '?' for help or 'none') [none]
  8. 这一步比较关键,系统发现了有块8.6GB大小,名称叫sda的硬盘
  9. 问你需不需要使用硬盘,因为Alpine Linux可以运行在内存里,这里的默认选项是不使用硬盘
  10. 所以要手动键入sda,后面的步骤才会将系统安装在硬盘上
  11. The following disk is selected:
  12. sda (8.6 GB ATA VBOX HARDDISK )
  13. How would you like to use it? ('sys', 'data', 'lvm' or '?' for help) [?]
  14. 以何种方式安装系统,这里需要键入"sys",表示把整个系统安装在硬盘上。
  15. 其他选项并不适用于虚拟机
  16. WARNING: The following disk(s) will be erased:
  17. sda (8.6 GB ATA VBOX HARDDISK )
  18. WARNING: Erase the above disk(s) and continue? [y/N]
  19. 向导让你确认选择的sda磁盘上的数据会全部丢失
  20. 虚拟机磁盘,初始肯定是没数据的,键入"y"确认
复制代码

 

 

随着屏幕滚动,系统会安装一阵子,等出现这行字时,就算安装完成了。

  1. Instalation is complete. Please reboot
  2. 安装完成,请重启(先别着急)
复制代码

看一下安装完成后系统空间的使用情况。

 

 重启后登录

 

三、问题

1.Alpine Linux 缺少VIM命令

(1)报错

(2)原因分析

在Alpine Linux上安装Vim有3种方法:

①APK是Alpine Linux的包管理器,可以使用它来安装Vim。要安装Vim,请打开终端,然后键入以下命令:

  1. apk add vim
复制代码

安装完成后,您可以使用以下命令检查Vim的版本:

  1. vim --version
复制代码

②使用源代码安装Vim

如果想要安装最新版本的Vim,则可以使用源代码安装。要安装Vim,请打开终端,然后键入以下命令:

  1. wget https://github.com/vim/vim/archive/master.zip
  2. unzip master.zip
  3. cd vim-master
  4. ./configure
  5. make
  6. make install
复制代码

安装完成后,您可以使用以下命令检查Vim的版本:

  1. vim --version
复制代码

③ 使用Vim容器安装Vim

如果您想要安装Vim,但又不想安装太多依赖包,则可以使用Vim容器。要安装Vim容器,请打开终端,然后键入以下命令:

  1. docker pull vim
复制代码

安装完成后,您可以使用以下命令检查Vim的版本:

  1. docker run --rm -it vim --version
复制代码

(3)解决方法

安装

查看版本

 

2.Alpine Linux SSH连接不上

(1)原因分析

未修改配置文件

(2)解决方法

  1. vim /etc/ssh/sshd_config
复制代码

(3)增加

  1. PermitRootLogin yes
  2. GatewayPorts yes
  3. AllowTcpForwarding yes
  4. PermitEmptyPasswords yes
复制代码

 service sshd restart
映射虚拟机22端口

SSH 连接成功

 

3.Alpine Linux IP配置

(1)解决方法

添加网卡

 

  1. #编辑interfaces
  2. vim /etc/network/interfaces
  3. #配置
  4. auto lo
  5. iface lo inet loopback
  6. auto eth0
  7. iface eth0 inet dhcp
  8. auto eth1
  9. iface eth1 inet dhcp
  10. hostname localhost
复制代码

 重启网络服务

  1. service networking restart
复制代码

 

四、总结

Alpine Linux安装非常简单,快速。虽然是命令行安装,比CentOS的安装都容易。

系统安装完后,体积上面有着非常明显的优势。

 

 

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

中国红客联盟公众号

联系站长QQ:5520533

admin@chnhonker.com
Copyright © 2001-2026 Discuz Team. Powered by Discuz! X3.5 ( 粤ICP备13060014号 )|天天打卡 本站已运行