💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、豆包、星火、月之暗面及文生图、文生视频 广告
## 4.4 配置服务器 使用`authorized_keys`方法来对用户进行认证。 **设置认证** 创建一个操作系统用户`git`,并为其建立一个`.ssh`目录: ``` $ sudo adduser git $ su git $ cd $ mkdir .ssh && chmod 700 .ssh $ touch .ssh/authorized_keys && chmod 600 .ssh/authorized_keys ``` 为系统用户`git`的`authorized_keys`文件添加一些开发者 SSH 公钥: ``` $ cat /tmp/id_rsa.john.pub >> ~/.ssh/authorized_keys ``` 为开发者新建一个空仓库: ``` $ cd /opt/git $ mkdir project.git $ cd project.git $ git init --bare ``` 假定这个设置了`git`用户和 Git 仓库的服务器使用`gitserver`作为主机名。 同时,该服务器运行在内网,并且已在 DNS 配置中将`gitserver`指向此服务器。 那么任意一个拥有权限的用户,均可以推送本地库进行远程库的初始化: ``` # on John's computer $ cd myproject $ git init $ git add . $ git commit -m 'initial commit' $ git remote add origin git@gitserver:/opt/git/project.git $ git push origin master ``` 其他开发者可以克隆此仓库,并推回各自的改动: ``` $ git clone git@gitserver:/opt/git/project.git $ cd project $ vim README $ git commit -am 'fix for the README file' $ git push origin master ``` 注意:目前所有(获得授权的)开发者用户都能以系统用户`git`的身份登录服务器从而获得一个普通 shell。 如果你想对此加以限制,则需要修改`passwd`文件中(`git`用户所对应)的 shell 值。 **设置权限** 借助一个名为`git-shell`的受限 shell 工具,你可以方便地将用户`git`的活动限制在与 Git 相关的范围内。 首先确保`git-shell`已存在于`/etc/shells`文件中: $ cat /etc/shells # see if `git-shell` is already in there. If not... $ which git-shell # make sure git-shell is installed on your system. $ sudo vim /etc/shells # and add the path to git-shell from last command 使用`chsh <username>`命令修改任一系统用户的 shell: ``` $ sudo chsh git # and enter the path to git-shell, usually: /usr/bin/git-shell ``` 这样,用户`git`就只能利用 SSH 连接对 Git 仓库进行推送和拉取操作,而不能登录机器并取得普通 shell。