To login without password, I use ssh-copy-id I ~/.ssh/key_name root@server_ip.

If I don't copy my public key, then each time I use ssh root@server_ip I would be asked for my password.

Now I want to create an automated script for checking the installation of Ubuntu instances of our colleagues.

I want to know whether they have configured their Ubuntu correctly or not.

One item I should check is to see if they have copied their public keys to our server, so that they can connect without password.

How can I know it? Is there a way for me to know it?

  • @user68186, we're using Ubuntu 22.04 for clients, and Debian bullseye 11 for our servers. Nov 27, 2022 at 14:01
  • I tried the answer by @pLumo. It works for me.
    – user68186
    Nov 28, 2022 at 23:32

2 Answers 2


Just try ...

if ssh -o IdentitiesOnly=yes -o BatchMode=yes -i ~/.ssh/key_name -q user@host exit; then
    echo "Connection succesful"
    echo "Connection not succesful"
  • -o IdentitiesOnly=yes means that, only the give key is checked
  • -o BatchMode=yes tells ssh not to fall back to a password prompt.
  • -q make ssh not output anything.
  • exit will let the connection quit immediately
  • Thank you for this script. However, when I run it, it freezes in the first line. Nov 24, 2022 at 9:09
  • @user68186, yep, I did not copy/paste blindly. I read and understood the code and replaced the parameters. Nov 27, 2022 at 14:01

This answer is not a script. I hope this answer can gave you more ideas about your issue.

There are 3 ways to check this issue.

  1. There will be a connection established for each user if they can log in. You can use netstat -anpl|grep 22 to check the connections.

  2. You can also use command last to check the users whoever logged in in the past. If there is entry for that user, that means they can log in with the key without problem.

  3. There is a file named authorized_keys under .ssh directory. It is under each user's home directory on server side.

    The SSH authorized_keys file is a file that contains a list of public keys that are authorized to log in to the server.

    This file is used to prevent unauthorized users from connecting to the SSH server. SSH daemon on the server side checks whether the SSH key is correct or not by calculating the SSH key fingerprint.

    If the SSH key is correct, it allows the user to log in without asking username or password. So for your question, you can check this file to see if they copied their public key to this server.

  • Thank you. But my question is about client machines, not the server itself. I want to know whether a client machine has already copied its id to the server or not, without considering server. Nov 27, 2022 at 13:11
  • It is hard to check that on client side. You can add an echo "ssh-copy-id ~/.ssh/key_name root@server_ip" in your script to ask the users to copy public key manually to the remote server If the passwordless login doesn't work for that user.
    – aiziyou
    Nov 27, 2022 at 14:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.