alien:.deb与.rpm包转换工具

deb 与 rpm 是GNU/Linux 流行的软件包格式。我们“alien”可以在rpm\dpkg\slackware tgz\deb\slp 格式见进行转换。

Debian/Ubuntu 可使用下面命令安装alien:

$ sudo apt-get install alien
从RPM转到DEB

举例:假设在目录/tmp 有myprogram.rpm 文件,使用下面命令转换:

$ sudo alien /tmp/myprogram-1.10-2.i386.rpm

myprogram-1.10-3.i386.deb generated

这时,在/tmp 目录下就会发现有 myprogram.deb ,这时就可以使用dpkg安装:

$ sudo dpkg -i /tmp/miprogramma.deb

继续阅读alien:.deb与.rpm包转换工具

resize2fs:Memory alloction failed while trying to resize

最近入手一块 12T 的西数红盘,打算安装到 WD MyCloud Gen1 上,从 4TB 版本升级到 12TB 版本,作为 TimeMachine 来用。

使用 拯救死翘翘了的WD MyCloud 方式恢复镜像之后,最后一步执行

$ resize2fs /dev/sda4

结果报告如下错误:

$ resize2fs /dev/sda4
resize2fs 1.42.5 (29-Jul-2012)
Resizing the filesystem on /dev/sda4 to 2928542715 (4k) blocks.
resize2fs: Memory allocation failed while trying to resize /dev/sda4
Please run 'e2fsck -fy /dev/sda4' to fix the filesystem
after the aborted resize operation.

这个原因网上查询了很久,基本上断定是 e2fsprogs 1.42.5 的 BUG,这个问题在  e2fsprogs 1.42.9 版本修复。修复内容参考代码下的 debian/changelog

关键日志如下:

e2fsprogs (1.42.9-1) unstable; urgency=low

  * Fixed a large number of bugs in resize2fs, e2fsck, debugfs, to
    handle bigalloc and 64-bit file systems.

编译过程参考 How to successfully build packages for WD My Cloud from source 中对于 e2fsprogs 1.42.13 的编译。

参考链接


Gettext po文件编辑器

Gettext 是一个非常老牌和成熟的国际化和本地化解决方案,在 Linux 下几乎每个 GNU 程序中都能见到 Gettext 的身影。在 Gettext 中,每个 locale 对应一个 po 文件,虽说 po 文件是纯文本,但是如果用普通的文本编辑器来编辑是非常麻烦的。

正好这两天国际化 ,搜到了几个 Linux 下的 po 编辑器~,推荐 GtranslatorWordPress 翻译( i18n )的时候使用这些工具。

macOS 下安装如下命令:

$ brew install gettext

$ brew link --force gettext

$ brew install Gtranslator

$ gtranslator

继续阅读Gettext po文件编辑器

ubuntu 18.04 "nvidia-340 导致 /usr/lib/x86_64-linux-gnu/libGL.so.1 转移到 /usr/lib/x86_64-linux-gnu/libGL.so.1.distrib"

$ sudo apt-get install cuda
正在读取软件包列表... 完成
正在分析软件包的依赖关系树
正在读取状态信息... 完成
cuda 已经是最新版 (10.1.243-1)。
您也许需要运行“apt --fix-broken install”来修正上面的错误。
下列软件包有未满足的依赖关系:
cuda-drivers : 依赖: libnvidia-gl-418 (>= 418.87.00) 但是它将不会被安装
libnvidia-ifr1-418 : 依赖: libnvidia-gl-418 但是它将不会被安装
nvidia-driver-418 : 依赖: libnvidia-gl-418 (= 418.87.00-0ubuntu1) 但是它将不会被安装
推荐: libnvidia-compute-418:i386 (= 418.87.00-0ubuntu1)
推荐: libnvidia-decode-418:i386 (= 418.87.00-0ubuntu1)
推荐: libnvidia-encode-418:i386 (= 418.87.00-0ubuntu1)
推荐: libnvidia-ifr1-418:i386 (= 418.87.00-0ubuntu1)
推荐: libnvidia-fbc1-418:i386 (= 418.87.00-0ubuntu1)
推荐: libnvidia-gl-418:i386 (= 418.87.00-0ubuntu1)
E: 有未能满足的依赖关系。请尝试不指明软件包的名字来运行“apt --fix-broken install”(也可以指定一个解决办法)。

$ sudo apt --fix-broken install
正在读取软件包列表... 完成
正在分析软件包的依赖关系树
正在读取状态信息... 完成
正在修复依赖关系... 完成
下列软件包是自动安装的并且现在不需要了:
lib32gcc1 libc6-i386
使用'sudo apt autoremove'来卸载它(它们)。
将会同时安装下列软件:
libnvidia-gl-418
下列【新】软件包将被安装:
libnvidia-gl-418
升级了 0 个软件包,新安装了 1 个软件包,要卸载 0 个软件包,有 0 个软件包未被升级。
有 68 个软件包没有被完全安装或卸载。
需要下载 0 B/32.2 MB 的归档。
解压缩后会消耗 164 MB 的额外空间。
您希望继续执行吗? [Y/n]
获取:1 file:/var/cuda-repo-10-1-local-10.1.243-418.87.00 libnvidia-gl-418 418.87.00-0ubuntu1 [32.2 MB]
(正在读取数据库 ... 系统当前共安装有 293566 个文件和目录。)
正准备解包 .../libnvidia-gl-418_418.87.00-0ubuntu1_amd64.deb ...
dpkg-query: 没有找到与 libnvidia-gl-410 相匹配的软件包
nvidia-340 导致 /usr/lib/x86_64-linux-gnu/libGL.so.1 转移到 /usr/lib/x86_64-linux-gnu/libGL.so.1.distrib
dpkg-divert: 错误: 删除 被 libnvidia-gl-418 转移的 /usr/lib/x86_64-linux-gnu/libGL.so.1 时
软件包名不匹配
发现了 nvidia-340 导致 /usr/lib/x86_64-linux-gnu/libGL.so.1 转移到 /usr/lib/x86_64-linux-gnu/libGL.so.1.distrib
dpkg: 处理归档 /var/cuda-repo-10-1-local-10.1.243-418.87.00/./libnvidia-gl-418_418.87.00-0ubuntu1_amd64.deb (--unpack)时出错:
new libnvidia-gl-418:amd64 package pre-installation script subprocess returned error exit status 2
在处理时有错误发生:
/var/cuda-repo-10-1-local-10.1.243-418.87.00/./libnvidia-gl-418_418.87.00-0ubuntu1_amd64.deb
E: Sub-process /usr/bin/dpkg returned an error code (1)

解决方案如下:

# 解除nvidia 340全部依赖

$ LC_MESSAGES=C dpkg-divert --list '*nvidia-340*' | sed -nre 's/^diversion of (.*) to .*/\1/p' | xargs -rd'\n' -n1 -- sudo dpkg-divert --remove

$ dpkg-divert --package nvidia-340 --remove /usr/lib/i386-linux-gnu/libGL.so.1

参考链接


发现了 nvidia-340 导致 /usr/lib/i386-linux-gnu/libGL.so.1 /usr/lib/i386-linux-gnu/libGL.so.1.distrib...

ubuntu 18.04 systemd-udevd进程CPU占用特别高,CUDA Toolkit 10.1 Update 2安装之后出现

最近在T440笔记本的ubuntu 18.04系统上安装最新的`CUDA Toolkit 10.1 Update 2`之后,发现 `systemd-udevd` 进程CPU占用特别高,执行 `sudo /lib/systemd/systemd-udevd -D` ,会发现持续输出如下信息:

RUN '/bin/systemctl start --no-block nvidia-persistenced.service' /lib/udev/rules.d/71-nvidia.rules:12
RUN '/sbin/modprobe nvidia-modeset' /lib/udev/rules.d/71-nvidia.rules:16
RUN '/sbin/modprobe nvidia-drm' /lib/udev/rules.d/71-nvidia.rules:20
RUN '/sbin/modprobe nvidia-uvm' /lib/udev/rules.d/71-nvidia.rules:24
RUN '/usr/bin/nvidia-smi' /lib/udev/rules.d/71-nvidia.rules:28
starting '/bin/systemctl start --no-block nvidia-persistenced.service'
Process '/bin/systemctl start --no-block nvidia-persistenced.service' succeeded.
starting '/sbin/modprobe nvidia-modeset'
seq 115679 queued, 'remove' 'module'
seq 115680 queued, 'add' 'module'
seq 115681 queued, 'add' 'slab'
seq 115682 queued, 'add' 'drivers'
seq 115681 running
seq 115682 running
seq 115681 processed
seq 115683 queued, 'remove' 'slab'
seq 115684 queued, 'remove' 'drivers'
seq 115683 running
seq 115683 processed
seq 115682 processed
seq 115684 running
seq 115684 processed
seq 115685 queued, 'remove' 'module'
'/sbin/modprobe nvidia-modeset'(err) 'modprobe: ERROR: could not insert 'nvidia_modeset': No such device'
Process '/sbin/modprobe nvidia-modeset' failed with exit code 1.

解决方法如下:

$ sudo rm -rf /lib/udev/rules.d/71-nvidia.rules

参考链接


Ubuntu通过apt-get安装指定版本和查询指定软件有多少个版本

一、通过apt-get安装指定版本

$ apt-get install <<package name>>=<<version>>

二、查询指定软件有多少个版本

说明:在Linux用这个查询并不能完全的把所有版本都列举出来,因为每个版本都与系统版本和CPU架构有关,比如一个软件支持Ubuntu系统的16.04的CPU架构为amd64的版本只有1.0和1.2,其余都不支持,所以列举时就只有两款。

列举版本列表:

0、通过网站搜索:

https://packages.ubuntu.com/

1、

$ apt-cache madison <<package name>>

将列出所有来源的版本。如下输出所示:

$ apt-cache madison vim
   vim | 2:7.3.547-1 | http://debian.mirrors.tds.net/debian/ unstable/main amd64 Packages
   vim | 2:7.3.429-2 | http://debian.mirrors.tds.net/debian/ testing/main amd64 Packages
   vim | 2:7.3.429-2 | http://http.us.debian.org/debian/ testing/main amd64 Packages
   vim | 2:7.3.429-2 | http://debian.mirrors.tds.net/debian/ testing/main Sources
   vim | 2:7.3.547-1 | http://debian.mirrors.tds.net/debian/ unstable/main Sources

`madison` 是一个 `apt-cache` 子命令,可以通过 `man apt-cache` 查询更多用法。

2、

$ apt-cache policy <<package name>>

将列出所有来源的版本。信息会比上面详细一点,如下输出所示:

$ apt-cache policy gdb
gdb:
  Installed: 7.7.1-0ubuntu5~14.04.2
  Candidate: 7.7.1-0ubuntu5~14.04.2
  Version table:
 *** 7.7.1-0ubuntu5~14.04.2 0
        500 http://fr.archive.ubuntu.com/ubuntu/ trusty-updates/main amd64 Packages
        100 /var/lib/dpkg/status
     7.7-0ubuntu3 0
        500 http://fr.archive.ubuntu.com/ubuntu/ trusty/main amd64 Packages
        500 http://archive.ubuntu.com/ubuntu/ trusty/main amd64 Packages

`policy` 是一个 `apt-cache` 子命令,可以通过 `man apt-cache`查询更多用法。

3、

$ apt-cache showpkg <<package name>>

4、

$ apt-get install -s <<package-name>>

说明:这个命令只是模拟安装时会安装哪些软件列表,但不会例举出每个软件有多少个版本

5、

$ aptitude versions <<package name>>

参考:https://manpages.debian.org/unstable/aptitude/aptitude.8.en.html

6、

$ apt-show-versions -a <<package name>>

说明:列举出所有版本,且能查看是否已经安装。还可以通过apt-show-versions -u <<package name>>来查询是否有升级版本。

参考:http://manpages.ubuntu.com/manpages/trusty/man1/apt-show-versions.1p.html

7、

$ whohas -d Debian,Ubuntu <<package name>> | tr -s ' ' '\t' | cut -f 1-3 | column -t

8、

$ rmadison -u debian,ubuntu,bpo <<package name>> | cut -d "|" -f 1-3

单个详情:

1、

$ apt-cache show <<package name>>

说明:查询指定包的详情,不管是否已经安装。

2、

$ dpkg -l <<package name>>

说明:效果和上面基本一致,但是结果是列表详情展示,会提示是否已经删除了之后还有依赖包没有删除等。

3、

$ dpkg -s <<package name>>

说明:必须是安装的包才能显示详情。

4、

$ dpkg-query -s <<package name>>

说明:同上,效果一致。

使用技巧:

1、可以在查询后面带上一些参数来实现筛选

$ apt-cache show package | grep Version
$ apt-show-versions | more

参考链接


Ubuntu通过apt-get安装指定版本和查询指定软件有多少个版本

OpenMediaVault 4.1.26省电设置

目前版本的OpenMediaVault 4.1.26缺乏必要的电源管理,但是对于NAS来说,电源管理是非常必要的,尤其是其会进行7x24小时的连续运行的时候。

当前找到的是tlp,这个工具据说不错,在此试用一段时间看看效果。

$ sudo apt-get update

$ sudo apt-get install tlp

$ sudo tlp start

# 省电的话建议,强制切换到电池模式,启用电池模式的设置
$ sudo tlp bat

$ sudo systemctl enable tlp.service

# 检查服务运行状态
$ sudo tlp-stat -s

# 更详细的配置信息,修改 /etc/default/tlp

参考链接


Ubuntu架设OpenVPN实现内网穿透

前言

家里的网络因为没有公网 IP,有时候想要连接到家里的树莓派或者电脑就无法实现。这个时候可以采用内网穿透的方法远程连接家中的机器,内网穿透的方案有很多,下面介绍一种采用 OpenVPN 实现内网穿透的方案。


配置

主机:腾讯云/阿里云
操作系统:Ubuntu 16.04/18.04/20.04

教程

安装 OpenVPN

首先,我们需要在服务器安装 OpenVPN。在 Ubuntu 系统中我们可以通过 apt 简单的进行安装。同时我们也需要安装 easy-rsa,它可以帮助我们生成 VPN 使用过程中所需的 CA 证书。

$ sudo apt-get update 

$ sudo apt-get install openvpn easy-rsa
设置 CA 目录

OpenVPN 是使用 TLS/SSL 的 VPN。这意味着它利用证书来加密服务器和客户端之间的通信。为了发布受信任的证书,我们需要建立一个自己的简单的证书颁发机构(CA)。 使用 `make-cadir` 命令复制 easy-rsa 模板到 home 目录。

$ make-cadir ~/openvpn/openvpn-ca

接着进入刚刚新建的目录准备配置 CA:

$ cd ~/openvpn/openvpn-ca

$ ln -s openssl-1.0.0.cnf openssl.cnf
配置 CA 变量

进入 ~/openvpn/openvpn-ca 目录后,我们需要修改 vars 文件,以便于生成需要的 CA 值。

$ vim vars

在文件底部找到以下配置:

...
export KEY_COUNTRY="US"
export KEY_PROVINCE="CA"
export KEY_CITY="SanFrancisco"
export KEY_ORG="Fort-Funston"
export KEY_EMAIL="me@myhost.mydomain"
export KEY_OU="MyOrganizationalUnit"
...

将这些变量修改为任意你喜欢的值,但是不要为空:

...
export KEY_COUNTRY="CN"
export KEY_PROVINCE="ZJ"
export KEY_CITY="HZ"
export KEY_ORG="www.mobibrw.com"
export KEY_EMAIL="xx.com"
export KEY_OU="com.mobibrw"
...

然后,我们还要修改紧接着出现的 `KEY_NAME` 的值,为了简单起见,我们改为 `server`  (这个不要修改成其他名字,后续的配置文件中默认是这个名字), 默认是 `EasyRSA`:

export KEY_NAME="server"
构建 CA 证书

针对 ubuntu 16.04 LTS/ubuntu 18.04 LTS 执行如下操作

首先进入你的 CA 目录,然后执行 `source vars` :

$ cd ~/openvpn/openvpn-ca

$ source vars

接着会有以下输出:

NOTE: If you run ./clean-all, I will be doing a rm -rf on /home/xxxx/openvpn/openvpn-ca/keys

执行下列操作确保操作环境干净:

$ ./clean-all

现在我们可以构建根 CA:

# 如果执行时候报错
# grep: /etc/openvpn/easy-rsa/2.0/openssl.cnf: No such file or directory
# pkitool: KEY_CONFIG (set by the ./vars script) is pointing to the wrong
# version of openssl.cnf: /etc/openvpn/easy-rsa/2.0/openssl.cnf
# The correct version should have a comment that says: easy-rsa version 2.x
# 则可以执行如下命令修复
# ln -s openssl-1.0.0.cnf openssl.cnf

$ ./build-ca

这将会启动创建根证书颁发密钥、证书的过程。由于我们刚才修改了 vars 文件,所有值应该都会自动填充。所以,一路回车就好了:

Generating a 2048 bit RSA private key
..........................................................................................+++
...............................+++
writing new private key to 'ca.key'
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [CN]:
State or Province Name (full name) [ZJ]:
Locality Name (eg, city) [HZ]:
Organization Name (eg, company) [www.mobibrw.com]:
Organizational Unit Name (eg, section) [com.mobibrw]:
Common Name (eg, your name or your server's hostname) [www.mobibrw.com CA]:
Name [mobibrw]:
Email Address [xxx.com]:

现在,我们就有了创建以下步骤需要的 CA 证书。

针对 ubuntu 20.04.6 LTS, 由于OpenVPN版本升级,已经没有上面的命令了,需要调整脚本为如下:

$ cd ~/openvpn/openvpn-ca

$ ./easyrsa init-pki

$ ./easyrsa build-server-full <SERVER_NAME> nopass
创建服务器端证书、密钥和加密文件

通过下列命令生成服务器端证书和秘钥:

$ ./build-key-server server

注:`server` 就是刚才在 `vars` 文件中修改的 `KEY_NAME` 变量的值。请不要使用别的名字! 然后一直回车选择默认值即可,不要设置 `challenge password` ,直接回车即可。到最后,你需要输入两次 y 注册证书和提交。

...
Certificate is to be certified until May  1 17:51:16 2026 GMT (3650 days)
Sign the certificate? [y/n]:y
1 out of 1 certificate requests certified, commit? [y/n]y
Write out database with 1 new entries
Data Base Updated

然后还需要生成一些其他东西。我们可以在密钥交换过程中生成一个强大的 Diffie-Hellman 密钥:

$ ./build-dh

这个操作大约会花费几分钟不等。 然后,我们可以生成 HMAC 签名加强服务器的 TLS 完整性验证功能:

$ openvpn --genkey --secret keys/ta.key
配置 OpenVPN 服务

首先将刚刚生成的各类文件复制到 OpenVPN 目录下:

$ cd ~/openvpn/openvpn-ca/keys

$ sudo cp ca.crt ca.key server.crt server.key ta.key dh2048.pem /etc/openvpn

然后,解压并复制一个 OpenVPN 配置文件到 OpenVPN 目录:

$ gunzip -c /usr/share/doc/openvpn/examples/sample-config-files/server.conf.gz | sudo tee /etc/openvpn/server.conf

$ sudo mkdir -p /etc/openvpn/ccd

接着更改配置,注释掉 `udp` 更改协议为 `tcp` :

proto tcp
;proto udp

找到 tls-auth 位置,去掉注释,并在下面新增一行:

tls-auth ta.key 0 # This file is secret
key-direction 0

去掉 usergroup 行前的注释:

user nobody
group nogroup

去掉 client-to-client 行前的注释允许客户端之间互相访问:

client-to-client

开启客户端固定 IP 配置文件夹:

client-config-dir ccd

去掉注释后的完整配置如下:

#################################################
# Sample OpenVPN 2.0 config file for            #
# multi-client server.                          #
#                                               #
# This file is for the server side              #
# of a many-clients <-> one-server              #
# OpenVPN configuration.                        #
#                                               #
# OpenVPN also supports                         #
# single-machine <-> single-machine             #
# configurations (See the Examples page         #
# on the web site for more info).               #
#                                               #
# This config should work on Windows            #
# or Linux/BSD systems.  Remember on            #
# Windows to quote pathnames and use            #
# double backslashes, e.g.:                     #
# "C:\\Program Files\\OpenVPN\\config\\foo.key" #
#                                               #
# Comments are preceded with '#' or ';'         #
#################################################

# Which local IP address should OpenVPN
# listen on? (optional)
;local a.b.c.d

# Which TCP/UDP port should OpenVPN listen on?
# If you want to run multiple OpenVPN instances
# on the same machine, use a different port
# number for each one.  You will need to
# open up this port on your firewall.
port 1194

# TCP or UDP server?
proto tcp
;proto udp

# "dev tun" will create a routed IP tunnel,
# "dev tap" will create an ethernet tunnel.
# Use "dev tap0" if you are ethernet bridging
# and have precreated a tap0 virtual interface
# and bridged it with your ethernet interface.
# If you want to control access policies
# over the VPN, you must create firewall
# rules for the the TUN/TAP interface.
# On non-Windows systems, you can give
# an explicit unit number, such as tun0.
# On Windows, use "dev-node" for this.
# On most systems, the VPN will not function
# unless you partially or fully disable
# the firewall for the TUN/TAP interface.
;dev tap
dev tun

# Windows needs the TAP-Win32 adapter name
# from the Network Connections panel if you
# have more than one.  On XP SP2 or higher,
# you may need to selectively disable the
# Windows firewall for the TAP adapter.
# Non-Windows systems usually don't need this.
;dev-node MyTap

# SSL/TLS root certificate (ca), certificate
# (cert), and private key (key).  Each client
# and the server must have their own cert and
# key file.  The server and all clients will
# use the same ca file.
#
# See the "easy-rsa" directory for a series
# of scripts for generating RSA certificates
# and private keys.  Remember to use
# a unique Common Name for the server
# and each of the client certificates.
#
# Any X509 key management system can be used.
# OpenVPN can also use a PKCS #12 formatted key file
# (see "pkcs12" directive in man page).
ca ca.crt
cert server.crt
key server.key  # This file should be kept secret

# Diffie hellman parameters.
# Generate your own with:
#   openssl dhparam -out dh2048.pem 2048
dh dh2048.pem

# Network topology
# Should be subnet (addressing via IP)
# unless Windows clients v2.0.9 and lower have to
# be supported (then net30, i.e. a /30 per client)
# Defaults to net30 (not recommended)
;topology subnet

# Configure server mode and supply a VPN subnet
# for OpenVPN to draw client addresses from.
# The server will take 10.8.0.1 for itself,
# the rest will be made available to clients.
# Each client will be able to reach the server
# on 10.8.0.1. Comment this line out if you are
# ethernet bridging. See the man page for more info.
server 10.8.0.0 255.255.255.0

# Maintain a record of client <-> virtual IP address
# associations in this file.  If OpenVPN goes down or
# is restarted, reconnecting clients can be assigned
# the same virtual IP address from the pool that was
# previously assigned.
ifconfig-pool-persist ipp.txt

# Configure server mode for ethernet bridging.
# You must first use your OS's bridging capability
# to bridge the TAP interface with the ethernet
# NIC interface.  Then you must manually set the
# IP/netmask on the bridge interface, here we
# assume 10.8.0.4/255.255.255.0.  Finally we
# must set aside an IP range in this subnet
# (start=10.8.0.50 end=10.8.0.100) to allocate
# to connecting clients.  Leave this line commented
# out unless you are ethernet bridging.
;server-bridge 10.8.0.4 255.255.255.0 10.8.0.50 10.8.0.100

# Configure server mode for ethernet bridging
# using a DHCP-proxy, where clients talk
# to the OpenVPN server-side DHCP server
# to receive their IP address allocation
# and DNS server addresses.  You must first use
# your OS's bridging capability to bridge the TAP
# interface with the ethernet NIC interface.
# Note: this mode only works on clients (such as
# Windows), where the client-side TAP adapter is
# bound to a DHCP client.
;server-bridge

# Push routes to the client to allow it
# to reach other private subnets behind
# the server.  Remember that these
# private subnets will also need
# to know to route the OpenVPN client
# address pool (10.8.0.0/255.255.255.0)
# back to the OpenVPN server.
;push "route 192.168.10.0 255.255.255.0"
;push "route 192.168.20.0 255.255.255.0"

# To assign specific IP addresses to specific
# clients or if a connecting client has a private
# subnet behind it that should also have VPN access,
# use the subdirectory "ccd" for client-specific
# configuration files (see man page for more info).

# EXAMPLE: Suppose the client
# having the certificate common name "Thelonious"
# also has a small subnet behind his connecting
# machine, such as 192.168.40.128/255.255.255.248.
# First, uncomment out these lines:
client-config-dir ccd
;route 192.168.40.128 255.255.255.248
# Then create a file ccd/Thelonious with this line:
#   iroute 192.168.40.128 255.255.255.248
# This will allow Thelonious' private subnet to
# access the VPN.  This example will only work
# if you are routing, not bridging, i.e. you are
# using "dev tun" and "server" directives.

# EXAMPLE: Suppose you want to give
# Thelonious a fixed VPN IP address of 10.9.0.1.
# First uncomment out these lines:
;client-config-dir ccd
;route 10.9.0.0 255.255.255.252
# Then add this line to ccd/Thelonious:
#   ifconfig-push 10.9.0.1 10.9.0.2

# Suppose that you want to enable different
# firewall access policies for different groups
# of clients.  There are two methods:
# (1) Run multiple OpenVPN daemons, one for each
#     group, and firewall the TUN/TAP interface
#     for each group/daemon appropriately.
# (2) (Advanced) Create a script to dynamically
#     modify the firewall in response to access
#     from different clients.  See man
#     page for more info on learn-address script.
;learn-address ./script

# If enabled, this directive will configure
# all clients to redirect their default
# network gateway through the VPN, causing
# all IP traffic such as web browsing and
# and DNS lookups to go through the VPN
# (The OpenVPN server machine may need to NAT
# or bridge the TUN/TAP interface to the internet
# in order for this to work properly).
;push "redirect-gateway def1 bypass-dhcp"

# Certain Windows-specific network settings
# can be pushed to clients, such as DNS
# or WINS server addresses.  CAVEAT:
# http://openvpn.net/faq.html#dhcpcaveats
# The addresses below refer to the public
# DNS servers provided by opendns.com.
;push "dhcp-option DNS 208.67.222.222"
;push "dhcp-option DNS 208.67.220.220"

# Uncomment this directive to allow different
# clients to be able to "see" each other.
# By default, clients will only see the server.
# To force clients to only see the server, you
# will also need to appropriately firewall the
# server's TUN/TAP interface.
client-to-client

# Uncomment this directive if multiple clients
# might connect with the same certificate/key
# files or common names.  This is recommended
# only for testing purposes.  For production use,
# each client should have its own certificate/key
# pair.
#
# IF YOU HAVE NOT GENERATED INDIVIDUAL
# CERTIFICATE/KEY PAIRS FOR EACH CLIENT,
# EACH HAVING ITS OWN UNIQUE "COMMON NAME",
# UNCOMMENT THIS LINE OUT.
;duplicate-cn

# The keepalive directive causes ping-like
# messages to be sent back and forth over
# the link so that each side knows when
# the other side has gone down.
# Ping every 10 seconds, assume that remote
# peer is down if no ping received during
# a 120 second time period.
keepalive 10 120

# For extra security beyond that provided
# by SSL/TLS, create an "HMAC firewall"
# to help block DoS attacks and UDP port flooding.
#
# Generate with:
#   openvpn --genkey --secret ta.key
#
# The server and each client must have
# a copy of this key.
# The second parameter should be '0'
# on the server and '1' on the clients.
tls-auth ta.key 0 # This file is secret
key-direction 0

# Select a cryptographic cipher.
# This config item must be copied to
# the client config file as well.
;cipher BF-CBC        # Blowfish (default)
;cipher AES-128-CBC   # AES
;cipher DES-EDE3-CBC  # Triple-DES
;cipher AES-256-CBC
cipher AES-256-GCM # 尽量使用GCM模式,CBC模式容易遭受Padding Oracle攻击

# Enable compression on the VPN link.
# If you enable it here, you must also
# enable it in the client config file.
# comp-lzo 
# 从2.5版本开始 comp-lzo 被废弃,要求使用 compress lzo 配置
# https://github.com/Nyr/openvpn-install/issues/430
compress lzo

# The maximum number of concurrently connected
# clients we want to allow.
;max-clients 100

# It's a good idea to reduce the OpenVPN
# daemon's privileges after initialization.
#
# You can uncomment this out on
# non-Windows systems.
user nobody
group nogroup

# The persist options will try to avoid
# accessing certain resources on restart
# that may no longer be accessible because
# of the privilege downgrade.
persist-key
persist-tun

# Output a short status file showing
# current connections, truncated
# and rewritten every minute.
status openvpn-status.log

# By default, log messages will go to the syslog (or
# on Windows, if running as a service, they will go to
# the "\Program Files\OpenVPN\log" directory).
# Use log or log-append to override this default.
# "log" will truncate the log file on OpenVPN startup,
# while "log-append" will append to it.  Use one
# or the other (but not both).
;log         openvpn.log
;log-append  openvpn.log
log         /var/log/openvpn/openvpn.log
log-append  /var/log/openvpn/openvpn.log

# Set the appropriate level of log
# file verbosity.
#
# 0 is silent, except for fatal errors
# 4 is reasonable for general usage
# 5 and 6 can help to debug connection problems
# 9 is extremely verbose
verb 3

# Silence repeating messages.  At most 20
# sequential messages of the same message
# category will be output to the log.
;mute 20

去掉注释之后,精简的关键信息如下:

port 1194
proto tcp
dev tun
ca ca.crt
cert server.crt
key server.key
dh dh2048.pem
server 10.8.0.0 255.255.255.0
ifconfig-pool-persist ipp.txt
client-config-dir ccd
client-to-client
keepalive 10 120
tls-auth ta.key 0
key-direction 0

# 尽量使用GCM模式,CBC模式容易遭受Padding Oracle攻击
cipher AES-256-GCM

# 从2.5版本开始 comp-lzo 被废弃,要求使用 compress lzo 配置
# https://github.com/Nyr/openvpn-install/issues/430
compress lzo

user nobody
group nogroup
persist-key
persist-tun
status openvpn-status.log
verb 3
调整服务器网络配置
调整 `UFW` 规则

首先,确认`UFW`防火墙状态,如果状态为开启,则调整。如果防火墙是关闭的,则不需要调整。

$ sudo ufw status

如果输出是`Status: inactive`, 那么不需要进行下面的调整。

注意,下面的调整如果中间出现问题,可能导致防火墙拦截`SSH`进而无法进行恢复操作。如果出现问题,可以使用阿里云或者腾讯云提供的远程控制台在网页端进行恢复操作。

注意,如果服务器是从早期版本的`ubuntu 14.04`升级到`ubunt 16.04`的,那么可能会出现如下警告信息:

WARN: Duplicate profile 'Apache', using last found
WARN: Duplicate profile 'Apache Secure', using last found
WARN: Duplicate profile 'Apache Full', using last found

原因为存在两个版本的防火墙配置文件 `apache2-utils.ufw.profile`(`ubunt 16.04`) , `apache2.2-common`(`ubuntu 14.04`) ,这两个配置文件。

我们需要删除早期的防火墙配置文件,如下:

$ sudo rm -rf /etc/ufw/applications.d/apache2.2-common

如果服务器上`UFW`首次启用, 需要手工许可若干常用服务,否则无法远程访问:

# 默认情况下存在 "Apache"(80) "Apache Secure"(443) "Apache Full"(80,443) 三个配置项,我们一般使用"Apache Full"
$ sudo ufw allow "Apache Full"

# 同理 Nginx 对应的是 "Nginx Full"

# 许可SSH服务,注意: 如果 SSH 服务端口修改过,需要确认两者端口是否一致(默认是22),配置文件位于 "/etc/ufw/applications.d/" 目录
$ sudo ufw allow "OpenSSH"

当然,也可以直接配置需要开放的端口,但是不推荐,如下:

$ sudo ufw allow 80/tcp
$ sudo ufw allow 443/tcp

如果需要删除特定的规则,那么参考如下命令:

$ sudo ufw status numbered
Status: active

     To                         Action      From
     --                         ------      ----               
[ 1] Apache Full                ALLOW IN    Anywhere                              
[ 2] Apache Full (v6)           ALLOW IN    Anywhere (v6) 

$ sudo ufw delete <编号>

打开 OpenVPN 端口并使变化生效

$ sudo ufw allow 1194/tcp

$ sudo ufw disable

$ sudo ufw enable
启动OpenVPN

执行:

$ sudo mkdir -p /etc/openvpn/ccd

$ sudo systemctl start openvpn@server

#如果启动失败,则执行如下命令观察日志
# sudo tail -f /var/log/syslog

设置开机自启:

$ sudo systemctl enable openvpn@server

创建客户端配置

生成客户端证书、密钥对
$ cd ~/openvpn/openvpn-ca

$ source vars

$ cp /etc/openvpn/ca.crt ./keys/

$ sudo cp /etc/openvpn/ca.key ./keys/

$ sudo cp /etc/openvpn/ta.key ./keys/

$ sudo chmod 755 ./keys/ca.crt

$ sudo chmod 755 ./keys/ta.key

$ ./build-key client-mobibrw

`client-mobibrw` 为密钥对名称,生成过程中回车选择默认选项即可。

注意,下面这一步一定要输入 `y` 否则会跳过最后一步,导致生成证书的时候会缺少部分信息。

Certificate is to be certified until May 29 11:40:12 2029 GMT (3650 days)

Sign the certificate? [y/n]:y

1 out of 1 certificate requests certified, commit? [y/n]y
Write out database with 1 new entries
Data Base Updated

注意,如果有多个客户端的话,建议为每个客户端生成一份配置文件。

创建客户端配置

执行下列命令,生成客户端配置的基础文件:

$ mkdir -p ~/client-configs/files

$ chmod 700 ~/client-configs/files

$ cp /usr/share/doc/openvpn/examples/sample-config-files/client.conf ~/client-configs/base.conf

然后打开 `~/client-configs/base.conf` 文件,修改 `remote my-server-1 1194` 为服务器公网IP或者域名。 然后更改客户端协议为 `tcp`:

# Are we connecting to a TCP or
# UDP server?  Use the same setting as
# on the server.
proto tcp
;proto udp

去掉 `user` 和 `group` 前的注释:

# Downgrade privileges after initialization (non-Windows only)
user nobody
group nogroup

找到 ca/cert/key,注释掉:

# SSL/TLS parms.
# See the server config file for more
# description.  It's best to use
# a separate .crt/.key file pair
# for each client.  A single ca
# file can be used for all clients.
#ca ca.crt
#cert client.crt
#key client.key

在文件中新增一行:

key-direction 1

保存退出文件。

##############################################
# Sample client-side OpenVPN 2.0 config file #
# for connecting to multi-client server.     #
#                                            #
# This configuration can be used by multiple #
# clients, however each client should have   #
# its own cert and key files.                #
#                                            #
# On Windows, you might want to rename this  #
# file so it has a .ovpn extension           #
##############################################

# Specify that we are a client and that we
# will be pulling certain config file directives
# from the server.
client

# Use the same setting as you are using on
# the server.
# On most systems, the VPN will not function
# unless you partially or fully disable
# the firewall for the TUN/TAP interface.
;dev tap
dev tun

# Windows needs the TAP-Win32 adapter name
# from the Network Connections panel
# if you have more than one.  On XP SP2,
# you may need to disable the firewall
# for the TAP adapter.
;dev-node MyTap

# Are we connecting to a TCP or
# UDP server?  Use the same setting as
# on the server.
proto tcp
;proto udp

# The hostname/IP and port of the server.
# You can have multiple remote entries
# to load balance between the servers.
remote www.mobibrw.com 1194
;remote my-server-2 1194

# Choose a random host from the remote
# list for load-balancing.  Otherwise
# try hosts in the order specified.
;remote-random

# Keep trying indefinitely to resolve the
# host name of the OpenVPN server.  Very useful
# on machines which are not permanently connected
# to the internet such as laptops.
resolv-retry infinite

# Most clients don't need to bind to
# a specific local port number.
nobind

# Downgrade privileges after initialization (non-Windows only)
user nobody
group nogroup

# Try to preserve some state across restarts.
persist-key
persist-tun

# If you are connecting through an
# HTTP proxy to reach the actual OpenVPN
# server, put the proxy server/IP and
# port number here.  See the man page
# if your proxy server requires
# authentication.
;http-proxy-retry # retry on connection failures
;http-proxy [proxy server] [proxy port #]

# Wireless networks often produce a lot
# of duplicate packets.  Set this flag
# to silence duplicate packet warnings.
;mute-replay-warnings

# SSL/TLS parms.
# See the server config file for more
# description.  It's best to use
# a separate .crt/.key file pair
# for each client.  A single ca
# file can be used for all clients.
#ca ca.crt
#cert client.crt
#key client.key
key-direction 1

# Verify server certificate by checking that the
# certicate has the correct key usage set.
# This is an important precaution to protect against
# a potential attack discussed here:
#  http://openvpn.net/howto.html#mitm
#
# To use this feature, you will need to generate
# your server certificates with the keyUsage set to
#   digitalSignature, keyEncipherment
# and the extendedKeyUsage to
#   serverAuth
# EasyRSA can do this for you.
remote-cert-tls server

# If a tls-auth key is used on the server
# then every client must also have the key.
;tls-auth ta.key 1

# Select a cryptographic cipher.
# If the cipher option is used on the server
# then you must also specify it here.
;cipher x
;cipher AES-256-CBC
# 尽量使用GCM模式,CBC模式容易遭受Padding Oracle攻击
cipher AES-256-GCM

# Enable compression on the VPN link.
# Don't enable this unless it is also
# enabled in the server config file.
# comp-lzo
# 从2.5版本开始 comp-lzo 被废弃,要求使用 compress lzo 配置
# https://github.com/Nyr/openvpn-install/issues/430
compress lzo

# Set log file verbosity.
verb 3

# Silence repeating messages
;mute 20

去掉注释后的完整配置为:

client
dev tun
proto tcp
remote www.mobibrw.com 1194
resolv-retry infinite
nobind
user nobody
group nogroup
persist-key
persist-tun
remote-cert-tls server

# 尽量使用GCM模式,CBC模式容易遭受Padding Oracle攻击
cipher AES-256-GCM 

key-direction 1

# 从2.5版本开始 comp-lzo 被废弃,要求使用 compress lzo 配置
# https://github.com/Nyr/openvpn-install/issues/430
compress lzo

verb 3

创建配置生成脚本

新建 ~/client-configs/make_config.sh 文件,复制如下内容:

#!/bin/bash
# First argument: Client identifier

cd ~/openvpn/openvpn-ca
source vars
./build-key ${1}

KEY_DIR=~/openvpn/openvpn-ca/keys
OUTPUT_DIR=~/client-configs/files
BASE_CONFIG=~/client-configs/base.conf
cat ${BASE_CONFIG} \
    <(echo -e '<ca>') \
    ${KEY_DIR}/ca.crt \
    <(echo -e '</ca>\n<cert>') \
    ${KEY_DIR}/${1}.crt \
    <(echo -e '</cert>\n<key>') \
    ${KEY_DIR}/${1}.key \
    <(echo -e '</key>\n<tls-auth>') \
    ${KEY_DIR}/ta.key \
    <(echo -e '</tls-auth>') \
    > ${OUTPUT_DIR}/${1}.ovpn

针对 ubuntu 20.04.6 LTS, 由于OpenVPN版本升级,已经没有上面的命令了,需要调整脚本为如下:

#!/bin/bash
# First argument: Client identifier

cd ~/openvpn/openvpn-ca
#source vars
./easyrsa build-client-full ${1} nopass

PKI_DIR=~/openvpn/openvpn-ca/pki
KEY_DIR=${PKI_DIR}/issued
PRIV_DIR=${PKI_DIR}/private
OUTPUT_DIR=~/client-configs/files
BASE_CONFIG=~/client-configs/base.conf
cat ${BASE_CONFIG} \
    <(echo -e '<ca>') \
    ${PKI_DIR}/ca.crt \
    <(echo -e '</ca>\n<cert>') \
    ${KEY_DIR}/${1}.crt \
    <(echo -e '</cert>\n<key>') \
    ${PRIV_DIR}/${1}.key \
    <(echo -e '</key>\n<tls-auth>') \
    ${PKI_DIR}/ta.key \
    <(echo -e '</tls-auth>') \
    > ${OUTPUT_DIR}/${1}.ovpn

保存并赋予执行权限:

$ chmod 700 ~/client-configs/make_config.sh

生成客户端配置

执行:

$ cd ~/client-configs

$ ./make_config.sh client-mobibrw

使用此脚本生成一个配置文件方便客户端使用。 注:需要生成客户端密钥后才可使用脚本生成配置文件,`client-mobibrw.ovpn` 为刚刚生成的客户端密钥名称 生成后的脚本储存在 `~/client-configs/files` 目录下,名称为 `client-mobibrw.ovpn`。将文件下载到本地即可使用了。

为客户端设置固定IP

首先在OpenVPN所在的文件夹内创建`ccd`文件夹:

$ sudo mkdir -p /etc/openvpn/ccd

然后进入该文件夹并创建与客户端密钥同名的文件:

$ cd /etc/openvpn/ccd

$ vim client-mobibrw

文件内容如下:

ifconfig-push 10.8.0.8 10.8.0.9

此内容意为固定`client-mobibrw`客户端的`OpenVPN`内网`IP`为`10.8.0.8`。

注意,上面的配置在客户端是Windows系统的时候,会报告如下错误:

There is a problem in your selection of --ifconfig endpoints [local=10.8.0.8, remote=10.8.0.9].  The local and remote VPN endpoints cannot use the first or last address within a given 255.255.255.252 subnet.  This is a limitation of --dev tun when used with the TAP-WIN32 driver.  Try 'openvpn --show-valid-subnets' option for more info.

导致这个错误的原因是 TAP-WIN32 使用默认子网掩码 255.255.255.252 因此一个网段之内只能有两个相邻IP,一个是网关,一个是子网设备。

这个问题在 Linux/macOS 系统上是不存在的,这个是 TAP-WIN32 驱动的限制。

因此,如果系统是 Windows 并且使用 TAP-WIN32 驱动,那么配置的地址只能是如下情况:

ifconfig-push 10.8.0.10 10.8.0.9

或者

ifconfig-push 10.8.0.11 10.8.0.10

或者

ifconfig-push 10.8.0.20 10.8.0.19

前面为设备地址,后面为网关IP. 规律就是 设备地址是 网关地址 +1
继续阅读Ubuntu架设OpenVPN实现内网穿透

OpenmediaVault-4.1.3上编译Linux Kernel 4.14.118

也可以使用下面的方式正常编译 `Linux Kernel 4.14.118` 。

建议使用 `VirtualBox` 上建立虚拟机,保证磁盘空间不小于 `32GB`,给 `root` 用户设置密码,远程通过 `shell` 登录到虚拟机。

查询虚拟机的 `IP`地址的时候,使用

# Debian 9 系统上 ip 命令替代了ifconfig
$ ip addr

命令查询。虚拟机网卡设置的时候,设置为桥接模式,默认的 `NAT` 模式无法通过 `SSH` 登录。

然后执行如下命令:

# 解压缩xz格式的linux内核
$ sudo apt-get install xz-utils

$ cd ~

$ wget https://cdn.kernel.org/pub/linux/kernel/v4.x/linux-4.14.118.tar.xz -O linux-4.14.118.tar.xz

$ tar -zxvf linux-4.14.118.tar.xz

$ cd linux-4.14.118/

# 升级系统工具,如果不更新,有时候会出现无法编译的情况
$ sudo apt-get update

$ sudo apt-get upgrade

$ sudo apt-get install make

$ sudo apt-get install build-essential

$ sudo apt-get install kernel-package

$ sudo apt-get install ncurses-dev

$ sudo apt-get install libelf-dev

#一路回车到底
$ make oldconfig

$ make-kpkg --initrd kernel-image kernel_headers

完成后,在当前用户目录下生成 `linux-image-4.14.118_4.14.118-10.00.Custom_amd64.deb` 安装包,执行如下命令安装:

$ cd ~

$ sudo dpkg -i linux-image-4.14.118_4.14.118-10.00.Custom_amd64.deb

参考链接


解决Btrfs文件系统ext2_saved文件磁盘占用异常

最近在 `OpenMediaVault 4.1.22-1(Arrakis)` 进行磁盘文件占用分析的时候,发现磁盘空间异常占用问题,如下:

root@openmediavault:/media/6357625f-c966-49ba-9c90-9e8f8ff50433# du -ah --max-depth=1
0	./lost+found
1.5T	./MyCloud
8.0K	./aquota.user
8.0K	./aquota.group
1.3T	./ext2_saved
4.0K	./WDMyCloudSync.sh
2.7T	.

这个 `1.3T ./ext2_saved`  文件是不存在的,并且没办法通过常规命令删除。

这个文件产生的原因是,执行过 从 `EXT3/4` 转换转化分区到 `Btrfs` :

$ btrfs-convert /media/6357625f-c966-49ba-9c90-9e8f8ff50433

完成转换需要删除备份的 `ext2_saved` 子卷,否则空间会不能释放。 请注意,如果没了它(备份子卷),你将没办法还原回 `EXT3/4` 文件系统。

$ sudo apt-get install btrfs-tools

# 执行,然后无视报错
$ btrfs subvolume delete /media/6357625f-c966-49ba-9c90-9e8f8ff50433/ext2_saved

可能会报错:

ERROR: cannot access subvolume /media/6357625f-c966-49ba-9c90-9e8f8ff50433/ext2_saved: No such file or directory

无视这个报错即可。

完成后再次查询文件占用空间:

root@openmediavault:/media/6357625f-c966-49ba-9c90-9e8f8ff50433# du -ah --max-depth=1
0	./lost+found
1.5T	./MyCloud
8.0K	./aquota.user
8.0K	./aquota.group
4.0K	./WDMyCloudSync.sh
1.5T	.

可以看到空间已经被释放了。

参考链接