You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
Copy file name to clipboardExpand all lines: config/lxd/README.md
+135-1Lines changed: 135 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -41,7 +41,17 @@ If both commands return version information, you can skip the installation steps
41
41
# Add your user to the lxd group
42
42
sudo usermod -a -G lxd $USER
43
43
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
45
55
```
46
56
47
57
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
55
65
56
66
## Configuration
57
67
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
+
58
124
The LXD configuration consists of:
59
125
60
126
-**`main.tf`** - OpenTofu configuration defining the LXD container and profile
@@ -107,6 +173,26 @@ To provision the container:
107
173
108
174
Type `yes` when prompted to confirm the creation.
109
175
176
+
**Note**: If you encounter LXD socket permission issues, use:
# SSH access (if you configured your SSH key and networking)
123
212
ssh torrust@<container-ip-address>
124
213
```
@@ -131,14 +220,21 @@ To provision the container:
131
220
# Using lxc directly
132
221
lxc exec torrust-vm -- /bin/bash
133
222
223
+
# If you have permission issues, use sg to switch to lxd group
224
+
sg lxd -c "lxc exec torrust-vm -- /bin/bash"
225
+
134
226
# Or via SSH (if you configured your SSH key)
135
227
ssh torrust@<container-ip-address>
136
228
```
137
229
138
230
### Check Container Status
139
231
140
232
```bash
233
+
# Preferred (if group membership is active)
141
234
lxc list
235
+
236
+
# If you get permission denied, use:
237
+
sg lxd -c "lxc list"
142
238
```
143
239
144
240
### Stop the Container
@@ -197,13 +293,26 @@ To destroy the container and clean up resources:
197
293
198
294
Type `yes` when prompted to confirm the destruction.
199
295
296
+
**Note**: If you encounter LXD permission issues, use:
297
+
298
+
```bash
299
+
sg lxd -c "tofu destroy"
300
+
```
301
+
200
302
## Troubleshooting
201
303
202
304
### Common Issues
203
305
204
306
1.**LXD not found**: Ensure LXD is installed via snap
205
307
2.**Permission errors**: Make sure your user is in the `lxd` group
206
308
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
207
316
208
317
### Useful Commands
209
318
@@ -219,6 +328,29 @@ lxc info torrust-vm
219
328
220
329
# View container logs
221
330
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
# 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
222
354
```
223
355
224
356
## GitHub Actions Support
@@ -231,6 +363,8 @@ This configuration is designed specifically for CI/CD environments like GitHub A
231
363
-**Status**: Fully supported and tested
232
364
-**No special requirements**: Works in standard GitHub Actions runners
233
365
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.
0 commit comments