Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學

Ubuntu 搭建 SVN 伺服器完整教學:svnserve + authz 權限控制 + 開機自啟動

編輯:Linux 系統常見問題解答

前言

Subversion (SVN) 仍是許多團隊的主力版控工具。本文在 Ubuntu 上以 **svnserve 獨立模式** 快速建立代碼倉庫,配置細粒度權限並設定開機自啟動。

1. 安裝 Subversion

sudo apt-get update
sudo apt-get install -y subversion

2. 建立倉庫根目錄與專案倉庫

sudo mkdir -p /home/svnroot
cd /home/svnroot
sudo svnadmin create example
# 產生 example/conf/{authz,passwd,svnserve.conf}

3. 配置權限三件套

passwd:使用者帳密

# /home/svnroot/example/conf/passwd
[users]
xiaoming = 1111
xiaowang = 2222
xiaoli   = 3333
laowang  = 4444
shuangshuang = 5555
dashi    = 6666

authz:群組與路徑權限

# /home/svnroot/example/conf/authz
[groups]
admin = xiaoli
dev   = laowang,shuangshuang,dashi,xiaowang,xiaoming

[/]
@admin = rw
@dev   = r

[/trunk]
@admin = rw
@dev   = r

[/tags]
@admin = rw
@dev   = r

[/branches]
@admin = rw
@dev   = rw

權限字元:`r`=讀,`w`=寫,空白=無權限。路徑為倉庫相對路徑。

svnserve.conf:服務行為

# /home/svnroot/example/conf/svnserve.conf
[general]
anon-access = none      # 禁止匿名
auth-access = write     # 認證用戶可寫
password-db = passwd
authz-db = authz
realm = My First Repository

[sasl]
# use-sasl = true      # 如需 SASL 加密再啟用

4. 建立系統服務腳本實現開機自啟動

# 建立啟動腳本
cat > /etc/init.d/svnd.sh <<'EOF'
#!/bin/bash
svnserve -d -r /home/svnroot
EOF

chmod +x /etc/init.d/svnd.sh

# 加入 rc.local(Ubuntu 16.04+ 需確保 rc-local 服務啟用)
echo "/etc/init.d/svnd.sh" >> /etc/rc.local
chmod +x /etc/rc.local

# 或使用 update-rc.d (SysV)
# update-rc.d svnd.sh defaults

5. 驗證服務啟動

# 重啟或手動啟動
/etc/init.d/svnd.sh

# 檢查進程
ps -ef | grep svnserve
# 應看到 svnserve -d -r /home/svnroot

6. 客戶端測試

# 檢出
svn checkout svn://192.168.1.100/example --username xiaoming

# 提交測試
echo "test" > test.txt
svn add test.txt
svn commit -m "init"

進階:多倉庫管理

`svnserve -r /home/svnroot` 以根目錄為基準,URL 為 `svn://host/repo_name`。新增倉庫只需在 `/home/svnroot` 下 `svnadmin create newrepo` 並配置對應 conf 即可。

結語

svnserve 輕量、無需 Apache 模組、配置簡單,適合中小團隊內網版控。核心在於 **authz 精細權限** 與 **開機自啟動** 落地。

Copyright © Windows教程網 All Rights Reserved