# 中间件自查
## 1.redis 权限最小化
```
添加redis 用户
useradd -M -s /sbin/nologin redis
(解决su redis报错问题)
usermod -s /bin/bash redis
用root用户身份复制redis默认配置文件到redis的根目录/home/redis/:
cp /home/server/redis/conf/redis.conf /home/redis/
修改配置文件中redis运行使用到的相关文件和目录的路径
把 pidfile /var/run/redis_6379.pid
修改成为 pidfile /home/redis/run/redis_6379.pid
修改文件目录
将`dir ./`修改为`dir /home/redis/redis`
在redis用户根目录下创建这个文件和目录
mkdir /home/redis/run
在run 目录下创建文件
touch redis_6379.pid
mkdir /home/redis/redis
redis-server全局命令
ln -s /home/server/redis/bin/redis-server /usr/bin/redis-server
将redis用户家目录下的所有文件所属者与所属组修改为redis:redis
chown -R redis:redis /home/redis
变更redis目录权限为redis用户所有
chown -R redis:redis /home/server/redis/
切换到redis用户
su redis
启动redis【注意:一定要在redis的根目录下(/home/redis)执行启动命令,否则无法启动】
redis-server /home/redis/redis.conf
查看是否运行成功
ps -ef|grep redis
恢复无法登陆状态
usermod -s /sbin/nologin redis
```
## 2.kafka 未开启kafka身份认证机制
```
1.server.properties添加配置:
listeners=SASL_PLAINTEXT://172.0.0.1:9092
advertised.listeners=SASL_PLAINTEXT://172.0.0.1:9092
security.inter.broker.protocol=SASL_PLAINTEXT
sasl.enabled.mechanisms=PLAIN
sasl.mechanism.inter.broker.protocol=PLAIN
authorizer.class.name=kafka.security.auth.SimpleAclAuthorizer
super.users=User:root
sasl.kerberos.service.name=kafka
2.新建kafka_server_jaas.conf配置文件(congfig目录下),设置用户:
KafkaServer {
org.apache.kafka.common.security.plain.PlainLoginModule required
username=""root""
password=""root@root""
user_root=""root@root""
user_alice=""alice@root"";
};
3.修改kafka-server-start.sh文件,添加启动参数:
export KAFKA_OPTS='-Djava.security.auth.login.config=/home/server/kafka_2.12-2.3.0/config/kafka_server_jaas.conf'
4.重启kafka
./kafka-server-start.sh -daemon ../config/server.properties
5.代码中添加配置
spring.kafka.producer.properties.sasl.mechanism= PLAIN
spring.kafka.producer.properties.security.protocol=SASL_PLAINTEXT
spring.kafka.producer.properties.sasl.jaas.config=org.apache.kafka.common.security.plain.PlainLoginModule required username="root" password="root@root123";
spring.kafka.consumer.properties.sasl.mechanism= PLAIN
spring.kafka.consumer.properties.security.protocol=SASL_PLAINTEXT
spring.kafka.consumer.properties.sasl.jaas.config=org.apache.kafka.common.security.plain.PlainLoginModule required username="root" password="root@root123";
```
## 3.kafka ip地址限定
```
server.properties 添加
host.name=127.0.0.1
```
## 4.kafka 修改端口
```
添加
port:19092
修改
listeners=SASL_PLAINTEXT://172.0.0.1:19092
如果有就改,没有就忽略
advertised.listeners=SASL_PLAINTEXT://172.0.0.1:19092
```
## 5.kafka 弱口令
```
2.新建kafka_server_jaas.conf配置文件(congfig目录下),设置用户,指定密码为数字,字母和特殊字符
KafkaServer {
org.apache.kafka.common.security.plain.PlainLoginModule required
username=""root""
password=""root@root123""
user_root=""root@root""
user_alice=""alice@root"";
};
```
## 6.kafka 权限最小化
```
root 用户操作
useradd kafka
chown -R kafka:kafka /home/kafka
chown -R kafka:kafka /home/server/kafka_2.12-2.3.0/
./kafka-server-stop.sh
切换用户 启动kafka
su kafka
./kafka-server-start.sh -daemon ../config/server.properties
```
## 7.zookeeper 未严格设置节点权限
```
cd /zokeeper/bin
./zkCli.sh
create /home root
addauth digest root:root123
setAcl /home auth:root:cdwra
getAcl /home
get /home
```
## 8.zookpeeper 未设置默认监听所有地址
```
修改或添加conf/zoo.cfg配置文件:
clientPortAddress=172.0.0.1
```
## 9.zookeeper 采用dataDir 记录日志
```
修改或添加conf/zoo.cfg配置文件:
dataLogDir /var/log/zookeeper
```
## 10.zookeeper 使用默认端口2181
```
修改或添加conf/zoo.cfg配置文件:
clientPort=2182
```
## 11.注意事项
```
1.修改完zookeeper 需要对kafka 的server.properties进行修改
zookeeper.connect=172.0.0.1:2182
2.zookeeper启动报错
删掉 /zookeeper/data/version-2
3.启动和停止zookeeper
./zkServer.sh stop
./zkServer.sh start
4.连接zookeeper(修改端口后)
./zkCli.sh -server 172.31.186.116:2182
```