歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux服務器 >> 用Linux架設FTP服務器(2)

用Linux架設FTP服務器(2)

日期:2017/3/2 16:56:33   编辑:Linux服務器
為FTP站點的用戶建立沒有shell的帳號
    首先,創建一個新的用戶,這個用戶被允許連接到ftp服務器上。因為要有“chroot”的環境,這個帳號不同於正常的用戶帳號,不能受訪問限制。“chroot”使用戶產生這樣的感覺好像自己已經在文件系統的最頂層了。
    第一步
    用下面的命令在“/etc/passwd”文件中創建用戶。對於每個允許訪問ftp服務器的新用戶都要重復這個步驟。
    [root@deep]# mkdir /home/ftp
    [root@deep]# useradd -d /home/ftp/ftpadmin/ -s /dev/null ftpadmin > /dev/null 2>&1
    [root@deep]# passwd ftpadmin
    Changing password for user ftpadmin
    New UNIX password:
    Retype new UNIX password:
    passwd: all authentication tokens updated successfully
    第二步
    編輯“/etc/shells”文件並加入一個空shell,如:null。這個假的shell可以限制用戶對ftp服務器的訪問。
    [root@deep]# vi /etc/shells
  
    /bin/bash
    /bin/sh
    /bin/ash
    /bin/bsh
    /bin/tcsh
    /bin/csh
    /dev/null ? This is our added no existent shell
   第三步
    現在編輯“/etc/passwd”文件,手工加上“/./”把“/home/ftp”目錄和“/ftpadmin”目錄分開,用戶“ftpadmin”會自動轉到(chdir)“/ftpadming”目錄下。在“passwd”文件中每添加一個ftp用戶都要重復這個步驟。
    編輯“passwd”文件(vi /etc/passwd),把下面這一行改為:
    ftpadmin:x:502:502::/home/ftp/ftpadmin/:/dev/null
    改為:
    ftpadmin:x:502:502::/home/ftp/./ftpadmin/:/dev/null
    帳號為“ftpadmin”,這這個帳號的家目錄有一些奇怪。第一部分“/home/ftp/”表示“chroot”時作為根目錄的目錄。被點號分開的“/ftpadmin”表示當登錄ftp服務器的時候會自動轉到這個目錄。“/dev/null”這個空shell不允許“ftpadmin”像正常用戶那樣登錄。經過這些改變,“ftpadmin”用戶用的不是真正的shell而是偽shell,這樣訪問ftp服務器就受到限制。 創建一個“chroot”用戶環境
  先要創建一個簡單的根文件系統(root file system),包含有足夠的文件,如果二進制程序、口令文件,等等。當用戶登錄的時候,Unix就可以改變根文件系統(chroot)。注意一下,如果編譯的時候象上面那樣加上“--enable-ls”參數,“/home/ftp/bin”和“/home/ftp/lib”兩個目錄就可以不要了,因為WU-FTP會用自己帶的“ls”。不過我們還是介紹一下舊的方法,也就是把“/bin/ls”拷貝到“/home/ftp/bin”(chroot之後就是“/bin”)目錄下,然後把相關的運行庫拷貝到“/home/ftp/lib”目錄下。
    第一步
    創建改變根文件系統(chrooted)環境所需要的所有的目錄:
    [root@deep]# mkdir /home/ftp/dev
    [root@deep]# mkdir /home/ftp/etc
    [root@deep]# mkdir /home/ftp/bin (require only if you are not using the “--enable-ls” option)
    [root@deep]# mkdir /home/ftp/lib (require only if you are not using the “--enable-ls” option)
   第二步
    把新目錄的權限設成0511:
    [root@deep]# chmod 0511 /home/ftp/dev
    [root@deep]# chmod 0511 /home/ftp/etc
    [root@deep]# chmod 0511 /home/ftp/bin (require only if you are not using the “--enable-ls” option)
    [root@deep]# chmod 0511 /home/ftp/lib (require only if you are not using the “--enable-ls” option)
    上面這些“chmod”命令把chrooted之後的“dev”、“etc”、“bin”和“lib”目錄設置成超級用戶“root”可讀、可執行,用戶組和所有用戶可執行。
    第三步
    把“/bin/ls”文件拷貝到“/home/ftp/bin”目錄下,並把“ls”的權限改為0111(不運行用戶改變這個文件)。
    [root@deep]# cp /bin/ls /home/ftp/bin (require only if you are not using the “--enable-ls” option)
    [root@deep]# chmod 0111 /bin/ls /home/ftp/bin/ls (require only if you are not using the “--enable-ls” option)
    第四步
    找到“ls”程序所需的共享庫:
    [root@deep]# ldd /bin/ls (require only if you are not using the “--enable-ls” option)
    libc.so.6 => /lib/libc.so.6 (0x00125000)
    /lib/ld-linux.so.2 => /lib/ld-linux.so.2 (0x00110000)
    把共享庫拷貝到“/home/ftp/lib”目錄下:
    [root@deep]# cp /lib/libc.so.6 /home/ftp/lib/ (require only if you are not using the “--enable-ls” option)
    [root@deep]# cp /lib/ld-linux.so.2 /home/ftp/lib/ (require only if you are not using the “--enable-ls” option)
  注意:如果想用Linux的“ls”程序而不是用WU-ftpd自帶的“ls”(編譯時加上“--enable-ls”參數),才需要第三和第四步。
  
    第五步
    創建“/home/ftp/dev/null”文件:
    [root@deep]# mknod /home/ftp/dev/null c 1 3
    [root@deep]# chmod 666 /home/ftp/dev/null
  
    第六步
    把“group”和“passwd”文件拷貝到“/home/ftp/etc”目錄下,然後再改變這兩個文件。
    [root@deep]# cp /etc/passwd /home/ftp/etc/
    [root@deep]# cp /etc/group /home/ftp/etc/
    編輯“passwd”文件(vi /home/ftp/etc/passwd)把除了“root”和允許使用ftp的用戶之外的所有其它項刪掉。這對於改變根文件系統的環境很重要,改變之後的“passwd”文件會是象下面這樣的:
    root:x:0:0:root:/:/dev/null
    ftpadmin:x:502:502::/ftpadmin/:/dev/null
    編輯“group”文件(vi /home/ftp/etc/group),把除了“root”和允許使用ftp的用戶之外的所有其它項刪掉。改變之後的“group”文件會是象下面這樣的:
    root:x:0:root
    ftpadmin:x:502:
    配置
    可以到這去下載“floppy.tgz”文件:http://pages.infinit.net/lotus1/doc/opti/floppy.tgz。把“floppy.tgz”文件解開之後,可以在相應的目錄下發現我們在這本書中介紹的所有軟件的配置文件。這樣就沒有必要手工重新生成這些文件,或者用拷貝粘貼的方法把它們粘貼到配置文件中去。不管是打算自己動手生成配置文件還是拷貝現成的,你都要學會自己修改配置文件並且把配置文件拷貝到正確的目錄下。下面將具體說明。
    為了運行FTP服務器,必須創建或者把下面的文件拷貝到相應的目錄下:
    把“ftpaccess”文件拷貝到“/etc”目錄下
    把“ftpusers”文件拷貝到“/etc”目錄下
    把“ftphosts”文件拷貝到“/etc”目錄下
    把“ftpgroups”文件拷貝到“/etc”目錄下
    把“ftpconversion”文件拷貝到“/etc”目錄下
    把“ftp”文件拷貝到“/etc/pam.d”目錄下
    把“ftpd”文件拷貝到“/etc/logrotate.d”目錄下
  
    可以把“floppy.tgz”解壓之後,找到上面列出來的文件,並拷貝到相應的目錄下,或者用拷貝粘貼的方法從本書中直接粘貼出。

Copyright © Linux教程網 All Rights Reserved