I recently took a look at Kaggle. It offers 30h of free GPU compute per week, but it provides the environment through Jupyter Notebook. I'm not used to working with Python in a notebook; it feels better suited to quickly prototyping ideas with single-file code. For production-grade code with many layers, it can leave the code feeling fragmented, so I wanted to see whether I could connect to it over SSH.
A viable approach
Use an ngrok tunnel to expose the SSH server running on Kaggle. Here's the code to run in a notebook cell:
# 1. 安装并配置ngrok
!pip install pyngrok
from pyngrok import ngrok
# 设置ngrok认证token
ngrok.set_auth_token("your_auth_token")
# 2. 启动SSH服务
!DEBIAN_FRONTEND=noninteractive apt-get install -y openssh-server
!mkdir -p /var/run/sshd
# 配置SSH服务,允许root登录并启用密码认证
!echo "root:your_password" | chpasswd
!echo 'PermitRootLogin yes' >> /etc/ssh/sshd_config
!echo 'PasswordAuthentication yes' >> /etc/ssh/sshd_config
!service ssh start
# 3. 使用ngrok暴露SSH端口
ssh_tunnel = ngrok.connect(22, "tcp")
print("SSH Tunnel:", ssh_tunnel.public_url)
# 4. 输出本地SSH连接命令
ssh_host, ssh_port = ssh_tunnel.public_url.replace("tcp://", "").split(":")
print(f"Use the following command to connect via SSH:\nssh root@{ssh_host} -p {ssh_port}")
Limitations
After testing it, I found that ngrok requires you to link a credit or debit card to use TCP ports. However, cards issued in mainland China don't seem to pass verification.
Exposing the HTTP service
Tunnel the HTTP service running on Kaggle out to the public internet.
%cd ../../
%cd kaggle
%ls
%cp -r input/testfilebox /my
%cd /my
%cd fcb-fronted
!npm install
!npm run build
%cd ../
%pip install -r requirements.txt
# 设置ngrok认证token
ngrok.set_auth_token("your token")
# 2. 使用ngrok暴露FastAPI服务的HTTP端口
http_tunnel = ngrok.connect(12345, "http")
print("Public URL:", http_tunnel.public_url)
!python main.py