如何通过 SSH 连接到我的 Samba 文件服务器?

如何通过 SSH 连接到我的 Samba 文件服务器?

所以我在我的 Debian(3.2) 机器上制作了一个专用的 Samba 文件服务器。我在 Windows 和 Unix 上都取得了巨大的成功。我可以在本地网络上通过 SSH 连接到它。

当我尝试通过公共 IP 地址通过 SSH 连接到它时,它说连接被拒绝。

我希望能够远程 ssh 进入它,直接进入 Samba 共享。我该怎么做呢?我听说我可能需要向前移动?我需要更改 smb.conf 文件中的任何内容吗?

这是我的 sshd_config 文件:

# Package generated configuration file
# See the sshd_config(5) manpage for details

# What ports, IPs and protocols we listen for
Port 22
# Use these options to restrict which interfaces/protocols sshd will bind to
#ListenAddress ::
#ListenAddress 0.0.0.0
Protocol 2
# HostKeys for protocol version 2
HostKey /etc/ssh/ssh_host_rsa_key
HostKey /etc/ssh/ssh_host_dsa_key
#Privilege Separation is turned on for security
UsePrivilegeSeparation yes

# Lifetime and size of ephemeral version 1 server key
KeyRegenerationInterval 3600
ServerKeyBits 768

# Logging
SyslogFacility AUTH
LogLevel INFO

# Authentication:
LoginGraceTime 120
PermitRootLogin yes
StrictModes yes

RSAAuthentication yes
PubkeyAuthentication yes
#AuthorizedKeysFile %h/.ssh/authorized_keys

# Don't read the user's ~/.rhosts and ~/.shosts files
IgnoreRhosts yes
# For this to work you will also need host keys in /etc/ssh_known_hosts
RhostsRSAAuthentication no
# similar for protocol version 2
HostbasedAuthentication no
# Uncomment if you don't trust ~/.ssh/known_hosts for RhostsRSAAuthentication
#IgnoreUserKnownHosts yes

# To enable empty passwords, change to yes (NOT RECOMMENDED)
PermitEmptyPasswords no

# Change to yes to enable challenge-response passwords (beware issues with
# some PAM modules and threads)
ChallengeResponseAuthentication no

# Change to no to disable tunnelled clear text passwords
#PasswordAuthentication yes

# Kerberos options
#KerberosAuthentication no
#KerberosGetAFSToken no
#KerberosOrLocalPasswd yes
#KerberosTicketCleanup yes

# GSSAPI options
#GSSAPIAuthentication no
#GSSAPICleanupCredentials yes

X11Forwarding yes
X11DisplayOffset 10
PrintMotd no
PrintLastLog yes
TCPKeepAlive yes
#UseLogin no

#MaxStartups 10:30:60
#Banner /etc/issue.net

# Allow client to pass locale environment variables
AcceptEnv LANG LC_*

Subsystem sftp /usr/lib/openssh/sftp-server

# Set this to 'yes' to enable PAM authentication, account processing,
# and session processing. If this is enabled, PAM authentication will
# be allowed through the ChallengeResponseAuthentication and
# PasswordAuthentication.  Depending on your PAM configuration,
# PAM authentication via ChallengeResponseAuthentication may bypass
# the setting of "PermitRootLogin without-password".
# If you just want the PAM account and session checks to run without
# PAM authentication, then enable this but set PasswordAuthentication
# and ChallengeResponseAuthentication to 'no'.
UsePAM yes

答案1

Scriptonaut,您的问题可能与 Samba 无关,而是与端口转发/NAT 有关。如果您的 SAMBA 服务 Debian 计算机位于 LAN 网络中的路由器后面,则需要将其配置为将对其某些端口的请求传输到运行 SAMBA 的计算机:

在此输入图像描述

首先,我将介绍路由器的传出连接如何工作。当两台机器通过 TCP/IP 进行通信时,每台机器(源机器和目标机器)都使用一对 IP/端口号进行寻址,因此连接由 2 对确定:源 IP/端口号和目标 IP/端口号。

当您在 Mozilla 中打开一个选项卡并在 192.168.1.2 机器上访问 Google 时,它​​会将一些 IP 数据包传输到路由器,其源地址为 IP=192.168.1.2 以及为该浏览器选项卡分配的任意传出 TCP 端口号(例如43694),并要求路由器将该数据包传输到 Google 机器,该机器的 80 端口上具有特定 IP,因为 80 是传入 http 连接的标准端口(您可以在 Linux 上的文件中查看标准 TCP 端口列表/etc/services)。路由器随机分配自己的端口(例如12345),用自己的WAN IP(74.25.14.86)和端口12345替换该数据包中的源IP/端口对,并记住,如果它在端口12345上从Google获得响应,它就会应自动将该响应传输回 192.168.1.2,端口 43694。

现在,当外部机器想要访问您的服务器时会发生什么?

当您尝试从外部机器访问 SAMBA 服务器时,它会向您的 WAN IP=74.25.14.86 的 22 端口发送 IP 数据包(因为 22 是用于侦听 SSH 连接的标准 TCP 端口,您可以看到列表Linux 上文件中的标准 TCP 端口/etc/services)。您的路由器接收该数据包。默认情况下,路由器上的防火墙配置为阻止到任何端口的所有传入连接(如果没有绑定到该端口的传出连接)(因此,当您在之前的情况下访问 Google 时,路由器不会阻止 Google 对端口的响应12345 本身,因为它记住您的 192.168.1.2 发起了与 Google 的连接,并且来自 google 的响应应该到达端口 12345)。但它会阻止从外部世界向其端口 22 发起连接的尝试,因为端口 22 未映射到任何来自 LAN 的连接。

因此,您需要做的就是配置您的路由器,将所有从外部到其端口 22 的连接转移到您的 192.168.1.2 的端口 22 上。这可以在硬件路由器的网络界面中完成,通常您需要的菜单选项称为“端口转发”或“NAT - 网络地址转换”。

相关内容