阿里云学生认证免费用9年ECS云服务器
本帖最后由 辰东 于 2025-8-21 10:55 编辑配置2C0.5G 80Mbps,空间也不多弄个发行版Alpine差不多了
感觉只能用来内网穿透啊
**** Hidden Message *****
基本设置可以参考如下流水账:
一、 基本设置
1. ssh修改为密码登录 (不推荐)
doas vi /etc/ssh/sshd_config.d/50-cloud-init.conf
2. 软链接sudo
doas ln -s $(which doas) /usr/local/bin/sudo
3. 修改主机名
sudo vi /etc/hostname
reboot
4. 调整时区
sudo ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
5. 修改root密码
sudo passwd root
6. alpine用户提权
su - root
passwd alpine
7 .替换阿里云镜像源
# 把原本的注释掉, 添加下面两个镜像源
http://mirrors.cloud.aliyuncs.com/alpine/v3.20/main
http://mirrors.cloud.aliyuncs.com/alpine/v3.20/community
# 更新
sudo apk update
二、 系统环境
1. 安装pip
sudo apk add py3-pip
2. 创建python虚拟环境
cd /path
python3 -m venv myenv
source myenv/bin/activate
pip install requests colorlog
3. 管理定时任务
# 查看当前用户的 crontab
crontab -l
# 编辑当前用户的 crontab
crontab -e
4. 流量监控
1. 脚本名aliyun_traffic.sh
先修改 Bark 通知相关配置
#!/bin/sh
# Bark 通知相关配置
BARK_URL="你的Bark通知API"
BARK_GROUP="通知分组"
BARK_ICON="https://kechang.uk/pic/kechang_icon_round.png"
echo "------------------------------"
# 获取当前脚本的绝对路径
SCRIPT_PATH=$(realpath "\$0")
# 保存流量数据的文件
TRAFFIC_FILE="/var/tmp/network_traffic.dat"
CURRENT_MONTH=$(date +"%Y-%m")
SHUTDOWN_THRESHOLD=$((20 * 1024 * 1024 * 1024))# 20GB 转换为字节的整数表示
NOTIFICATION_THRESHOLD=$((19 * 1024 * 1024 * 1024))# 19GB 转换为字节的整数表示
# 自动检测活跃的网络接口(排除 lo 环回接口)
INTERFACES=$(ls /sys/class/net | grep -v lo)
# 如果流量文件不存在或者月份不同,则创建并初始化
if [ ! -f $TRAFFIC_FILE ]; then
echo "$CURRENT_MONTH 0 0" > $TRAFFIC_FILE
else
saved_month=$(awk '{print $1}' $TRAFFIC_FILE)
if [ "$saved_month" != "$CURRENT_MONTH" ]; then
echo "$CURRENT_MONTH 0 0" > $TRAFFIC_FILE
fi
fi
# 读取之前的接收和发送累计流量
read saved_month last_total_in last_total_out < $TRAFFIC_FILE
# 初始化本次启动后的累计流量
current_total_in=0
current_total_out=0
# 遍历每个接口,获取并输出流量信息
for INTERFACE in $INTERFACES; do
# 获取当前接收和发送的字节数
in_bytes=$(cat /proc/net/dev | grep $INTERFACE | awk '{print $2}')
out_bytes=$(cat /proc/net/dev | grep $INTERFACE | awk '{print $10}')
# 本次启动后的累计流量
current_total_in=$((current_total_in + in_bytes))
current_total_out=$((current_total_out + out_bytes))
done
# 计算本次启动前后的累计流量
total_in=$((last_total_in + current_total_in - last_total_in))
total_out=$((last_total_out + current_total_out - last_total_out))
total_bytes=$((total_in + total_out))
# 检查是否达到19GB的通知阈值
if [ "$total_bytes" -ge "$NOTIFICATION_THRESHOLD" ] && [ "$total_bytes" -lt "$SHUTDOWN_THRESHOLD" ]; then
BARK_TITLE="aliyun_x86%E6%B5%81%E9%87%8F%E5%91%8A%E8%AD%A6"
BARK_MSG="%E6%80%BB%E6%B5%81%E9%87%8F%E5%B7%B2%E8%BE%BE%E5%88%B0%2019GB,%20%E8%AF%B7%E6%B3%A8%E6%84%8F%E6%B5%81%E9%87%8F%E4%BD%BF%E7%94%A8%E6%83%85%E5%86%B5"
curl -s -X GET "$BARK_URL/$BARK_TITLE/$BARK_MSG?group=$BARK_GROUP&icon=$BARK_ICON" > /dev/null
echo "总流量已达到 19GB, 请注意流量使用情况"
fi
# 检查是否达到20GB的关机阈值
if [ "$total_bytes" -ge "$SHUTDOWN_THRESHOLD" ]; then
BARK_TITLE="aliyun_x86%E6%B5%81%E9%87%8F%E4%B8%8A%E9%99%90"
BARK_MSG="%E6%80%BB%E6%B5%81%E9%87%8F%E5%B7%B2%E8%BE%BE%E5%88%B0%2020GB,%20%E7%B3%BB%E7%BB%9F%E5%8D%B3%E5%B0%86%E5%85%B3%E6%9C%BA..."
curl -s -X GET "$BARK_URL/$BARK_TITLE/$BARK_MSG?group=$BARK_GROUP&icon=$BARK_ICON" > /dev/null
echo "总流量已达到 20GB, 系统即将关机..."
sudo shutdown -h now
fi
# 自适应单位输出
if [ $total_bytes -lt 1024 ]; then
total="$total_bytes bytes"
elif [ $total_bytes -lt $((1024 * 1024)) ]; then
total=$(echo "scale=2; $total_bytes / 1024" | bc)
total="$total KB"
elif [ $total_bytes -lt $((1024 * 1024 * 1024)) ]; then
total=$(echo "scale=2; $total_bytes / 1024 / 1024" | bc)
total="$total MB"
else
total=$(echo "scale=2; $total_bytes / 1024 / 1024 / 1024" | bc)
total="$total GB"
fi
# 输出结果
echo "本月已使用流量: $total"
echo "------------------------------"
# 将本次启动后的流量数据保存到文件
echo "$CURRENT_MONTH $current_total_in $current_total_out" > $TRAFFIC_FILE
# 检查是否已经存在cron任务
CRON_CMD="*/5 * * * * $SCRIPT_PATH"
(crontab -l | grep -F "$CRON_CMD") || {
# 尝试添加cron任务,并捕获错误
(crontab -l 2>/dev/null; echo "$CRON_CMD") | crontab - 2>/tmp/cron_error.log
# 检查是否出现了权限错误
if grep -q "you are not allowed to use this program" /tmp/cron_error.log; then
echo "无法添加定时任务:没有权限。请以root用户或管理员权限运行此脚本。" >&2
elif grep -q "permission denied" /tmp/cron_error.log; then
echo "无法添加定时任务:权限被拒绝。请以root用户或管理员权限运行此脚本。" >&2
fi
# 删除错误日志
rm -f /tmp/cron_error.log
}
2. 赋予执行权限
sudo chmod +x aliyun_traffic.sh
3. 执行一次, 保证定时任务开启
./aliyun_traffic.sh
5. FRP内网穿透
1. 服务端
# 1. 安装frp
sudo apk add frp
# 2. 查看配置文件
sudo apk info -L frp
# 3. 编辑配置文件
sudo vi /etc/frp/frps.toml
bindPort = 7000
# 配置 frp dashboard
webServer.addr = "0.0.0.0"
webServer.port = 7500
webServer.user = "用户名"
webServer.password = "密码"
# 配置 token 认证,frpc 客户端也需指定一样的token
auth.method = "token"
auth.token = "密码"
# 4. 添加启动项
sudo rc-update add frps default
# 5. 启动服务
sudo rc-service frps start
sudo rc-service frps stop # 停止
sudo rc-service frps restart# 重启
sudo rc-service frps status # 查看状态
2. 客户端
# 1. 下载对应架构的frp, 并上传到客户端解压, 然后cd到解压后的目录
https://github.com/fatedier/frp/releases
# 2. 复制文件
sudo cp frpc /usr/local/bin/
chmod +x /usr/local/bin/frpc
sudo mkdir -p /etc/frp/
sudo cp frpc.toml /etc/frp/frpc.toml
# 3. 编辑客户端配置文件
sudo vim /etc/frp/frpc.toml
serverAddr = "服务端ip"
serverPort = 7000
auth.method = "token"
auth.token = "密码"
[]
name = "代理名称"
type = "tcp"
localIP = "127.0.0.1"
localPort = 本地端口
remotePort = 远程端口
# 4. 配置启动项
sudo vim /etc/systemd/system/frpc.service
Description=FRP Client
After=network.target
Type=simple
User=nobody
ExecStart=/usr/local/bin/frpc -c /etc/frp/frpc.toml
Restart=always
WantedBy=multi-user.target
# 5. 开机启动
chmod 777 /etc/systemd/system/frpc.service
sudo systemctl daemon-reload# 重新加载systemd配置,以识别新的服务文件
sudo systemctl enable frpc # 设置开机启动
sudo systemctl start frpc # 立即启动服务
sudo systemctl status frpc # 查看状态
6. htop
sudo apk add htop
三、 恢复镜像
1. 上传镜像到OSS
https://oss.console.aliyun.com/bucket/oss-cn-wulanchabu
2. 导入镜像
https://ecs.console.aliyun.com/image/region/cn-wulanchabu
3. 备份数据&停止实例&更换操作系统
装了docker和alist-tvbox,还剩100M 内存,其实能跑的东西很多。
感谢分享 666 感谢分享
页:
[1]