如何在 Ubuntu 18.04 上設置 MySecureShell SFTP 服務器

在本文中,我將向您解釋如何在 Ubuntu 18.04 上使用 MySecureShell 設置 SFTP 服務器。 SFTP 是一種使用加密 SSH 連接傳輸文件的安全方式。 儘管現代 FTP 客戶端廣泛支持它,但它是一種與 FTP(文件傳輸協議)完全不同的協議。

您可能會問我為什麼使用 MySecureShell 而不是傳統的 FTP 服務器。 這裡有幾個特點,

  • 使用 SSH 進行安全數據傳輸
  • 無需管理 SSL 證書
  • 易於安裝和配置
  • 限制帶寬使用
  • 文件和文件夾限制
  • 使用 IP/用戶名/組/虛擬主機的訪問控制列表
  • 限制用戶只能使用 sftp(默認情況下禁用 shell 訪問)
  • 增強的日誌系統

所以讓我們先從安裝開始,FTP的基本知識足以理解本教程。

安裝

從 ubuntu 15.04 及更高版本開始,MySecureShell 在默認存儲庫中可用。 我們正在使用 ubuntu 18.04 進行此安裝。 只需運行以下命令即可安裝 MySecureShell。

apt-get install mysecureshell

如果不可用,請執行以下步驟,確保以 root 用戶身份運行以下所有步驟。

vim /etc/apt/sources.list

添加以下兩行,

deb https://mysecureshell.free.fr/repository/index.php/ubuntu testing main
deb-src https://mysecureshell.free.fr/repository/index.php/ubuntu testing main

現在添加 gpg 密鑰,

gpg --keyserver hkp://pool.sks-keyservers.net --recv-keys E328F22B; gpg --export E328F22B | apt-key add
gpg: keybox '/root/.gnupg/pubring.kbx' created
gpg: /root/.gnupg/trustdb.gpg: trustdb created
gpg: key 7601D76CE328F22B: public key "MySecureShell repository " imported
gpg: Total number processed: 1
gpg:           	imported: 1
OK

添加存儲庫後,您可以開始安裝

apt-get update
apt-get install mysecureshell

現在您可以啟動服務並檢查其狀態

systemctl start mysecureshell.service
systemctl status mysecureshell.service

樣本輸出

# systemctl start mysecureshell.service
root@li1004-153:~# systemctl status mysecureshell.service
* mysecureshell.service - LSB: MySecureShell SFTP Server
   Loaded: loaded (/etc/init.d/mysecureshell; generated)
   Active: active (exited) since Fri 2018-05-18 01:02:17 UTC; 4min 44s ago
     Docs: man:systemd-sysv-generator(8)
    Tasks: 0 (limit: 2322)
   CGroup: /system.slice/mysecureshell.service

May 18 01:02:17 004-153 systemd[1]: Starting LSB: MySecureShell SFTP Server...
May 18 01:02:17 004-153 mysecureshell[1314]: Starting MySecureShell SFTP Server: mysecureshell is now online with restricted features
May 18 01:02:17 004-153 mysecureshell[1314]: Note: To enable all features you have to change mysecureshell binary rights to 4755
May 18 01:02:17 004-153 systemd[1]: Started LSB: MySecureShell SFTP Server.

# systemctl status mysecureshell.service
* mysecureshell.service - LSB: MySecureShell SFTP Server
   Loaded: loaded (/etc/init.d/mysecureshell; generated)
   Active: active (exited) since Fri 2018-05-18 01:02:17 UTC; 50min ago
     Docs: man:systemd-sysv-generator(8)
    Tasks: 0 (limit: 2322)
   CGroup: /system.slice/mysecureshell.service

May 18 01:02:17 004-153 systemd[1]: Starting LSB: MySecureShell SFTP Server...
May 18 01:02:17 004-153 mysecureshell[1314]: Starting MySecureShell SFTP Server: mysecureshell is now online with restricted features
May 18 01:02:17 004-153 mysecureshell[1314]: Note: To enable all features you have to change mysecureshell binary rights to 4755
May 18 01:02:17 004-153 systemd[1]: Started LSB: MySecureShell SFTP Server.
#

使用以下命令停止和重新啟動 mysecureshell 服務

systemctl stop mysecureshell.service
systemctl restart mysecureshell.service

MySecureShell SFTP 用戶創建

首先,我們要找到MySecureShell的安裝位置,

whereis mysecureshell

正如您在下面的輸出中看到的,mysecureshell 安裝路徑是 /usr/bin/mysecureshell。

mysecureshell: /usr/bin/mysecureshell /usr/share/man/man8/mysecureshell.8.gz

現在,在驗證路徑後讓我們創建一個用戶

useradd -m -s /usr/bin/mysecureshell testsftpuser
passwd testsftpuser

正如您在上面的命令中看到的,我們正在創建一個用戶並分配 mysecureshell 位置的路徑。 此外,您可以使用以下命令分配現有用戶訪問和使用 MySecureShell:

sudo usermod -s /usr/bin/mysecureshell testsftpuser

用戶連接

現在“testsftpuser”用戶可以使用您的機器IP地址從您的客戶端連接到您的SFTP服務器,如下所示,

sftp [email protected]

你機器的IP

Connected to 45.33.54.153
sftp>

此外,您可以使用任何圖形客戶端(例如 FileZilla)從您的客戶端系統登錄到您的 SFTP 服務器。

MySecureShell 命令

MySecureShell 具有以下一組命令來管理您的 SFTP 服務器。

  • sftp-admin
  • sftp-kill
  • sftp 狀態
  • sftp用戶
  • sftp驗證
  • sftp-who

sftp-admin

此命令允許遠程管理 MySecureShell。

sftp-admin [ssh options] user@hostname

sftp-kill

它將斷開用戶與 FTP 服務器的連接。

sftp-kill testsftpuser
Kill testsftpuser on PID 1961
(Press "Y" when requested)

sftp 狀態

顯示 ftp 服務器狀態

# sftp-state

樣本輸出

# sftp-state
Server is up
#

sftp用戶

該命令允許您創建 SFTP 用戶,而無需指定我們之前所做的路徑。

sftp-user create test

列出 SFTP 用戶

sftp-user list
test
testsftpuser

命令將刪除用戶 test

sftp-user delete test

sftp驗證

此命令將驗證並更正 MySecureShell 服務器上的問題。

sftp-verif
################################################################################
MySecureShell Verification Tool
################################################################################

### Verifing file existance ###

/bin/MySecureShell                                                       [ OK ]
/bin/sftp-who                                                            [ OK ]
/bin/sftp-kill                                                           [ OK ]
/bin/sftp-state                                                          [ OK ]
/bin/sftp-admin                                                          [ OK ]
/bin/sftp-verif                                                          [ OK ]
/bin/sftp-user                                                           [ OK ]

### Verifing rights ###

Verifing file rights of /etc/ssh/sftp_config                             [ OK ]
Verifing file rights of /bin/sftp-who                                    [ OK ]
Verifing file rights of /bin/sftp-verif                                  [ OK ]
Verifing file rights of /bin/sftp-user                                   [ OK ]
Verifing file rights of /bin/sftp-kill                                   [ OK ]
Verifing file rights of /bin/sftp-state                                  [ OK ]
Verifing file rights of /bin/sftp-admin                                  [ OK ]
Verifing file rights of /bin/MySecureShell                               [ OK ]

### Verifing rotation logs ###
Rotation logs have been found                                            [ OK ]
### Verifing server status ###
Verifing server status (ONLINE)                                          [ OK ]
[...]

sftp-who

此命令將告訴您當前登錄到 FTP 服務器的人員。

sftp-who
# sftp-who
--- 1 / 10 clients ---
Global used bandwidth : 0 bytes/s / 0 bytes/s
PID: 2207   Name: testsftpuser   IP: pa39-178-9-194.pa.nsw.optusnet.com.au
        Home: /home/testsftpuser
        Status: idle    Path: /
        File:
        Connected: 2018/05/18 01:30:50 [since 03mins 59s]
        Speed: Download: 0 bytes/s [5.00 kbytes/s]  Upload: 0 bytes/s [unlimited]
        Total: Download: 924 bytes   Upload: 100 bytes
#

配置

MySecureShell 的主要配置文件是 /etc/ssh/sftp_config。 您可以在配置文件中配置上傳和下載帶寬、chroot 用戶、最大連接數等。 您可以將此選項設置為所有人或僅適用於特定組。

cat /etc/ssh/sftp_config

默認標籤

如果要為所有用戶應用配置,則使用默認標記

#Default rules for everybody
Default
        GlobalDownload          50k     #total speed download for all clients
                                        # o -> bytes   k -> kilo bytes   m -> mega bytes
        GlobalUpload            0       #total speed download for all clients (0 for unlimited)
        Download                5k      #limit speed download for each connection
        Upload                  0       #unlimit speed upload for each connection
        StayAtHome              true    #limit client to his home
        VirtualChroot           true    #fake a chroot to the home account
        LimitConnection         10      #max connection for the server sftp
        LimitConnectionByUser   1       #max connection for the account
        LimitConnectionByIP     2       #max connection by ip for the account
        Home                    /home/$USER     #overrite home of the user but if you want you can use
                                                #       environment variable (ie: Home /home/$USER)
        IdleTimeOut             5m      #(in second) deconnect client is idle too long time
        ResolveIP               true    #resolve ip to dns
        LogFile         /var/log/sftp-server_ftp.log
#       IgnoreHidden            true    #treat all hidden files as if they don't exist
#       DirFakeUser             true    #Hide real file/directory owner (just change displayed permissions)
#       DirFakeGroup            true    #Hide real file/directory group (just change displayed permissions)
#       DirFakeMode             0400    #Hide real file/directory rights (just change displayed permissions)
                                        #Add execution right for directory if read right is set
        HideNoAccess            true    #Hide file/directory which user has no access
#       MaxOpenFilesForUser     20      #limit user to open x files on same time
#       MaxWriteFilesForUser    10      #limit user to x upload on same time
#       MaxReadFilesForUser     10      #limit user to x download on same time
        DefaultRights           0640 0750       #Set default rights for new file and new directory
#       MinimumRights           0400 0700       #Set minimum rights for files and dirs

        ShowLinksAsLinks        false   #show links as their destinations
#       ConnectionMaxLife       1d      #limits connection lifetime to 1 day

#       Charset                 "ISO-8859-15"   #set charset of computer

Default

筆記: 默認情況下不啟用日誌,您可以在配置文件中定義日誌文件位置。

文件規格標籤

FileSpec 標籤用於在文件和目錄上創建過濾器。

    # Only check against filenames/folder names only
FileSpec
    UseFullPath false

    # we can use multiple deny/allow directives for clarity
    Order DenyAllow
    Deny ".*.exe$"
    Deny ".*.sh$"
    Allow all
FileSpec

用戶標籤
用戶標記為特定用戶主文件夾定義

User tom 
Home /home/tom 
User

虛擬主機標籤

VirtualHost 標籤可以根據虛擬主機名設置限制

    # Set home directory for this virtualhost
VirtualHost
    Home                    /var/www/html/www.mysftpsite.com
    # Set dedicated log file
    LogFile                 /var/log/sftp/www.mysftpsite.com
    # Override the maximum number of connection per user
    LimitConnectionByUser   5
VirtualHost

gFTP 安裝

現在,讓我們按照以下步驟在 Ubuntu 18.04 上嘗試 gFTP 安裝,

sudo apt-get install gftp
  • 從應用程序打開 gFTP
  • Enter SFTP 服務器的 IP 地址、端口號(默認/配置服務器時指定)、用戶名和密碼以及使用 SSH2。 按 Enter 登錄
  • 如果登錄嘗試成功。 我們使用 gFTP 連接到 SFTP

目前為止就這樣了。 在此階段,您將擁有一個可以工作的 SFTP 服務器。 希望您喜歡本教程,我為您留下了一些配置部分供您自學,請評論您為自己所做的配置,這有助於我們的 Linoxide 社區學習和成長。 謝謝你。