Skip to content

Commit aa9903e

Browse files
committed
docs: enhance LXD documentation with proper group setup and security best practices
- Add comprehensive LXD group setup guide based on official documentation - Document the permission denied socket issue and proper solutions - Add reboot as most reliable method for group membership activation - Distinguish between CI workarounds (sudo chmod 666) and local best practices - Update GitHub workflow with security warnings about CI-specific approaches - Add troubleshooting section with diagnostic commands (id -nG, getent group) - Include example of successful provisioning output - Clarify difference between being in lxd group vs having active group membership - Add proper sg lxd -c alternative for temporary group access - Based on real-world testing and official LXD security documentation
1 parent 617f77b commit aa9903e

File tree

3 files changed

+149
-3
lines changed

3 files changed

+149
-3
lines changed

.github/workflows/test-lxd-provision.yml

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
name: Test LXD Container Provisioning
22

3+
# NOTE: This workflow uses CI-specific approaches like 'sudo chmod 666' on the LXD socket
4+
# and 'sudo' with LXD commands. These approaches are NOT recommended for local development.
5+
# For local use, follow the proper group membership approach documented in config/lxd/README.md
6+
37
on:
48
push:
59
branches: [main, develop]
@@ -29,7 +33,9 @@ jobs:
2933
# Add runner to lxd group
3034
sudo usermod -a -G lxd runner
3135
32-
# Fix socket permissions for CI environment
36+
# IMPORTANT: This approach is ONLY for CI environments
37+
# For local development, use proper group membership instead
38+
# Fix socket permissions for CI environment (NOT recommended for local use)
3339
sudo chmod 666 /var/snap/lxd/common/lxd/unix.socket
3440
3541
# Test basic LXD functionality
@@ -61,7 +67,8 @@ jobs:
6167
- name: Apply configuration
6268
working-directory: config/lxd
6369
run: |
64-
# Run with sudo to ensure LXD access
70+
# Run with sudo to ensure LXD access in CI environment
71+
# NOTE: For local development, use "sg lxd -c 'tofu apply'" instead
6572
sudo -E tofu apply -auto-approve
6673
6774
- name: Wait for container to be ready
@@ -130,6 +137,8 @@ jobs:
130137
working-directory: config/lxd
131138
run: |
132139
echo "Cleaning up container..."
140+
# Use sudo for CI environment cleanup
141+
# NOTE: For local development, use "sg lxd -c 'tofu destroy'" instead
133142
sudo -E tofu destroy -auto-approve || true
134143
sudo lxc delete torrust-vm --force || true
135144

config/lxd/README.md

Lines changed: 135 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,17 @@ If both commands return version information, you can skip the installation steps
4141
# Add your user to the lxd group
4242
sudo usermod -a -G lxd $USER
4343

44-
# You may need to log out and back in for group changes to take effect
44+
# IMPORTANT: For group membership to take effect, you MUST do ONE of:
45+
# Option 1 (Temporary): Use 'newgrp lxd' to start a new shell with the group active
46+
# Option 2: Log out and log back in completely
47+
# Option 3: Close terminal and open a new one
48+
# Option 4 (Most Reliable): Reboot your system if other methods don't work
49+
50+
# Verify you're in the lxd group (should show your username)
51+
getent group lxd | grep "$USER"
52+
53+
# Verify the group is active in your session (lxd should be in the list)
54+
id -nG
4555
```
4656

4757
2. **OpenTofu**: Install from [https://opentofu.org/](https://opentofu.org/)
@@ -55,6 +65,62 @@ If both commands return version information, you can skip the installation steps
5565

5666
## Configuration
5767

68+
### Proper LXD Group Setup (Required for Direct LXD Access)
69+
70+
**The Problem**: LXD's client (`lxc`) connects to the daemon via a Unix socket that only members of the `lxd` group can access. Even if your user is in the `lxd` group, your current shell session might not have picked up that membership yet, resulting in "permission denied" errors.
71+
72+
**The Solution**: Based on the [official LXD documentation](https://documentation.ubuntu.com/lxd/en/latest/tutorial/first_steps/#add-the-current-user-to-the-lxd-group), follow these steps:
73+
74+
1. **Check if you're already in the lxd group**:
75+
76+
```bash
77+
getent group lxd | grep "$USER"
78+
```
79+
80+
If this returns your username, you're already in the group.
81+
82+
2. **Verify your current session has the group active**:
83+
84+
```bash
85+
id -nG
86+
```
87+
88+
If `lxd` is not in the list, your session hasn't picked up the group membership.
89+
90+
3. **If not in the group, add yourself**:
91+
92+
```bash
93+
sudo usermod -aG lxd "$USER"
94+
```
95+
96+
4. **Activate the group membership** (choose ONE method):
97+
98+
**Method 1 (Temporary)**: Start a subshell with the group active:
99+
100+
```bash
101+
newgrp lxd
102+
```
103+
104+
**Method 2 (Permanent)**: Log out and log back in completely
105+
106+
**Method 3 (Permanent)**: Close your terminal and open a new one
107+
108+
**Method 4 (Most Reliable)**: Reboot your system - this ensures all processes pick up the new group membership
109+
110+
**Note**: In some cases, logging out/in or closing the terminal may not be sufficient, and a full system reboot may be required to properly activate the group membership.
111+
112+
5. **Verify the setup works**:
113+
114+
```bash
115+
lxc version
116+
```
117+
118+
If this works without `sudo` or permission errors, you're all set!
119+
120+
**Important**: The official LXD documentation states that if `lxd init --minimal` results in an error, "your group membership might not have taken effect. In this case, close and re-open your terminal, then try again."
121+
122+
### Container Configuration Files
123+
58124
The LXD configuration consists of:
59125

60126
- **`main.tf`** - OpenTofu configuration defining the LXD container and profile
@@ -107,6 +173,26 @@ To provision the container:
107173

108174
Type `yes` when prompted to confirm the creation.
109175

176+
**Note**: If you encounter LXD socket permission issues, use:
177+
178+
```bash
179+
sg lxd -c "tofu apply"
180+
```
181+
182+
Example successful output:
183+
184+
```text
185+
Apply complete! Resources: 2 added, 0 changed, 0 destroyed.
186+
187+
Outputs:
188+
container_info = {
189+
"image" = "ubuntu:22.04"
190+
"ip_address" = "10.140.190.155"
191+
"name" = "torrust-vm"
192+
"status" = "Running"
193+
}
194+
```
195+
110196
5. **Get container information**:
111197

112198
```bash
@@ -119,6 +205,9 @@ To provision the container:
119205
# Direct shell access
120206
lxc exec torrust-vm -- /bin/bash
121207

208+
# If you have LXD permission issues, use:
209+
sg lxd -c "lxc exec torrust-vm -- /bin/bash"
210+
122211
# SSH access (if you configured your SSH key and networking)
123212
ssh torrust@<container-ip-address>
124213
```
@@ -131,14 +220,21 @@ To provision the container:
131220
# Using lxc directly
132221
lxc exec torrust-vm -- /bin/bash
133222

223+
# If you have permission issues, use sg to switch to lxd group
224+
sg lxd -c "lxc exec torrust-vm -- /bin/bash"
225+
134226
# Or via SSH (if you configured your SSH key)
135227
ssh torrust@<container-ip-address>
136228
```
137229

138230
### Check Container Status
139231

140232
```bash
233+
# Preferred (if group membership is active)
141234
lxc list
235+
236+
# If you get permission denied, use:
237+
sg lxd -c "lxc list"
142238
```
143239

144240
### Stop the Container
@@ -197,13 +293,26 @@ To destroy the container and clean up resources:
197293

198294
Type `yes` when prompted to confirm the destruction.
199295

296+
**Note**: If you encounter LXD permission issues, use:
297+
298+
```bash
299+
sg lxd -c "tofu destroy"
300+
```
301+
200302
## Troubleshooting
201303

202304
### Common Issues
203305

204306
1. **LXD not found**: Ensure LXD is installed via snap
205307
2. **Permission errors**: Make sure your user is in the `lxd` group
206308
3. **Socket permissions**: In CI environments, you may need to adjust socket permissions
309+
4. **LXD unix socket not accessible**: If you get `Error: LXD unix socket "/var/snap/lxd/common/lxd/unix.socket" not accessible: permission denied`, follow the [Proper LXD Group Setup](#proper-lxd-group-setup-required-for-direct-lxd-access) section above. Quick fixes:
310+
- **Most Reliable**: Reboot your system after adding yourself to the `lxd` group
311+
- **Alternative**: Log out and log back in after adding yourself to the `lxd` group
312+
- **Temporary**: Use `sg lxd -c "lxc command"` to run LXD commands with proper group access
313+
- **Alternative**: Use `newgrp lxd` to activate the group in your current session
314+
- **CI environments only**: As a temporary workaround (not recommended for regular use), you can use `sudo chmod 666 /var/snap/lxd/common/lxd/unix.socket` but this creates security risks
315+
- **Last resort**: Use `sudo` with LXD commands, though this is not recommended for regular use
207316

208317
### Useful Commands
209318

@@ -219,6 +328,29 @@ lxc info torrust-vm
219328

220329
# View container logs
221330
lxc info torrust-vm --show-log
331+
332+
# Troubleshoot LXD permissions
333+
# Check if you're in the lxd group
334+
groups $USER
335+
336+
# Check current active groups (lxd should be in this list)
337+
id -nG
338+
339+
# If lxd is missing from id -nG, your session hasn't picked up group membership
340+
# Fix with: newgrp lxd (temporary), log out/in, or reboot (most reliable)
341+
342+
# Check socket permissions
343+
ls -la /var/snap/lxd/common/lxd/unix.socket
344+
345+
# If permission denied error occurs, try activating lxd group
346+
newgrp lxd
347+
348+
# Or use sg (switch group) to run commands with proper group access
349+
sg lxd -c "lxc list"
350+
sg lxd -c "lxc info torrust-vm"
351+
352+
# Or as a workaround, use sudo (not recommended for regular use)
353+
sudo lxc list
222354
```
223355

224356
## GitHub Actions Support
@@ -231,6 +363,8 @@ This configuration is designed specifically for CI/CD environments like GitHub A
231363
- **Status**: Fully supported and tested
232364
- **No special requirements**: Works in standard GitHub Actions runners
233365

366+
**Important Note**: The GitHub workflow uses `sudo chmod 666` on the LXD socket as a workaround for CI environments where terminal restarts aren't practical. **This approach is not recommended for local development** due to security implications. For local use, follow the proper group membership approach described in the troubleshooting section.
367+
234368
### Pros and Cons
235369

236370
**✅ Advantages:**

project-words.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
cloudinit
22
cpus
33
debootstrap
4+
getent
45
larstobi
56
lxdbr
67
multipassd
8+
newgrp
79
noninteractive
810
NOPASSWD
911
resolv
1012
runcmd
13+
subshell
1114
sysfs
1215
tfstate
1316
tfvars

0 commit comments

Comments
 (0)