Deploying a Linux Application Service on a NAS Virtual Machine: A Practical Guide
This guide walks through deploying a maintainable application service on a Linux virtual machine running on a NAS. It covers resource allocation, initial system setup, SSH key-based login, a dedicated service account, systemd service management, backup and restore, and common troubleshooting.
This guide deploys a maintainable application service on a Linux virtual machine hosted on a NAS. It covers resource allocation, system initialization, SSH key-based login, a dedicated service account, systemd service management, backup and restore, and common troubleshooting. Before you change permissions or remote access settings, keep at least one working login session and the NAS console available.
1. Resource Planning
Resource planning determines long-term stability. If you allocate too few resources, the application will lag often. If you allocate too many, the NAS’s own storage and services will slow down. Before deployment, confirm three things: the application type, the number of services that will run at the same time, and the NAS’s current free resources.
The table below lists starting configurations for common scenarios. Use them as initial values and adjust them based on the actual workload.
| Scenario | CPU | Memory | System disk | Data disk |
|---|---|---|---|---|
| Single lightweight application (e.g., file sync, small website) | 2 cores | 4 GB | 20 GB | Add based on data size |
| Multiple services or database applications | 4 cores | 8 GB | 40 GB | Separate virtual disk or NAS shared folder |
| Memory-intensive application (e.g., Java service, search index engine) | 4 cores | 8 GB or more | 40 GB | Separate virtual disk |
Before you allocate resources, check the total memory and CPU usage on the NAS. Keep enough headroom for the NAS system itself. Do not allocate so much that the virtual machine uses all available memory.
After deployment, run free -h and top inside the virtual machine. Record data during peak business hours and monitor for one week. Increase resources only if either of these conditions holds for a full week: the sum of user and system CPU usage on the %Cpu line at the top of top stays above 70%, or the available column in free -h stays below 20% of total memory. Increase only one resource at a time, and observe for another week after each change.
If other devices need direct access to the application, use bridged networking. If only the NAS needs to access it, NAT mode is simpler. The fixed IP setup differs between the two modes:
- Bridged mode: The virtual machine is on the same subnet as the router. Bind the virtual machine’s MAC address to a fixed IP in the router’s DHCP settings.
- NAT mode: The virtual machine sits on a subnet inside the virtualization platform, so the router cannot see its MAC address. Set a fixed IP for the virtual machine in the NAS virtualization platform. If other devices need access, also configure port forwarding.
2. System Installation and Initialization
Install the minimal version of Ubuntu Server LTS or Debian stable. The server edition has no graphical interface. It uses less memory when idle and requires fewer open ports. The desktop edition typically uses more than 1 GB of memory even when idle. Choose the desktop edition only if you need a graphical interface, and include its overhead in your resource plan.
Before installing, confirm that your NAS virtualization package supports the operating system you chose. After installation, check the versions:
cat /etc/os-releasesystemctl --version | head -n 1
After installation, immediately create a snapshot of the virtual machine on the NAS and name it “Initial installation complete”. Before every major change afterward, create a new snapshot first.
After you log in to the virtual machine, run the following initialization commands:
sudo apt update && sudo apt upgrade -ysudo hostnamectl set-hostname nas-app01sudo timedatectl set-timezone Asia/Shanghaisudo apt install -y curl vim htop ufw fail2ban
These commands update the system, set the hostname and time zone, and install common tools. ufw is a firewall tool. fail2ban blocks IP addresses that repeatedly fail to log in.
Enable automatic security updates to reduce the risk of vulnerabilities staying unpatched for long periods:
sudo apt install -y unattended-upgradessudo dpkg-reconfigure -plow unattended-upgrades
3. SSH Remote Management and Permission Hardening
3.1 Create an Administrator Account
Do not log in directly as root for daily operations. Create a dedicated administrator account and grant it sudo privileges:
sudo adduser opsadminsudo usermod -aG sudo opsadmin
3.2 Set Up Key-Based Login
On your own computer, generate a key pair. Skip this step if you already have one:
ssh-keygen -t ed25519 -C "home-nas-admin"ssh-copy-id -i ~/.ssh/id_ed25519.pub opsadmin@192.168.1.50
Replace the IP address with the virtual machine’s actual address. Then test login with the new key:
ssh -i ~/.ssh/id_ed25519 opsadmin@192.168.1.50
After key-based login works, you can change the SSH service configuration. Do not skip this check.
3.3 Modify the SSH Configuration
To avoid errors when editing the main configuration file, create a separate file at /etc/ssh/sshd_config.d/99-hardening.conf:
PermitRootLogin noPasswordAuthentication noKbdInteractiveAuthentication noPubkeyAuthentication yesMaxAuthTries 3LoginGraceTime 30AllowUsers opsadmin
These settings do the following: block direct root login, disable password login so that only keys are accepted, limit the number of authentication attempts per connection, and allow only the specified user to log in.
The main SSH configuration file on Ubuntu and Debian, /etc/ssh/sshd_config, must include the line Include /etc/ssh/sshd_config.d/*.conf, usually near the top. Check it with grep Include /etc/ssh/sshd_config. Without this line, the separate file has no effect.
After saving the file, check the syntax, then restart the service:
sudo sshd -tsudo systemctl restart ssh
After restarting, keep the current terminal session open. Open a second terminal and test login with your key. Close the original session only after the test succeeds. If the syntax check reports an error, fix the error as the message indicates. Do not restart the service until the check passes.
3.4 Configure the Firewall and Failed-Login Limits
Allow SSH before you enable the firewall. Reversing this order can lock you out:
sudo ufw allow OpenSSHsudo ufw allow 8080/tcpsudo ufw enablesudo ufw status verbose
8080 is an example port. Replace it with the port your application actually listens on. Open a port only if external access to the application is required, and open only the ports you need.
Add the following to /etc/fail2ban/jail.local, then restart fail2ban:
[sshd]enabled = truemaxretry = 5findtime = 10mbantime = 1h
sudo systemctl restart fail2bansudo fail2ban-client status sshd
If you want to change the SSH port, follow these steps in order. Changing the port and enabling the firewall at the same time can prevent you from connecting.
- In
/etc/ssh/sshd_config.d/99-hardening.conf, keep bothPort 22andPort 2222lines. Then runsudo sshd -tto check the syntax. - Run
