處理步驟:
1.關(guān)閉防火墻和selinux
[root@localhost ~]# systemctl status firewalld ● firewalld.service - firewalld - dynamic firewall daemon ? Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled) ? Active: inactive (dead) ? ? Docs: man:firewalld(1) [root@localhost ~]# systemctl stop firewalld [root@localhost ~]# systemctl disable firewalld [root@localhost ~]# vi /etc/sysconfig/selinux # 把文件中的SELINUX=enforcing 改為SELINUX=disabled [root@localhost ~]# setenforce 0 setenforce: SELinux is disabled
2.查看ssh是否已安裝并啟動(dòng)
[root@localhost ~]# ssh -V OpenSSH_8.2p1, OpenSSL 1.1.1f 31 Mar 2020 [root@localhost ~]# systemctl status sshd ● sshd.service - OpenSSH server daemon ? Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; vendor preset: enabled) ? Active: active (running) since Thu 2022-12-01 11:50:01 CST; 4h 26min ago ? ? Docs: man:sshd(8) ? ? ? ? ? man:sshd_config(5) Main PID: 1393 (sshd) ? Tasks: 1 ? Memory: 1.8M ? CGroup: /system.slice/sshd.service ? ? ? ? ? └─1393 sshd: /usr/sbin/sshd -D [listener] 0 of 10-100 startups
3.新建用戶組、用戶和sftp目錄
1.新建目錄
[root@localhost ~]# mkdir -p /data/sftp/sftpuser [root@localhost ~]# chown root:root /data/sftp/sftpuser [root@localhost ~]# chmod 755 /data/sftp/sftpuser
2.新建用戶組
[root@localhost ~]# groupadd sftp
3.新建用戶
[root@localhost ~]# useradd -g sftp -d /data/sftp/sftpuser -M -s /sbin/nologin sftpuser -g指定用戶組、-d指定家目錄、-s nologin 不能登錄系統(tǒng)、-M不創(chuàng)建家目錄 [root@localhost ~]# echo 'xxxxx'|passwd --stdin sftpuser
4.新建sftp可寫目錄
[root@localhost ~]# mkdir -p /data/sftp/sftpuser/upload [root@localhost ~]# chown -R sftpuser:sftp /data/sftp/sftpuser/upload [root@localhost ~]# chmod -R 777 /data/sftp/sftpuser/upload
4.配置SSH和SFTP 服務(wù)器
1.備份sshd_config配置文件并查看文件中的有效配置
[root@localhost ~]# cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak [root@localhost ~]# egrep -v '^$|^#' /etc/ssh/sshd_config HostKey /etc/ssh/ssh_host_rsa_key HostKey /etc/ssh/ssh_host_ecdsa_key HostKey /etc/ssh/ssh_host_ed25519_key SyslogFacility AUTHPRIV PermitRootLogin yes AuthorizedKeysFile .ssh/authorized_keys PasswordAuthentication yes ChallengeResponseAuthentication no GSSAPIAuthentication yes GSSAPICleanupCredentials no UsePAM yes X11Forwarding yes PrintMotd no AcceptEnv LANG LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY LC_MESSAGES AcceptEnv LC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE LC_MEASUREMENT AcceptEnv LC_IDENTIFICATION LC_ALL LANGUAGE AcceptEnv XMODIFIERS Subsystem sftp /usr/libexec/openssh/sftp-server
2.修改sshd_config配置文件文章來源:http://www.zghlxwxcb.cn/news/detail-443593.html
注釋掉: Subsystem sftp /usr/libexec/openssh/sftp-server 新增: Subsystem sftp internal-sftp Match Group sftp ChrootDirectory /data/sftp/%u ForceCommand internal-sftp # 下面兩項(xiàng)是與安全有關(guān) AllowTcpForwarding no X11Forwarding no #設(shè)置不允許SSH的X轉(zhuǎn)發(fā) ? [root@localhost upload]# egrep -v '^$|^#' /etc/ssh/sshd_config HostKey /etc/ssh/ssh_host_rsa_key HostKey /etc/ssh/ssh_host_ecdsa_key HostKey /etc/ssh/ssh_host_ed25519_key SyslogFacility AUTHPRIV PermitRootLogin yes AuthorizedKeysFile .ssh/authorized_keys RSAAuthentication yes PubkeyAuthentication yes PasswordAuthentication yes ChallengeResponseAuthentication no GSSAPIAuthentication yes GSSAPICleanupCredentials no UsePAM yes X11Forwarding no PrintMotd no AcceptEnv LANG LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY LC_MESSAGES AcceptEnv LC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE LC_MEASUREMENT AcceptEnv LC_IDENTIFICATION LC_ALL LANGUAGE AcceptEnv XMODIFIERS Subsystem sftp internal-sftp Match Group sftp ChrootDirectory /data/sftp/%u ForceCommand internal-sftp AllowTcpForwarding no
5.重啟ssh服務(wù)
[root@localhost ~]# systemctl restart sshd
6.新建測試文件并授權(quán)
[root@localhost upload]# echo 111 > 1.txt [root@localhost upload]# ll 總用量 4.0K -rw------- 1 root root 4 12月 1 17:43 1.txt 新建的文件權(quán)限默認(rèn)為600,需要手動(dòng)授權(quán)777 [root@localhost upload]# chmod 777 1.txt [root@localhost upload]# ll 總用量 4.0K -rwxrwxrwx 1 root root 4 12月 1 17:43 1.txt
7.從另一臺(tái)機(jī)器測試sftp可用性
[root@centos-01 data]# sftp sftpuser@192.168.137.16 sftpuser@192.168.137.16's password: Connected to 192.168.137.16. sftp> ls -l drwxrwxrwx ? 2 1002 ? ? 1002 ? ? ? ? 4096 Dec 1 09:38 upload sftp> cd upload/ sftp> ls -l -rwxrwxrwx ? 1 root ? ? root ? ? ? ? ? 4 Dec 1 09:36 1.txt sftp> get 1.txt Fetching /upload/1.txt to 1.txt /upload/1.txt ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 100% ? 4 ? ? 7.4KB/s ? 00:00 ? ? sftp> exit [root@centos-01 ~]# ll -rwxr-xr-x 1 root root ? ? 4 Dec 1 17:44 1.txt
OK!文章來源地址http://www.zghlxwxcb.cn/news/detail-443593.html
到了這里,關(guān)于Linux安裝sftp服務(wù)的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!