FreeBSD 12.1 Handbook Notes Part 1 Getting Started

2023-09-24 26 0

Part 1. Getting Started

Chapter 2. Installing FreeBSD

  1. FreeBSD的镜像分为多种,如:CD的、DVD的、memstick。用于U盘的,是memstick.img的。
        Using dd to write the image: # dd if=FreeBSD-12.1-RELEASE-amd64-memstick.img of=/dev/sdb bs=1M conv=sync
        If this command fails, verify that the USB stick is not mounted and that the device name is for the disk, not a partition. Some operating systems might require this command to be run with sudo. The dd syntax varies slightly across different platforms; for example, Mac OS requires a lower-case bs=1m. Systems like Linux might buffer writes. To force all writes to complete, use sync.
  2. FreeBSD Boot Loader Menu,按数字键6可进入Boot Options,Toggle “Verbose” On,可以在启动过程中显示出详细信息。
  3. When laying out file systems, remember that hard drives transfer data faster from the outer tracks to the inner. Thus, smaller and heavier-accessed file systems should be closer to the outside of the drive, while larger partitions like /usr should be placed toward the inner parts of the disk. It is a good idea to create partitions in an order similar to: /, swap, /var, and /usr.
  4. As a rule of thumb, the swap partition should be about double the size of physical memory (RAM).
  5. On larger systems with multiple SCSI disks or multiple IDE disks operating on different controllers, it is recommended that swap be configured on each drive, up to four drives. The swap partitions should be approximately the same size. The kernel can handle arbitrary sizes but internal data structures scale to 4 times the largest swap partition. Keeping the swap partitions near the same size will allow the kernel to optimally stripe swap space across disks. Large swap sizes are fine, even if swap is not used much. It might be easier to recover from a runaway program before being forced to reboot.
  6. A standard FreeBSD GPT installation uses at least three partitions: freebsd-boot - Holds the FreeBSD boot code; freebsd-ufs - A FreeBSD UFS file system; freebsd-swap - FreeBSD swap space. Another partition type worth noting is freebsd-zfs, used for partitions that will contain a FreeBSD ZFS file system.
  7. Proper sector alignment provides the best performance, and making partition sizes even multiples of 4K bytes helps to ensure alignment on drives with either 512-byte or 4K-byte sectors. Generally, using partition sizes that are even multiples of 1M or 1G is the easiest way to make sure every partition starts at an even multiple of 4K. There is one exception: the freebsd-boot partition should be no larger than 512K due to current boot code limitations.
  8. system clock / machine’s CMOS clock :set to UTC.
  9. Username: A common convention is to use the first letter of the first name combined with the last name, as long as each username is unique for the system. The username is case sensitive and should not contain any spaces.
  10. Arch的useradd的"-c, --comment COMMENT",就可以用来设置user’s full name,可为任意字符串,可包含空格。Arch的useradd不加-m选项就不会为新用户创建login directory家目录,具体描述参考Arch的$ man useradd、/etc/defaults/useradd、/etc/login.defs。
  11. 若user needs administrative access,则user需被加入wheel组。

Chapter 3. FreeBSD Basics

  1. FreeBSD将ttyv0当作"system console",“system messages are configured by default to display on the system console.”(例如,在text-based界面下,先在ttyv0登录,然后Alt+F3,切换到ttyv2并用root登录,则ttyv0 也就是system console 中就会输出一条root在ttyv2登录了的消息),ttyv1~ttyv7为"virtual console"。switching between virtual consoles,用Alt+F1;When using Xorg as a graphical console, the combination becomes Ctrl+Alt+F1 to return to a text-based virtual console。
  2. Note that the last virtual console (ttyv8) is used to access the graphical environment if Xorg has been installed and configured as described in “Chapter 5, The X Window System” (配置xdm时).
  3. /etc/ttys,如果将某个ttyv的“status”中的“secure”去掉,则root不能在此ttyv登录。如果将console这行的secure改为insecure,则进入单用户模式需要root密码。
    Note: If the root password is forgotten, booting into single user mode is still possible, but may be difficult for someone who is not familiar with the FreeBSD booting process.
    那么忘了root密码,如何进single user mode?可能在handbook Part Ⅲ有讲?
  4. vidcontrol - system console control and configuration utility。可以调很多console设置,但是我是在虚拟机,只换了换foreground和background,video mode调不了,显示不出可用的mode……调了的设置要想每次在开机时启用,可写到/etc/rc.conf。
  5. FreeBSD允许多用户在同一时间使用电脑,While only one user can sit in front of the screen and use the keyboard at any one time, any number of users can log in to the system through the network。
  6. FreeBSD System Accounts:daemon、operator、bind、news、ww等等,The reason for this is security; if all services ran as the superuser, they could act without restriction。The superuser account,usually called root。
  7. Login class:Login classes are an extension to the group mechanism that provide additional flexibility when tailoring the system to different users.
  8. "Always double and triple-check any commands issued as the superuser, since an extra space or missing character can mean irreparable ([ɪˈrepərəbl]) data loss."
  9. FreeBSD中root用户的primary group是wheel,而在Linux中是root。
  10. 要想使用su命令转到root,用户必须是wheel的一员 (“In particular, by default only users in the ‘wheel’ group can switch to UID 0 (‘root’)”)。而上一篇记录Arch的笔记中曾提到,Arch Wiki说su、sudo的实现均不需要用户处于wheel组。可看FreeBSD和Arch的/etc/pam.d/su,内容是有差别的,有需要的话,可以进行修改。
  11. 使用su从当前普通用户转换到另一个普通用户,$USER、$HOME、$SHELL会改变,其他环境变量不改变。例如,现在是vanellope用户,环境中有个变量是QWE,那么su liberty后:$PWD、$QWE保持不变,$USER变为liberty;但若执行的是su - liberty,那么$USER仍会变为liberty,但是$QWE就没有了,$PWD会变为liberty的家目录。
        而当从普通用户su到root时:例如,现在是vanellope,执行su root后:$PWD不变,$USER不变;若执行的是su - root:$PWD变为root家目录,$USER变为root。(对比一下,su到普通用户时,$USER会变,而su到root时,只有加’-’,$USER才会变。)
        在man su中,“By default, the environment is unmodified with the exception of USER, HOME and SHELL. HOME and SHELL are set to the target login’s default values.USER is set to the target login, unless the target login has a user ID of 0, in which case it is unmodified.”,
        $ su -$ su -l一样(在Linux中是$ su --login == $ su -l == $ su -),“Simulate a full login. The environment is discarded except for HOME, SHELL, PATH, TERM, and USER.”,在Arch中是这样描述的:“Start the shell as a login shell with an environment similar to a real login: clears all the environment variables except TERM and variables specified by --whitelist-environment;initializes the environment variables HOME, SHELL, USER, LOGNAME, and PATH;changes to the target user’s home directory;sets argv[0] of the shell to ‘-’ in order to make the shell a login shell”。“It is recommended to always use the --login option (instead of its shortcut - ) to avoid side effects caused by mixing environments.”(这个不知道为什么,是因为’–login’比’-'的意思更清晰?二者的结果是一样的呀)
        FreeBSD和Arch的有些options不一样。推荐一个由ArchWiki管理团队创建并维护的Arch manual pages
  12. 在Handbook中,“If - is specified when running this command, the user will also inherit the root user’s environment”,这个inherit,就是上条说的 discarded自己的环境,然后用root用户的环境。(有错误理解请帮忙指正)
  13. sudo:An alternative is to install the security/sudo package or port. This software provides activity logging and allows the administrator to configure which users can run which commands as the superuser.
  14. FreeBSD的adduser和rmuser都是interactive mode;Arch的是useradd -m username、userdel -r username。被删除用户的除了家目录、邮箱之外的文件,需要手动删除。
  15. chpass:可以修改用户信息。当执行者是regular user时,只能修改shell、全名、工作地点电话等等;当执行者是superuser时,还可以改home、uid、gid、密码和帐户时效等等。
  16. pw可以创建、删除、修改、显示用户和组的信息,pw useradd、pw usermod、pw groupadd、pw groupmod等等,pw groupmod groupname -M memberlist:将groupname的成员替换为memberlist,如果不想替换,只想加就是-m,只减就是-d。还有很多很多command和option,用时再看吧。Arch有usermod - modify a user account,可以用来修改一些用户信息。给用户添加supplementary groups可以用usermod -aG group1,group2... username,删除组就用-G group1,group2,group3… username,想保留哪些组,就写上哪些组,不写的组就会被删除,但是 不写primary group,也不会把用户从primary group中删除。用usermod -g可以修改用户的primary group。
  17. 对一个目录有rx,则可以正常cd进去并ls查看目录下的东西;若只有r,则只可以ls看到目录下的文件名和类型;若只有x,则可以cd进目录,但是不能ls看到目录下有哪些东西。
  18. 上一篇笔记摘抄过Linux的man chmod。这次看到FreeBSD的man chmod,觉得还有必要再摘抄一下:(感觉FreeBSD这儿没说全,可以将linux和FreeBSD的man chmod结合起来看)
        chmod -R, change the modes of the file hierarchies rooted in the files, instead of just the files themselves. Beware of unintentionally matching the ‘. .’ hard link to the parent directory when using wildcards like ‘.*’ ———— 比如说,想对pp目录下的目录名符合‘.*’的目录进行-R式修改权限,这时就会把‘. .’,也就是把pp的parent dir也进行了修改 (如果对pp的parent dir有足够权限的话)。所以要小心!
        Modes may be absolute or symbolic. An absolute mode is an octal number constructed from the sum of one or more of the following values:
        4000:(the setuid bit). Executable files with this bit set will run with effective uid set to the uid of the file owner. Directories with this bit set will force all files and subdirectories created in them to be owned by the directory owner and not by the uid of the creating process, …
        2000:(the setgid bit). Executable files with this bit set will run with effective gid set to the gid of the file owner.
        1000:(the sticky bit). A directory whose ‘sticky bit’ is set becomes an append-only directory, or, more accurately, a directory in which the deletion of files is restricted. A file in a sticky directory may only be removed or renamed by a user if the user has write permission for the directory and the user is the owner of the file, the owner of the directory, or the super-user. This feature is usefully applied to directories such as /tmp which must be publicly writable but should deny users the license to arbitrarily delete or rename each others’ files.
        s: The set-user-ID-on-execution and set-group-ID-on-execution bits.
        X: The execute/search bits if the file is a directory or any of the execute/search bits are set in the original (unmodified) mode. Operations with the perm symbol ‘X’ are only meaningful in conjunction with the op symbol ‘+’, and are ignored in all other cases.
        上次看到过的real user ID和effective user ID:The real user ID is the UID who owns or starts the process. The effective UID is the user ID the process runs as. As an example, passwd runs with the real userID when a user changes their password. However, in order to update the password database, the command runs as the effective ID of the root user. This allows users to change their passwords without seeing a Permission Denied error.
            2) 在使用chmod这类命令对’.‘开头的文件目录进行权限更改时,比如现在想改hh目录下的’.'开头的文件的权限,$ chmod g+w hh/.*,因为hh目录下包含.和. .目录,如果用户拥有hh目录,那么hh的权限也会被更改,不只是更改了hh下“.”开头的文件的权限;如果用户还拥有目录“. .”,那么hh的父目录的权限也会被更改。所以使用时要注意".*"的用法,如果用户只是拥有目录下“.”开头的普通文件和目录,那么当前目录和当前目录的父目录的权限则不会受到影响。(shell的“*”不会匹配文件目录名开头的“.”,“.*”才能匹配到开头是“.”的文件/目录名,但这也会包括“.”和“. .”。
  19. chflags: In addition to file permissions, FreeBSD supports the use of “file flags”. These flags add an additional level of security and control over files, but not directories. With file flags, even root can be prevented from removing or altering files.
  20. “Note: In these examples, even though the shell script in question is an executable file, it will not run with a different EUID or effective user ID. This is because shell scripts may not access the setuid(2) system calls.”
  21. A mount point is a directory where additional file systems can be grafted onto a parent file system (usually the root file system). Standard mount points include /usr/, /var/, /tmp/, /mnt/, and /cdrom/.
  22. # man hier :A complete description of the file system hierarchy is available in hier(7). 有时间的话应该好好看看man hier。(也看看linux的man hier)
  23. 为什么会有 /usr/home/ 这个文件夹? 看了一下,在FreeBSD中,/home/是link到/usr/home/的,而Arch中 /home/不是link;在FreeBSD中,/bin/ (user utilities fundamental to both single-user and multi-user environments)、/sbin/ (system programs and administration utilities fundamental to both single-user and multi-user environments)、/lib/ (critical system libraries needed for binaries in /bin and /sbin) 就是正常dir;而在Arch中,/bin/和/sbin/都是link到/usr/bin/的,/lib/和/lib64/是link到/usr/lib/的。
  24. motd :快速清空文件 % : > filename (%是FreeBSD普通用户的命令提示符)
  25. motd :“You can press Ctrl-L while in the shell to clear the screen”,或者执行clear也能清屏。
  26. 假定现在有三个文件系统,是A、B、C,每个文件系统都有一个root目录,每个root下又有两个目录,分别为A1和A2、B1和B2、C1和C2。然后把文件系统B挂载到A1,此时文件系统B的根目录就会取代A1,B1、B2中的东西就可以通过路径/A1/B1、/A1/B2获取,而/A1下原有的内容则会暂时隐藏,待B从A unmount后,会重新显现。
  27. 安装FreeBSD时可以只使用一个很大的分区(根分区/),而不划分其他分区(如/boot、/home、/usr、/var),但是不推荐这样做,因为有些缺点(列举了3个缺点、1个优点),具体看Handbook 3.6节。
        那“1个优点”应该是指:安装FreeBSD时只用一个根分区/的话,若后续使用中发现分区容量需要扩展时,只需要对这一个根分区扩展就行。(但是必须经过如下步骤:备份数据、以新容量扩展分区并创建文件系统、恢复数据)(然鹅,FreeBSD搞了一个命令:growfs - expand an existing UFS file system,which makes it possible to increase the size of file system on the fly, removing this limitation. 也就是可以在线扩展UFS文件系统,无需备份数据等那些操作,所以即使有多个分区,那只要对相应的分区进行在线扩展就好了,意味着那“1个优点”也算不上什么优点了。)
  28. FreeBSD中的区(partitions)的用词:假如现在有一块MBR的disk,根据MBR,这个disk上最多有4个primary。然后现在在此disk上划出3个primary区,这叫作slices,然后每个slice里又可以有partitions (比如slice1里可以装Windows,slice1里划分几个partitions作为CDEF盘,slice2、slice3可以装FreeBSD,每个slice可以划分出partitions,装FreeBSD需要的文件系统),partitions标号从a到h,然后每个partition只能包含一个file system或者swap。i.e. “ada1s3e” is the fifth partition in the third slice of the second SATA disk drive.",ada一般表示硬盘为SATA型,1表示第二块硬盘,s3表示第三个slice,e表示第五个partition。
  29. 接上条,体会FreeBSD(UNIX)的partition和dos的partition。Handbook中举了个栗子Example 3.13 Conceptual Model of a disk:假定现在电脑上挂着的disks的第一块是250G的SATA disk,上面有两个slice,第一个slice是170GB (MS-DOS partitions),装着Windows NTFS file system , C:,第二个slice是装着FreeBSD,有四个data partitions and a swap partition。见图,图更直观:Example3.13ConceptualModelOfaDisk
        Partition letter c refers to the entire slice, and so is not used for ordinary partitions. Partition letter a一般指root file system所在分区,b 是 swap,d 以前有特殊含义 现在没了,和efgh正常使用。
  30. Daemons: Some programs are not designed to be run with continuous user input and disconnect from the terminal at the first opportunity. For example, a web server responds to web requests, rather than user input. Mail servers are another example of this type of application.
        (These types of programs are known as daemons. The term daemon comes from Greek mythology and represents an entity that is neither good nor evil, and which invisibly performs useful tasks. This is why the BSD mascot is the cheerful-looking daemon with sneakers and a pitchfork.)
        There is a convention to name programs that normally run as daemons with a trailing “d”.比如httpd、sshd等。This is only a naming convention.
  31. ps命令:To display a static list of … : PIDs are assigned starting at 1, go up to 99999, then wrap around back to beginning. The TT column show the tty the program is running on and STAT shows the program’s state. TIME is the amount of time the program has been running on the CPU. This is usually not the elapsed time since the program was started, as most programs spend a lot of time waiting for things to happen before they need to spend time on the CPU. Finally, COMMAND is the command that was used to start the program.
        最常用的选项set之一就是 auxww, where ‘a’ displays information about all the running processes of all users, ‘u’ displays the username and memory usage of the process’ owner, ‘x’ displays information about daemon processes, and ‘ww’ causes ps to display the full command line for each process, rather than truncating it once it gets too long to fit on the screen.
  32. top命令:To display all the running processes and update the display every few seconds in order to interactively see what the computer is doing. 其输出分为两部分:第一部分是最开始的五六行,The header shows the PID of the last process to run (因为是动态显示,所以可看到系统中最后一个运行的程序的PID,每当一个新的进程运行,其PID就可作为last pid,可看到last pid的显示会更新), the system load averages (which are a measure of how busy the system is), …, and how much time the system is spending in different CPU states. If the ZFS file system module has been loaded, an ARC line indicates how much data was read from the memory cache instead of from disk.
        By default, top also displays the amount of memory space taken by the process. This is split into two columns: one for total size and one for resident size. Total size is how much memory the application has needed and the resident size is how much it is actually using now.
    top automatically updates the display every two seconds. A different interval can be specified with -s.
  33. Killing Processes: Two signals can be used to stop a process: SIGTERM and SIGKILL.
        SIGTERM is the polite way to kill a process as the process can read the signal, close any log files it may have open, and attempt to finish what it is doing before shutting down. In some cases, a process may ignore SIGTERM if it is in the middle of some task that cannot be interrupted.
        SIGKILL cannot be ignored by a process. Sending a SIGKILL to a process will usually stop that process there and then. (确实有些任务暂时不能中断,只有当任务变成可以中断了,进程才能被killed。栗子看FreeBSD 12.1 Handbook P78 页面最下方的注释。)
  34. Procedure 3.1. Sending a Signal to a Process 举了一个栗子,它用kill给进程发送信号,但用的是/bin/kill。“Why use /bin/kill ? Many shells provide kill as a built in command, meaning that the shell will send the signal directly, rather than running /bin/kill . Be aware that different shells have a different syntax for specifying the name of the signal to send. Rather than try to learn all of them, it can be simpler to specify /bin/kill .”
  35. Arch中,ls和grep要显示颜色的话,是用"–color=auto",而FreeBSD里grep还是"–color=auto",而ls要用"-G",或者设置变量CLICOLOR或COLORTERM;命令source是csh、bash的shell built-in command,sh没有它。
  36. 在FreeBSD里,发现delete键不能用,一按就是输出’~’,这就需要设置一下,在csh配置文件里写入bindkey "^[[3~" delete-char,然后source一下使设置生效。命令bindkey、bindkey -l可以看到更多可用的设置。(有时间的话看看man csh)(bash是bind)
  37. 设置环境变量:在sh、bash中设置环境变量 与 在tcsh、csh中设置的区别:前者是export EDITOR="/usr/local/bin/emacs",后者是setenv EDITOR /usr/local/bin/emacs
  38. 在linux的tty下,屏幕翻页是shift+page up/page down;在FreeBSD的ttyv下是,先按下Scroll lock键,然后就可以方向键↑/↓滚动查看屏幕,看完以后想要继续敲命令,再按下Scroll lock键。
  39. When using ZFS as the file system the “df” command is reporting the pool size and not file system sizes. It also does not know about descendent ZFS datasets, snapshots, quotas, and reservations with their individual space usage. Use the built-in “zfs list” command to get a better overview of space usage: # zfs list -o space.

Chapter 4. Installing Applications: Packages and Ports

  1. 升级已安装软件:# pkg upgrade;列出所有已安装软件:# pkg info,查看某个已安装软件的信息:# pkg info pkgname;删除软件:# pkg delete pkgname;检测已安装的软件的vulnerabilities:# pkg audit -F# pkg clean可删除/var/cache/pkg目录下过时的二进制包,保留下载的最新版本的二进制包,在这条命令后添加-a选项,则是直接清空/var/cache/pkg目录。
  2. 自动删除无用的包:# pkg autoremove;the packages that were explicity installed not as a dependency to another package, can be listed using:# pkg prime-list,所列出的包的源可用# pkg prime-origins查看;Marking an installed package as automatic can be done using: # pkg set -A 1 devel/cmake, Once a package is a leaf package and is marked as automatic, it gets selected by pkg autoremove; Marking an installed package as not automatic can be done using: # pkg set -A 0 devel/cmake
  3. The Ports Collection is a set of Makefiles, patches, and description files. Each set of these files is used to compile and install an individual application on FreeBSD, and is called a port.
  4. 如果安装FreeBSD的过程中没有安装The Ports Collection,那么FreeBSD安装完成后,有两种方法安装The Ports Collection:
        1)Portsnap Method:首先下载压缩的The Ports Collection的snapshot到/var/db/portsnap : # portsnap fetch,然后extract the snapshot into /usr/ports : # portsnap extract,这两步可以合起来:# portsnap fetch extract。这便完成了portsnap的第一次使用,等以后需要更新/usr/ports的时候,执行# portsnap fetch update,也可以分开写成两条命令。
        2)Subversion Method : If more control over the ports tree is needed or if local changes need to be maintained, Subversion can be used to obtain the Ports Collection. Subversion的安装也有两种方法,如果系统已经有了/usr/ports (比如FreeBSD安装时安装了Ports),则# cd /usr/ports/devel/subversion# make install clean;若没有安装ports,则用pkg,# pkg install subversion。Subversion安装完成后,check out a copy of the ports tree: # svn checkout /usr/ports,等之后使用时需要更新/usr/ports : # svn update /usr/ports
  5. /usr/ports/下是软件类别,每个类别目录下才是相应的软件,i.e. /usr/ports/games/xonotic/,就是游戏类别下有个游戏xonotic(第一人称射击,可以单机玩,挺好)。然后每个软件的目录内又包含一些文件,称为"ports skeleton",每个port skeleton一般包含以下文件和目录:
        Makefile: contains statements that specify how the application should be compiled and where its components should be installed.
        distinfo: contains the names and checksums of the files that must be downloaded to build the port.
        files/: this directory contains any patches needed for the program to compile and install on FreeBSD. This directory may also contain other files used to build the port.
        pkg-descr: provides a more detailed description of the program.
        pkg-plist: a list of all the files that will be installed by the port. It also tells the ports system which files to remove upon deinstallation.
        The port does not include the actual source code, also known as a distfile. The extract portion of building a port will automatically save the downloaded source to /usr/ports/distfiles.
  6. (4.5.1. Installing Ports)用ports安装软件:(Using the Ports Collection assumes a working Internet connection. It also requires superuser privilege.)To compile and install the port, change to the directory of the port to be installed, then type make install at the prompt: # cd /usr/ports/sysutils/lsof, # make install clean
        因为一些shell会有命令缓存,所以新安装port以后,在tcsh中可以执行% rehash,这样新装的命令在执行时就不必输入full path。(sh shell用% hash -r
  7. ( Customizing Ports Installation) 一些ports会提供编译选项,然后可能它依赖的ports也会提供编译选项,这样在安装时就会中途暂停好几次以让用户选择编译选项,但是可以在the port skeleton (要安装的软件的ports目录 /usr/ports/xxx/xxx/)中执行# make config-recursive,to do all of the configuration in one batch, 然后再# make install clean
    make config、make showconfig、make rmconfig 可以用来增加、删除或者改变已安装软件的build options。
        ports安装时使用fetch来下载源文件,它支持各自环境变量。对于那些不能一直联网的用户,# make fetch can be run within /usr/ports, to fetch all distfiles, or within a category, or within the specific port skeleton. (应该就是 因为联网时间可能不长,然后用户可以根据需要用fetch下载所需distfiles————要么是全部ports的distfiles,要么是某个类别的distfiles,要么是某个软件的distfiles,然后进行安装) 但是当在category和ports skeleton中执行fetch时,如果要安装的软件有依赖在其他categories,则feth不会从其他categories下载所需distfiles,这可以用make fetch-recursive来解决。
        如果fetch时有指定的local distfiles repository,可以用变量进行指定: # make MASTER_SITE_OVERRIDE=xxxxxxxxx fetch (在/usr/ports/下、在category下、在ports skeleton下应该都可以指定);还可以通过变量改变 ports安装时进行编译等工作的目录 和 ports被安装的目录:# make WRKDIRPREFIX=/usr/home/example/ports install will compile the port in /usr/home/example/ports and install everything under /usr/local, # make PREFIX=/usr/home/example/local install will compile the port in /usr/ports and install it in /usr/home/example/local. And: # make WRKDIRPREFIX=../ports PREFIX=../local install will combine the two. 如果这些变量经常用,可以设置为shell环境变量,不用每次写。
        所以,一般用ports装软件时,可以酱紫:# make config-recursive# make install clean
  8. 删除已安装的ports,可以用pkg delete; alternately, make deinstall can be run in the port’s directory: # cd /usr/ports/sysutils/lsofmake deinstall。最好在port被卸载时看一看输出的信息,因为如果有其他已安装软件依赖于被卸载软件,那么输出中会显示信息,但是卸载过程仍将继续,所以这时还得重新安装已卸载的软件,不然就会break dependencies。
  9. 升级ports:首先要更新ports tree,上面的第4条讲了两种安装the Ports Collection的方法:portsnap和svn,这俩也可以用来更新ports tree。
        如果安装FreeBSD时没有安装The Ports Collection,然后FreeBSD安装完成后用portsnap或者svn安装了The Ports Collection,那么要更新ports tree的话:portsnap 先fetch然后update;svn 先checkout然后update。
        如果安装FreeBSD时已经安装了The Ports Collection,且现在是第一次更新ports tree,那么用portsnap的话,得先fetch,然后extract,再update。因为如果ports tree是用portsnap维护的,那么/usr/ports下会有一个.portsnap.INDEX文件,而安装FreeBSD时安装的/usr/ports下没有这个portsnap的索引文件,所以不能直接fetch后update,要extract后才能update。等以后再次用portsnap更新ports tree时,就可以portsnap fetch后update了,此时执行update后,如果有新变化需要加入本地ports tree,update就会将新目录和文件extract到相应位置。
        如果安装FreeBSD时已经安装了The Ports Collection,且现在是第一次更新ports tree,那么用svn的话(得先安装上svn),就是先chekout,然后update。用svn更新过的/usr/ports下会有一个.svn目录。
        综上,升级ports tree时,svn随便用,而用portsnap,则要求ports tree目录下有portsnap的INDEX文件。(如果之前不是用portsnap,然后现在想用,必须得执行extract,但是extract会清空当前ports tree上用户做的本地修改,但是extract可以与选项path一起用,即只extract部分ports tree。如果自己对ports tree做了本地修改,那么请认真看portsnap和svn的命令)
        用svn checkout时可能出现错误:svn: E120106: ra_serf: The server sent a truncated HTTP response body. 据说这是因为文件太大,然后下载得太慢,解决办法为:每出现一次这个错误,就先在工作目录下执行svn cleanup、svn update,然后再接着checkout。如此循环,直至checkout完成。(也可以修改httpd.conf,把Timeout得值设得大一些,比如Timeout=6000或者=12000。但是我没在我的系统上find到这个文件…)
        下图依次显示了安装FreeBSD时安装的/usr/ports、Portsnap的ports tree和SVN的ports tree的对比,因为svn时下载的文件太大且网速太慢,所以没checkout完,我就停止了。
  10. 接着上条,更新完ports tree后,就可以升级ports了。可以用# pkg version -l "<"看哪些installed ports are out of date。注意:升级ports前,先看看/usr/ports/UPDATING,如果有涉及到待会儿更新的ports,看看可能会有什么问题、变化,该怎么做。升级工具大部分使用Portmaster、Portupgrade,Synth是一个新的升级工具。
        Using Portmaster: ports-mgmt/portmaster is designed to use the tools installed with the FreeBSD base system without depending on other ports or databases. 没有安装portmaster的话可先用port安装。Portmaster定义了四种类型的ports: Root port: 它不依赖其他ports,任何其他ports也不会依赖于它;Trunk port: 它不依赖其他ports,但其他ports可能会依赖于它;Branch port: 它可能会依赖别的ports,且其他的ports可能依赖于它;Leaf port: 它可能依赖于其他ports,但是没有ports会依赖于它. (果然我翻译一下反而不好懂了,还是看英文原话比较清晰) To list these categories and search for updates: # portmaster -L, to upgrade all outdated ports: # portmaster -a. Portmaster会在升级时自动对旧的ports进行备份,如果升级成功,才会删除备份,可用-b选项使其不自动删除备份,使用-i选项可使Portmaster进入interactive mode,每升级一个port都会请求确认。如果升级过程中出现错误,加-f可重新升级并建立ports: # portmaster -af。Portmaster也可以用来安装new ports,它可以自动更新new ports安装时需要的所有依赖,举个栗子:# portmaster shells/bash。更多用法可看Portmaster的ports目录下的pkg-descr文件。
        Portmaster升级(有新版本的)每个port时,会创建一个package形式的备份,如果这个port成功升级了,那么调用pkg-delete删除package备份,如果升级port失败了,则会显示提示信息,告诉用户如何找到package备份。(默认情况,这些package备份会存放到变量$PACKAGES指定的目录(usually /usr/ports/packages)下的名为"portmaster-backup"的目录下,比如/usr/ports/packages/portmaster-backup/icu-65.1,1.txz就是icu的pkg形式的备份) -B选项可以用来让portmaster不创建package备份,-b选项可使package备份在成功升级port后不被自动删除。在升级过程中,添加-D选项,可以"no cleaning of distfiles",-d可以"always clean distfiles",避免有时需要一个一个确认是否删除某个port的stale distfiles。
        所以在man portmaster的栗子中,给出了升级并删除stale distfiles的命令用法:# portmaster -aD; portmaster --clean-distfiles,即先升级ports,这时不删,等全部升级完了,再删除stale distfiles。(distfiles就是make fetch下来的源码包,保存在/usr/ports/distfiles)
        portmaster --clean-distfiles,可以添加-t选项,这样就是 如果有任意port依赖于某个stale file,即使这个port还未安装,那也不删除这个stale file;不加-t就是只看已安装的ports里有没有依赖某个stale file的,没有的话 就删除这个stale file;-y和-n可以用来避免一个个确认是否删除这个stale file,要么全yes要么全no。
        Using Portupgrade: 想用的话可先用ports安装,然后看文档
  11. pkg install和pkg upgrade时 会自动进行 执行pkg update时做的事情(“Updating FreeBSD repository catalogue”),所以除非是换了新的pkg源,那么不用单独执行pkg update。(理解错了请指正)
        pkg info 会列出installed packages,portmaster -L会列出installed ports及可更新的版本,但是这俩命令列出的是一样的东西:用pkg和ports安装的所有包,不是:pkg列出用pkg install的包,portmaster列出用make install clean, portmaster列出的可更新的包。如果一个软件(比如vim)是用pkg安装的(或作为依赖安装的),那不能用# portmaster vim-8.1.2372这样直接只更新某个包,但可以# portmaster editors/vim,这样更新vim,相当于在ports下make install,比用pkg upgrade慢。
  12. 用ports安装软件确实有点麻烦……我用portsnap更新ports tree,然后ports源用的USTC的,但是这俩更新不能时刻同步,所有就会有ports tree更新了新版本的一个依赖包,但是USTC的freebsd-ports源的distfiles还没有同步新版本的那个源码包,这样ports系统fetch不到需要的包,就会转向GitHub找,虽然能找到链接,但是下载是从AmazonAWS下载的,所以还是不行……除非翻外网,否则只能等ustc同步了以后,再make install。
  13. ports-mgmt/pkg_cutleaves automates the task of removing installed ports that are no longer needed. (用pkg autoremove应该也行,之前用ports安装emacs,编译没成功,就准备删了安装的东西 重来,然后用了一下pkg autoremove,能把编译emacs时编译安装的其他依赖软件删掉)
  14. 更换pkg源:创建/usr/local/etc/pkg/repos/FreeBSD.conf,写入:
    FreeBSD: {
        url: “pkg+${ABI}/quarterly”,
    更换ports源:创建文件/etc/make.conf,写入:MASTER_SITE_OVERRIDE?=${DIST_SUBDIR}/ ,等用ports安装软件时,就能看到输出显示从这个源fetch distfiles。
  15. 这节 4.6. Building Packages with Poudriere,关于Poudriere的介绍和用法,可以安装ports-mgmt/poudriere后看man,或者看GitHub 。
  16. Post-Installation Considerations:
    1)大多数app都会安装至少一个配置文件,并将其放入/usr/local/etc目录下,如果有多个配置文件,则会创建子目录来存放。通常模板配置文件以.sample后缀结尾,To edit a sample file, first copy it without the .sample extension.
    2)第三方app的文档会存放在/usr/local/share/doc目录下,也会安装manual pages。
    3)有些app会运行服务(开机自启服务),其相关配置需要写入/etc/rc.conf,然后再开启服务。These applications usually install a startup script in /usr/local/etc/rc.d 。
    4)Users of csh should run rehash to rebuild the known binary list in the shells PATH.
    5)Use pkg info to determine which files, man pages, and binaries were installed with the application.
  17. 用ports安装Emacs时,装完 archivers/lzo2 这个依赖并将其注册为automatic时,输出了错误信息:“pkg-static: Cannot get an exclusive lock on a database, it is locked by another process”,我没有重新在emacs的port skeleton下make install,因为它输出了还有哪些没有成功安装,所以我cd到剩余的每个port skeleton,执行make install clean,然后pkg set -A 1 xxx/xxx,就是指定这个依赖所在目录及软件名,将其注册为automatic,但是像cmake这样的依赖,就不将其注册为automatic了。
        在man pkg-static中:pkg-static is a statically linked variant of pkg typically only used for the initial installation of pkg. There are some differences in functionality. See pkg.conf(5) for details.

Chapter 5. The X Window System

  1. bsdinstall安装时不会自动安装用户界面,想要在安装时就安装上用户界面的用户可以去 ,“TrueOS | FreeBSD Based Operating System with ZFS”。(国内目前打不开此网站…什么时候能解封此类网站,FreeNAS的官网也打不开…)
  2. 安装Xorg:用pkg安装,或者用ports安装。也可以安装x11/xorg-minimal,但是有些未安装的xorg组件可能是其他app需要的。
  3. 配置Xorg:将需要运行xorg的用户加入video或wheel组,以启用3D acceleration:# pw groupmod video -m jru || pw groupmod wheel -m jru。执行% startx即可启动xorg,默认的window manager是TWM (可以自己换成i3wm,这个用了很久,感觉用着挺习惯了)。
        在/boot/loader.conf中添加一行 “kern.vty=vt” 以启用vt。这步应该不是必须的,因为如果不写那行,新版(9.0、10.0之后的版本)的FreeBSD会默认使用vt。在vt出现之前是sc,但是好像会发生这样的问题:从X界面退出到virtual console后,会黑屏。
        配置显示器:大多数显示器支持EDID(the Extended Display Identification Data standard),Xorg能通过EDID确定显示器适合哪个分辨率和刷新频率。如果没有自动选择最好的分辨率或者想自己改分辨率,可以使用xrandr进行修改。想要每次使用自定义的分辨率、刷新频率、笔记本外接显示器的位置等设置,可以写xorg配置文件进行配置,或者在.xinitrc文件里用xrandr设置。显示器的相关配置也和Linux差不多,可以参考Arch Wiki里有关显示器配置的相关文档。
        可以设置关闭X界面的快捷键,参见Handbook Keyboards。 Mice and Pointing Devices讲了一点鼠标键的设置,比如可以设定鼠标上的键的个数为7:Section “InputDevice”、Option “Buttons” “7”。
        有时Xorg的自动配置可能对某些硬件不起作用,那这时可以自己配:先执行# Xorg -configure,这会基于已检测到的硬件生成一个传统的single配置文件/root/,然后在这个文件中写入或修改自己想要的配置,然后测试:# Xorg -config /root/。等调到自己想要的状态后,可以把xorg.conf.new拆成小配置文件(就是之前说的,一个小配置文件对一个硬件进行配置),放入/usr/local/etc/X11/xorg.conf.d/。
  4. 字体配置:在字体配置文件中添加新字体的目录、然后# fc-cache -f、是否需要添加xorg启动时load的模块。字体配置尽量都写在/usr/local/etc/fonts/local.conf文件中,以XML格式。需要Xorg去load的模块,看能不能写到/usr/local/etc/X11/xorg.conf.d/xxxxxx.conf文件中,文件名不知道能不能自己随便起,内容包括Section “Module” Load “xxx”。配置文件的写法参照man fonts-conf、man xorg.conf、/usr/local/etc/fonts/、/etc/X11/xorg.conf 等。(在虚拟机用时,有些地方的字体我没配好,比如fluxbox的window title不能显示中文,但是我安装了Google Noto的中文字体)
  5. 在虚拟机(我用的是VirtualBox)中使用FreeBSD时,安装了xorg,然后startx,会报错"freebsd xorg cannot run in framebuffer mode …",然后我看了看虚拟机里的Ubuntu,安装了一个vmware display driver,所以在FreeBSD虚拟机里也装一下# pkg install xf86-video-vmware,然后就能startx进入界面了。(之前的虚拟机一般都装arch,不需要界面,所以从来不知道虚拟机里的linux/unix得有xf86-video-vmware驱动才能开界面)
  6. 壁纸:之前用Arch时,是i3wm作为窗口管理器,然后有个 (加入.xinitrc 使sh在启动x界面时开始执行),这个sh的内容大致就是在指定目录抽一张图片,作为壁纸,每15分钟换一次。设定壁纸是用feh --bg-scale实现,现在是新看到了一个选项:use --no-xinerama to treat the whole X display as one screen when setting wallpapers。这个还没有用过,因为以前都是笔记本外接显示器,i3wm会自动分配workspace,然后两个屏幕壁纸都是一样的。不知道两个屏幕不一样大时(一个屏幕是笔记本屏,一个是大显示器),用上–no-xinerama会是什么样,一张图片 左半部分在笔记本屏 右半部分在外接屏 尺寸还不同,感觉不好看吧……等以后配了台式机,弄两个一样的大屏,这样应该会好看。
  7. 在Gentoo Wiki中看到的关于URxvt的一些以前不知道的东西:urxvt可以作为daemon,这样能减少资源消耗、加速新终端的启动。推荐在启动X时启动daemon ( It is a good idea to start the daemon at the beginning of the X session.)。在相应文件中(比如.xinitrc)写入:urxvtd --quiet --opendisplay --fork,After this, new clients can be opened on the single daemon process, rather than spawning new processes for each terminal,之后想要打开新终端时用命令urxvtc代替urxvt。如果daemon被意外终止了,那么所有在daemon上打开的urxvtc调用和实例都会被关闭。在文件 ~/.urxvt/urxvtd-hostname 中还可以通过变量RXVT_SOCKET指定不同的listening socket。
  8. URxvt的color theme可以参照Gentoo Wiki的格式,然后自己设想要的颜色。有关urxvt的更多资料建议参考Arch Wiki、Gentoo Wiki、man pages。
  9. GNU Screen。以前用Linux时,图形界面一直用的i3wm,在字符界面也只是进行一些简单任务,所以一直不明白screen有什么用…然后最近试用FreeBSD,一直在字符界面操作,有时需要多个终端,就手动ttyv0、ttyv1、ttyv2等来回切换着用,这时也没想到screen,然后偶然看Gentoo Wiki的时候又看到了GNU Screen,才突然理解了screen的用处。
        当在tty命令行输入$ screen -S xxx后,即开启了一个名为xxx的screen session,这个session会开启一个全屏的新的字符界面,称为window,这个window中默认开启的应用是shell (好像可以更改默认开启的应用),但是这时还是只有一个终端。
        接下来可以通过按键对当前region进行划分(可以认为screen -S xxx后开启了一个大的region,然后这个region里有一个运行着shell的window):先按Ctrl+a再按S,Split current region horizontally into two regions,也就是将目前region划分为上下两个regions;先Ctrl+a再|,Split current region vertically into two regions,即将当前region划分为左右两个region。新划出的小region是空白的,什么程序都没有运行着,可以通过Ctrl+a tab将focus切换到新region,然后Ctrl+a c,在新region上打开一个默认运行着shell的window。
        这样一个tty里就有了两个终端了,可以将每个region不断进行水平、垂直划分,获得多个region,每个region上可以开启一个window,以获得多个终端。当有两个及以上window时,每个window都会有window title,比如第一个region 有一个window 运行着bash,那么第一个window的title就是"0 bash",第二个region和第一个情况一样的话,title就编号往下走,为"1 bash"。
        $ screen -S session_name,to create a named session
        $ screen -list,to print a list of “” strings identifying your screen sessions
        $ screen -x session_name$ screen -r session_name,to attach to a named screen session
        Ctrl+a ?,Displays commands and their defaults
        Ctrl+a S,Split current region horizontally into two regions
        Ctrl+a |,Split current region vertically into two regions
        Ctrl+a tab,Switch the input focus to the next region
        Ctrl+a c,Create a new window (with shell)
        Ctrl+a ",Window list,可以用方向键的上下进行选择,然后enter确认,将所选的那个window显示在当前region上
        Ctrl+a 0,opens window 0, Ctrl+a 1就是打开window 1,切换到某个region,然后Ctrl+a 再按某个数字键x,即可将window x显示在当前region上
        Ctrl+a a,Sends Ctrl+a to the current window,Arch Wiki中的栗子 “A common scenario”:用户在终端开启了一个screen session,然后在这个screen session里开启了ssh session,然后在ssh session里又开了一个screen session。这时如果按键Ctrl+a a,那么Ctrl+a首先被第一个开启的screen session (the outer screen session) 处理,然后按键序列的第二个a被第二个screen session (the inner screen session) 处理,第二个screen session接收Ctrl+a。 For example: Ctrl+a a d Detaches the inner screen session. Ctrl+a a K Kills the inner screen session.
        Ctrl+a Ctrl+a,Toggle between current and previous region,在当前region和上一个region之间切换。如果现在整个tty只有左右两个region,然后左边是0 bash,右边是空白,这时先将focus放在0 bash上,然后Ctrl+a tab将focus移到右边的region,然后Ctrl+a Ctrl+a,则右边region也显示0 bash,那么现在左右region就是同步显示的,会显示相同的动作及结果。可以用这个在tty里弄"田"字分布的四个0 bash,然后screenfetch或者neofetch,截个屏/拍个照。
        Ctrl+a Esc,Enter Copy Mode (use enter to select a range of text)
        Ctrl+a ],Paste text
        Ctrl+a [,不知道在哪看的说是查看历史。其实和Ctrl+a Esc一样,是进入Copy Mode,然后可以上下移动光标,所以可以达到查看屏幕buffer的目的。(进入Copy Mode后,按下space或enter可以选择文本,移动光标选择完成后,再按space或enter可以确认将选中的内容Copied into buffer,按其他键 比如q、Esc 可以终止Copy Mode。)
        Ctrl+a Q,Close all regions but the current one
        Ctrl+a X,Close the current region
        Ctrl+a d,Detach from the current screen session, and leave it running.
        Ctrl+a :quit,Closes all windows and closes screen session
        Ctrl+a \,和上条一样,但是会问你 Really???
        Turn off visual bell: 在文件~/.screenrc里添加一行:vbell off,with this setting, Screen will not make an ugly screen flash instead of a bell sound.
        上述命令差不多满足目前的日常使用了,更多用法请参考Arch Wiki的简单介绍和GNU Screen的手册。

FreeBSD 12.1 Handbook 的剩余Parts 的Notes以后再写。



武汉:社区万家宴热闹开席 万道自制菜肴迎新春