简体   繁体   中英

redis-cli how to AUTH using password and issue a command?

When no password is set, we can issue for instance;

>> redis-cli keys * 

or

>> redis-cli config set requirepass "aaaaaa"

However, after we have have issued the latter, the first no longer works and results in:

>> redis-cli keys *
  (error) NOAUTH Authentication required.  

We need to authenticate. Sure.

>> redis-cli AUTH aaaaaa
   OK
>> redis-cli keys *
   (error) NOAUTH Authentication required. 

How do we authenticate and then able to execute a command?

Is this not possible? Heredocs only?

I've tried:

>> redis-cli AUTH aaaaaa && config set requirepass "aaaaaa"

But did not work. Also semicolon after aaaaaa. Not work.

How?

You can pass the -a argument for authenticating the redis-cli command like this:

redis-cli -h 127.0.0.1 -p 6379 -a mypassword keys *

The AUTH commands only last for the duration of the tcp connection. Each new invocation of redis-cli creates a new connection, thus you have to authenticate at each invocation.

It is possible to execute several redis commands on one invocation of redis-cli : they must be separated by \n

Thus this would work:

echo -e 'AUTH aaaaaa\nkeys *' | redis-cli  

Note: The other answer also provides a way to pass arguments separated by \n to redis-cli

This seems to work:

redis-cli <<- 'EOF'
        AUTH aaaaaa
        config set requirepass aaaaaa
EOF

The answer from @aureliar is great. I prefer to use environment variables and ( since Redis 6 ) the default user:

▶ echo -e "AUTH default ${REDIS_PASSWORD}\nkeys *" | redis-cli
OK
1) "foo:superkey:13-June-2022"

Using a user instead of a global password is much better as you can restrict the commands and keys of a user. More details here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM