diff --git a/lib/client.js b/lib/client.js index 5c921c9..36f9748 100644 --- a/lib/client.js +++ b/lib/client.js @@ -508,20 +508,26 @@ class ClawClient { _setHostname(clawId) { const hostname = `claw-${clawId}`; - // clawd runs as root (no User= in service), no sudo needed. - // Use ';' not '&&' so each command runs independently even if one fails. - const cmd = [ - `hostname ${hostname}`, - `echo "${hostname}" > /etc/hostname`, - `sed -i 's/^127\\.0\\.1\\.1.*/127.0.1.1 ${hostname}/' /etc/hosts`, - ].join('; '); - exec(cmd, { shell: true }, (err) => { - if (err) { - log.warn('clawd', `hostname set failed: ${err.message}`); - } else { - log.info('clawd', `hostname -> ${hostname}`); - } - }); + + // 运行时 hostname(无需文件权限) + exec(`hostname ${hostname}`, { shell: true }); + + // 写 /etc/hostname(fs 直接写文件,不需要目录可写) + try { + fs.writeFileSync('/etc/hostname', hostname + '\n', 'utf8'); + } catch (e) { + log.warn('clawd', `write /etc/hostname failed: ${e.message}`); + } + + // 更新 /etc/hosts(fs 读写,绕过 sed -i 需要目录可写的限制) + try { + const content = fs.readFileSync('/etc/hosts', 'utf8'); + const updated = content.replace(/^127\.0\.1\.1.*/m, `127.0.1.1 ${hostname}`); + fs.writeFileSync('/etc/hosts', updated, 'utf8'); + log.info('clawd', `hostname -> ${hostname}`); + } catch (e) { + log.warn('clawd', `write /etc/hosts failed: ${e.message}`); + } } // ── OpenClaw 配置 ──────────────────────────────────────────────────────────── diff --git a/package.json b/package.json index fa934c4..11a9688 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "clawd", - "version": "1.2.9", + "version": "1.3.0", "description": "Claw Box daemon - connects local Linux box to claw.cutos.ai via WebSocket", "main": "lib/client.js", "bin": {