跳转至

安卓上的termux

【高级终端Termux】在安卓手机/平板上使用Termux 搭建 Debian 环境并运行 PC 级 Linux 应用教程(含安装WPS,VS Code)

安装 ubuntu 的教程:文章

termux 安装

浏览器搜索 termux,在 github 页面的 releases 处下载安装包。

为 termux 申请手机系统权限

1
termux-setup-storage

换源

1
termux-change-repo

选择 china mainland 镜像源组。

更新 apt

1
2
apt upgrade
apt update

使用 ssh 远程连接

安装 openssh

1
apt install openssh

设置密码

1
passwd

查询用户名以及 ip 地址

用户名:

1
whoami

ip 地址:

需要下载 net-tools

1
apt install net-tools

输入 ifconfig 查看 ip 地址

1
ifconfig

启动 ssh 服务器

1
sshd

连接 ssh 服务器

1
ssh -p 8022 user@ip

远程主机公钥发生变化

远程主机发生变化时本地可能会报错:

1
2
3
4
5
6
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@    WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!     @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
Someone could be eavesdropping on you right now (man-in-the-middle attack)!
It is also possible that a host key has just been changed.

ssh服务是通过公钥和私钥来进行连接的,它会把每个曾经访问过计算机或服务器的公钥(public key),记录在~/.ssh/known_hosts 中,当下次访问曾经访问过的计算机或服务器时,ssh就会核对公钥,如果和上次记录的不同,OpenSSH会发出警告。

解决方法是清除该 ip 地址的记录,然后重新连接。

1
ssh-keygen -R [ip]:port

使用 proot-distro 部署 Linux 环境

建议使用 Debian 而不是 Ubuntu,因为 Ubuntu 的 snap 商店在 proot 中无法使用,而一些应用会强制要求使用 snap 商店,导致无法安装。因此不如直接使用 Debian。

安装 Debian 并进入环境

安装所需 proot-distro 组件:

1
apt install proot-distro

proot-distro 中查看可用 Linux 发行版:

1
proot-distro list

选择下载 Debian:

1
proot-distro install debian
下载完成后,进入 Debian 环境:

1
proot-distro login debian

使用 fastfetch 查看系统详细信息

1
apt install fastfetch
1
fastfetch

换源

修改 /etc/apt/sources.list 文件。可以将原来的备份一份:

1
cp /etc/apt/sources.list /etc/apt/sources.list.backup

使用 nano 编辑 /etc/apt/sources.list 文件:

1
nano /etc/apt/sources.list

添加国内镜像源(以清华源为例)

清华大学镜像站 debian 源使用说明网站

注意选择对应的 debian 版本的镜像源,例如 Debian 13 (trixie)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
# 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
deb https://mirrors.tuna.tsinghua.edu.cn/debian/ trixie main contrib non-free non-free-firmware
# deb-src https://mirrors.tuna.tsinghua.edu.cn/debian/ trixie main contrib non-free non-free-firmware

deb https://mirrors.tuna.tsinghua.edu.cn/debian/ trixie-updates main contrib non-free non-free-firmware
# deb-src https://mirrors.tuna.tsinghua.edu.cn/debian/ trixie-updates main contrib non-free non-free-firmware

deb https://mirrors.tuna.tsinghua.edu.cn/debian/ trixie-backports main contrib non-free non-free-firmware
# deb-src https://mirrors.tuna.tsinghua.edu.cn/debian/ trixie-backports main contrib non-free non-free-firmware

# 以下安全更新软件源包含了官方源与镜像站配置,如有需要可自行修改注释切换
deb https://security.debian.org/debian-security trixie-security main contrib non-free non-free-firmware
# deb-src https://security.debian.org/debian-security trixie-security main contrib non-free non-free-firmware

在这之后更新下软件源

1
apt update

如果显示 SSL connection failed 等无法连接 https 的问题,可能由于缺失了 apt-transport-httpsca-certificates,安装这两个包后再进行更新:

1
apt install apt-transport-https ca-certificates

部署 code-server

以下均在 proot-distro 环境下执行。

安装 code-server

下载 code-server 安装程序

1
curl -fsSL https://code-server.dev/install.sh | sh

安装完成后运行安装程序

1
sh install.sh

修改配置文件

先启动一遍来生成配置文件。

1
code-server

Ctrl+C 退出。

使用 nano 编辑 code-server 配置文件:

1
nano ~/.config/code-server/config.yaml
  • bind-addr 改为 0.0.0.0:8080
  • auth 项若改为 none 则不需要密码登录。
  • password 改为自己的密码。

使用 nano 修改 code-server 插件商店的配置:

1
nano /usr/lib/code-server/lib/vscode/product.json

添加以下内容:

1
2
3
4
5
"extensionsGallery": {
    "serviceUrl": "https://marketplace.visualstudio.com/_apis/public/gallery",
    "cacheUrl": "https://vscode.blob.core.windows.net/gallery/index",
    "itemUrl": "https://marketplace.visualstudio.com/items"
},

启动 code-server

1
code-server

接下来就可以在同设备浏览器中访问 http://127.0.0.1:8080 来打开 code-server 了。

可以选择安装 c++ 编译环境:

1
apt install gcc g++ gdb

使用 zsh 代替 bash

安装 zsh

1
apt install zsh

自动化配置工具 oh-my-zsh

安装 oh-my-zsh 的前置需要安装 git

1
apt install git

安装 oh-my-zsh

1
2
# 通过curl下载
sh -c "$(curl -fsSL https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"
1
2
# 通过weget下载
sh -c "$(wget -O- https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"

termux 中也可选择 oh-my-termux

1
bash -c "$(curl -fsSL https://git.io/oh-my-termux)"

安装成功后如图

oh-my-zsh 安装成功

设置 zsh 为默认终端

1
chsh -s zsh

重启终端后检查是否为 zsh

1
echo $SHELL

输出应为

1
/data/data/com.termux/files/usr/bin/zsh

安装自动补全和语法高亮插件

从 github 克隆插件仓库

自动补全插件

1
zsh-autosuggestions

1
git clone https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:=~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions
语法高亮插件

1
zsh-syntax-highlighting
1
git clone https://github.com/zsh-users/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting

启用插件

使用 nano 打开 ~/.zshrc 文件

1
nano ~/.zshrc

编辑 ~/.zshrc 文件,找到 plugins 项,添加 zsh-autosuggestionszsh-syntax-highlighting 两个插件。最好换行并在前面加 Tab 键。

1
2
3
4
5
plugins=(
    git
    zsh-autosuggestions
    zsh-syntax-highlighting
)

切换主题为 powerlevel10k

编辑 ~/.zshrc 中的 ZSH_THEME 项为 powerlevel10k/powerlevel10k

1
ZSH_THEME="powerlevel10k/powerlevel10k"
1
powerlevel10k/powerlevel10k

克隆主题仓库并运行配置文件

1
2
git clone --depth=1 https://github.com/romkatv/powerlevel10k.git ${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}/themes/powerlevel10k
source ~/.zshrc

接下来根据引导进行安装配置即可。

重新配置 powerlevel10k 主题

1
p10k configure