Linux上SVN的搭建使用

小菜鸟战斗机 2018-3-21 165

开发人员经常会上传代码,或者改对代码做一些更改。svn就是用来将修改后的代码更新到服务器上的。下面就来看一下怎么在Linux环境下搭建svn服务(subversion)。

步骤:
1、检查是否已经有svn
2、安装subversion
3、检查是否安装成功
4、创建svn资源仓库
5、新增用户及密码,配置权限,配置资源库权限
6、启动或者重启服务
7、从机安装subversion
8、测试

一、检查是否已经有svn

如果没有安装就会是下面的样子,提示找不到命令。

[root@localhost  ~]#  svnserve  --version
-bash:  svnserve:  command  not  found
1
2
如果已经安装,会显示版本信息:

[root@localhost  ~]#    svnserve  --version
svnserve,  version  1.6.11  (r934486)
      compiled  Aug  17  2015,  08:37:43

Copyright  (C)  2000-2009  CollabNet.
Subversion  is  open  source  software,  see  http://subversion.tigris.org/
This  product  includes  software  developed  by  CollabNet  (http://www.Collab.Net/).

The  following  repository  back-end  (FS)  modules  are  available:

*  fs_base  :  Module  for  working  with  a  Berkeley  DB  repository.
*  fs_fs  :  Module  for  working  with  a  plain  file  (FSFS)  repository.

Cyrus  SASL  authentication  is  available.二、安装
在Linux下安装的是subversion,直接用yum  安装即可。

[root@localhost  ~]#
[root@localhost  ~]#  yum  install  -y  subversion
1
2
三、检查安装是否成功
同样用的是  svnserve  –version成功安装会显示版本信息

[root@localhost  ~]#  svnserve  --version
1
四、创建svn资源仓库
配置文件就是在这一步生成。

[root@localhost  ~]#  svnadmin  create  /svndir
[root@localhost  ~]#  cd  /svndir/
[root@localhost  svndir]#  ls
conf    db    format    hooks    locks    README.txt
[root@localhost  svndir]#  cd  conf/
[root@localhost  conf]#  ls
authz    passwd    svnserve.conf
1
2
3
4
5
6
7
五、新增用户及密码,配置权限
已经看到在仓库下面生成了三个文件
authz  #权限配置文件
passwd  #用户名密码文件
svnserve.conf  #资源库配置文件

[root@localhost  conf]#  vim  passwd
###  This  file  is  an  example  password  file  for  svnserve.
###  Its  format  is  similar  to  that  of  svnserve.conf.  As  shown  in  the
###  example  below  it  contains  one  section  labelled  [users].
###  The  name  and  password  for  each  user  follow,  one  account  per  line.

[users]
#  harry  =  harryssecret
#  sally  =  sallyssecret
yunwei  =  123456
~
1
2
3
4
5
6
7
8
9
10
11
新增一行:
yunwei  =  123456
新增用户“yunwei”,密码是“123456”

[root@localhost  conf]#  vim  authz


[groups]
#  harry_and_sally  =  harry,sally
#  harry_sally_and_joe  =  harry,sally,&joe

[/]
*  =  r
@admin  =  rw
dangerman  =

[svndir:/]
@admin  =  rw
1
2
3
4
5
6
7
8
9
10
11
12
13
14
在[groups]下面加入:
*  =  r  #所有用户有读权限
dangerman  =  ##危险分子?什么是危险分子?没有任何权限

[svndir:/]  ###定义目录,项目的根目录
*  =  rw

[root@localhost  conf]#  vim  svnserve.conf
1
这个配置文件打开下面几行前面的注释,删除最前面的空格:
anon-access  =  read
auth-access  =  write
password-db  =  passwd
authz-db  =  authz
realm  =  My  First  Repository

六、启动或者重启服务

[root@localhost  conf]#  /etc/init.d/svnserve  start
Starting  svnserve:                                                                          [    OK    ]
1
2
如果要指定目录要加参数:

[root@localhost  svndir]#  mkdir  /svndir/svn
[root@localhost  svndir]#  svnserve  -d  -r  /svndir/svn    ####(只是看一下可以指定目录,这个实验不需要)
svnserve:  Can't  bind  server  socket:  Address  already  in  use
1
2
3
问题来了!!!问题来了:
显示Address  already  in  use

原因在这里:svnserve  -d  -r  /svndir/svn  这条命令就是指定目录的启动。但是前面已经启动一次了。解决办法:

[root@localhost  svndir]#  /etc/init.d/svnserve  stop
Stopping  svnserve:                                                                                  [    OK    ]
[root@localhost  svndir]#  svnserve  -d  -r  /svndir/
[root@localhost  svndir]#  ls
conf    db    format    hooks    locks    README.txt 
[root@localhost  svndir]#  netstat  -antlp  |  grep  svn
tcp                0            0  0.0.0.0:3690                                0.0.0.0:*                                      LISTEN            5045/svnserve
1
2
3
4
5
6
7
七、测试,从机安装subversion
在次从机安装也安装一个subversion  用来测试。

注:
服务主机:192.168.1.65
从机:192.168.1.121

在从机上checkout  根目录

[root@localhost  ~]#  svn  checkout  svn://192.168.1.65/svndir/
Checked  out  revision  0.
1
2
需要注意的这里check的目录跟服务主机里面定义的[svndir]要一样。

[root@localhost  ~]#  svn  checkout  svn://192.168.1.65/svndir/
svn:  URL  'svn://192.168.1.65/svndir'  doesn't  exist
1
2
如果出现在这个报错,就要检查服务主机的auth配置文件了:
如果配置文件的的目录指定的是[svndir:/],而且svndir的目录在根下(/svndir)
那启动的时候即嫑指定目录直接用/etc/init.d/svnserve  start  启动即可。我们这里目录符合默认目录,不用指定了。直接用/etc/init.d/svnserve  start,或者不用指定目录。
svnserve  -d  -r  /svndir/  这表示指定目录到/svndir/
目录不对应会报错。

7.1:在从机上从机:192.168.1.121上提交

[root@localhost  ~]#  ls
Desktop        Downloads    Pictures    svndir          Videos
Documents    Music            Public        Templates
[root@localhost  ~]#  cd  svndir/
[root@localhost  svndir]#  ls
[root@localhost  svndir]#  touch  xiao
1
2
3
4
5
6
[root@localhost  svndir]#  ls
xiao
[root@localhost  svndir]#  pwd
/root/svndir
[root@localhost  svndir]#  ls
xiao
[root@localhost  svndir]#  svn  add  /root/svndir/xiao
A                  /root/svndir/xiao
1
2
3
4
5
6
7
8
[root@localhost  svndir]#  svn  commit  /root/svndir/xiao  -m  1
Authentication  realm:  <svn://192.168.1.65:3690>  My  First  Repository
Password  for  'root':
Authentication  realm:  <svn://192.168.1.65:3690>  My  First  Repository
Username:  ^Csvn:  Commit  failed  (details  follow):
    n:  Caught  signal
[root@localhost  svndir]#  svn  commit  /root/svndir/xiao  -m  1
Authentication  realm:  <svn://192.168.1.65:3690>  My  First  Repository
Password  for  'root':
Authentication  realm:  <svn://192.168.1.65:3690>  My  First  Repository
Username:  yunwei
Password  for  'yunwei':

-----------------------------------------------------------------------
ATTENTION!    Your  password  for  authentication  realm:

      <svn://192.168.1.65:3690>  My  First  Repository

can  only  be  stored  to  disk  unencrypted!    You  are  advised  to  configure
your  system  so  that  Subversion  can  store  passwords  encrypted,  if
possible.    See  the  documentation  for  details.

You  can  avoid  future  appearances  of  this  warning  by  setting  the  value
of  the  'store-plaintext-passwords'  option  to  either  'yes'  or  'no'  in
'/root/.subversion/servers'.
-----------------------------------------------------------------------
Store  password  unencrypted  (yes/no)?  yes
Adding                  xiao
Transmitting  file  data  .
Committed  revision  1.
1注意:
1、提交代码前,必须先cd到/root/svndir/(就是checkout下来的)目录里;
2、服务器上没有的文件,在客户端需要先add预提交,再commit,如果服务器端已有的文件,直接commit

预提交的命令:

  svn  add  /root/svndir/xiao
1
提交的命令:

svn  commit  /root/svndir/xiao  -m  1
1
出现committed  revision  1  提交成功了。

到服务端查看有没有提交成功:
服务主机:192.168.1.65

[root@localhost  svndir]#  svn  checkout  svn://192.168.1.65/svndir/
A        svndir/xiao
Checked  out  revision  1.
[root@localhost  svndir]#  ls
conf    db    format    hooks    locks    README.txt    svn    svndir
[root@localhost  svndir]#  cd  svndir/
[root@localhost  svndir]#  ls
xiao
1
2
3
4
5
6
7
8
接下来测试更新:

从机:192.168.1.121:

[root@localhost  svndir]#  ls
xiao
[root@localhost  svndir]#  vim  xiao

hello  koby  bryant  !
~
1
2
3
4
5
6
修改了xiao  这个文件的内容,之前是空文件,现在加上一行内容。
然后重新提交:

[root@localhost  svndir]#  svn  commit  /root/svndir/xiao  -m  2
Sending                xiao
Transmitting  file  data  .
Committed  revision  2.
1
2
3
4
服务器主机:192.168.1.65

[root@localhost  svndir]#  svn  up
          xiao
Updated  to  revision  2.
[root@localhost  svndir]#  ls
xiao
[root@localhost  svndir]#  vim  xiao
hello  koby  bryant  !
~
1
2
3
4
5
6
7
8
内容已经更改。

因为是更改内容,目录和文件已经有了,所以不用checkout了,直接svn  up就可以了。

7.2、在服务器主机:192.168.1.65上提交

[root@localhost  svndir]#  ls
xiao    yao
[root@localhost  svndir]#  pwd
/svndir/svndir
[root@localhost  svndir]#  svn  add  /svndir/svndir/yao
A                  /svndir/svndir/yao
[root@localhost  svndir]#  svn  commit  /svndir/svndir/yao  -m  3
Authentication  realm:  <svn://192.168.1.65:3690>  My  First  Repository
Password  for  'root':
Authentication  realm:  <svn://192.168.1.65:3690>  My  First  Repository
Username:  yunwei
Password  for  'yunwei':

-----------------------------------------------------------------------
ATTENTION!    Your  password  for  authentication  realm:

      <svn://192.168.1.65:3690>  My  First  Repository

can  only  be  stored  to  disk  unencrypted!    You  are  advised  to  configure
your  system  so  that  Subversion  can  store  passwords  encrypted,  if
possible.    See  the  documentation  for  details.

You  can  avoid  future  appearances  of  this  warning  by  setting  the  value
of  the  'store-plaintext-passwords'  option  to  either  'yes'  or  'no'  in
'/root/.subversion/servers'.
-----------------------------------------------------------------------
Store  password  unencrypted  (yes/no)?  yes
Adding                  yao
Transmitting  file  data  .
Committed  revision  3.
1注意:还是要cd到/svndir/svndir里面再预提交,然后提交。
显示Committed  revision  3.就说明提交成功

到从机:192.168.1.121上更新看效果:

[root@localhost  svndir]#  svn  up
A        yao
Updated  to  revision  3.
[root@localhost  svndir]#  ls
xiao    yao
[root@localhost  svndir]#  cat  yao
hello  rayallen
1
2
3
4
5
6
7
上面提交不管是在服务器主机上还是在从机上,都需要输入服务器主机的root用户密码,以及在conf文件里面设置的用户和密码。上面测试是用的文件测试,目录同样可以。
比如:
从机上:192.168.1.121

[root@localhost  svndir]#  mkdir  xiaoyao
[root@localhost  svndir]#  ls
xiao    xiaoyao    yao
[root@localhost  svndir]#  svn  add  /root/svndir/xiaoyao/
A                  /root/svndir/xiaoyao
[root@localhost  svndir]#  svn  commit  /root/svndir/xiaoyao/  -m  4
Adding                  xiaoyao

Committed  revision  4.
1
2
3
4
5
6
7
8
9
服务器主机上:192.168.1.65

[root@localhost  svndir]#  svn  up
A        xiaoyao
Updated  to  revision  4.
[root@localhost  svndir]#  ls
xiao    xiaoyao    yao
1
最新回复 (0)
返回