Deploying an AI Coding Service in a Virtual Machine: A Complete Practical Guide

    This guide explains how to deploy an AI coding service in an isolated Linux virtual machine. It covers planning VM resources and networking, creating the VM, configuring SSH for remote management, preparing the runtime environment, running the application as a system service, troubleshooting common problems, and applying basic security and maintenance practices.

    1. Planning the Deployment

    Before deploying an AI coding service, define where it will run, how users will reach it, and how many resources it needs. For personal learning, lightweight code assistance, or automated script execution, a Linux virtual machine on a home server or a machine on the local network is enough. A dedicated VM keeps the host environment unchanged and makes snapshots, backups, migration, and recovery easier.

    Plan these areas:

    1. Virtual machine resources

    Lightweight services usually do not need high-end hardware. Start with 2 virtual processors and 4 GB of memory, then adjust based on the actual workload. If you also run code analysis, dependency installation, several background processes, or other development services, add more memory. Reserve at least 40 GB of disk space for system files, software dependencies, caches, logs, and project code.

    1. Network mode

    If only the local machine uses the service, host-only or internal network mode is enough. If other computers on the local network need to manage it, the VM must get a local network address. When you use bridged or equivalent network mode, check address assignment on the LAN, the gateway configuration, and firewall policies.

    1. Management method

    You can complete the system installation through the graphical console of your virtualization platform. After installation, switch to SSH for remote management. This reduces reliance on a graphical desktop and makes it easier to copy commands, check logs, and run maintenance tasks.

    2. Creating and Installing the Linux Virtual Machine

    In your virtualization manager, create a new Linux VM. Select the prepared system image, then set the processors, memory, disk, and network based on your host’s resources.

    Use a long-term support (LTS) release or another distribution with a long maintenance cycle. Such releases generally:

    • have relatively stable software repositories;
    • receive security updates for a longer period;
    • have more community documentation;
    • offer better compatibility for dependency packages;
    • cost less to upgrade and troubleshoot later.

    During installation, you can choose a Chinese interface to make setup easier to follow. If you will mainly maintain the system from the command line, an English environment works equally well. Whichever language you choose, set the system and terminal character encodings to the same value. This prevents garbled text in logs, scripts, and file names.

    After the first login, confirm that networking works:

    1. ip addr
    2. ping -c 4 <gateway-address>

    If the VM has a local network address and can reach the gateway, the basic network is working. Next, update the package index and install common tools:

    1. sudo apt update
    2. sudo apt upgrade -y
    3. sudo apt install -y git curl wget vim openssh-server ca-certificates unzip

    Git manages project code. curl and wget download dependencies and install scripts. vim edits configuration files. The SSH server enables remote login.

    3. Configuring SSH Remote Management

    Check the SSH service status:

    1. sudo systemctl status ssh

    If the service is not running, start and enable it:

    1. sudo systemctl enable --now ssh

    Find the VM’s local IP address:

    1. hostname -I

    From another computer, connect with an SSH client:

    1. ssh <username>@<vm-ip-address>

    Log in as a regular user and avoid using the administrator account for daily work. A common shortcut is to allow direct administrator (root) login over SSH with password authentication. This is simple, but it significantly increases security risk. If many devices on the LAN can reach the VM, a leaked administrator password could give an attacker full system privileges.

    If you need to change the SSH configuration, back up the file first:

    1. sudo cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak

    Then edit the file:

    1. sudo vim /etc/ssh/sshd_config

    Follow these principles:

    • keep direct remote login disabled for the administrator account;
    • log in as a regular user and use sudo for administrative tasks;
    • where possible, disable password-only login and use key-based authentication;
    • do not expose SSH to the public internet just for convenience;
    • check the configuration syntax before restarting the service.

    Check the configuration syntax:

    1. sudo sshd -t

    If there are no errors, restart the service:

    1. sudo systemctl restart ssh

    If a firewall is enabled, allow the SSH port:

    1. sudo ufw allow 22/tcp
    2. sudo ufw enable

    If you change the default SSH port, update the firewall rules and client connection settings to match. A mismatch can lock you out.

    4. Preparing the Runtime Environment

    After logging in, create a separate working directory under your regular user’s home directory:

    1. mkdir -p ~/workspace
    2. cd ~/workspace

    Do not deploy applications directly in system directories or the administrator’s home directory. A separate directory isolates project files, caches, and logs, and it makes backup and migration simpler.

    Check the basic environment:

    1. uname -a
    2. free -h
    3. df -h
    4. git --version
    5. curl --version

    If the service requires a specific runtime version, follow the project’s own installation instructions first. Use the system package repository or an officially maintained version manager. Do not run one-line install scripts from unknown sources. In particular, do not run downloaded programs with administrator privileges without reading the script first.

    Before downloading the project or installing dependencies, confirm the current user and directory:

    1. whoami
    2. pwd

    This prevents files from landing in an unexpected location because of a user switch or a wrong path.

    5. Starting and Verifying the Deployment

    After installation, verify three things: the program runs, the service is reachable, and the service recovers after a reboot.

    Step 1: Start the program directly. Watch the terminal output for missing dependencies, port conflicts, or permission errors.

    Step 2: Check the listening ports:

    1. ss -lntp

    If the service listens only on 127.0.0.1, only processes inside the VM can reach it. For LAN access, configure the application to listen on the VM’s LAN address or on all network interfaces, depending on what the application supports. Restrict access with firewall rules to avoid unnecessary exposure.

    Step 3: Test with a local request:

    1. curl http://127.0.0.1:<service-port>

    If the service provides a web interface or API, also send a request from another device on the LAN. This confirms that the network path and access controls work.

    For long-running programs, use a system service manager instead of running the program in an SSH session. When the SSH session closes, a foreground process may exit with it. A service unit lets the program start at boot, restart automatically, and use centralized logging.

    A service configuration usually includes:

    • the user that runs the service;
    • the working directory;
    • the start command;
    • environment variables;
    • the log output location;
    • the restart policy after failure;
    • service dependencies.

    After you write the configuration, manage the service with these commands:

    1. sudo systemctl daemon-reload
    2. sudo systemctl enable --now <service-name>
    3. sudo systemctl status <service-name>

    View the logs:

    1. journalctl -u <service-name> -f

    6. Common Problems and Troubleshooting

    1. SSH connection fails

    First, confirm that the VM is online and that its IP address has not changed:

    1. hostname -I

    Next, check the SSH service:

    1. sudo systemctl status ssh

    Then check port 22 listening:

    1. ss -lntp | grep 22

    If the service is running but you still cannot connect, check the virtual network mode, firewall rules, LAN isolation, and IP address conflicts.

    2. Software installation fails

    Update the package index first:

    1. sudo apt update

    Check disk space:

    1. df -h

    Check memory and swap:

    1. free -h

    Many installation failures come from insufficient disk space, unstable networks, dependency version conflicts, or insufficient user permissions rather than from the software itself.

    3. The service exits right after starting

    Check the program’s own logs and the system logs first. Focus on configuration file paths, environment variables, port conflicts, file permissions, and runtime versions. Do not just repeat the start command. Use the logs to find the specific cause.

    4. The service disappears after the VM reboots

    If you started the application from a terminal, it will not come back after a reboot. Configure it as a system service or use a reliable process manager, and enable start at boot.

    7. Security and Maintenance

    After deployment, apply basic security practices:

    • install system security updates regularly;
    • use strong passwords for regular user accounts;
    • use SSH key authentication where possible;
    • limit access to management ports;
    • do not expose management ports to the public internet;
    • clean up expired logs and caches regularly;
    • back up VM disks, configuration files, and project data;
    • create a VM snapshot before major upgrades;
    • apply least privilege to external input, code execution, and file access.

    If the service can run scripts, call system commands, or access project files, run it under a dedicated user and restrict the directories it can reach. Do not run untrusted code directly on the host or in a high-privilege account.

    This