向git服務器添加shh公鑰

git大全:  https://git-scm.com/book/zh/v2/%E6%9C%8D%E5%8A%A1%E5%99%A8%E4%B8%8A%E7%9A%84-Git-%E5%8D%8F%E8%AE%AE


git服務器搭建以後,用戶但願拉取和提交代碼的時候不須要輸入密碼,能夠經過將
用戶的ssh公鑰(~/.ssh.id_rsa.pub)加入到git服務器的 ~/.ssh/authorized_keys 文件中

步驟一,從客戶端得到 SSH 公鑰

爲了使客戶端能夠向 Git 服務器提供 SSH 公鑰,首先要確認客戶端擁有公鑰。SSH 的密鑰存儲在 ~/.ssh/ 目錄下,下面咱們查看一下這裏面都有哪些文件:html

[user@local ~]$ ls .ssh/ id_rsa id_rsa_osc id_rsa_osc.pub id_rsa.pub known_hosts [user@local ~]$git

上面的 xxx 和 xxx.pub 分別是一個 SSH 私鑰和公鑰。算法

這裏 id_rsa(私鑰) 和 id_rsa.pub(公鑰) 是一對兒,而 id_rsa_osc 和 id_rsa_osc.pub 又是一對兒私鑰和公鑰。因而可知,一個用戶是能夠擁有多個密鑰的,可是這並不影響咱們後面對 Git 服務器的配置,使用任何一個公鑰均可以。vim

若是用戶沒有密鑰文件,甚至連 .ssh 目錄都沒有,那麼說明用戶尚未建立 SSH 密鑰,咱們使用 ssh-keygen 命令能夠爲其生成密鑰。服務器

[user@local ~]$ ssh-keygen -t RSA -C "user@126.com" Generating public/private RSA key pair. Enter file in which to save the key (/home/user/.ssh/idrsa): Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /home/user/.ssh/idrsa. Your public key has been saved in /home/user/.ssh/id_rsa.pub. The key fingerprint is: e1:ec:5c:cd:89:8f:83:a2:aa:5d:8a:7f:93:12:90:f4 user@126.com The key's randomart image is: +--[ RSA 2048]----+ | | | . | |… . | |o E o . + . | | . S o + | | . o o o | | …. + o . | | o.o+. . . | |oo=+.. | +-----------------+ [user@local ~]$dom

-t RSA 參數表示使用 RSA 算法。ssh

-C 參數指定用戶的電子郵箱地址。ide

接下來 ssh-keygen 命令會詢問用戶密鑰文件的存儲路徑以及密碼等,若是不設置密碼直接鍵入回車便可。測試

密鑰文件默認保存在 ~/.ssh/id_rsa 和 ~/.ssh/id_rsa.pub,公鑰文件內容相似以下:spa

[user@local ~]$ cat .ssh/id_rsa.pub ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDVzaGljR5OjgA3VUPO/C/0eIBBhcUQ9v2glTmim1iJ2nOmqTg1lBUlgQCwaIw6f9qJk6J+ibypzJifnic90dfsItlPLBaiMd+/KqZmJymsPOsB2+aQhGXwbj3StTkA1S3KCbUFSRYj3M1CwCGBLxLSyG/wKS/wUeVXtkwAHfSfR7jzkcB5ZyZY6ioxHsMvkCA7ORPaw5zE4MJNw0K9o25sCrgC5RyPUIcEvEt4lo7weaifhNwp5Ql21lVHKknyTALXqETeAhkYrSgueH54srszYJ3A4l+JvpFhHWC/0lF+lZaWtD/VKzs9HSvyYKAs+ovGZAfZY+AC7Et+MWLtlsmf user@126.com [user@local ~]$

至此,從客戶端得到密鑰的步驟就完成了,接下來須要把公鑰發送個 Git 服務器倉庫管理員。

步驟二,搭建 Git 服務器(已有 Git 服務器管理用戶的能夠跳過此步驟)

爲了便於管理,須要在系統中創建一個單獨的用戶來管理全部的 Git 倉庫。

[user@local ~]$ sudo adduser git Adding user git' ... Adding new groupgit' (1001) … Adding new user `git' (1001) with group `git' … Creating home directory /home/git' ... Copying files from/etc/skel' … Enter new UNIX password: Retype new UNIX password: passwd: password updated successfully Changing the user information for git Enter the new value, or press ENTER for the default Full Name []: git Room Number []: Work Phone []: Home Phone []: Other []: Is the information correct? [Y/n] y [user@local ~]$

咱們給這個用戶取名爲 git。

全部的遠程 Git 倉庫均可以在這個用戶名下創建,因此之後創建新庫或者將新用戶的 SSH 公鑰添加到服務器時,都使用這個用戶操做就能夠了。

步驟三,創建遠程倉庫(已有遠程倉庫的能夠跳過此步驟)

切換到新建的 git 賬號,並創建一個空的遠程倉庫。

[user@local ~]$ su git Password: git@Linux:/home/user$ cd ~ git@Linux:~$ mkdir project.git git@Linux:~$ cd project.git git@Linux:~/project.git$ git init --bare Initialized empty Git repository in /home/git/project.git/ git@Linux:~$

創建遠程倉庫使用 git init 命令,也能夠增長 --bare 參數。

寫不寫 --bare 參數有什麼區別呢?

咱們知道,通常從遠程 clone 下來的倉庫會生成一個獨立的目錄,在這個目錄下有當前分支最新版本的文件,同時還有一個 .git 文件夾,與 .git 同級的文件夾稱爲咱們的「工做目錄」,咱們的修改都在這個目錄中進行。而 .git 就是咱們 Git 本地倉庫的工做目錄,咱們 add 和 commit 的東西都會被提交到這個目錄中。

對 git init 命令添加 --bare 參數就表示初始化 Git 倉庫的時候不要建立本地工做目錄,因此至關於 .git 文件夾下的全部內容直接建立到當前目錄下,而不是被放到 .git 目錄下。

在 Git 服務器上創建好倉庫之後,用戶就能夠克隆這個倉庫了。等等。。還沒配置用戶 SSH 公鑰呢,這麼就讓用戶去下載,確定仍是要輸入密碼才行的。

步驟四,在 Git 服務器上爲用戶配置 SSH 公鑰

仍是先在 Git 服務器上使用 authorized_keys 文件來管理全部用戶的 SSH 公鑰。

git@Linux:~$ mkdir .ssh git@Linux:~$ touch .ssh/authorizedkeys git@Linux:~$ chmod 600 .ssh/authorizedkeys git@Linux:~$

ssh/authorized_keys git@Linux:~$ cat /tmp/id_rsa_user2.pub >> ~/.ssh/authorized_keys git@Linux:~$ cat /tmp/id_rsa_user3.pub >> ~/.ssh/authorized_keys git@Linux:~$
使用 ssh -T username@remoteIp 進行鏈接測試,若是要輸入密碼則不正確,反之成功

如今 user一、user2 和 user3 就能夠經過 SSH 公鑰來操做遠程 Git 倉庫了,快去試試吧。

以centOS系統爲例,記錄下修改Jenkins以root用戶運行的方法。

修改Jenkins配置文件
# 打開配置文件
vim /etc/sysconfig/jenkins
# 修改$JENKINS_USER,並去掉當前行註釋
$JENKINS_USER="root"

修改Jenkins相關文件夾用戶權限
chown -R root:root /var/lib/jenkins
chown -R root:root /var/cache/jenkins
chown -R root:root /var/log/jenkins

重啓Jenkins服務並檢查運行Jenkins的用戶是否已經切換爲root
# 重啓Jenkins(如果其餘方式安裝的jenkins則重啓方式略不一樣)
service jenkins restart
# 查看Jenkins進程所屬用戶
ps -ef | grep jenkins
# 若顯示爲root用戶,則表示修改完成
相關文章
相關標籤/搜索