Termux 使用

1 初始化配置

1.1 更换镜像源

sed -i 's@^\(deb.*stable main\)$@#\1\ndeb https://mirrors.tuna.tsinghua.edu.cn/termux/termux-packages-24 stable main@' $PREFIX/etc/apt/sources.list

sed -i 's@^\(deb.*games stable\)$@#\1\ndeb https://mirrors.tuna.tsinghua.edu.cn/termux/game-packages-24 games stable@' $PREFIX/etc/apt/sources.list.d/game.list

sed -i 's@^\(deb.*science stable\)$@#\1\ndeb https://mirrors.tuna.tsinghua.edu.cn/termux/science-packages-24 science stable@' $PREFIX/etc/apt/sources.list.d/science.list

# 更新软件列表
pkg update

1.2 安装基础软件vim curl wget git tree

pkg install vim curl wget git tree -y

1.3 获取外部存储访问权限

termux-setup-storage

1.4 创建目录软连接

ln -s /data/data/com.termux/files/home/storage/shared/termux termux

.
├── storage
│   ├── dcim -> /storage/emulated/0/DCIM
│   ├── downloads -> /storage/emulated/0/Download
│   ├── movies -> /storage/emulated/0/Movies
│   ├── music -> /storage/emulated/0/Music
│   ├── pictures -> /storage/emulated/0/Pictures
│   └── shared -> /storage/emulated/0
├── termux -> /data/data/com.termux/files/home/storage/shared/termux

1.5 安装ssh,并开机启动sshd

安装:

pkg install openssh

启动:

sshd

开机启动ssh:

vim ~/.bashrc

 输入以下内容,并保存

if pgrep -x "sshd" >/dev/null
  then
    echo "ssh-service运行中.."
  else
    sshd >/dev/null
    echo "ssh-service已开启..."
fi

加载修改的配置,使配置立即生效

source ~/.bashrc

或者

 . ~/.bashrc

1.6 超级管理员身份

利用proot可以为手机没有root的用户来模拟一个root的环境

安装:

pkg install proot -y

2 开发环境配置

2.1 安装数据库(MariaDB)

安装:

pkg install mariadb

查看是否安装成功

mysql --version

成功,则展示如下内容

mysql  Ver 15.1 Distrib 10.6.4-MariaDB, for Android (aarch64) using  EditLine wrapper

后台启动mysql

nohup mysqld &

开机启动mysql

将以下内容追加到~/.bashrc文件内

if pgrep -x "mysqld_safe" >/dev/null
  then
    echo "mysql运行中..."
  else
    mysqld_safe -u root >/dev/null  &
    echo "mysql已开启..."
fi

2.2 安装jdk

安装:

pkg install openjdk-17 -y

验证:

java -version

成功,则展示如下内容

openjdk version "17-internal" 2021-09-14
OpenJDK Runtime Environment (build 17-internal+0-adhoc..src)
OpenJDK 64-Bit Server VM (build 17-internal+0-adhoc..src, mixed mode)

2.3 安装redis

pkg install redis

安装后启动:

报如下错误

WARNING Your kernel has a bug that could lead to data corruption during background save. Please upgrade to the latest stable kernel.

需要修改配置文件忽略此错误

 vim /data/data/com.termux/files/usr/etc/redis.conf

取消最后一行的注释,再启动redis即可

开机启动redis

将以下内容追加到~/.bashrc文件内

		# 启动redis
		if pgrep -x "redis-server" >/dev/null
		then
			echo "--------------"
			echo "redis-server运行中..."
			echo "--------------"
		else
			redis-server /data/data/com.termux/files/usr/etc/redis.conf  >/dev/null  &
			echo "--------------"
			echo "redis-server已开启..."
			echo "--------------"
		fi
source ~/.bashrc

3 系统安装

确保已经安装如下软件

  • proot
  • git
  • wget

安装Ubuntu

# 回到home目录
cd ~

# 克隆安装脚本
git clone https://github.com/MFDGaming/ubuntu-in-termux.git

# 跳转到ubuntu-in-termux目录
cd ubuntu-in-termux

# 授权
chmod +x ubuntu.sh

# 执行安装脚本
./ubuntu.sh -y

启动Ubuntu

# 启动ubuntu
./startubuntu.sh

4 基础配置脚本

#!/data/data/com.termux/files/usr/bin/bash

# 配置开机启动项
function initBoot(){
	echo
	echo "-->删除.bashrc文件"
	rm -f ~/.bashrc
	
	echo
	echo "-->配置sshd开机启动项"
	if isInstall "sshd"
	then
		echo '
		# 启动sshd
		if pgrep -x "sshd" >/dev/null
		then
			echo "ssh-service运行中.."
		else
			sshd >/dev/null
			echo "ssh-service已开启..."
		fi
		' >> ~/.bashrc
		echo "sshd 开机启动项配置完成"
	else
		echo "sshd 未安装"
	fi
	
	echo
	echo "-->配置mariadb开机启动项"
	if isInstall "mariadb"
	then
		echo '
		# 启动mariadb
		if pgrep -x "mysqld_safe" >/dev/null
		then
			echo "mysql运行中..."
		else
			mysqld_safe -u root >/dev/null  &
			echo "mysql已开启..."
		fi
		' >> ~/.bashrc
		echo "mariadb 开机启动项配置完成"
	else
		echo "mariadb 未安装"
	fi
	
	echo
	echo "-->使配置立即生效"
	source ~/.bashrc
}

# 判断多个应用是否都安装成功
function isAllInstalled(){
	for soft in $1;do
		if isInstall $soft
		then
			continue
		else
			return 1
		fi
	done
}

# 判断应用是否安装成功
function isInstall(){
	if ! type $1 >/dev/null 2>&1; then
		echo "$1 未安装"
		return 1
	else
		echo "$1 已安装"
		return 0
	fi
}

# 设置mariadb的root用户密码,允许远程访问数据库
function initMariadb(){
	echo "-->修改root账户密码"
	while :
	do
		read -p "请输入root账户密码:" -s db_root_password
		if test -z "$db_root_password"
		then
			echo "密码不能为空"
		else
			break
		fi
	done
	
	echo
	echo "-->添加远程访问数据库的账户"
	while :
	do
		read -p "请输入远程访问数据库的用户名:" db_remote_username
		if test -z "$db_remote_username"
		then
			echo "用户名不能为空"
		else
			break
		fi
	done
	
	echo
	echo "-->输入$db_remote_username对应的密码"
	while :
	do
		read -p "请输入$db_remote_username账户密码:" -s db_remote_password
		if test -z "$db_remote_username"
		then
			echo "用户名不能为空"
		else
			break
		fi
	done

	echo
	mysql -u $(whoami) -e "use mysql;set password for 'root'@'localhost' = password('$db_root_password');CREATE USER '$db_remote_username'@'%' IDENTIFIED BY '$db_remote_password';GRANT ALL ON *.* TO '$db_remote_username'@'%';flush privileges;"
	
	echo "数据库账户信息:"
	mysql -u $(whoami) -e "use mysql;select host,user,password from user;"
}

# is empty
function isEmpty(){
	if test -z "$1"
		then
			echo ""
		else  
			echo "dmin is set !"
	fi
}

echo "Termux初始化脚本"
echo
echo "(1)更换为清华镜像源..."
sed -i 's@^\(deb.*stable main\)$@#\1\ndeb https://mirrors.tuna.tsinghua.edu.cn/termux/termux-packages-24 stable main@' $PREFIX/etc/apt/sources.list
sed -i 's@^\(deb.*games stable\)$@#\1\ndeb https://mirrors.tuna.tsinghua.edu.cn/termux/game-packages-24 games stable@' $PREFIX/etc/apt/sources.list.d/game.list
sed -i 's@^\(deb.*science stable\)$@#\1\ndeb https://mirrors.tuna.tsinghua.edu.cn/termux/science-packages-24 science stable@' $PREFIX/etc/apt/sources.list.d/science.list
# 更新软件列表
pkg update -y
echo
echo "(2)安装基础软件(vim curl wget git tree proot openssh)..."
pkg install vim curl wget git tree proot openssh -y

echo
echo "(3)安装开发软件(mariadb jdk)..."

echo "(3.1)安装mariadb"
read -p "是否安装mariadb,输入yes安装:" mariadb_sure 
if test $mariadb_sure = "yes"
then
	pkg install mariadb -y
	# 启动mariadb
	if isInstall mariadb
	then
		echo "mariadb版本信息:"
		mysql --version
		echo "启动mariadb"
		mysqld_safe -u root >/dev/null &
		echo "初始化mariadb"
		initMariadb
	else
		:
	fi
else
	echo '已跳过安装:mariadb'
fi
	
echo "(3.2)安装jdk"
read -p "是否安装jdk,输入yes安装:" jdk_sure 
if test $jdk_sure = "yes"
then
	pkg install openjdk-17 -y
	echo "jdk版本信息:"
	java -version	
else
	echo '已跳过安装:jdk'
fi

echo
echo "(4)-设置sshd, mariadb开机启动项"
initBoot

echo
echo "(5)获取外部存储访问权限"
termux-setup-storage

echo
echo "(6)设置新密码"
passwd

5 初始化安装配置脚本

#!/data/data/com.termux/files/usr/bin/bash

# 配置开机启动项
function initBoot(){
	echo
	echo "-->删除.bashrc文件"
	rm -f ~/.bashrc
	
	echo
	echo "-->配置sshd开机启动项"
	if isInstall "sshd"
	then
		echo '
		# 启动sshd
		if pgrep -x "sshd" >/dev/null
		then
			echo "--------------"
			echo "sshd运行中..."
			echo "--------------"
		else
			sshd >/dev/null
			echo "--------------"
			echo "sshd已开启..."
			echo "--------------"
		fi
		' >> ~/.bashrc
		echo "sshd 开机启动项配置完成"
	else
		echo "sshd 未安装"
	fi
	
	echo
	echo "-->配置mariadb开机启动项"
	if isInstall "mariadb"
	then
		echo '
		# 启动mariadb
		if pgrep -x "mysqld_safe" >/dev/null
		then
			echo "--------------"
			echo "mysql运行中..."
			echo "--------------"
		else
			mysqld_safe -u root >/dev/null  &
			echo "--------------"
			echo "mysql已开启..."
			echo "--------------"
		fi
		' >> ~/.bashrc
		echo "mariadb 开机启动项配置完成"
	else
		echo "mariadb 未安装"
	fi
	
	echo
	echo "-->配置ubuntu开机启动项"
	if test -e /data/data/com.termux/files/home/ubuntu-in-termux/startubuntu.sh
	then
		echo '
		# 启动Ubuntu系统...
		/data/data/com.termux/files/home/ubuntu-in-termux/startubuntu.sh
		' >> ~/.bashrc
		echo "ubuntu 开机启动项配置完成"
	else
		echo "ubuntu 未安装"
	fi	

	echo
	echo "-->使配置立即生效"
	source ~/.bashrc
}

# 设置mariadb的root用户密码,允许远程访问数据库
function initMariadb(){
	echo "-->修改root账户密码"
	while :
	do
		read -p "请输入root账户密码:" -s db_root_password
		if test -z "$db_root_password"
		then
			echo "密码不能为空"
		else
			break
		fi
	done
	
	echo
	echo "-->添加远程访问数据库的账户"
	while :
	do
		read -p "请输入远程访问数据库的用户名:" db_remote_username
		if test -z "$db_remote_username"
		then
			echo "用户名不能为空"
		else
			break
		fi
	done
	
	echo
	echo "-->输入$db_remote_username对应的密码"
	while :
	do
		read -p "请输入$db_remote_username账户密码:" -s db_remote_password
		if test -z "$db_remote_username"
		then
			echo "用户名不能为空"
		else
			break
		fi
	done

	echo
	mysql -u $(whoami) -e "use mysql;set password for 'root'@'localhost' = password('$db_root_password');CREATE USER '$db_remote_username'@'%' IDENTIFIED BY '$db_remote_password';GRANT ALL ON *.* TO '$db_remote_username'@'%';flush privileges;"
	
	echo "数据库账户信息:"
	mysql -u $(whoami) -e "use mysql;select host,user,password from user;"
}

# 判断多个应用是否都安装成功
function isAllInstalled(){
	for((i=0;i<${#base_soft_array[@]};i++));do
		if isInstall ${base_soft_array[$i]}
		then
			continue
		else
			return 1
		fi
	done
}

# 判断应用是否安装成功
function isInstall(){
	if ! type $1 >/dev/null 2>&1; then
		echo "$1 未安装"
		return 1
	else
		echo "$1 已安装"
		return 0
	fi
}


command_info="
-----------------------------------------------------
执行Termux初始化安装配置脚本
输入以下指令编号,执行具体任务
0-退出
1-更换镜像源(清华)
2-安装基础软件(vim curl wget git tree proot openssh)
3-安装开发软件(jdk, mariadb)
4-安装ubuntu系统
5-配置开机启动项
6-Termux获取外部存储访问权限
ll-显示所有命令
-----------------------------------------------------
"
echo "$command_info"
echo
echo
echo
# 根据指令执行具体任务
while :
do
	echo
	read -p "请输入指令:" command
	case $command in
		0)
			echo "退出"
			break
			;;
		1)
			echo "(1)更换镜像源..."
			sed -i 's@^\(deb.*stable main\)$@#\1\ndeb https://mirrors.tuna.tsinghua.edu.cn/termux/termux-packages-24 stable main@' $PREFIX/etc/apt/sources.list
			sed -i 's@^\(deb.*games stable\)$@#\1\ndeb https://mirrors.tuna.tsinghua.edu.cn/termux/game-packages-24 games stable@' $PREFIX/etc/apt/sources.list.d/game.list
			sed -i 's@^\(deb.*science stable\)$@#\1\ndeb https://mirrors.tuna.tsinghua.edu.cn/termux/science-packages-24 science stable@' $PREFIX/etc/apt/sources.list.d/science.list
			# 更新软件列表
			pkg update -y
			;;
		
		2)
			echo "(2)安装基础软件(vim curl wget git tree proot openssh)..."
			pkg install vim curl wget git tree proot openssh -y
			base_soft_array=("vim" "curl" "wget" "git" "tree" "proot" "openssh")
			if isAllInstalled $base_soft_array
			then
				echo "基础软件(vim curl wget git tree proot openssh)安装完成"
			else
				:
			fi
			;;
		3)  
			echo "(3)安装开发软件(jdk, mariadb)..."
			echo "(3.1)安装mariadb"
			read -p "输入yes安装安装mariadb:" mariadb_sure 
			if test $mariadb_sure = "yes"
			then
				pkg install mariadb -y
				# 启动mariadb
				if isInstall mariadb
				then
					echo "mariadb版本信息:"
					mysql --version
					echo "启动mariadb"
					mysqld_safe -u root >/dev/null &
					echo
					echo "初始化mariadb"
					initMariadb
				else
					:
				fi
			else
				echo '已跳过安装:mariadb'
			fi
				
			echo "(3.2)安装jdk"
			read -p "输入yes安装jdk:" jdk_sure 
			if test $jdk_sure = "yes"
			then
				pkg install openjdk-17 -y
				echo "jdk版本信息:"
				java -version	
			else
				echo '已跳过安装:jdk'
			fi
			;;
		4)  
			echo "(4)安装ubuntu系统"
			# 判断是否安装git,wget,proot
			soft_array=("git" "wget" "proot")
			if isAllInstalled $soft_array
			then
				# 执行安装ubuntu命令
				# 回到home目录
				cd ~
				# 克隆安装脚本
				git clone https://github.com/MFDGaming/ubuntu-in-termux.git
				# 跳转到ubuntu-in-termux目录
				cd ubuntu-in-termux
				# 授权
				chmod +x ubuntu.sh
				# 执行安装脚本
				./ubuntu.sh -y
			else
				echo "缺少必要的软件,建议先执行指令2"
			fi
			;;
		5)  
			echo "(5)配置开机启动项(sshd,mariadb,ubuntu)"
			read -p "原先配置将删除,输入yes确认:" init_sure 
			if test $init_sure = "yes"
			then
				initBoot
			else
				echo "取消配置开机启动项"
			fi
			;;
		6)  
			echo "(6)Termux获取外部存储访问权限"
			termux-setup-storage
			;;
		ll)  
			echo "$command_info"
			;;
		*)  
			echo '指令错误'
			continue
			;;
	esac
done


 

  • 2
    点赞
  • 42
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值