Setting Up an AI Agent Environment on a NAS Virtual Machine with SSH Security Management
This article guides you through building a foundational private infrastructure for AI agents by deploying an Ubuntu virtual machine on a NAS virtualization platform. It covers VM preparation, system initialization, and SSH remote management, with a strong focus on security hardening using key-based authentication and firewall configurations. It also provides troubleshooting tips and post-deployment maintenance checks to ensure a stable environment for running AI agent frameworks.
1. Deployment Goals and Overall Route
This article focuses on the infrastructure of a private environment: setting up an Ubuntu virtual machine (VM) on a NAS virtualization platform and enabling remote management via SSH. The specific installation of AI agent frameworks is out of scope and should be performed after the environment stabilizes.
The overall process consists of four steps:
- Prepare the Ubuntu image and upload it to a NAS storage directory;
- Create a VM on the virtualization platform, allocating CPU, memory, and disk resources;
- Install the OS, configure the SSH service, and apply security hardening;
- Connect to the VM using an SSH client for ongoing maintenance.
Compared to installing directly on a physical machine, using a VM offers three main benefits: environment isolation, rapid rollback via snapshots when issues occur, and flexible resource quota adjustments. If a VM encounters errors, other services on the NAS remain unaffected.
2. VM Preparation
2.1 Choosing the Image
Ubuntu offers Desktop and Server installation versions. The Server version lacks a graphical interface, consumes fewer resources, and is better suited for long-term running services. The Desktop version includes a GUI, making initial configuration more intuitive.
If you plan to manage the VM solely via SSH, the Server version is recommended. If you prefer a GUI, you can choose the Desktop version, but you will need to perform an extra step to install the SSH service after the OS installation. It is recommended to select the current mainstream Long Term Support (LTS) version, such as 24.04 LTS. After downloading, upload the image to a NAS storage directory for the virtualization platform to mount.
2.2 Resource Configuration
Resource allocation depends on how the AI agent operates. If the agent primarily calls external model APIs, the computational load is mostly in the cloud, and the local machine only handles the OS, dependencies, and logs. The table below provides reference values:
| Use Case | CPU | Memory | Disk |
|---|---|---|---|
| Lightweight agent calling external APIs only (Minimum) | 2 Cores | 4GB | 40GB |
| Lightweight agent calling external APIs only (Recommended) | 2 Cores | 4GB | 60GB |
| Running large models locally | Plan based on model size | Usually much higher than 4GB | Additional space for model files required |
A 2-core/4GB configuration is only suitable for scenarios involving external API calls. If you need to perform local model inference, the required memory and compute resources will increase significantly, which is not covered in this article.
For networking, the default NAT mode only allows the host machine to access the VM. If you want other devices on the local network to directly connect via SSH, you should select Bridged mode and ensure the VM and router are on the same subnet.
2.3 Enabling SSH During Installation
Different installers have different interfaces and require separate handling:
- Server Installer: An “Install OpenSSH server” option will appear during installation. Check this box.
- Desktop Installer: Usually lacks this option. After the system installation completes, execute the commands in Section 3 via the terminal to install it.
3. System Initialization
Log in using the regular user account created during installation; there is no need to switch to the root account. Execute the following commands to update the package sources and install basic tools:
sudo apt updatesudo apt install -y curl git wget vim openssh-serversystemctl status ssh
These tools are used for downloading files, version control, fetching network resources, and text editing, respectively. The last command confirms that the SSH service is running. An output containing “active (running)” indicates normal operation.
Next, query the VM’s IP address:
ip addr
In the output, find the network interface with an UP status (the name might be ens18, enp1s0, etc.) and note the address following inet. You will need this for later connections.
At this point, it is recommended to create the first snapshot for the VM on the virtualization platform. If issues arise when modifying SSH configurations later, you can quickly revert to this state.
4. SSH Security Configuration
The basic principles of SSH configuration are: disable root remote login, prefer key-based authentication, and avoid password logins.
4.1 Generating Keys on the Client
Execute the following on your local computer:
ssh-keygen -t ed25519 -C "your-email@example.com"ssh-copy-id regular_username@VM_IP
The first command generates a key pair, saved by default in the ~/.ssh/ directory. The second command copies the public key to the VM. Afterward, test the connection:
ssh regular_username@VM_IP
If you can log in without entering a password, key-based authentication is working. Only after confirming that key login works normally can you disable password login. Otherwise, you will be locked out of the VM once the configuration takes effect.
4.2 Modifying Server-Side Configuration
Edit the SSH service configuration file on the VM:
sudo nano /etc/ssh/sshd_config
Confirm the values for the following items:
Port 22PermitRootLogin noPasswordAuthentication noPubkeyAuthentication yes
These mean: listen on port 22, disable remote root login, disable password login, and only allow key-based authentication.
Note that Ubuntu reads additional configuration files in the /etc/ssh/sshd_config.d/ directory. Some cloud images set PasswordAuthentication yes there, which overrides the main configuration file. Therefore, after modifying, verify the actual effective values:
sudo sshd -T | grep -Ei 'permitrootlogin|passwordauthentication'
The output of this command is authoritative. If it does not match your expectations, find the corresponding additional configuration file and modify it as well.
4.3 Validate Syntax Before Restarting
After modifying the configuration, check the syntax first, then restart the service:
sudo sshd -tsudo systemctl restart ssh
If sshd -t produces no output, the syntax is correct. If there are errors, fix the error lines before restarting.
Restarting the SSH service will drop the current connection. Please observe the following during this operation:
- Keep the current terminal session open, and open a new terminal to test the login.
- If the new terminal cannot log in, return to the current session or the virtualization platform’s VNC console, restore the original configuration, and restart.
- The snapshot taken before modifying the configuration can serve as a last resort for rollback.
4.4 Configuring Firewall to Restrict Sources
Firewall rules should allow SSH before enabling the firewall. Reversing this order will lock you out:
sudo ufw allow from 192.168.1.0/24 to any port 22 proto tcpsudo ufw enablesudo ufw status
Replace 192.168.1.0/24 with your local network segment. This ensures only devices on that segment can access port 22.
The VM should not be exposed directly to the public internet. If external access is required, it is recommended to use a VPN to access the internal network, or use a reverse proxy as a unified entry point, rather than directly opening port 22.
4.5 Temporary Password Login Scenarios
If you absolutely need to temporarily enable password login for troubleshooting, ensure the following conditions are met:
- The VM is located in an isolated intranet environment;
- Use a sufficiently complex password;
- The firewall restricts source IPs;
- After troubleshooting, immediately change
PasswordAuthenticationback tono, and re-runsshd -tand the restart command.
5. Remote Connection and Troubleshooting
Use the following command to connect to the VM:
ssh regular_username@VM_IP -p 22
On the first connection, the terminal will display the server’s fingerprint information. Verify the IP address is correct, then type yes.
If the connection fails, troubleshoot using the following directions:
- Connection refused: The SSH service is not running. Run
sudo systemctl status sshto check the status, andsudo systemctl start sshif necessary. - Connection timed out: Usually caused by a wrong IP, unreachable network mode, or firewall blocking. Check the VM IP, network mode, and run
sudo ufw statusto confirm port 22 is allowed. - Permission denied (publickey): Keys are not configured correctly. Confirm
ssh-copy-idexecuted successfully, and check that the~/.sshdirectory permissions on the VM are 700, and theauthorized_keysfile permissions are 600. - Server-side logs: Run
sudo journalctl -u ssh -n 50to view the latest 50 logs, which usually helps locate the cause. - REMOTE HOST IDENTIFICATION HAS CHANGED: Common after reinstalling the OS or rolling back a snapshot. Run
ssh-keygen -R VM_IPon the client to delete the old record before reconnecting.
6. Post-Deployment Operations and Maintenance
Once the base environment is stable, it is recommended to periodically check the following metrics:
| Check Item | Command | Reference Standard |
|---|---|---|
| Disk Usage | df -h |
Clean up or expand if over 80% |
| Memory Usage | free -h |
Consider expansion if consistently near full |
| System Load | uptime |
Investigate processes if load exceeds CPU cores |
| Log Size | sudo du -sh /var/log/ |
Check rotation config if it exceeds several GBs |
| Snapshots | Check on NAS platform | Take one after basic setup and before major changes |
Application logs should be written to a fixed directory and configured with logrotate. For example, retaining the last 7 days and splitting files exceeding 100MB can prevent logs from filling the disk.
External services should be consolidated through a reverse proxy, exposing only necessary ports inside the virtual machine.
