打开redis端口进行远程连接

时间:2013-09-30 09:44:41

标签: ubuntu redis

我可以在服务器上ping pong redis

# redis-cli ping
PONG

但远程问题:

$ src/redis-cli -h REMOTE.IP ping
Could not connect to Redis at REMOTE.IP:6379: Connection refused

在配置中我得到了标准端口:

# Accept connections on the specified port, default is 6379.
# If port 0 is specified Redis will not listen on a TCP socket.
port 6379

那么也许我应该在远程ubuntu机器上打开端口6379?怎么做?

10 个答案:

答案 0 :(得分:168)

您是否设置了bind选项以允许在redis服务器上进行远程访问?

之前(档案/etc/redis/redis.conf

bind 127.0.0.1

bind 0.0.0.0

并运行sudo service redis-server restart以重新启动服务器。如果这不是问题,您可能需要检查可能阻止访问的任何防火墙。

重要提示:如果您不使用防火墙(iptables,ufw ..)来控制连接到正在使用的端口的用户,则任何人都可以连接到此Redis实例。不使用Redis' AUTH表示任何人都可以访问/更改/删除您的数据。安全!

答案 1 :(得分:16)

对我来说,我需要做以下事情:

1-评论bind 127.0.0.1

2-将protected-mode更改为no

3-使用iptableshttps://www.digitalocean.com/community/tutorials/how-to-implement-a-basic-firewall-template-with-iptables-on-ubuntu-14-04

保护我的服务器

答案 2 :(得分:7)

快速说明,在不进一步保护Redis服务器的情况下执行此操作并不是一个好主意,因为它可能会让您受到攻击。一定要实现AUTH或以其他方式保护它。有关详细信息,请参阅http://redis.io/topics/security

答案 3 :(得分:3)

1-注释掉绑定127.0.0.1

2- set requirepass yourpassword

然后检查防火墙是否阻止了您的端口

iptables -L -n

service iptables stop

答案 4 :(得分:3)

步骤1:转到位置:/etc/redis.conf

步骤2:命令输出bind 127.0.0.1

第3步:重启Redis: -        sudo systemctl start redis.service

第4步:禁用Firewalld         systemctl禁用firewalld

第5步:停止Firewalld         systemctl stop firewalld

然后试试。

redis-cli -h 192.168.0.2(ip)-a redis(用户名)

答案 5 :(得分:1)

  1. 打开$ REDIS_HOME / redis.conf并取消注释Organization,然后在指定的行中写下密码。

  2. 使用redis-cli登录到Redis,并使用requirepass -YOUR-PASSWORD-HERE-命令在数据库中验证密码。

  3. 通过将$ REDIS_HOME / redis.conf中的字符串更改为auth -YOUR-PASSWORD-HERE-来禁用保护模式。

  4. 搜索所有绑定端口值并注释所有值。只需将protected-mode no添加到$ REDIS_HOME / redis.conf文件中即可。

  5. 禁用防火墙或打开redis端口。

  6. 使用bind 0.0.0.0开始Redis。

  7. 通过./redis-server $REDIS_HOME/redis.conf检查配置。

  8. 通过./redis-cli -h -YOUR-IP- -a -YOUR-PASSWORD-HERE-检查配置。

答案 6 :(得分:1)

就我而言,我正在使用redis-stable

Go to redis-stable path 
 cd /home/ubuntu/software/redis-stable

打开redis.conf

vim redis.conf

bind 127.0.0.1更改为bind 0.0.0.0

protected-mode yes更改为protected-mode no

重新启动redis服务器:

/etc/init.d/redis-server stop
 redis-server redis.conf

答案 7 :(得分:1)

绑定和保护模式都是必不可少的步骤。 但是,如果启用 ufw ,则必须在ufw中允许redis端口允许。

  1. 检查{strong> ufw 的状态ufw status是否为Status: active,然后允许redis端口ufw allow 6379
  2. vi /etc/redis/redis.conf
  3. bind 127.0.0.1更改为bind 0.0.0.0
  4. protected-mode yes更改为protected-mode no

答案 8 :(得分:0)

快速注意一点,如果您使用的是AWS ec2实例,那么我认为还有一个额外的步骤也是必需的。我错过了步骤3,花了整整一天的时间才想出向安全组添加入站规则

第1步(如前所述):在redis.conf中,将bind 127.0.0.1更改为bind 0.0.0.0

Step2(如前所述):在您的redis.conf中,将保护模式yes更改为保护模式no

对于Amazon Ec2实例很重要:

Step3:在当前的ec2计算机中,转到安全组。添加入站规则 用于具有6379端口的自定义TCP,然后选择选项“从任何地方使用”。

答案 9 :(得分:0)

另一个可能有用的注释。

Redis可以绑定到多个IP-当您不想将其开放给整个世界(0.0.0.0),而仅使其可以在本地网络中访问时,这非常有用。

  1. sudo nano /etc/redis/redis.conf
  2. 将您的本地网络IP添加到bind设置的末尾:

bind 127.0.0.1 10.0.0.1

  1. 重新启动服务:sudo service redis-server restart

现在,您可以轻松地从同一网络中的其他计算机访问redis,例如 redis-cli -h 10.0.0.1