在CentOS 7/CentOS 8上安装snapd和snap应用程序

时间:2020-02-23 14:31:16  来源:igfitidea点击:

在本指南中,我将指导我们完成在CentOS 8/CentOS 7服务器上安装和使用Snap的步骤。对于那些不熟悉的新术语,snap来自Snappy,它是Canonical的软件包管理和软件部署系统。它最初是为Ubuntu手机操作系统设计的。

什么是snap?

snap是包含应用程序代码的squashFS文件系统和包含特定元数据的snap.yaml文件。它具有只读文件系统,并且一旦安装,便具有可写区域。它捆绑了所需的大多数库和运行时,可以在不影响系统其余部分的情况下进行更新和还原。它通过安全机制在OS和其他应用程序中受到限制,但可以根据细粒度与其他快照交换内容和功能。由用户控制的策略和操作系统默认设置。

什么是Snapd?

Snapd是一种REST API守护程序服务,可在Linux系统上运行以管理快照包(快照)。它与快照存储进行交互,并提供用于与之交互的命令客户端"快照"。必须先安装napd,然后才能在任何Linux发行版上开始管理快照。

为什么要使用snap?

Snap可以为每个Linux桌面,服务器,云或者设备打包任何应用程序。快照的安装速度更快,更易于创建,运行更安全,并且它们会自动进行事务更新,因此应用程序始终是全新的且永不中断。我们可以带上自己的构建基础架构或者使用我们的基础架构。

在CentOS 8/CentOS 7上安装和使用Snapd

请按照以下步骤在CentOS 7服务器上安装Snapd。

在CentOS 8上安装Snap

添加EPEL存储库

sudo dnf -y install https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm
sudo dnf -y upgrade

在CentOS 8上安装Snap

sudo dnf -y install snapd
sudo systemctl enable --now snapd.socket
sudo ln -s /var/lib/snapd/snap /snap

在CentOS 7上安装Snap

通过添加epel存储库并安装copryum插件开始安装。

sudo yum install epel-release
sudo yum install yum-plugin-copr

然后添加仓库:

$sudo yum copr enable ngompa/snapcore-el7

Loaded plugins: copr, fastestmirror

You are about to enable a Copr repository. Please note that this
repository is not part of the main Fedora distribution, and quality
Jan vary.

The Fedora Project does not exercise any power over the contents of
this repository beyond the rules outlined in the Copr FAQ at
<https://fedorahosted.org/copr/wiki/UserDocs#WhatIcanbuildinCopr>, and
packages are not held to any quality or securty level.

Please do not file bug reports about these packages in Fedora
Bugzilla. In case of problems, contact the owner of this repository.

Do you want to continue? [y/N]: y
copr done

添加存储库后,请安装installsnapd软件包。

sudo yum -y install snapd

等待安装完成,然后启用napd套接字:

$sudo systemctl enable --now snapd.socket
Created symlink from /etc/systemd/system/sockets.target.wants/snapd.socket to /usr/lib/systemd/system/snapd.socket.

经典的限制要求在/snap'下捕捉或者从/snap到'/var/lib/snapd/snap的符号链接。为它创建一个符号链接,如下所示:

sudo ln -s /var/lib/snapd/snap /snap

Snapd现在可以使用了。我们可以使用snap命令与其进行交互。请参阅下面的帮助页面:

# snap --help
Usage:
  snap [OPTIONS] <command>

Install, configure, refresh and remove snap packages. Snaps are
'universal' packages that work across many different Linux systems,
enabling secure distribution of the latest apps and utilities for
cloud, servers, desktops and the internet of things.

This is the CLI for snapd, a background service that takes care of
snaps on the system. Start with 'snap list' to see installed snaps.

Application Options:
      --version  Print the version and exit

Help Options:
  -h, --help     Show this help message

Available commands:
  abort       Abort a pending change
  ack         Adds an assertion to the system
  alias       Sets up a manual alias
  aliases     Lists aliases in the system
  buy         Buys a snap
  changes     List system changes
  connect     Connects a plug to a slot
  disable     Disables a snap in the system
  disconnect  Disconnects a plug from a slot
  download    Downloads the given snap
  enable      Enables a snap in the system
  find        Finds packages to install (aliases: search)
  get         Prints configuration options
  help        Help
  info        show detailed information about a snap
  install     Installs a snap to the system
  interface   Lists snap interfaces
  interfaces  Lists interfaces in the system
  known       Shows known assertions of the provided type
  list        List installed snaps
  login       Authenticates on snapd and the store
  logout      Log out of the store
  logs        Retrieve logs of services
  pack        pack the given target dir as a snap
  prefer      Prefer aliases from a snap and disable conflicts
  refresh     Refreshes a snap in the system
  remove      Removes a snap from the system
  restart     Restart services
  revert      Reverts the given snap to the previous state
  run         Run the given snap command
  services    Query the status of services
  set         Changes configuration options
  start       Start services
  stop        Stop services
  switch      Switches snap to a different channel
  tasks       List a change's tasks (aliases: change)
  try         Tests a snap in the system
  unalias     Unalias a manual alias or an entire snap
  version     Shows version details
  watch       Watch a change in progress
  whoami      Prints the email the user is logged in with.

在CentOS 7/CentOS 8上安装Snap应用程序

我们使用" snap"命令行工具与Snap Store中可用的快照进行交互。

搜索快照:

要搜索快照,请使用

$snap find <search terms>

这将查询商店并列出结果及其版本号,开发人员名称和说明。

病了一个在CentOS 7服务器上安装Microsoft PowerShell自动化和配置管理平台的示例。

# snap search powershell
Name                Version          Developer             Notes    Summary
powershell-preview  6.1.0-preview.4  microsoft-powershell  classic  PowerShell for every system!
powershell          6.0.4            microsoft-powershell  classic  PowerShell for every system!

通过运行以下命令安装快照:

sudo yum install -y icu
sudo snap install powershell --classic

等待下载完成,应该花很短的时间才能完成。由于二进制文件位于/snap/bin /下,我们需要将其添加到$PATH变量中。

$sudo vim /etc/profile
export PATH="$PATH:/snap/bin/"

源文件以获取新路径

source /etc/profile

通过启动pwsh会话进行测试。

# pwsh 
PowerShell v6.0.4
Copyright (c) Microsoft Corporation. All rights reserved.

https://aka.ms/pscore6-docs
Type 'help' to get help.

PS /root>

列出已安装的快照:

# snap list
Name        Version    Rev   Developer             Notes
core        16-2.34.3  5145  canonical             core
powershell  6.0.4      8     microsoft-powershell  classic

通过运行"快照刷新"来手动更新快照

# snap refresh powershell 
snap "powershell" has no updates available

移除快照

快速打开快照,我们需要做的就是运行。snapremove &lt;snap name>在我们的例子中,只需执行以下操作:

# snap remove powershell
powershell removed

检查捕捉信息:

使用命令" snap info"来检查有关快照包的更多信息。

[theitroad@localhost ~]# snap info powershell
name:      powershell
summary:   PowerShell for every system!
publisher: microsoft-powershell
contact:   https://github.com/powershell/powershell
description: |
  PowerShell is an automation and configuration management platform.
  It consists of a cross-platform (Windows, Linux, and macOS)
  command-line shell and associated scripting language.
  
  See
  https://docs.microsoft.com/en-us/powershell/scripting/powershell-core-support
  for support details.
snap-id:     JSNnoJl3EqkMuWoy5Dgq8PMqZ0uNcpie
channels:              
  stable:    6.0.4 (8) 58MB classic
  candidate: 6.0.4 (8) 58MB classic
  beta:      6.0.4 (8) 58MB classic
  edge:      6.0.4 (8) 58MB classic

回滚到应用程序的先前版本

使用snap revert 快照还原

$sudo snap revert <snap name>