debian11安装code-server以非root用户开机启动

7 篇文章 0 订阅
3 篇文章 0 订阅

debian11安装code-server以非root用户开机启动

code-server是什么

Run VS Code on any machine anywhere and access it in the browser.
code-server 是一个可以在远程服务器上运行 VS Code 的工具。 通过浏览器访问,它可以让你: 在 Chromebook、平板电脑和笔记本电脑上都有一致的开发环境。
在这里插入图片描述

安装debian11虚拟机

Linux machine with WebSockets enabled, 1 GB RAM, and 2 CPUs

下载code-server安装包

在这里插入图片描述
运行

sudo dpkg -i code-server_4.4.0_amd64.deb

显示如下

~$ sudo dpkg -i code-server_4.4.0_amd64.deb 
[sudo] 用户的密码:
正在选中未选择的软件包 code-server。
(正在读取数据库 ... 系统当前共安装有 42710 个文件和目录。)
准备解压 code-server_4.4.0_amd64.deb  ...
正在解压 code-server (4.4.0) ...
正在设置 code-server (4.4.0) ...
(base) 用户@codeServer:~$ ls

直接启动只能在linux 系统访问

~$ code-server
 info  Wrote default config file to ~/.config/code-server/config.yaml
 info  code-server 4.4.0 b088ec7adf9e17bc75215f79e21498eb40da03ed
 info  Using user-data-dir ~/.local/share/code-server
 info  Using config file ~/.config/code-server/config.yaml
 info  HTTP server listening on http://127.0.0.1:8080/ 
 info    - Authentication is enabled
 info      - Using password from ~/.config/code-server/config.yaml
 info    - Not serving HTTPS 

所以,我们要修改一下配置文件

vi ~/.config/code-server/config.yaml

比如

bind-addr: 0.0.0.0:8080
auth: password
password: 111
cert: false

绑定IP为本地,端口为8080,密码为111,重新启动就可以了。

设置非root开机启动

本段主要解决debian11开机启动,并且非root启动
在这里插入图片描述

这里用的是添加rc.local的方法。查看sudo systemctl status rc-local的状态

下面,我们就来添加rc.local。输入命令 vi /etc/rc.local编辑脚本

#!/bin/sh -e
# 开头
# 中间写你的命令
sh /home/zzyy/code-server-start.sh
# 结尾
exit 0

我这里是在/etc/rc.local调用脚本

#! /bin/sh

su zzyy -c 'export PASSWORD="111"; cd /home/zzyy; /usr/bin/code-server --host 0.0.0.0 --port 8080 --auth password'

记得加上权限

chmod +x /etc/rc.local

再次执行sudo systemctl status rc-local查看状态,解决方案是复制一下/lib/systemd/system/rc-local.service/etc/systemd/system/rc-local.service修改一下,可以看参考的文章。

sudo cp /lib/systemd/system/rc-local.service /etc/systemd/system/rc-local.service
vi /etc/systemd/system/rc-local.service

主要是添加Install字段

(base) zzyy@codeServer:~$ cat /etc/systemd/system/rc-local.service
#  SPDX-License-Identifier: LGPL-2.1-or-later
#
#  This file is part of systemd.
#
#  systemd is free software; you can redistribute it and/or modify it
#  under the terms of the GNU Lesser General Public License as published by
#  the Free Software Foundation; either version 2.1 of the License, or
#  (at your option) any later version.

# This unit gets pulled automatically into multi-user.target by
# systemd-rc-local-generator if /etc/rc.local is executable.
[Unit]
Description=/etc/rc.local Compatibility
Documentation=man:systemd-rc-local-generator(8)
ConditionFileIsExecutable=/etc/rc.local
After=network.target

[Service]
Type=forking
ExecStart=/etc/rc.local start
TimeoutSec=0
RemainAfterExit=yes
GuessMainPID=no

[Install]
WantedBy=multi-user.target

其中Unit字段主要描述服务的启动顺序以及依赖关系,Service字段主要描述如何启动,Install字段描述如何安装这个服务。

sudo systemctl enable rc-local

启动,查看状态

sudo systemctl start rc-local.service
sudo systemctl status rc-local.service

查看状态

(base) zzyy@codeServer:~$ sudo systemctl status rc-local
[sudo] zzyy 的密码:
● rc-local.service - /etc/rc.local Compatibility
     Loaded: loaded (/etc/systemd/system/rc-local.service; enabled; vendor preset: enabled)
    Drop-In: /usr/lib/systemd/system/rc-local.service.d
             └─debian.conf
     Active: activating (start) since Wed 2022-05 ; 22min ago
       Docs: man:systemd-rc-local-generator(8)
Cntrl PID: 508 (rc.local)
      Tasks: 2 (limit: 2297)
     Memory: 2.0M
        CPU: 17ms
     CGroup: /system.slice/rc-local.service
             ├─508 /bin/sh -e /etc/rc.local start
             └─511 sh /home/zzyy/code-server-start.sh

5月  codeServer systemd[1]: Starting /etc/rc.local Compatibility...
5月  codeServer su[512]: (to zzyy) root on none
5月  codeServer su[512]: pam_unix(su:session): session opened for user zzyy(uid=1000) by (uid=0)

如果你的系统是刚安装的,应该跟我的不一样。

参考

Debian 10 配置开机自启动脚本
systemd系统开机运行rc.local
仓库地址

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值