A Complete Guide to Deploying AI Agents and Connecting LLMs on Linux Servers
This article provides a comprehensive guide on deploying AI agents on Linux servers and connecting them to large language model (LLM) inference services. It covers environment preparation, installation and validation of agent-cli, configuring custom model providers, securely storing API keys, and performing layered connectivity tests from DNS resolution to real conversation requests. Additionally, it offers a troubleshooting matrix to help identify issues related to local configuration, network, or server-side errors.
I. What This Article Covers
AI agent tools can read context, call external tools, and complete tasks step-by-step. These capabilities rely on the inference services provided by large language models (LLMs). Deploying an agent on a server requires three main steps: preparing the runtime environment, verifying the application itself, and connecting the model service to the tool.
This article uses a Linux Long Term Support (LTS) server as an example. The command-line entry in this article is uniformly referred to as agent-cli; the actual name, installation path, and subcommands should follow the tool’s official documentation. The model interface is exemplified by a conversation interface compatible with the OpenAI format, where the request URL is the base_url appended with /chat/completions. If the service provider uses special signatures or proprietary protocols, the request examples in this article do not apply, and you should use the request method specified in the provider’s documentation.
After reading this article, you will be able to:
- Verify the system version and CPU architecture to select the correct installation package
- Install and validate
agent-cli - Add a custom model provider via an interactive wizard
- Securely store API keys with file permissions set to 600
- Perform layered tests for DNS, HTTPS, authentication, and real conversation requests
- Determine whether errors originate from local configuration, network, or the server side
II. Environment Preparation
2.1 System Requirements
Before deployment, confirm the conditions in the table below.
| Item | Requirement | Description |
|---|---|---|
| Operating System | Ubuntu 22.04 or 24.04 LTS, Debian 12, RHEL 9 series | Other systems are not verified in this article; refer to the official documentation |
| Account | A standard user with sudo privileges | Avoid running the service as root long-term to reduce the risk of misoperation and privilege escalation |
| Network | Able to access the model service domain via HTTPS | For intranet deployments, ensure the egress policy allows access to this domain |
| Memory & Disk | Based on the tool’s official instructions, reserving extra capacity is recommended | Long conversations and multi-turn tasks will significantly increase memory usage |
This article does not specify a particular version number for agent-cli. Check the official documentation for the minimum version requirements and use that version as your validation baseline.
2.2 Verify System Version and CPU Architecture
Run the following commands:
grep -E '^(ID|VERSION_ID)=' /etc/os-releaseuname -m
The ID field identifies the distribution, and VERSION_ID identifies the version number. Below are two common outputs:
ID=ubuntuVERSION_ID="22.04"
ID=debianVERSION_ID="12"
Compare these two fields with the table above. ID=ubuntu with version 22.04 or 24.04, and ID=debian with version 12, are within the supported range. For RHEL-based distributions (such as RHEL, Rocky Linux, AlmaLinux), as long as the major version is 9, you can verify against the official support list. Other combinations are not verified in this article; it is recommended to check the official documentation first.
The output of uname -m determines the type of installation package:
- Output
x86_64: Download the x86_64 (often written as amd64) package - Output
aarch64: Download the aarch64 (often written as arm64) package
Selecting the wrong architecture may result in the package failing to run or causing errors during installation.
2.3 Run as a Non-Root User
It is recommended to create a dedicated account instead of running the agent as root:
sudo useradd -m -s /bin/bash agentsudo su - agent
Subsequent steps should be executed under this account. If you need to run it as a long-term service, you can set User=agent in the systemd unit file.
III. Installing agent-cli
3.1 Download and Verify
Download the installation package matching your architecture from the official channel and verify the SHA256 checksum published officially:
sha256sum agent-cli-linux-x86_64.tar.gz
Compare the output character by character with the value published on the official page. If they do not match, do not proceed with the installation; download it again. The filename is just an example; replace it with the actual name provided officially.
3.2 Inspect Archive Contents and Install
Before installing, list the files inside the archive to confirm the actual path and name of the executable:
tar -tzf agent-cli-linux-x86_64.tar.gz
The binary file name inside the archive may differ from the archive name, for example, agent-cli-linux-x86_64/agent-cli. Replace the path in the following command with the real path identified in the previous step:
mkdir -p ~/.local/bintar -xzf agent-cli-linux-x86_64.tar.gzinstall -m 755 ./agent-cli ~/.local/bin/agent-cli
If the official documentation provides other installation methods, such as a package manager or a one-click script, prioritize the official method. Ensure that ~/.local/bin is added to your PATH:
echo $PATH | grep -q "$HOME/.local/bin" && echo "PATH is configured correctly"
If the output is empty, you can add export PATH="$HOME/.local/bin:$PATH" to the end of your ~/.bashrc and log in again.
3.3 Version Verification
agent-cli --version
The output should include a version number, such as agent-cli x.y.z. Compare this version with the minimum requirement in the official documentation. If the version is lower than required, upgrade before continuing. If the command returns command not found, go back to step 3.2 to check your PATH.
IV. Configuring Model Providers
4.1 Launching the Interactive Wizard
agent-cli provider add
The wizard usually prompts for the following parameters in sequence. Field names are subject to the actual interface; different CLI versions might name the key environment variable field api_key_env or something else:
| Parameter | Input | Example |
|---|---|---|
| Name | Custom name to distinguish multiple providers | my-provider |
| Interface Type | Select OpenAI-compatible format | openai-compatible |
| base_url | The API root address provided by the service, usually ending with /v1 |
https://api.provider.example/v1 |
| model | The model name activated in the provider’s console | your-model-name |
| api_key | Choose to read from an environment variable, set variable name to AGENT_API_KEY |
AGENT_API_KEY |
The request URL is formed by concatenating base_url with /chat/completions. For example, with the configuration above, the final request URL is https://api.provider.example/v1/chat/completions.
Note two things:
- When the wizard prompts for the key, select the environment variable method; do not paste the plaintext key directly.
- The
modelmust exactly match the model name in the provider’s console, including case and version suffixes.
After configuration, list the configured providers to confirm it was saved successfully:
agent-cli provider list
The output should include the name you just added, and the status should not show as missing a key.
V. Secure API Key Storage
5.1 Create a Key File with 600 Permissions
mkdir -p ~/.config/agentinstall -m 600 /dev/null ~/.config/agent/secrets.env
Open this file with an editor and write one line:
AGENT_API_KEY=replace_this_with_your_key
After saving, check the permissions and ownership:
stat -c '%a %U %n' ~/.config/agent/secrets.env
%n will output the full file path, so the expected output is:
600 agent /home/agent/.config/agent/secrets.env
The first two items are permissions and owner, and the third is the path. If your home directory is not /home/agent, the path will change accordingly. If the permission is not 600, run chmod 600 ~/.config/agent/secrets.env to fix it.
5.2 Load the Key into the Current Session
set -asource ~/.config/agent/secrets.envset +a
Check if the variable exists without printing the plaintext:
echo "Key length: ${#AGENT_API_KEY}"
A length of 0 indicates that it was not loaded successfully. An unusually short or long length usually means spaces or newlines were included during copying.
If you prefer not to write the key into a file, you can input it interactively in the current session. The input will not be echoed and will not enter shell history:
read -rsp "Enter API Key: " AGENT_API_KEY; echoexport AGENT_API_KEY
5.3 Mandatory Security Rules
- Do not write keys into source code, configuration templates, or scripts.
- Do not commit
secrets.envto Git repositories. If the file exists in a project directory, add it to.gitignore. - Do not directly write
Authorization: Bearer plaintext_keyin the command line. This leaves the key in the shell history and makes it visible in the process list. - When running as a service, reference this file using
EnvironmentFile=in the systemd unit, keeping the file permission at 600. - Set minimal permissions for the key in the provider’s console, enabling only the necessary models.
Key rotation falls into two scenarios. If a leak is suspected, revoke the old key in the console first, generate a new key, then update secrets.env and reload. There will be a period of service unavailability during this time, which is an acceptable trade-off. For routine rotation, the order can be reversed: generate the new key first, update secrets.env, verify the new key works using the tests in Section VI, and finally revoke the old key. When running as a service, remember to restart the service after updating.
VI. Layered Connectivity Testing
It is recommended to test layer by layer from the bottom up. If a lower layer fails, do not skip directly to the next layer.
6.1 Layer 1: Local Application
agent-cli --versionagent-cli provider list
If both commands output normally, it indicates that the application and configuration file are basically functional.
6.2 Layer 2: DNS Resolution
getent hosts api.provider.example
It should return an IP address. If there is no output, domain resolution failed, which could be due to a misspelled domain, a DNS service issue, or unconfigured intranet resolution.
6.3 Layer 3: HTTPS Connectivity
First, access the model list interface without a key to check if the network and TLS are reachable:
curl -sS -o /dev/null -w "HTTP %{http_code}\n" --max-time 10 https://api.provider.example/v1/models
The implications of this step’s results are as follows:
- Returns timeout, certificate error, or connection failure: There is a network or TLS issue; refer to Section VII.
- Returns
HTTP 200: The address is accessible, and the network and TLS are generally fine. However, since no key was carried in this step, a 200 response does not prove authentication passed, as some services do not require authentication for this endpoint. - Returns
HTTP 401: The request reached the server, and the server requires authentication. This means the network and TLS are reachable, but authentication is not yet complete.
Once the network is reachable, access the same interface with the key:
curl -sS -o /dev/null -w "HTTP %{http_code}\n" --max-time 10 \https://api.provider.example/v1/models \-H "Authorization: Bearer ${AGENT_API_KEY}"
Returning HTTP 200 with the key indicates that the key passed authentication for this interface. If it still returns 401, check whether the key has been loaded (see Section 5.2) or if it has been revoked.
Some providers do not offer a /models interface. In this case, a 404 response does not indicate a network failure; you can proceed directly to the next layer of testing.
If the network requires a proxy, curl will use the HTTPS_PROXY environment variable by default. You can also specify it explicitly with --proxy http://proxy_address:port. When the proxy is misconfigured, the symptom is usually a connection timeout rather than a certificate error, so you need to verify the proxy settings first.
6.4 Layer 4: Authentication and Real Conversation Request
curl -sS -w "\nHTTP %{http_code}\n" --max-time 30 \https://api.provider.example/v1/chat/completions \-H "Authorization: Bearer ${AGENT_API_KEY}" \-H "Content-Type: application/json" \-d '{"model":"your-model-name","messages":[{"role":"user","content":"Please reply: Connection successful"}],"max_tokens":32}'
The expected result is that HTTP 200 appears at the end, and the response body contains the choices field with the reply text generated by the model. The model field needs to be replaced with the actual model name.
This approach uses environment variable expansion, so the key is not written into the command history. However, note that the shell will replace ${AGENT_API_KEY} with plaintext before executing curl. The replaced parameters may appear in the process list during curl’s execution, which can be seen by other users on the same machine via ps. On a shared host, a safer approach is to write the request header to a file with 600 permissions and read it using -H @file_path:
printf 'Authorization: Bearer %s\n' "$AGENT_API_KEY" > ~/.config/agent/auth.headerchmod 600 ~/.config/agent/auth.headercurl -sS -w "\nHTTP %{http_code}\n" --max-time 30 \https://api.provider.example/v1/chat/completions \-H @$HOME/.config/agent/auth.header \-H "Content-Type: application/json" \-d '{"model":"your-model-name","messages":[{"role":"user","content":"Please reply: Connection successful"}],"max_tokens":32}'
After testing, delete the auth.header file. In production environments, prioritize using configuration files or short-term tokens supported by the provider, rather than long-term storage in temporary files.
6.5 Layer 5: Agent End-to-End
agent-cli run "Please reply: Connection successful"
The expected output is the model’s reply content. If Layer 4 succeeds but Layer 5 fails, the problem usually lies in the agent-cli configuration. There are two common reasons: the provider name is not bound as the default model, or the environment variables are not loaded in the runtime environment. In this case, check the key loading steps in Section V.
If it still fails, troubleshoot in the following order:
- Run
agent-cli --helpto see if there are parameters to specify the configuration file or default provider. - Locate the configuration file and logs as per the official documentation, and check the provider binding relationships.
- Try the verbose output parameter from the official documentation (commonly
--verboseor-v) to observe the request destination and returned status code.
VII. Troubleshooting Guide
When troubleshooting, first determine which category the error belongs to:
- Local configuration: command not found, missing configuration items, empty environment variables.
- Network: DNS failure, TLS certificate error, connection timeout.
- Server-side: returns 4xx or 5xx status codes, and the network layer has already passed.
| Symptom | Common Cause | Troubleshooting Command or Method | Fix Action |
|---|---|---|---|
command not found |
Installation directory not in PATH | echo $PATH |
Add ~/.local/bin to PATH and log in again |
| Domain cannot be resolved | Misspelled domain or DNS issue | getent hosts domain |
Correct base_url or check DNS configuration |
| TLS certificate error | System CA certificates expired or proxy replaced certificate | curl -v https://domain/v1/models |
Update CA certificates |
