在 CentOS 上修改 Redis 端口,一般按下面步骤操作即可(适用于 Redis 通过 yum 或源码安装的常见情况)。
常见路径:
/etc/redis.conf
或(如果是源码安装):
/usr/local/redis/redis.conf
如果不确定,可以查找:
find / -name redis.conf 2>/dev/null
vim /etc/redis.conf
找到这一行(默认是 6379):
port 6379
修改为你想要的端口,例如:
port 6380
✅ 保存并退出(:wq)
有些安装方式会在 systemd 里指定端口,需要确认:
vim /usr/lib/systemd/system/redis.service
或:
vim /etc/systemd/system/redis.service
查看是否有类似:
ExecStart=/usr/bin/redis-server /etc/redis.conf --port 6379
如果有 --port,删除它,让 Redis 只使用 redis.conf。
systemctl daemon-reload
firewall-cmd --add-port=6380/tcp --permanent
firewall-cmd --reload
查看是否生效:
firewall-cmd --list-ports
iptables -I INPUT -p tcp --dport 6380 -j ACCEPT
service iptables save
service iptables restart
systemctl restart redis
或:
service redis restart
查看状态:
systemctl status redis
ss -lntp | grep 6380
或:
netstat -lntp | grep redis
redis-cli -p 6380
port 配置bind 127.0.0.1修改端口后,建议同时:
bind 0.0.0.0
requirepass your_password
或只允许内网访问。
如果你愿意,可以把 Redis 版本、CentOS 版本、安装方式 发给我,我可以给你更精确的配置方案。