CentOS 7 ssh和sftp服务分离

    由于安全需要,客户这边想把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文件中需要做下面的修改:

  

1

2

[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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

[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

  在文件末尾增加以下内容:

1

2

3

4

5

6

7

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 9122 sftpuser@127.0.0.1

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值