check allowed ssh keys on deb
- User
check allowed ssh keys on deb
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
check allowed ssh keys on deb
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Of course. Here is a comprehensive guide on how to check for allowed SSH keys on a Debian system, from the most common method to more advanced configurations.
For a specific user, the allowed public keys are almost always stored in a file in their home directory.
To check the keys for the current user:
cat ~/.ssh/authorized_keys
To check the keys for another user (e.g., jane):
# You will need root/sudo privileges for this
sudo cat /home/jane/.ssh/authorized_keys
To check the keys for the root user:
sudo cat /root/.ssh/authorized_keys
SSH key authentication works by matching a private key (held by the client) with a public key (stored on the server). The server checks a specific file to see which public keys are "authorized" to log in as a particular user.
authorized_keys FileThis is the standard and most common location. Each user has their own list of keys that can be used to log in to their account.
Navigate to the SSH directory:
The keys are stored in a hidden directory named .ssh inside the user's home directory (~).
# For your own user
cd ~/.ssh
# For another user, e.g., 'webadmin'
cd /home/webadmin/.ssh
If the directory doesn't exist, that user likely cannot log in with an SSH key.
Display the contents of the authorized_keys file:
Use cat or less to view the file.
cat authorized_keys
What you'll see:
Each line in this file represents one authorized public key. The format is typically:
[options] key-type base64-encoded-key [comment]
[options] (Optional): Restrictions applied to this key, like from="192.168.1.100" to limit the source IP.key-type: The algorithm, e.g., ssh-ed25519 or ssh-rsa.base64-encoded-key: The long string of random-looking characters is the key itself.[comment] (Optional): Usually an identifier like user@hostname to help you remember what the key is for.Example authorized_keys file:
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAICpGz3B3f... user@desktop-dev
from="10.0.0.5" ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQ... jenkins-server
sshd_config)Sometimes, the location of the authorized keys file can be changed in the main SSH server configuration. It's a good practice to verify this, especially if you can't find the file in the default location.
Open the SSH daemon configuration file:
This file controls the behavior of the SSH service on your server. You'll need sudo to read it.
sudo nano /etc/ssh/sshd_config
# Or use less to view it without editing
sudo less /etc/ssh/sshd_config
Look for the AuthorizedKeysFile directive:
You can search within your editor or use grep.
sudo grep -i "AuthorizedKeysFile" /etc/ssh/sshd_config
The default on Debian and most Linux systems is:
# The default is the following
AuthorizedKeysFile .ssh/authorized_keys .ssh/authorized_keys2
The # means it's a commented-out line, but it shows the default value the server is using. If this line is present without a # and has a different path, then your server is looking for keys in a non-standard location.
Check other important settings: While you're in this file, ensure that public key authentication is actually enabled.
sudo grep -i "PubkeyAuthentication" /etc/ssh/sshd_config
This should be set to yes.
PubkeyAuthentication yes
authorized_keys Files on the SystemTo get a complete picture of all users who have authorized keys set up, you can search the entire system.
This command will search the /home and /root directories for any file named authorized_keys and print its path.
sudo find /home /root -name "authorized_keys"
To view the contents of all the files it finds, you can combine it with xargs:
sudo find /home /root -name "authorized_keys" -print0 | sudo xargs -0 -I{} sh -c 'echo "--- {} ---"; cat {}'
This command is safer as it handles spaces in filenames and prints a header before the content of each file.
Checking the keys is one thing; ensuring they are secure is another.
Check File Permissions: Incorrect permissions can prevent key authentication from working or, worse, create a security hole.
.ssh directory should be 700 (drwx------). Only the owner can read, write, and execute.authorized_keys file should be 600 (-rw-------). Only the owner can read and write.Check permissions for the current user:
ls -ld ~/.ssh ~/.ssh/authorized_keys
Expected output:
drwx------ 2 username username 4096 Oct 26 10:00 /home/username/.ssh
-rw------- 1 username username 576 Oct 26 10:01 /home/username/.ssh/authorized_keys
To fix incorrect permissions:
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys
Check File Ownership: The .ssh directory and the authorized_keys file must be owned by the user.
ls -ld from the previous step also shows ownership (username username).
To fix incorrect ownership (e.g., for user jane):
sudo chown -R jane:jane /home/jane/.ssh
Review and Remove Old Keys: Regularly review the authorized_keys files for all users. If you see keys for former employees, old systems, or services you no longer use, delete those lines from the file.