继续阅读ubuntu 18.04 systemd-udevd进程CPU占用特别高,禁用WiFi可以解决
ubuntu 18.04 systemd-udevd进程CPU占用特别高,禁用WiFi可以解决
继续阅读ubuntu 18.04 systemd-udevd进程CPU占用特别高,禁用WiFi可以解决
Ubuntu(友邦拓,网友也喜欢称之为“乌班图”)是一个以桌面应用为主的Linux操作系统,其名称来自非洲南部祖鲁语或豪萨语的“ubuntu”一词,意思是“人性”、“我的存在是因为大家的存在”,是非洲传统的一种价值观,类似华人社会的“仁爱”思想.
远程桌面在 Linux
中一般使用 VNC
,下面我们总结一下 ubuntu 16.04/18.04
下的配置总结:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
$ sudo apt-get update # ubuntu 18.04/20.04 默认使用gdm3,导致VNC工作异常,需要切换到 lightdm # ubuntu 16.04 默认使用 lightdm 因此一般不需要调整 $ sudo apt install lightdm # 配置切换到 lightdm $ sudo dpkg-reconfigure gdm3 $ sudo apt install xserver-xorg-video-dummy # 如果从 gdm3 切换到 lightdm 需要重启系统,否则不能正常工作 $ sudo reboot $ sudo apt-get install x11vnc # ubuntu 20.04 # sudo apt-get install net-tools # 设置登录密码,如果不设置密码,会导致任意人都可以登录 $ sudo x11vnc -storepasswd /etc/x11vnc.pass # 需要手工设置一下权限,默认设置的权限可能会导致其他用户无法正常读取 $ sudo chmod 755 /etc/x11vnc.pass # '-rfbport' 参数指定监听端口,'-forever' 参数指定客户端断开后不要停止服务而是继续等待下一次的连接请求 '-rfbauth' 参数指定验证密码的存储文件 $ sudo x11vnc -auth guess -rfbauth /etc/x11vnc.pass -rfbport 5900 -forever -display :0 |
设置开机启动
1 2 3 |
$ sudo apt-get install vim $ sudo vim /etc/systemd/system/x11vnc.service |
里面内容如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
[Unit] Description=x11vnc (Remote access) After=network-online.target [Service] Type=simple ExecStart=/usr/bin/x11vnc -auth guess -rfbauth /etc/x11vnc.pass -rfbport 5900 -forever -display :0 ExecStop=/bin/kill -TERM $MAINPID ExecReload=/bin/kill -HUP $MAINPID KillMode=control-group Restart=on-failure [Install] WantedBy=graphical.target |
配置 systemd
启用服务
1 2 3 4 5 |
$ sudo systemctl daemon-reload $ sudo systemctl enable x11vnc $ sudo systemctl start x11vnc |
如果登录之后,只出现一个桌面背景,没有任何菜单,如下:
参考 Intel NUC(NUC6i3SYH)在不接显示器的情况下VNC不显示桌面(Ubuntu 18.04) 解决,网上搜索相关问题的时候可以使用关键词 HEADLESS X11 查找解决方案,其实就是不插入显示器的情况下,如何强制显卡渲染。
如果系统是ubuntu 21.10版本,则需要编辑
1 2 3 |
# ubuntu 21.10 版本配置文件 $ sudo vim /etc/X11/xorg.conf |
然后在文件尾部,增加如下配置:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
Section "Device" Identifier "Configured Video Device" Driver "dummy" EndSection Section "Monitor" Identifier "Configured Monitor" HorizSync 31.5-48.5 VertRefresh 50-70 EndSection Section "Screen" Identifier "Default Screen" Monitor "Configured Monitor" Device "Configured Video Device" DefaultDepth 24 SubSection "Display" Depth 24 Modes "1920x1080" EndSubSection EndSection |
完成后,重启系统。
注意:如果系统上使用nvidia显卡,需要首先通过nvidia-xconfig生成默认配置,如果没有默认配置,会导致 nvidia-smi找不到显卡,一些使用显卡的计算任务或者机器学习框架会出现问题。
参考命令如下:
1 |
$ sudo nvidia-xconfig -a --virtual=1920x1200 |
可能会生成类似如下配置内容:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# nvidia-xconfig: X configuration file generated by nvidia-xconfig # nvidia-xconfig: version 495.44 Section "ServerLayout" Identifier "Default Layout" Screen 0 "Screen0" InputDevice "Keyboard0" "CoreKeyboard" InputDevice "Mouse0" "CorePointer" EndSection Section "InputDevice" # generated from default Identifier "Keyboard0" Driver "kbd" EndSection Section "InputDevice" # generated from default Identifier "Mouse0" Driver "mouse" Option "Protocol" "auto" Option "Device" "/dev/psaux" Option "Emulate3Buttons" "no" Option "ZAxisMapping" "4 5" EndSection Section "Monitor" Identifier "Monitor0" VendorName "Unknown" ModelName "Unknown" Option "DPMS" EndSection Section "Device" Identifier "Device0" Driver "nvidia" VendorName "NVIDIA Corporation" BoardName "NVIDIA GeForce RTX 3060" EndSection Section "Screen" Identifier "Screen0" Device "Device0" Monitor "Monitor0" DefaultDepth 24 SubSection "Display" Virtual 1920 1200 Depth 24 EndSubSection EndSection |
然后在配置文件的尾部增加
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
Section "Device" Identifier "Configured Video Device" Driver "dummy" EndSection Section "Monitor" Identifier "Configured Monitor" HorizSync 31.5-48.5 VertRefresh 50-70 EndSection Section "Screen" Identifier "Default Screen" Monitor "Configured Monitor" Device "Configured Video Device" DefaultDepth 24 SubSection "Display" Depth 24 Virtual 1920 1200 Modes "1920x1200" EndSubSection EndSection |
最后修改Section "ServerLayout"字段里的Screen 0为新增的屏幕(Section "Screen" 字段中的 Identifier定义的名字)。
修改参考如下:
1 2 3 4 5 6 |
Section "ServerLayout" Identifier "Default Layout" Screen 0 "Default Screen" InputDevice "Keyboard0" "CoreKeyboard" InputDevice "Mouse0" "CorePointer" EndSection |
修改后的完整内如如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# nvidia-xconfig: X configuration file generated by nvidia-xconfig # nvidia-xconfig: version 495.44 Section "ServerLayout" Identifier "Default Layout" Screen 0 "Default Screen" InputDevice "Keyboard0" "CoreKeyboard" InputDevice "Mouse0" "CorePointer" EndSection Section "InputDevice" # generated from default Identifier "Keyboard0" Driver "kbd" EndSection Section "InputDevice" # generated from default Identifier "Mouse0" Driver "mouse" Option "Protocol" "auto" Option "Device" "/dev/psaux" Option "Emulate3Buttons" "no" Option "ZAxisMapping" "4 5" EndSection Section "Monitor" Identifier "Monitor0" VendorName "Unknown" ModelName "Unknown" Option "DPMS" EndSection Section "Device" Identifier "Device0" Driver "nvidia" VendorName "NVIDIA Corporation" BoardName "NVIDIA GeForce RTX 3060" EndSection Section "Screen" Identifier "Screen0" Device "Device0" Monitor "Monitor0" DefaultDepth 24 SubSection "Display" Virtual 1920 1200 Depth 24 EndSubSection EndSection Section "Device" Identifier "Configured Video Device" Driver "dummy" EndSection Section "Monitor" Identifier "Configured Monitor" HorizSync 31.5-48.5 VertRefresh 50-70 EndSection Section "Screen" Identifier "Default Screen" Monitor "Configured Monitor" Device "Configured Video Device" DefaultDepth 24 SubSection "Display" Depth 24 Virtual 1920 1200 Modes "1920x1200" EndSubSection EndSection |
尽管经过上面的设置,可以正确使用VNC,但是更推荐使用RDP协议,功能更丰富,性能更高,安全性更好。参考 VNC 还是 RDP? 云上的远程桌面究竟该如何选。
ubuntu 16.04.5
升级到 ubuntu 18.04.1
启动时出现 "Error: no symbol table"
1 2 |
# 升级系统执行如下命令 $ sudo do-release-upgrade -d |
重启之后,出现错误信息
1 2 |
No symbol table found. Press any key to continue... |
不做任何操作,过几秒之后,能跳过。
解决方法如下:
1 2 3 4 5 |
$ sudo grub-install /dev/sda $ sudo update-grub $ sudo reboot |
ubuntu 16.04.5
升级到 ubuntu 18.04.1
出现 systemd-shim
升级失败,重启系统之后,继续执行软件升级,出现如下错误信息:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
$ sudo apt-get dist-upgrade 正在读取软件包列表... 完成 正在分析软件包的依赖关系树 正在读取状态信息... 完成 正在计算更新... 完成 下列软件包将被【卸载】: systemd-shim 下列软件包将被升级: apache2 apache2-bin apache2-data apache2-utils libmspack0 linux-firmware 升级了 6 个软件包,新安装了 0 个软件包,要卸载 1 个软件包,有 0 个软件包未被升级。 有 1 个软件包没有被完全安装或卸载。 需要下载 0 B/72.5 MB 的归档。 解压缩后会消耗 4,250 kB 的额外空间。 您希望继续执行吗? [Y/n] (正在读取数据库 ... 系统当前共安装有 415046 个文件和目录。) 正在卸载 systemd-shim (9-1bzr4ubuntu1) ... 正在删除 systemd-shim 导致 /usr/share/dbus-1/system-services/org.freedesktop.systemd1.service 转移到 /usr/share/dbus-1/system-services/org.freedesktop.systemd1.service.systemd dpkg-divert: 错误: 更改 /usr/share/dbus-1/system-services/org.freedesktop.systemd1.service.systemd 文件名会覆盖 /usr/share/dbus-1/system-services/org.freedesktop.systemd1.service, 不允许此操作 dpkg: 处理软件包 systemd-shim (--remove)时出错: installed systemd-shim package post-removal script subprocess returned error exit status 2 在处理时有错误发生: systemd-shim E: Sub-process /usr/bin/dpkg returned an error code (1) |
如果语言是英文的,可能会出现如下信息:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
$ sudo apt-get dist-upgrade Reading package lists... Done Building dependency tree Reading state information... Done Calculating upgrade... Done The following packages will be REMOVED: systemd-shim The following packages will be upgraded: apache2 apache2-bin apache2-data apache2-utils libmspack0 linux-firmware 6 upgraded, 0 newly installed, 1 to remove and 0 not upgraded. 1 not fully installed or removed. Need to get 0 B/72.5 MB of archives. After this operation, 4,250 kB of additional disk space will be used. Do you want to continue? [Y/n] (Reading database ... 415046 files and directories currently installed.) Removing systemd-shim (9-1bzr4ubuntu1) ... Removing 'diversion of /usr/share/dbus-1/system-services/org.freedesktop.systemd1.service to /usr/share/dbus-1/system-services/org.freedesktop.systemd1.service.systemd by systemd-shim' dpkg-divert: error: rename involves overwriting '/usr/share/dbus-1/system-services/org.freedesktop.systemd1.service' with different file '/usr/share/dbus-1/system-services/org.freedesktop.systemd1.service.systemd', not allowed dpkg: error processing package systemd-shim (--remove): installed systemd-shim package post-removal script subprocess returned error exit status 2 Errors were encountered while processing: systemd-shim E: Sub-process /usr/bin/dpkg returned an error code (1) |
解决方案如下:
1 2 3 4 5 |
$ sudo mv /usr/share/dbus-1/system-services/org.freedesktop.systemd1.service /usr/share/dbus-1/system-services/org.freedesktop.systemd1.service.bak $ sudo apt-get dist-upgrade $ sudo rm -rf /usr/share/dbus-1/system-services/org.freedesktop.systemd1.service.bak |
更加推荐通过 Podman/Docker 方式进行部署,参考链接 Ubuntu 22.04使用Podman部署OpenGrok的详细教程 。
OpenGrok 是一个快速,便于使用的源码搜索引擎与对照引擎,它能够帮助我们快速的搜索、定位、对照代码树。接下来就具体讲解 Ubuntu 16.04/18.04/20.04 LTS
环境下 OpenGrok
的安装及使用。
OpenGrok 1.3.11/1.3.16
依赖 Java 1.8
, Tomcat 8
OpenGrok 1.5.12
依赖 Java 11
, Tomcat 9
。
从 OpenGrok 1.6.0
开始依赖 Java 11
, Tomcat 10
。
1.依旧参照 UBUNTU 13.10 APACHE 2.2 通过 AJP 整合 TOMCAT 7 中讲述的方法,进行 Tomcat 8/9
, Apache 2.4
的配置安装,只不过路径中的 Tomcat7
目录替换成 Tomcat8
(ubuntu 20.04 默认 Tomcat9
)。
2.安装 Tomcat 8
(ubuntu 18.04)
1 2 3 4 5 6 7 8 9 10 |
$ sudo apt-get install tomcat8 # 一般卸载 tomcat7 $ sudo apt-get remove --purge tomcat7* # 调整内存,默认的128MB 默认配置运行会导致OOM $ sudo touch /usr/share/tomcat8/bin/setenv.sh $ echo 'export JAVA_OPTS="-Xms512M -Xmx1024M"' | sudo tee -a /usr/share/tomcat8/bin/setenv.sh |
安装 Tomcat 9
(ubuntu 20.04)
1 2 3 4 5 6 7 8 9 10 11 12 |
# 一般卸载 tomcat7/8 $ sudo apt-get remove --purge tomcat7* $ sudo apt-get remove --purge tomcat8* $ sudo apt-get install tomcat9 # 调整内存,默认的128MB 默认配置运行会导致OOM # sudo touch /usr/share/tomcat9/bin/setenv.sh $ echo 'export JAVA_OPTS="-Xms512M -Xmx1024M"' | sudo tee -a /usr/share/tomcat9/bin/setenv.sh |
3.安装 universal-ctags
用于对 C\C++
代码的支持
给代码建立索引时,要使用到universal-ctags
工具,但是一般通过apt-get安装的都是exuberant-ctags
,所以要先删除原有的ctags版本,然后安装universal-ctags
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
$ sudo apt-get purge ctags $ sudo apt-get install git $ sudo apt-get install autoconf $ sudo apt-get install pkg-config $ cd ~ $ git clone --depth=1 https://github.com/universal-ctags/ctags.git $ cd ctags $ ./autogen.sh $ ./configure $ make $ sudo make install $ cd .. $ rm -rf ctags |
4.下载并安装OpenGrok
可以到"https://oracle.github.io/opengrok/"手工下载文件,然后上传到服务器,也可以直接用wget
命令来下载,一般选择安装在"/opt
"目录下面。
1 2 3 4 5 6 7 8 9 10 11 |
$ sudo apt-get install aria2 $ cd /opt $ sudo aria2c -c "https://github.com/oracle/opengrok/releases/download/1.3.11/opengrok-1.3.11.tar.gz" # sudo aria2c -c "https://github.com/oracle/opengrok/releases/download/1.3.16/opengrok-1.3.16.tar.gz" # sudo aria2c -c "https://github.com/oracle/opengrok/releases/download/1.5.12/opengrok-1.5.12.tar.gz" # sudo aria2c -c "https://github.com/oracle/opengrok/releases/download/1.7.25/opengrok-1.7.25.tar.gz" |
解压缩文件到当前目录"/opt
"
1 2 3 4 5 6 7 |
$ sudo tar xvf opengrok-1.3.11.tar.gz # sudo tar xvf opengrok-1.3.16.tar.gz # sudo tar xvf opengrok-1.5.12.tar.gz # sudo tar xvf opengrok-1.7.25.tar.gz |
创建一个软链接,方便后续的修改
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
$ sudo ln -s opengrok-1.3.11 /opt/opengrok # sudo ln -s opengrok-1.3.16 /opt/opengrok # sudo ln -s opengrok-1.5.12 /opt/opengrok # sudo ln -s opengrok-1.7.25 /opt/opengrok # 安装工具,必须是Python3 ,Python2 安装会失败 $ sudo apt-get install python3-pip $ sudo pip3 install --upgrade pip # 如果报错 ModuleNotFoundError: No module named 'pip._internal' # 执行如下命令 # sudo python3 -m pip install --upgrade pip # 解决 AttributeError: module 'setuptools.dist' has no attribute 'check_specifier' $ sudo pip3 install --upgrade setuptools $ sudo pip3 install /opt/opengrok/tools/opengrok-tools.tar.gz |
链接"/opt/opengrok/lib/source.war
"到 Tomcat8
的工程目录"/var/lib/tomcat8/webapps/
",比如我们有多个源代码工程,建议进行链接操作。
如下:
1 2 3 4 5 6 7 |
$ sudo ln -s /opt/opengrok/lib/source.war /var/lib/tomcat8/webapps/Android_4.2.2.war $ sudo ln -s /opt/opengrok/lib/source.war /var/lib/tomcat8/webapps/Android_7.0.0_r21.war $ sudo ln -s /opt/opengrok/lib/source.war /var/lib/tomcat8/webapps/Android_10.0.0_r40.war $ sudo ln -s /opt/opengrok/lib/source.war /var/lib/tomcat8/webapps/Chromium.war |
访问"http://localhost:8080/source/"确认OpenGrok是否已经安装成功,如果安装成功,出现下面的界面:
5.出于安全原因,禁止外网访问Tomcat的8080端口
只允许Tomcat在本地的8080端口监听即可,修改
1 |
$ sudo vim /var/lib/tomcat8/conf/server.xml |
添加 address="127.0.0.1"
1 2 3 |
<Connector port="8080" address="127.0.0.1" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" /> |
重启Tomcat8
1 |
$ sudo service tomcat8 restart |
6.配置Apache2对Tomcat通过AJP进行反向代理
Apache2
的配置文件“ /etc/apache2/sites-enabled/000-default.conf
” (如果开启了HTTPS,则需要同步修改 /etc/apache2/sites-enabled/000-default-le-ssl.conf
或者 /etc/apache2/sites-enabled/default-ssl.conf
) 中,增加如下配置:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<VirtualHost *:80> #for Tomcat 8 openGrok Android Source ProxyPass /Android_4.2.2/ ajp://127.0.0.1:8009/Android_4.2.2/ ProxyPassReverse /AndroidXRef_4.2.2/ ajp://127.0.0.1:8009/AndroidXRef_4.2.2/ ProxyPass /Android_7.0.0_r21/ ajp://127.0.0.1:8009/Android_7.0.0_r21/ ProxyPassReverse /Android_7.0.0_r21/ ajp://127.0.0.1:8009/Android_7.0.0_r21/ ProxyPass /Android_10.0.0_r40/ ajp://127.0.0.1:8009/Android_10.0.0_r40/ ProxyPassReverse /Android_10.0.0_r40/ ajp://127.0.0.1:8009/Android_10.0.0_r40/ #for Tomcat 8 OpenGrok Chromium ProxyPass /Chromium/ ajp://127.0.0.1:8009/Chromium/ ProxyPassReverse /Chromium/ ajp://127.0.0.1:8009/Chromium/ </VirtualHost> |
Tomcat8
的配置文件/var/lib/tomcat8/conf/server.xml
中增加如下配置<Context path="/Android_4.2.2" docBase="Android_4.2.2/"/>,解决跳转404问题。ProxyPass
后面必须携带“/”,否则就会出现404问题。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
<Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true"> <Context path="/Android_4.2.2" docBase="Android_4.2.2/"/> <Context path="/Android_7.0.0.r21" docBase="Android_7.0.0_r21/"/> <Context path="/Android_10.0.0_r40" docBase="Android_10.0.0_r40/"/> <Context path="/Chromium" docBase="Chromium/"/> <!-- SingleSignOn valve, share authentication between web applications Documentation at: /docs/config/valve.html --> <!-- <Valve className="org.apache.catalina.authenticator.SingleSignOn" /> --> <!-- Access log processes all example. Documentation at: /docs/config/valve.html Note: The pattern used is equivalent to using pattern="common" --> <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="localhost_access_log." suffix=".txt" pattern="%h %l %u %t "%r" %s %b" /> <!-- 隐藏版本号 --> <Valve className="org.apache.catalina.valves.ErrorReportValve" showReport="false" showServerInfo="false" /> </Host> |
7.配置OpenGrok
对源代码进行解析
设置要建立索引的源代码目录的位置,以存储在"/data/OpenGrok/Android_4.2.2
"上的Android
代码为例子:
1 2 3 |
$ export OPENGROK_INSTANCE_BASE=/data/OpenGrok/Android_4.2.2/ $ export OPENGROK_WEBAPP_CONTEXT=Android_4.2.2 |
注意:
由于我们使用了Apache2的反向代理才需要设置 OPENGROK_WEBAPP_CONTEXT
,如果没有设置反向代理,请不要设置。
OPENGROK_WEBAPP_CONTEXT
内容就是在 Apache2
中设置的 ProxyPassReverse
指定的参数。
在使用反向代理的时候如果不设置OPENGROK_WEBAPP_CONTEXT
会导致在点击具体的变量定义的时候,出现404。
不使用反向代理的时候请只设置OPENGROK_INSTANCE_BASE
。
这个变量的本质功能就是在建立文件索引的时候,在链接头部增加 OPENGROK_WEBAPP_CONTEXT
指定的路径,比如原来的路径是 https://www.mobibrw.com/a.html ,那么指定路径后,就变成了 https://www.mobibrw.com/ Android_4.2.2/a.html。
创建源代码索引
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
$ cd /data/OpenGrok/Android_4.2.2/src # 删除源代码下面可能存在的".git",".svn"隐藏目录 $ find . -name .svn -print0 | xargs -0 rm -r -f $ find . -name .git -print0 | xargs -0 rm -r -f # 删除无法进行索引的文件 $ find . -name *.apk -print0 | xargs -0 rm -r -f $ find . -name *.zip -print0 | xargs -0 rm -r -f $ find . -name *.jar -print0 | xargs -0 rm -r -f # 编译工具是没有必要的目录 $ rm -rf prebuilts $ export IDX_ROOT=/data/OpenGrok/Android_4.2.2/ # Android 源代码目录 $ export IDX_SRC=${IDX_ROOT}src/ $ export IDX_DATA=${IDX_ROOT}data/ $ export IDX_CONF=${IDX_ROOT}etc/configuration.xml # 清理以前生成的索引数据 # 索引数据目录 $ cd ${IDX_ROOT} $ rm -rf data # 日志目录 $ rm -rf log # 配置文件目录 $ rm -rf etc $ rm -rf logging.properties $ mkdir etc # 限制索引内存占用,避免建立索引过程中引发OOM, # 如果依旧出现OOM,则在参数中指定 “-T 1” 限制只能使用一个线程,不允许并发,减少内存开销 # 其实由于磁盘IO是整个索引的性能瓶颈,因此多线程并不能缩短太多的时间,反而占用了太多的内存 # 因此限制只能使用一个线程,在大多数情况下,是没问题的 # 同时使用 "-m 256" 参数增加 Lucene 4.x 使用的缓存的大小,避免出现问题 # Android 10的源代码至少需要2GB内存才能成功索引 $ export JAVA_OPTS="-Xmx2048m" $ opengrok-indexer -J=-Xmx2g -a /opt/opengrok/lib/opengrok.jar -- -m 256 -s $IDX_SRC -d $IDX_DATA -T 1 -H -P -S -G -W $IDX_CONF |
执行时间在40分钟左右,执行完成 。(如果通过SSH远程登录,可能会出现中途连接断开的情况,原因为某项操作比较耗时,导致长时间没有数据通信,网络超时断开。 参考 Linux SSH保持连接(解决Broken pipe))生成的索引文件在源代码的"data
"目录下面,重建索引的时候需要执行如下操作,才能再次建立索引
1 2 3 4 5 6 7 8 9 10 11 12 13 |
# 切换回到项目根目录 $ cd ${IDX_ROOT} # 索引数据目录 $ rm -rf data # 日志目录 $ rm -rf log # 配置文件目录 $ rm -rf etc $ rm -rf logging.properties |
注意,请务必删除源代码中的"prebuilts"目录,这个目录下面存储的是一系列的编译工具,在浏览代码的时候,完全用不上,但是占据的磁盘空间确是巨大的。
注意,如果服务器上面的内存比较有限,请使用如下命令进行内存限制,否则建立索引的时候,会触发内存不足的情况:
1 2 3 4 |
# 早期版本使用如下参数限制内存 $ export JAVA_OPTS="-Xmx1024m" # 最新版本OpenGrok-1.3.11/1.3.16使用-J=-Xmx2g 进行内存限制,Android 10的源代码至少需要2GB内存才能成功索引 |
修改OpenGrok
配置文件
1 |
$ sudo vim /var/lib/tomcat8/webapps/Android_4.2.2/WEB-INF/web.xml |
修改其中的
1 2 3 4 5 |
<context-param> <param-name>CONFIGURATION</param-name> <param-value>/var/opengrok/etc/configuration.xml</param-value> <description>Full path to the configuration file where OpenGrok can read it's configuration</description> </context-param> |
为具体的工程目录"/data/OpenGrok/Android_4.2.2
",修改后的配置如下:
1 2 3 4 5 |
<context-param> <param-name>CONFIGURATION</param-name> <param-value>/data/OpenGrok/Android_4.2.2/etc/configuration.xml</param-value> <description>Full path to the configuration file where OpenGrok can read it's configuration</description> </context-param> |
刷新浏览器,可以看到Android_4.2.2
的源码可以搜索出来了。
家里的网络因为没有公网 IP,有时候想要连接到家里的树莓派或者电脑就无法实现。这个时候可以采用内网穿透的方法远程连接家中的机器,内网穿透的方案有很多,下面介绍一种采用 OpenVPN 实现内网穿透的方案。
主机:腾讯云/阿里云
操作系统:Ubuntu 16.04/18.04/20.04
首先,我们需要在服务器安装 OpenVPN。在 Ubuntu 系统中我们可以通过 apt 简单的进行安装。同时我们也需要安装 easy-rsa,它可以帮助我们生成 VPN 使用过程中所需的 CA 证书。
1 2 3 |
$ sudo apt-get update $ sudo apt-get install openvpn easy-rsa |
OpenVPN 是使用 TLS/SSL 的 VPN。这意味着它利用证书来加密服务器和客户端之间的通信。为了发布受信任的证书,我们需要建立一个自己的简单的证书颁发机构(CA)。 使用 make-cadir
命令复制 easy-rsa 模板到 home 目录。
1 |
$ make-cadir ~/openvpn/openvpn-ca |
接着进入刚刚新建的目录准备配置 CA:
1 2 3 |
$ cd ~/openvpn/openvpn-ca $ ln -s openssl-1.0.0.cnf openssl.cnf |
进入 ~/openvpn/openvpn-ca
目录后,我们需要修改 vars
文件,以便于生成需要的 CA 值。
1 |
$ vim vars |
在文件底部找到以下配置:
1 2 3 4 5 6 7 8 |
... 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" ... |
将这些变量修改为任意你喜欢的值,但是不要为空:
1 2 3 4 5 6 7 8 |
... 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
:
1 |
export KEY_NAME="server" |
针对 ubuntu 16.04 LTS/ubuntu 18.04 LTS 执行如下操作:
首先进入你的 CA 目录,然后执行 source vars
:
1 2 3 |
$ cd ~/openvpn/openvpn-ca $ source vars |
接着会有以下输出:
1 |
NOTE: If you run ./clean-all, I will be doing a rm -rf on /home/xxxx/openvpn/openvpn-ca/keys |
执行下列操作确保操作环境干净:
1 |
$ ./clean-all |
现在我们可以构建根 CA:
1 2 3 4 5 6 7 8 9 |
# 如果执行时候报错 # 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 文件,所有值应该都会自动填充。所以,一路回车就好了:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
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版本升级,已经没有上面的命令了,需要调整脚本为如下:
1 2 3 4 5 |
$ cd ~/openvpn/openvpn-ca $ ./easyrsa init-pki $ ./easyrsa build-server-full <SERVER_NAME> nopass |
通过下列命令生成服务器端证书和秘钥:
1 |
$ ./build-key-server server |
注:server
就是刚才在 vars
文件中修改的 KEY_NAME
变量的值。请不要使用别的名字! 然后一直回车选择默认值即可,不要设置 challenge password
,直接回车即可。到最后,你需要输入两次 y 注册证书和提交。
1 2 3 4 5 6 |
... 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 密钥:
1 |
$ ./build-dh |
这个操作大约会花费几分钟不等。 然后,我们可以生成 HMAC 签名加强服务器的 TLS 完整性验证功能:
1 |
$ openvpn --genkey --secret keys/ta.key |
首先将刚刚生成的各类文件复制到 OpenVPN 目录下:
1 2 3 |
$ cd ~/openvpn/openvpn-ca/keys $ sudo cp ca.crt ca.key server.crt server.key ta.key dh2048.pem /etc/openvpn |
然后,解压并复制一个 OpenVPN 配置文件到 OpenVPN 目录:
1 2 3 |
$ 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
:
1 2 |
proto tcp ;proto udp |
找到 tls-auth
位置,去掉注释,并在下面新增一行:
1 2 |
tls-auth ta.key 0 # This file is secret key-direction 0 |
去掉 user
和 group
行前的注释:
1 2 |
user nobody group nogroup |
去掉 client-to-client
行前的注释允许客户端之间互相访问:
1 |
client-to-client |
开启客户端固定 IP 配置文件夹:
1 |
client-config-dir ccd |
去掉注释后的完整配置如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 |
################################################# # 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 |
去掉注释之后,精简的关键信息如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
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
防火墙状态,如果状态为开启,则调整。如果防火墙是关闭的,则不需要调整。
1 |
$ sudo ufw status |
如果输出是Status: inactive
, 那么不需要进行下面的调整。
注意,下面的调整如果中间出现问题,可能导致防火墙拦截SSH
进而无法进行恢复操作。如果出现问题,可以使用阿里云或者腾讯云提供的远程控制台在网页端进行恢复操作。
注意,如果服务器是从早期版本的ubuntu 14.04
升级到ubunt 16.04
的,那么可能会出现如下警告信息:
1 2 3 |
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
) ,这两个配置文件。
我们需要删除早期的防火墙配置文件,如下:
1 |
$ sudo rm -rf /etc/ufw/applications.d/apache2.2-common |
如果服务器上UFW
首次启用, 需要手工许可若干常用服务,否则无法远程访问:
1 2 3 4 5 6 7 |
# 默认情况下存在 "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" |
当然,也可以直接配置需要开放的端口,但是不推荐,如下:
1 2 |
$ sudo ufw allow 80/tcp $ sudo ufw allow 443/tcp |
如果需要删除特定的规则,那么参考如下命令:
1 2 3 4 5 6 7 8 9 |
$ 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 端口并使变化生效
1 2 3 4 5 |
$ sudo ufw allow 1194/tcp $ sudo ufw disable $ sudo ufw enable |
执行:
1 2 3 4 5 6 |
$ sudo mkdir -p /etc/openvpn/ccd $ sudo systemctl start openvpn@server #如果启动失败,则执行如下命令观察日志 # sudo tail -f /var/log/syslog |
设置开机自启:
1 |
$ sudo systemctl enable openvpn@server |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
$ 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
否则会跳过最后一步,导致生成证书的时候会缺少部分信息。
1 2 3 4 5 6 7 |
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 |
注意,如果有多个客户端的话,建议为每个客户端生成一份配置文件。
执行下列命令,生成客户端配置的基础文件:
1 2 3 4 5 |
$ 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
:
1 2 3 4 5 |
# Are we connecting to a TCP or # UDP server? Use the same setting as # on the server. proto tcp ;proto udp |
去掉 user
和 group
前的注释:
1 2 3 |
# Downgrade privileges after initialization (non-Windows only) user nobody group nogroup |
找到 ca/cert/key,注释掉:
1 2 3 4 5 6 7 8 9 |
# 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 |
在文件中新增一行:
1 |
key-direction 1 |
保存退出文件。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
############################################## # 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 |
去掉注释后的完整配置为:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
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 文件,复制如下内容:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#!/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版本升级,已经没有上面的命令了,需要调整脚本为如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
#!/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 |
保存并赋予执行权限:
1 |
$ chmod 700 ~/client-configs/make_config.sh |
执行:
1 2 3 |
$ cd ~/client-configs $ ./make_config.sh client-mobibrw |
使用此脚本生成一个配置文件方便客户端使用。 注:需要生成客户端密钥后才可使用脚本生成配置文件,client-mobibrw.ovpn
为刚刚生成的客户端密钥名称 生成后的脚本储存在 ~/client-configs/files
目录下,名称为 client-mobibrw.ovpn
。将文件下载到本地即可使用了。
首先在 OpenVPN 所在的文件夹内创建ccd
文件夹:
1 |
$ sudo mkdir -p /etc/openvpn/ccd |
然后进入该文件夹并创建与客户端密钥同名的文件:
1 2 3 |
$ cd /etc/openvpn/ccd $ vim client-mobibrw |
文件内容如下:
1 |
ifconfig-push 10.8.0.8 10.8.0.9 |
此内容意为固定client-mobibrw
客户端的OpenVPN
内网IP
为10.8.0.8
。
注意,上面的配置在客户端是Windows系统的时候,会报告如下错误:
1 |
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 驱动,那么配置的地址只能是如下情况:
1 |
ifconfig-push 10.8.0.10 10.8.0.9 |
或者
1 |
ifconfig-push 10.8.0.11 10.8.0.10 |
或者
1 |
ifconfig-push 10.8.0.20 10.8.0.19 |
前面为设备地址,后面为网关IP. 规律就是 设备地址是 网关地址 +1
继续阅读Ubuntu架设OpenVPN实现内网穿透
最近在 OpenMediaVault 4.1.22-1(Arrakis)
进行磁盘文件占用分析的时候,发现磁盘空间异常占用问题,如下:
1 2 3 4 5 6 7 8 |
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
:
1 |
$ btrfs-convert /media/6357625f-c966-49ba-9c90-9e8f8ff50433 |
完成转换需要删除备份的 ext2_saved
子卷,否则空间会不能释放。 请注意,如果没了它(备份子卷),你将没办法还原回 EXT3/4
文件系统。
1 2 3 4 |
$ sudo apt-get install btrfs-tools # 执行,然后无视报错 $ btrfs subvolume delete /media/6357625f-c966-49ba-9c90-9e8f8ff50433/ext2_saved |
可能会报错:
1 |
ERROR: cannot access subvolume /media/6357625f-c966-49ba-9c90-9e8f8ff50433/ext2_saved: No such file or directory |
无视这个报错即可。
完成后再次查询文件占用空间:
1 2 3 4 5 6 7 |
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 . |
可以看到空间已经被释放了。
在编译陈硕github上面的代码时,遇到了链接错误:
1 2 3 4 5 6 7 8 9 10 |
../Thread.o: In function `(anonymous namespace)::ThreadNameInitializer::ThreadNameInitializer()': Thread.cpp:(.text+0x5f): undefined reference to `pthread_atfork' ../Thread.o: In function `muduo::Thread::~Thread()': Thread.cpp:(.text+0x3f9): undefined reference to `pthread_detach' ../Thread.o: In function `muduo::Thread::start()': Thread.cpp:(.text+0x50e): undefined reference to `pthread_create' ../Thread.o: In function `muduo::Thread::join()': Thread.cpp:(.text+0x5ce): undefined reference to `pthread_join' collect2: error: ld returned 1 exit status |
在编译时已经加了-lpthread,还是有这个问题。
在这里找到了答案:http://stackoverflow.com/questions/2373109/what-library-to-be-to-be-used-to-avoid-undefined-reference-to-pthread-atfork
pthread_atfork是POSIX标准,在编译时要加上-pthread
-lpthread是老版本的gcc编译器用的,在新版本中应该用-pthread取代-lpthread
解决undefined reference to pthread_atfork
/pthread_detach
/pthread_create
/pthread_join
ubuntu 16.04下使用APT安装的MySQL的数据库,目录同时接受apparmor的管理,因此在修改数据库目录的时候,需要同步更新apparmor的配置文件。如果只是迁移数据库的话
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
$ sudo service mysql stop $ sudo mv /var/lib/mysql /data/ $ sudo ln -s /data/mysql /var/lib/mysql $ sudo cp /etc/apparmor.d/usr.sbin.mysqld /etc/apparmor.d/usr.sbin.mysqld.bak # 增加数据库的访问目录权限,增加如下目录的权限 # /data/mysql/ r, # /data/mysql/** rw, $ sudo sed -i "s/^[ \t]*\/var\/lib\/mysql\/\*\* rw,/ \/var\/lib\/mysql\/** rw,\n \/data\/mysql r,\n \/data\/mysql\/** rwk,/g" /etc/apparmor.d/usr.sbin.mysqld # 检查完成无误之后,需要删除备份的 usr.sbin.mysqld.bak ,老版本的 apparmor 不会解析备份的文件/或者解析顺序变更了,但是新版本的会解析,导致出现两个不同的配置同时存在,引起异常 $ sudo rm -rf /etc/apparmor.d/usr.sbin.mysqld.bak $ sudo service apparmor restart $ sudo service mysql start |
对于日志文件一起迁移的情况
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
$ sudo service tomcat7 stop $ sudo service denyhosts stop $ sudo service php7.0-fpm stop $ sudo service apache2 stop $ sudo service mysql stop $ sudo service nginx stop $ sudo service apparmor stop $ sudo mv /var/log /data/ $ sudo ln -s /data/log /var/log # 配置日志文件的访问目录权限,增加如下目录的权限 # /data/log/mysql.err rw, # /data/log/mysql.log rw, # /data/log/mysql/ r, # /data/log/mysql/** rw, $ sudo sed -i "s/^[ \t]*\/var\/log\/mysql\/\*\* rw,/ \/var\/log\/mysql\/** rw,\n\n \/data\/log\/mysql.err rw,\n \/data\/log\/mysql.log rw,\n \/data\/log\/mysql\/ r,\n \/data\/log\/mysql\/** rw,/g" /etc/apparmor.d/usr.sbin.mysqld # 逐个启动服务,不如直接重启系统 $ sudo reboot |
如果依旧启动失败,并且 MySQL
是从低版本升级上来的,并且目前正在使用的版本大于或者等于 MySQL 5.7
,执行 journalctl -xe
观察到类似如下内容:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
-- Unit mysql.service has begun starting up. Aug 20 10:16:50 AY130422143404983ad9 audit[8762]: AVC apparmor="DENIED" operation="open" profile="/usr/sbin/mysqld" name="/proc/8762/status" pid=8762 comm="mysqld" requested_mask="r" denied_mask= Aug 20 10:16:50 AY130422143404983ad9 kernel: audit_printk_skb: 21 callbacks suppressed Aug 20 10:16:50 AY130422143404983ad9 kernel: audit: type=1400 audit(1566267410.233:325): apparmor="DENIED" operation="open" profile="/usr/sbin/mysqld" name="/proc/8762/status" pid=8762 comm="mysq Aug 20 10:16:50 AY130422143404983ad9 audit[8762]: AVC apparmor="DENIED" operation="open" profile="/usr/sbin/mysqld" name="/sys/devices/system/node/" pid=8762 comm="mysqld" requested_mask="r" deni Aug 20 10:16:50 AY130422143404983ad9 audit[8762]: AVC apparmor="DENIED" operation="open" profile="/usr/sbin/mysqld" name="/proc/8762/status" pid=8762 comm="mysqld" requested_mask="r" denied_mask= Aug 20 10:16:50 AY130422143404983ad9 kernel: audit: type=1400 audit(1566267410.237:326): apparmor="DENIED" operation="open" profile="/usr/sbin/mysqld" name="/sys/devices/system/node/" pid=8762 co Aug 20 10:16:50 AY130422143404983ad9 kernel: audit: type=1400 audit(1566267410.237:327): apparmor="DENIED" operation="open" profile="/usr/sbin/mysqld" name="/proc/8762/status" pid=8762 comm="mysq Aug 20 10:16:50 AY130422143404983ad9 mysqld[8762]: 2019-08-20T02:16:50.244672Z 0 [Warning] Changed limits: max_open_files: 1024 (requested 5000) Aug 20 10:16:50 AY130422143404983ad9 mysqld[8762]: 2019-08-20T02:16:50.244721Z 0 [Warning] Changed limits: table_open_cache: 431 (requested 2000) Aug 20 10:16:50 AY130422143404983ad9 mysqld[8762]: 2019-08-20T02:16:50.418600Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server Aug 20 10:16:50 AY130422143404983ad9 mysqld[8762]: 2019-08-20T02:16:50.418629Z 0 [Warning] 'NO_ZERO_DATE', 'NO_ZERO_IN_DATE' and 'ERROR_FOR_DIVISION_BY_ZERO' sql modes should be used with strict Aug 20 10:16:50 AY130422143404983ad9 mysqld[8762]: 2019-08-20T02:16:50.420618Z 0 [Note] /usr/sbin/mysqld (mysqld 5.7.27-0ubuntu0.16.04.1-log) starting as process 8762 ... Aug 20 10:16:50 AY130422143404983ad9 mysqld[8762]: 2019-08-20T02:16:50.422604Z 0 [ERROR] Could not open file '/var/log/mysql/error.log' for error logging: Permission denied Aug 20 10:16:50 AY130422143404983ad9 mysqld[8762]: 2019-08-20T02:16:50.423358Z 0 [ERROR] Aborting Aug 20 10:16:50 AY130422143404983ad9 mysqld[8762]: 2019-08-20T02:16:50.423499Z 0 [Note] Binlog end Aug 20 10:16:50 AY130422143404983ad9 mysqld[8762]: 2019-08-20T02:16:50.423685Z 0 [Note] /usr/sbin/mysqld: Shutdown complete Aug 20 10:16:50 AY130422143404983ad9 systemd[1]: mysql.service: Main process exited, code=exited, status=1/FAILURE |
那么,此时的MySQL
的 apparmor
配置文件可能还没有更新,这个时候,我们需要手工在/etc/apparmor.d/usr.sbin.mysqld
增加几个文件目录的权限,如下:
1 2 3 |
/proc/*/status r, /sys/devices/system/node/ r, /sys/devices/system/node/node0/meminfo r, |
另外,注意到我这边出现
1 |
Aug 20 10:16:50 AY130422143404983ad9 mysqld[8762]: 2019-08-20T02:16:50.422604Z 0 [ERROR] Could not open file '/var/log/mysql/error.log' for error logging: Permission denied |
此时,如果检查目录权限,出现如下现象:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
$ ls -la /var/log/mysql/ total 256260 dr--r-s--- 2 mysql adm 4096 Aug 20 10:04 . drwxrwxr-x+ 21 root syslog 4096 Aug 20 06:25 .. -rw-r--r-- 1 mysql adm 0 Aug 20 10:04 error.log -r--r-x---+ 1 mysql adm 2982 Aug 20 06:25 error.log.1 -r--r-x---+ 1 mysql adm 3195 Aug 19 01:52 error.log.2.gz -r--r-x---+ 1 mysql adm 20 Aug 17 06:25 error.log.3.gz -r--r-x---+ 1 mysql adm 20 Aug 16 06:25 error.log.4.gz -r--r-x---+ 1 mysql adm 20 Aug 15 06:25 error.log.5.gz -r--r-x---+ 1 mysql adm 20 Aug 14 06:25 error.log.6.gz -r--r-x---+ 1 mysql adm 20 Aug 13 06:25 error.log.7.gz -r--r-x---+ 1 mysql adm 20 Aug 12 06:25 error.log.8.gz -r--r-x---+ 1 mysql adm 11235835 Aug 10 06:25 mysql-bin.000637 -r--r-x---+ 1 mysql adm 9529581 Aug 11 06:25 mysql-bin.000638 -r--r-x---+ 1 mysql adm 8299217 Aug 12 06:25 mysql-bin.000639 -r--r-x---+ 1 mysql adm 15319155 Aug 13 06:25 mysql-bin.000640 -r--r-x---+ 1 mysql adm 15816427 Aug 14 06:25 mysql-bin.000641 -r--r-x---+ 1 mysql adm 47746113 Aug 15 06:25 mysql-bin.000642 -r--r-x---+ 1 mysql adm 29508705 Aug 16 06:25 mysql-bin.000643 -r--r-x---+ 1 mysql adm 29720349 Aug 17 06:25 mysql-bin.000644 -r--r-x---+ 1 mysql adm 34429847 Aug 18 06:25 mysql-bin.000645 -r--r-x---+ 1 mysql adm 10252663 Aug 18 19:10 mysql-bin.000646 -r--r-x---+ 1 mysql adm 3971401 Aug 19 01:51 mysql-bin.000647 -r--r-x---+ 1 mysql adm 1799987 Aug 19 06:25 mysql-bin.000648 -r--r-x---+ 1 mysql adm 44653600 Aug 20 06:25 mysql-bin.000649 -r--r-x---+ 1 mysql adm 416 Aug 19 06:25 mysql-bin.index |
那么需要变更用户的所有者,早期版本支持所有者为 adm
用户组,新版本需要 mysql
,我们执行如下命令:
1 2 3 |
$ sudo chown -R mysql:mysql /var/log/mysql* $ sudo service mysql restart |
最近在ubuntu 16.04
下执行adb shell
,时候报告如下错误:
1 2 3 4 |
$ adb shell * daemon not running. starting it now on port 5037 * * daemon started successfully * error: insufficient permissions for device |
sudo
执行即可解决此问题。
1 2 3 |
$ sudo adb kill-server $ sudo adb shell |