由於安全需要,客戶這邊想把sftp使用的端口與ssh使用的端口分開。
我們知道sftp沒有自己的服務器守護進程,它需要依賴sshd守護進程來完成客戶端的連接操作。sftp服務作為ssh的一個子服務,是通過/etc/ssh/sshd_config配置文件中的Subsystem實現的,如果沒有配置Subsystem參數,則系統是不能進行sftp訪問的。所以,要分離ssh和sftp服務的話,基本的思路是創建兩個sshd進程,分別監聽在不同的端口,一個作為ssh服務的deamon,另一個作為sftp服務的deamon。
分離步驟如下:
1.增加sftp的deamon
為了方便,我們將sftp服務的后台程序命名為/usr/sbin/sftpd。/usr/sbin/sftpd做一個連接指向/usr/sbin/sshd。
ln -sf /usr/sbin/sshd /usr/sbin/sftpd
或者 ln -sf /usr/local/openssh/sbin/sshd /usr/sbin/sftpd
2.增加sftp的service
實現sftp服務時,將/usr/lib/systemd/system/sshd.service 復制到 /etc/systemd/system/sftpd.service,然后修改sftpd.service文件內容。
cp -a /usr/lib/systemd/system/sshd.service /etc/systemd/system/sftpd.service
[root@tongweb RMANTEST]# vi /etc/systemd/system/sftpd.service [Unit] Description=Sftpd server daemon Documentation=man:sshd(8) man:sshd_config(5) After=network.target sshd-keygen.service Wants=sshd-keygen.service [Service] Type=notify EnvironmentFile=/etc/sysconfig/sftpd ExecStart=/usr/sbin/sftpd -f /etc/ssh/sftpd_config ExecReload=/bin/kill -HUP $MAINPID KillMode=process Restart=on-failure RestartSec=42s [Install] WantedBy=multi-user.target
紅字部分為需要修改的部分。
systemctl enable sftpd.service
如果,openssh采用二進制包升級過,比如從OS自帶的7.4p1版本升級到8.6p1版本,sftpd.service文件中需要做下面的修改:
[Service]
Type=simple
3.其他配套文件
通過rpm -ql openssh查看ssh包含了哪些文件,我們給sftp服務也准備相應的文件。
3.1 復制/etc/pam.d/目錄下的sshd文件,放到同目錄,命名為:sftpd
cp -a /etc/pam.d/sshd /etc/pam.d/sftpd
[root@tongweb data]# cat /etc/pam.d/sftpd #%PAM-1.0 auth required pam_sepermit.so auth substack password-auth auth include postlogin # Used with polkit to reauthorize users in remote sessions -auth optional pam_reauthorize.so prepare account required pam_nologin.so account include password-auth password include password-auth # pam_selinux.so close should be the first session rule session required pam_selinux.so close session required pam_loginuid.so # pam_selinux.so open should only be followed by sessions to be executed in the user context session required pam_selinux.so open env_params session required pam_namespace.so session optional pam_keyinit.so force revoke session include password-auth session include postlogin # Used with polkit to reauthorize users in remote sessions -session optional pam_reauthorize.so prepare
3.2 復制/etc/ssh/目錄下的sshd_config文件,放到同目錄,命名為:sftpd_config
cp -a /etc/ssh/sshd_config /etc/ssh/sftpd_config
3.3 復制/etc/sysconfig/目錄下的sshd文件,放到同目錄,命名為:sftpd
cp -a /etc/sysconfig/sshd /etc/sysconfig/sftpd
[root@tongweb data]# cat /etc/sysconfig/sftpd # Configuration file for the sshd service. # The server keys are automatically generated if they are missing. # To change the automatic creation uncomment and change the appropriate # line. Accepted key types are: DSA RSA ECDSA ED25519. # The default is "RSA ECDSA ED25519" # AUTOCREATE_SERVER_KEYS="" # AUTOCREATE_SERVER_KEYS="RSA ECDSA ED25519" # Do not change this option unless you have hardware random # generator and you REALLY know what you are doing SSH_USE_STRONG_RNG=0 # SSH_USE_STRONG_RNG=1
3.4 創建sftp服務運行pid文件
touch /var/run/sftpd.pid
4.修改配置文件
修改參數,適配sftp服務,同時在ssh服務中停掉sftp服務。
4.1 修改sftpd配置文件
vi /etc/ssh/sftpd_config
修改Port 22為Port 9122
修改#PidFile /var/run/sshd.pid,為PidFile /var/run/sftpd.pid
注釋掉Subsystem sftp /usr/local/openssh/libexec/sftp-server
在文件末尾增加以下內容:
Subsystem sftp internal-sftp Match User sftpuser ##限制用戶 ChrootDirectory /sftpdir ##限制sftp目錄 X11Forwarding no ##與sftp無關,所以關閉 AllowTcpForwarding no ##與sftp無關,所以關閉 PermitTTY no ##不允許登入TTY ForceCommand internal-sftp
備注:
Sftp目錄權限設置要遵循2點:
ChrootDirectory設置的目錄權限及其所有的上級文件夾權限,屬主必須是root;
ChrootDirectory設置的目錄權限及其所有的上級文件夾權限,只有屬主能擁有寫權限,權限最大設置只能是755。
另外注意:selinux的狀態。
setenforce 0
4.2 修改sshd配置文件
vi /etc/ssh/sshd_config
取消注釋,PidFile /var/run/sshd.pid
增加注釋,#Subsystem,即停掉sftp子服務
分別重啟兩個服務:
systemctl restart sshd
systemctl restart sftpd
可以看到9122端口已經起來,有兩個不同進程號的sshd守護進程。
5.OS增加sftp用戶
useradd sftpuser
passwd sftpuser
usermod -s /bin/false sftpuser ##使用/bin/false,限制該用戶通過shell登入OS。
6.建sftp目錄並賦權
mkdir /sftpuser/upload
chown root.sftpuser /sftpuser
chown sftpuser.sftpuser /sftpuser/upload
chmod -R 755 /sftpuser
/sftpuser/upload 為最終文件上傳下載目錄。注意,因為使用ChrootDirectory,所以sftp的時候不能使用絕對路徑/sftpuser/upload/xxx.
7.測試連接
sftp -P 8022 sftpuser@127.0.0.1