FreeBSD的两种软件包安装方式

1 概述

FreeBSD将许多系统工具捆绑作为基本系统的一部分。然而,可能还需要安装更多的第三方应用。FreeBSD提供了两种补充的技术,用来安装第三方软件:FreeBSD Ports套件(用于从源代码安装,二进制安装)以及packages(用以从预编译的二进制版本安装,类似于RPM或DEB)。这两种方法都可以用于从本地介质,或从网上直接安装。

2 软件安装预览

在UNIX系统,典型的第三方软件安装的步骤如下:

  1. 下载这个软件,软件的发行版可能是源代码格式,或是一个二进制包。
  2. 解开软件(其中代表性的是用 compress(1), gzip(1), 或 bzip2(1) 压缩过的tar包)。
  3. 阅读相关文档,了解如何安装。 (多半一个文件名是INSTALL或README, 或在doc/ 目录下的一些文档)
  4. 如果软件是以源代码形式发布的,那就需要编译它。可能需要编辑一个 Makefile文件, 或运行 configure脚本,和其他的一些工作。
  5. 测试和安装软件。

Packages和ports是互相依赖的。假设您想安装一个依赖于已经安装的特定库的应用程序。应用程序和那个库都已经应用于FreeBSD ports和packages。如果使用命令或ports系统来添加应用程序,两个都必须注意库是否被安装,如果没有,它会自动先安装库。

Package Benefits

  1. 一个压缩的package通常要比一个压缩的包含源代码的应用程序小得多。
  2. package不需要进行额外的编译。对于大型应用程序如Mozilla,KDE或GNOME来说这显得尤为重要,特别是在您的系统资源比较差的情况下。
  3. package不需要您知道如何在FreeBSD上编译软件的详细过程。

Ports Benefits

  1. package在编译时通常使用比较保守的选项,这是为了保证它们能够运行在大多数的系统上。通过从port安装,您可以细微调整编译选项来产生适合于处理器的代码。
  2. 一些软件包已经把与它们相关的能做和不能做的事情的选项都编译进去了。例如,Apache可能就配置了很多的选项。从port中安装时,您不一定要接受默认的选项,可以自己来设置。
  3. 在一些例子中,一个软件有不同的配置存在多个package。例如,Ghostscript存在ghostscript package和ghostscript-nox11 package两个配置package,这取决于您是否安装了X11服务器。这样的调整对package是可能的,但如果一个应用程序有超过一个或两个不同的编译时间选项时,就不行了。
  4. 一些软件的许可条件禁止采用二进制形式发行。它们必须带上源代码。
  5. 一些人不信任二进制发行形式。至少有了源代码,(理论上)可以亲自阅读它,寻找潜在的问题。
  6. 如果要自己对软件打补丁,就需要有源代码。
  7. 一些人喜欢整天围着源代码转,所以他们喜欢亲自阅读源代码,修改源代码等等。

2.1 一个package的安装

如果本地有安装包,可以直接安装,类似yum中的localinstall

root@FreeBSD:~ # ls
.cshrc			.k5login		.profile		.viminfo
.history		.login			.shrc			lsof-4.94.0,8.txz

root@FreeBSD:~ # pkg add lsof-4.94.0,8.txz 
Installing lsof-4.94.0,8...
Extracting lsof-4.94.0,8: 100%
=====
Message from lsof-4.94.0,8:

--
NOTE: Due to the way the FreeBSD build clusters make packages,
you may see a warning similar to the following:

lsof: WARNING: compiled for FreeBSD release 11.1-RELEASE-p6; this is 11.1-RELEASE-p4.

This is because the poudriere jails that build the packages don't have the  
corresponding kernel installed, so they don't know that the kernel version is different.

This warning can be ignored if you are using a system that is updated via freebsd-update
and are using pre-built packages.

root@FreeBSD:~ # pkg info lsof
lsof-4.94.0,8
Name           : lsof
Version        : 4.94.0,8
Installed on   : Thu Oct 14 20:19:09 2021 CST
Origin         : sysutils/lsof
Architecture   : FreeBSD:13:amd64
Prefix         : /usr/local
Categories     : sysutils
Licenses       : lsof
Maintainer     : ler@FreeBSD.org
WWW            : https://people.freebsd.org/~abe/
Comment        : Lists information about open files (similar to fstat(1))
Annotations    :
FreeBSD_version: 1300139
Flat size      : 235KiB
Description    :
Lsof (LiSt Open Files) lists information about files that are open by the
running processes.  An open file may be a regular file, a directory, a block
special file, a character special file, an executing text reference, a
library, a stream or a network file (Internet socket, NFS file or Unix domain
socket).

See also fstat(1) in the base system.

WWW: https://people.freebsd.org/~abe/

查找系统中软件包的位置

root@FreeBSD:~ # whereis lsof
lsof: /usr/local/sbin/lsof /usr/local/man/man8/lsof.8.gz

2.2 软件包管理

列出已安装的所有软件包列表和描述

root@FreeBSD:~ # pkg info
gettext-runtime-0.21           GNU gettext runtime libraries and programs
indexinfo-0.3.1                Utility to regenerate the GNU info page index
libffi-3.3_1                   Foreign Function Interface
mpdecimal-2.5.1                C/C++ arbitrary precision decimal floating point libraries
pkg-1.17.2                     Package manager
python38-3.8.12                Interpreted object-oriented programming language
readline-8.1.1                 Library for editing command lines as they are typed
tree-1.8.0                     Display a tree-view of directories with optional color or HTML output
vim-8.2.3458                   Improved version of the vi editor (console flavor)

统计安装包版本,并和仓库中的软件包做比较

root@FreeBSD:~ # pkg version
Updating FreeBSD repository catalogue...
Fetching meta.conf: 100%    163 B   0.2kB/s    00:01    
Fetching packagesite.pkg: 100%    6 MiB 111.6kB/s    01:00    
Processing entries: 100%
FreeBSD repository update completed. 31163 packages processed.
All repositories are up to date.
gettext-runtime-0.21               =
indexinfo-0.3.1                    =
libffi-3.3_1                       =
mpdecimal-2.5.1                    =
pkg-1.17.2                         =
python38-3.8.12                    =
readline-8.1.1                     =
tree-1.8.0                         =
vim-8.2.3458                       =

右边的符号包含的意思如下:

符号含义
=在本地ports树中与已安装的软件包版本相匹配。
<已安装的版本要比在ports树中的版本旧。
>已安装的版本要比在ports树中的版本新 (本地的port树可能没有更新)。
?已安装的软件包无法在ports索引中找到。 (可能发生这种事情,举个例子, 您早先安装的一个 port 从 port 树中移出或改名了)
*软件包有很多版本。
!已安装的软件包在索引中存有记录, 但是由于某些原因 pkg_version 无法比较已安装的软件包与索引中相对应的版本号。
软件仓库管理

FreeBSD的软件仓库配置文件是etc/pkg/FreeBSD.conf ,需要在里面修改仓库的链接,这里改成了国内的163的镜像站:

root@FreeBSD:~ # vim /etc/pkg/FreeBSD.conf 
# $FreeBSD$
#
# To disable this repository, instead of modifying or removing this file,
# create a /usr/local/etc/pkg/repos/FreeBSD.conf file:
#
#   mkdir -p /usr/local/etc/pkg/repos
#   echo "FreeBSD: { enabled: no }" > /usr/local/etc/pkg/repos/FreeBSD.conf
#

FreeBSD: {
  url: "pkg+http://mirrors.163.com/freebsd-pkg/$ABI/quarterly/",		#给出来的是仓库的路径,其中$ABI是获取的软件版本和架构,这里是FreeBSD:13:amd64,quarterly是一个子文件夹,代表每季度更新,还有latest
  mirror_type: "srv",
  signature_type: "fingerprints",
  fingerprints: "/usr/share/keys/pkg",
  enabled: yes
}
                                                                                

root@FreeBSD:~ # pkg update				#修改完成后需要更新
Updating FreeBSD repository catalogue...
Fetching meta.conf: 100%    163 B   0.2kB/s    00:01    
pkg: http://mirrors.163.com/freebsd-pkg/FreeBSD:13:amd64/quarterly//packagesite.pkg: Not Found
Fetching packagesite.txz: 100%    6 MiB 387.6kB/s    00:17    
Processing entries: 100%
FreeBSD repository update completed. 30749 packages processed.
All repositories are up to date.

安装软件包,测试是否更新成功

root@FreeBSD:~ # pkg install curl
Updating FreeBSD repository catalogue...
pkg: http://mirrors.163.com/freebsd-pkg/FreeBSD:13:amd64/quarterly//packagesite.pkg: Not Found		#使用的新镜像站
FreeBSD repository is up to date.
All repositories are up to date.
The following 3 package(s) will be affected (of 0 checked):

New packages to be INSTALLED:
	ca_root_nss: 3.63
	curl: 7.78.0
	libnghttp2: 1.43.0

Number of packages to be installed: 3

The process will require 5 MiB more space.
2 MiB to be downloaded.

Proceed with this action? [y/N]: y
[1/3] Fetching curl-7.78.0.txz: 100%    1 MiB 138.3kB/s    00:10    
[2/3] Fetching libnghttp2-1.43.0.txz: 100%  123 KiB  42.2kB/s    00:03    
[3/3] Fetching ca_root_nss-3.63.txz: 100%  277 KiB  56.7kB/s    00:05    
Checking integrity... done (0 conflicting)
[1/3] Installing libnghttp2-1.43.0...
[1/3] Extracting libnghttp2-1.43.0: 100%
[2/3] Installing ca_root_nss-3.63...
[2/3] Extracting ca_root_nss-3.63: 100%
[3/3] Installing curl-7.78.0...
[3/3] Extracting curl-7.78.0: 100%
=====
Message from ca_root_nss-3.63:

--
FreeBSD does not, and can not warrant that the certification authorities
whose certificates are included in this package have in any way been
audited for trustworthiness or RFC 3647 compliance.

Assessment and verification of trust is the complete responsibility of the
system administrator.


This package installs symlinks to support root certificates discovery by
default for software that uses OpenSSL.

This enables SSL Certificate Verification by client software without manual
intervention.

If you prefer to do this manually, replace the following symlinks with
either an empty file or your site-local certificate bundle.

  * /etc/ssl/cert.pem
  * /usr/local/etc/ssl/cert.pem
  * /usr/local/openssl/cert.pem

2.3 删除软件包

root@FreeBSD:~ # pkg delete tree
Checking integrity... done (0 conflicting)
Deinstallation has been requested for the following 1 packages (of 0 packages in the universe):

Installed packages to be REMOVED:
	tree: 1.8.0

Number of packages to be removed: 1

Proceed with deinstalling packages? [y/N]: y
[1/1] Deinstalling tree-1.8.0...
[1/1] Deleting files for tree-1.8.0: 100%

或者是模糊匹配:

root@FreeBSD:~ # pkg delete tre\*

2.4 其他操作

升级已安装的软件包

root@FreeBSD:~ # pkg upgrade
Updating FreeBSD repository catalogue...
pkg: http://mirrors.163.com/freebsd-pkg/FreeBSD:13:amd64/quarterly//meta.txz: No address record
repository FreeBSD has no meta file, using default settings
pkg: http://mirrors.163.com/freebsd-pkg/FreeBSD:13:amd64/quarterly//packagesite.pkg: No address record
FreeBSD repository is up to date.
All repositories are up to date.
Checking integrity... done (0 conflicting)
Your packages are up to date.

删除旧的包

root@FreeBSD:~ # pkg clean

自动删除未使用的包,但是可能会留下依赖包:

root@FreeBSD:~ # pkg autoremove

3 Ports Collection

Ports Collection是一组Makefile、补丁和描述文件。这些文件的每一组都用于在FreeBSD上编译和安装一个单独的应用程序,称为port。
默认情况下,Ports Collection本身存储在/usr/ports的子目录中。

3.1 获取ports collection

在使用ports之前,必须先获得Ports Collection──本质上是/usr/ports目录下的一堆Makefile、补丁和描述文件。
在安装FreeBSD系统的时候,sysinstall会询问您是否需要安装Ports Collection。如果您选择no,那可以用下面的指令来安装Ports Collection

  1. 下载压缩的Ports套件快照到/var/db/portsnap。可以根据需要在这之后关闭Internet连接。
root@FreeBSD:~ # portsnap fetch
Looking up portsnap.FreeBSD.org mirrors... 4 mirrors found.
Fetching snapshot tag from ipv4.aws.portsnap.freebsd.org... done.
Fetching snapshot metadata... done.
Fetching snapshot generated at Thu Oct 14 08:33:14 CST 2021:
268bb887434f04f10564ef8035047edbdc66b837315876          92 MB  547 kBps    00s
Extracting snapshot... done.
Verifying snapshot integrity... done.
Fetching snapshot tag from ipv4.aws.portsnap.freebsd.org... done.
Fetching snapshot metadata... done.
Updating from Thu Oct 14 08:33:14 CST 2021 to Thu Oct 14 21:09:47 CST 2021.
Fetching 5 metadata patches... done.
Applying metadata patches... done.
Fetching 0 metadata files... done.
Fetching 92 patches. 
(92/92) 100.00%  done.                                     
done.
Applying patches... 
done.
Fetching 3 new ports or files... done.
  1. 假如您是首次运行Portsnap, 则需要将快照释放到 /usr/ports:
root@FreeBSD:~ # portsnap extract
...
/usr/ports/x11/yakuake/
/usr/ports/x11/yalias/
/usr/ports/x11/yeahconsole/
/usr/ports/x11/yelp/
/usr/ports/x11/zenity/
Building new INDEX files... done.
  1. 如果有装好的/usr/ports而只想更新,则执行下面的命令:
root@FreeBSD:~ # portsnap update

3.2 安装ports

root@FreeBSD:~ # cd /usr/ports/sysutils/lsof/
root@FreeBSD:/usr/ports/sysutils/lsof # ls
Makefile	files		pkg-message
distinfo	pkg-descr	pkg-plist

当提到Ports Collection时,第一个要说明的就是何谓"skeleton"。简单地说,port skeleton是让一个程序在FreeBSD上简洁地编译并安装的所需文件的最小组合。每个port skeleton包含:

  • Makefile。Makefile包括好几个部分,指出应用程序是如何编译以及将被安装在系统的哪些地方。
  • distinfo文件。这个文件包括这些信息:这些文件用来对下载后的文件校验和进行检查(使用sha256),来确保在下载过程中文件没有被破坏。
  • files目录。这个目录包括在FreeBSD系统上编译和安装程序需要用到的补丁。这些补丁基本上都是些小文件,指出特定文件作了哪些修正。它们都是纯文本的的格式,基本上是这样的"删除第10行"或"将第26行改为这样…​",补丁文件也被称作"diffs",他们由diff程序生成。

这个目录也包含了在编译port时要用到的其它文件。

  • pkg-descr文件。这是一个提供更多细节,有软件的多行描述。
  • 个pkg-plist文件。这是即将被安装的所有文件的列表。它告诉ports系统在卸载时需要删除哪些文件。

一些ports还有些其它的文件,例如pkg-message。ports系统在一些特殊情况下会用到这些文件。
port里面包含着如何编译源代码的指令,但不包含真正的源代码。您可以在网上或CD-ROM上获得源代码。源代码可能被开发者发布成任何格式。一般来说应该是一个被tar和gzip过的文件,或者是被一些其他的工具压缩或未压缩的文件。ports中这个程序源代码标示文件叫"distfile", 安装 FreeBSD port的方法还不止这两种。

  1. 一旦进入了软件的目录,您将会看到这个port的结构。下一步就是make,或说"联编"这个port。只需在命令行简单地输入make命令就可轻松完成这一工作。做好之后,您可以看到下面的信息:
root@FreeBSD:/usr/ports/sysutils/neofetch # make
  1. 编译完成会回到命令行。 下一步安装 port,要安装它只需要在 make 命令后跟上一个单词install即可:
root@FreeBSD:/usr/ports/sysutils/neofetch # make install
...
cut.c:89:15: note: use '==' to turn this assignment into an equality comparison
  while (ltok = strsep (&larg, ","))
              ^
              ==
1 warning generated.
cc -shared -Wl,-soname,cut  -Wl,-export-dynamic -L/usr/local/lib -fstack-protector-strong  -o cut cut.o 
mkdir -p -- /usr/ports/shells/bash/work/stage/usr/local/lib/bash
mkdir -p -- /usr/ports/shells/bash/work/stage/usr/local/include/bash
mkdir -p -- /usr/ports/shells/bash/work/stage/usr/local/include/bash/builtins
mkdir -p -- /usr/ports/shells/bash/work/stage/usr/local/include/bash/include
install  -m 0644 ./support/bash.pc /usr/ports/shells/bash/work/stage/usr/local/libdata/pkgconfig/bash.pc
installing example loadable builtins in /usr/ports/shells/bash/work/stage/usr/local/lib/bash
print
truefalse
sleep
finfo
logname
basename
dirname
fdflags
tty
pathchk
tee
head
mkdir
rmdir
mkfifo
mktemp
printenv
id
whoami
uname
sync
push
ln
unlink
realpath
strftime
mypid
setpgid
seq
rm
accept
csv
cut
/bin/ln -sf bash /usr/ports/shells/bash/work/stage/usr/local/bin/rbash
install  -m 0644 /usr/ports/shells/bash/work/bash-5.1/doc/FAQ  /usr/ports/shells/bash/work/bash-5.1/doc/INTRO /usr/ports/shells/bash/work/stage/usr/local/share/doc/bash
for d in CHANGES COMPAT NEWS POSIX RBASH README; do  install  -m 0644 /usr/ports/shells/bash/work/bash-5.1/${d} /usr/ports/shells/bash/work/stage/usr/local/share/doc/bash;  done
====> Compressing man pages (compress-man)
===>  Installing for bash-5.1.8
===>  Checking if bash is already installed
===>   Registering installation for bash-5.1.8 as automatic
Installing bash-5.1.8...
===> SECURITY REPORT: 
      This port has installed the following files which may act as network
      servers and may therefore pose a remote security risk to the system.
/usr/local/lib/bash/accept

      If there are vulnerabilities in these programs there may be a security
      risk to the system. FreeBSD makes no guarantee about the security of
      ports included in the Ports Collection. Please type 'make deinstall'
      to deinstall the port if this is a concern.

      For more information, and contact details about the security
      status of this software, see the following webpage: 
https://www.gnu.org/software/bash/
===>   neofetch-7.1.0 depends on executable: bash - found
===>   Returning to build of neofetch-7.1.0
===>   Generating temporary packing list
install  -m 0644 /usr/ports/sysutils/neofetch/work/neofetch-7.1.0/README.md /usr/ports/sysutils/neofetch/work/stage/usr/local/share/doc/neofetch
====> Compressing man pages (compress-man)
===>  Installing for neofetch-7.1.0
===>  Checking if neofetch is already installed
===>   Registering installation for neofetch-7.1.0
Installing neofetch-7.1.0...
  1. 一旦返回到提示符,您就可以运行您刚刚安装的程序了。在编译和安装ports的时候,您应该留意任何出现的警告。

删除工作目录是个好主意,这个目录中包含了全部在编译过程中用到的临时文件。这些文件不仅会占用宝贵的磁盘空间,而且可能会给升级新版本的port时带来麻烦。

root@FreeBSD:/usr/ports/sysutils/neofetch # make clean
===>  Cleaning for bash-5.1.8
===>  Cleaning for bison-3.7.6,1
===>  Cleaning for m4-1.4.19,1
===>  Cleaning for libiconv-1.16
===>  Cleaning for texinfo-6.8,1
===>  Cleaning for help2man-1.48.5
===>  Cleaning for p5-Locale-gettext-1.07
===>  Cleaning for gettext-tools-0.21
===>  Cleaning for libtextstyle-0.21
===>  Cleaning for perl5-5.32.1_1
===>  Cleaning for gmake-4.3_2
===>  Cleaning for p5-Locale-libintl-1.32
===>  Cleaning for p5-Text-Unidecode-1.30
===>  Cleaning for p5-Unicode-EastAsianWidth-12.0
===>  Cleaning for neofetch-7.1.0
搜索

如果不知道安装包的位置,可以在/usr/ports目录下进行搜索

root@FreeBSD:/usr/ports # make search name=nmap-7.91_1
Port:	nmap-7.91_1
Path:	/usr/ports/security/nmap
Info:	Port scanning utility for large networks
Maint:	cy@FreeBSD.org
B-deps:	gettext-runtime-0.21 gmake-4.3_2 indexinfo-0.3.1 libiconv-1.16 pcre-8.45
R-deps:	pcre-8.45
WWW:	https://nmap.org/
卸载已经安装的Ports

Ports可以同packages以完全相同的方式卸载, 方法是使用pkg delete命令

root@FreeBSD:~ # pkg delete neofetch

也可以使用make的方式卸载

root@FreeBSD:~ # cd /usr/ports/sysutils/neofetch
root@FreeBSD:/usr/ports/sysutils/neofetch # make deinstall
升级Ports

首先,使用pkg version命令来列出Ports Collection中提供了更新版本的那些port:

root@FreeBSD:~ # pkg version -v
ports包的获取地址

服务器列表保存在/var/db/portsnap/serverlist中:

root@FreeBSD13:~ # cat /var/db/portsnap/serverlist
1 10 ipv4.aws.portsnap.freebsd.org
2 10 ipv6.aws.portsnap.freebsd.org
3 10 metapeer.portsnap.freebsd.org
3 10 your-org.portsnap.freebsd.org
  • 25
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

-风中叮铃-

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值