Generate Ssh Key For Specific User
- Manage user accounts and user attributes.
- For Windows targets, use the win_user module instead.
Parameter | Choices/Defaults | Comments |
---|---|---|
append boolean |
| If yes , add the user to the groups specified in groups .If no , user will only be added to the groups specified in groups , removing them from all other groups. |
authorization added in 2.8 | Does nothing when used with other platforms. Can set multiple authorizations using comma separation. To delete all authorizations, use authorization=' . | |
comment | Optionally sets the description (aka GECOS) of user account. | |
create_home boolean |
| Unless set to no , a home directory will be made for the user when the account is created or if the home directory does not exist.Changed from createhome to create_home in Ansible 2.5. |
expires | An expiry time for the user in epoch, it will be ignored on platforms that do not support this. Currently supported on GNU/Linux, FreeBSD, and DragonFlyBSD. Since Ansible 2.6 you can remove the expiry time specify a negative value. Currently supported on GNU/Linux and FreeBSD. | |
force boolean |
| This only affects state=absent , it forces removal of the user and associated directories on supported platforms.The behavior is the same as userdel --force , check the man page for userdel on your system for details and support.When used with generate_ssh_key=yes this forces an existing key to be overwritten. |
generate_ssh_key boolean |
| Whether to generate a SSH key for the user in question. This will not overwrite an existing SSH key unless used with force=yes . |
group string | Optionally sets the user's primary group (takes a group name). | |
groups list | List of groups user will be added to. When set to an empty string ' , the user is removed from all groups except the primary group.Before Ansible 2.3, the only input format allowed was a comma separated string. | |
hidden added in 2.6 |
| macOS only, optionally hide the user from the login window and system preferences. The default will be yes if the system option is used. |
home path | ||
local added in 2.4 |
| Forces the use of 'local' command alternatives on platforms that implement it. This is useful in environments that use centralized authentification when you want to manipulate the local users (i.e. it uses luseradd instead of useradd ).This will check /etc/passwd for an existing account before invoking commands. If the local account database exists somewhere other than /etc/passwd , this setting will not work properly.This requires that the above commands as well as /etc/passwd must exist on the target host, otherwise it will be a fatal error. |
login_class | Optionally sets the user's login class, a feature of most BSD OSs. | |
move_home boolean |
| If set to yes when used with home: , attempt to move the user's old home directory to the specified directory if it isn't there already and the old home exists. |
name string / required | aliases: user | |
non_unique boolean |
| Optionally when used with the -u option, this option allows to change the user ID to a non-unique value. |
password string | Optionally set the user's password to this crypted value. On macOS systems, this value has to be cleartext. Beware of security issues. To create a disabled account on Linux systems, set this to '!' or '*' .To create a disabled account on OpenBSD, set this to '*************' .See https://docs.ansible.com/ansible/faq.html#how-do-i-generate-encrypted-passwords-for-the-user-module for details on various ways to generate these password values. | |
password_lock boolean |
| Lock the password (usermod -L, pw lock, usermod -C). BUT implementation differs on different platforms, this option does not always mean the user cannot login via other methods. This option does not disable the user, only lock the password. Do not change the password in the same task. Currently supported on Linux, FreeBSD, DragonFlyBSD, NetBSD, OpenBSD. |
profile string | Sets the profile of the user. Can set multiple profiles using comma separation. Currently supported on Illumos/Solaris. | |
remove boolean |
| This only affects state=absent , it attempts to remove directories associated with the user.The behavior is the same as userdel --remove , check the man page for details and support. |
role string | Sets the role of the user. Can set multiple roles using comma separation. Currently supported on Illumos/Solaris. | |
seuser string | Optionally sets the seuser type (user_u) on selinux enabled systems. | |
shell string | On macOS, before Ansible 2.5, the default shell for non-system users was /usr/bin/false . Since Ansible 2.5, the default shell for non-system users on macOS is /bin/bash .On other operating systems, the default shell is determined by the underlying tool being used. See Notes for details. | |
skeleton string | Requires create_home option! | |
ssh_key_bits integer | Default: | Optionally specify number of bits in SSH key to create. |
ssh_key_comment string | Default: | Optionally define the comment for the SSH key. |
ssh_key_file path | If this is a relative filename then it will be relative to the user's home directory. | |
ssh_key_passphrase | Set a passphrase for the SSH key. If no passphrase is provided, the SSH key will default to having no passphrase. | |
ssh_key_type string | Default: | Optionally specify the type of SSH key to generate. Available SSH key types will depend on implementation present on target host. |
state string |
| Whether the account should exist or not, taking action if the state is different from what is stated. |
system boolean |
| When creating an account state=present , setting this to yes makes the user a system account. |
uid | Optionally sets the UID of the user. | |
update_password string |
| always will update passwords if they differ.on_create will only set the password for newly created users. |
- In order to generate a SSH key on Debian, you are going to need the ssh-keygen tool. By default, ssh-keygen is already installed on Debian 10. To create a SSH key pair, use the following command. $ ssh-keygen -t rsa -b 4096 -C ' email protected '.
- If you generate the key for the user you also have to have a secure method of getting the private key and it's pass phrase to the user. Much better the user generate the key and then just email you the public key. – user9517 Oct 22 '11 at 20:02.
- Simply used ssh-copy-id to send your public key to the remote host. In the case of the ubuntu user, you will need a copy of the private key and use ssh-copy-id ubuntu@aws which will copy your default ssh public key to the ubuntu user's /.ssh/authorizedkeys file.
- Jun 26, 2019 To set up SSH key authentication for one of your server’s users, add your public key to a new line inside the user’s authorizedkeys file. This file is stored inside a directory named.ssh/ under the user’s home folder. A user’s authorizedkeys file can store more than one public key, and each public key is listed on its own line.
- Apr 02, 2019 Linux Basics: How To Create and Install SSH Keys on the Shell. On this page. Generating a key pair offers users two lengthy strings of characters corresponding to a public as well as a private key. Users can, thus, place the public key on any server, and subsequently, unlock the same by connecting to it with a client that already possesses.
- Nov 10, 2011 How to Generate A Public/Private SSH Key Linux By Damien – Posted on Nov 10, 2011 Nov 18, 2011 in Linux If you are using SSH frequently to connect to a remote host, one of the way to secure the connection is to use a public/private SSH key so no password is transmitted over the network and it can prevent against brute force attack.
Note
Mar 31, 2020 If you need to add users who don't have SSH keys, generate a new SSH key for each new user. If you need to add users who have existing SSH keys, locate the public SSH key file for each user. Format any public SSH keys that you want to add so they work correctly with the tool that you use to edit metadata. Optionally, you can also format your.
- There are specific requirements per platform on user management utilities. However they generally come pre-installed with the system and Ansible will require they are present at runtime. If they are not, a descriptive error message will be shown.
- On SunOS platforms, the shadow file is backed up automatically since this module edits it directly. On other platforms, the shadow file is backed up by the underlying tools used by this module.
- On macOS, this module uses
dscl
to create, modify, and delete accounts.dseditgroup
is used to modify group membership. Accounts are hidden from the login window by modifying/Library/Preferences/com.apple.loginwindow.plist
. - On FreeBSD, this module uses
pwuseradd
andchpass
to create,pwusermod
andchpass
to modify,pwuserdel
remove,pwlock
to lock, andpwunlock
to unlock accounts. - On all other platforms, this module uses
useradd
to create,usermod
to modify, anduserdel
to remove accounts.
See also
- authorized_key – Adds or removes an SSH authorized key
- The official documentation on the authorized_key module.
- group – Add or remove groups
- The official documentation on the group module.
- win_user – Manages local Windows user accounts
- The official documentation on the win_user module.
Common return values are documented here, the following are the fields unique to this module:
Key | Returned | Description |
---|---|---|
append boolean | When state is 'present' and the user exists | Sample: |
comment | When user exists | Comment section from passwd file, usually the user name Agent Smith |
create_home boolean | When user does not exist and not check mode | Sample: |
force | When state is 'absent' and user exists | Whether or not a user account was forcibly deleted |
group integer | When user exists | Sample: |
groups | When groups is not empty and state is 'present' | List of groups of which the user is a member chrony,apache |
home string | When state is 'present' | Sample: |
move_home | When state is 'present' and user exists | Whether or not to move an existing home directory |
name string | always | Sample: |
password | When state is 'present' and password is not empty | Masked value of the password NOT_LOGGING_PASSWORD |
remove boolean | When state is 'absent' and user exists | Sample: |
shell | When state is 'present' | User login shell /bin/bash |
ssh_fingerprint string | When generate_ssh_key is True | Sample: 2048 SHA256:aYNHYcyVm87Igh0IMEDMbvW0QDlRQfE0aJugp684ko8 ansible-generated on host (RSA) |
ssh_key_file string | When generate_ssh_key is True | Sample: |
ssh_public_key | When generate_ssh_key is True | Generated SSH public key file 'ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC95opt4SPEC06tOYsJQJIuN23BbLMGmYo8ysVZQc4h2DZE9ugbjWWGS1/pweUGjVstgzMkBEeBCByaEf/RJKNecKRPeGd2Bw9DCj/bn5Z6rGfNENKBmo 618mUJBvdlEgea96QGjOwSB7/gmonduC7gsWDMNcOdSE3wJMTim4lddiBx4RgC9yXsJ6Tkz9BHD73MXPpT5ETnse+A3fw3IGVSjaueVnlUyUmOBf7fzmZbhlFVXf2Zi2rFTXqvbdGHKkzpw1U8eB8xFPP7y d5u1u0e6Acju/8aZ/l17IDFiLke5IzlqIMRTEbDwLNeO84YQKWTm9fODHzhYe0yvxqLiK07 ansible-generated on host' |
stderr string | When stderr is returned by a command that is run | Sample: |
stdout | When standard output is returned by the command that is run | Standard output from running commands |
system boolean | When system is passed to the module and the account does not exist | Sample: |
uid | When UID is passed to the module | User ID of the user account 1044 |
- This module is guaranteed to have backward compatible interface changes going forward. [stableinterface]
- This module is maintained by the Ansible Core Team. [core]
Red Hat Support¶
More information about Red Hat’s support of this module is available from this Red Hat Knowledge Base article.
Authors¶
- Stephen Fromm (@sfromm)
Hint
If you notice any issues in this documentation, you can edit this document to improve it.
Updated by LinodeWritten by Linode
Report an Issue View File Edit File
Password authentication is the default method most SSH (Secure Shell) clients use to authenticate with remote servers, but it suffers from potential security vulnerabilities, like brute-force login attempts. An alternative to password authentication is public key authentication, in which you generate and store on your computer a pair of cryptographic keys and then configure your server to recognize and accept your keys. Using key-based authentication offers a range of benefits:
Key-based login is not a major target for brute-force hacking attacks.
If a server that uses SSH keys is compromised by a hacker, no authorization credentials are at risk of being exposed.
Because a password isn’t required at login, you are able to able to log in to servers from within scripts or automation tools that you need to run unattended. For example, you can set up periodic updates for your servers with a configuration management tool like Ansible, and you can run those updates without having to be physically present.
This guide will explain how the SSH key login scheme works, how to generate an SSH key, and how to use those keys with your Linode.
NoteHow SSH Keys Work
SSH keys are generated in pairs and stored in plain-text files. The key pair (or keypair) consists of two parts:
A private key, usually named
id_rsa
. The private key is stored on your local computer and should be kept secure, with permissions set so that no other users on your computer can read the file.Caution
A public key, usually named
id_rsa.pub
. The public key is placed on the server you intend to log in to. You can freely share your public key with others. If someone else adds your public key to their server, you will be able to log in to that server.
When a site or service asks for your SSH key, they are referring to your SSH public key (id_rsa.pub
). For instance, services like GitHub and Gitlab allow you to place your SSH public key on their servers to streamline the process of pushing code changes to remote repositories.
The authorized_keys File
In order for your Linode to recognize and accept your key pair, you will need to upload your public key to your server. More specifically, you will need to upload your public key to the home directory of the user you would like to log in as. If you would like to log in to more than one user on the server using your key pair, you will need to add your public key to each of those users.
To set up SSH key authentication for one of your server’s users, add your public key to a new line inside the user’s authorized_keys
file. This file is stored inside a directory named .ssh/
under the user’s home folder. A user’s authorized_keys
file can store more than one public key, and each public key is listed on its own line. If your file contains more than one public key, then the owner of each key listed will be able to log in as that user.
Granting Someone Else Access to your Server
To give someone else access to your server’s user, simply add their public key on a new line in your authorized_keys
file, just as you would add your own. To revoke access for that person, remove that same line and save the changes.
Challenge-Response
When logging in to a server using SSH, if there is a public key on file on that server, the server will create a challenge. This challenge will be crafted in such a way that only the holder of the private SSH key will be able to decipher it.
This challenge-response action happens without any user interaction. If the person attempting to log in has the corresponding private key, then they will be safely logged in. If not, the login will either fail or fall back to a password-based authentication scheme.
SSH Key Passphrases
You can optionally provide an additional level of security for your SSH keys by encrypting them with a passphrase at the time of creation. When you attempt to log in using an encrypted SSH key, you will be prompted to enter its passphrase. This is not to be confused with a password, as this passphrase only decrypts the key file locally and is not transferred over the Internet as a password might be.
If you’d like to set up your logins so that they require no user input, then creating a passphrase might not be desirable, but it is strongly recommended nevertheless.
Linux and macOS
Generate a Key Pair
Perform the steps in this section on your local machine.
Create a new key pair.
Caution
This command will overwrite an existing RSA key pair, potentially locking you out of other systems.
If you’ve already created a key pair, skip this step. To check for existing keys, run
ls ~/.ssh/id_rsa*
.If you accidentally lock yourself out of the SSH service on your Linode, you can still use the Lish console to login to your server. After you’ve logged in via Lish, update your
authorized_keys
file to use your new public key. This should re-establish normal SSH access.The
-b
flag instructsssh-keygen
to increase the number of bits used to generate the key pair, and is suggested for additional security.Press Enter to use the default names
id_rsa
andid_rsa.pub
in the/home/your_username/.ssh
directory before entering your passphrase.While creating the key pair, you will be given the option to encrypt the private key with a passphrase. This means that the key pair cannot be used without entering the passphrase (unless you save that passphrase to your local machine’s keychain manager). We suggest that you use the key pair with a passphrase, but you can leave this field blank if you don’t want to use one.
Want to do your office tasks more quickly and in a well-organized form? It is 100% working and provides you with all the features of all 5 apps in the office. Office professional plus 2010. Then why not to install the 2010 toolkit. You will find no crash in your programs or systems.
Upload your Public Key
There are a few different ways to upload your public key to your Linode from Linux and macOS client systems:
Using ssh-copy-id
ssh-copy-id
is a utility available on some operating systems that can copy a SSH public key to a remote server over SSH.
To use
ssh-copy-id
, pass your username and the IP address of the server you would like to access:You’ll see output like the following, and a prompt to enter your user’s password:
Verify that you can log in to the server with your key.
Using Secure Copy (scp)
Secure Copy (scp
) is a tool that copies files from a local computer to a remote server over SSH:
Generate Ssh Key For User Linux
Cautionauthorized_keys
file on your server. If you have already set up other public keys on your server, use the ssh-copy-id
command or enter your key manually.Connect to your server via SSH with the user you would like to add your key to:
Create the
~/.ssh
directory andauthorized_keys
file if they don’t already exist:Give the
~/.ssh
directory andauthorized_keys
files appropriate file permissions:In another terminal on your local machine, use
scp
to copy the contents of your SSH public key (id_rsa.pub
) into theauthorized_keys
file on your server. Substitute in your own username and your server’s IP address:Verify that you can log in to the server with your key.
Manually Copy an SSH Key
You can also manually add an SSH key to a server:
Begin by copying the contents of your public SSH key on your local computer. You can use the following command to output the contents of the file:
You should see output similar to the following:
Note that the public key begins with
ssh-rsa
and ends with[email protected]
.Once you have copied that text, connect to your server via SSH with the user you would like to add your key to:
Create the
~/.ssh
directory andauthorized_keys
file if they don’t already exist:Give the
~/.ssh
directory andauthorized_keys
files appropriate file permissions:Open the
authorized_keys
file with the text editor of your choice (nano
, for example). Then, paste the contents of your public key that you copied in step one on a new line at the end of the file.Save and close the file.
Note
If you initially logged into the server as
root
but edited theauthorized_keys
file of another user, then the.ssh/
folder andauthorized_keys
file of that user may be owned byroot
. Set that other user as the files’ owner:Verify that you can log in to the server with your key.
Connect to the Remote Server
SSH into the server from your local machine:
If you chose to use a passphrase when creating your SSH key, you will be prompted to enter it when you attempt to log in. Depending on your desktop environment, a window may appear:
Caution
Do not allow the local machine to remember the passphrase in its keychain unless you are on a private computer which you trust.You may also see the passphrase prompt at your command line:
Enter your password. You should see the connection establish in the local terminal.
Windows
The following instructions use the PuTTY software to connect over SSH, but other options are available on Windows too.
Generate a Key Pair with PuTTY
Download PuTTYgen (
puttygen.exe
) and PuTTY (putty.exe
) from the official site.Launch
puttygen.exe
. TheRSA
key type at the bottom of the window is selected by default for an RSA key pair butED25519
(EdDSA using Curve25519) is a comparable option if your remote machine’s SSH server supports DSA signatures. Do not use theSSH-1(RSA)
key type unless you know what you’re doing.Increase the RSA key size from
2048
bits4096
and click Generate:PuTTY uses the random input from your mouse to generate a unique key. Once key generation begins, keep moving your mouse until the progress bar is filled:
When finished, PuTTY will display the new public key. Right-click on it and select Select All, then copy the public key into a Notepad file.
Save the public key as a
.txt
file or some other plaintext format. This is important–a rich text format such as.rtf
or.doc
can add extra formatting characters and then your private key won’t work:Enter a passphrase for the private key in the Key passphrase and Confirm passphrase text fields. Important: Make a note of your passphrase, you’ll need it later:
Click Save private key. Choose a file name and location in Explorer while keeping the
ppk
file extension. If you plan to create multiple key pairs for different servers, be sure to give them different names so that you don’t overwrite old keys with new:
Manually Copy the SSH Key with PuTTY
Launch
putty.exe
. Find the Connection tree in the Category window, expand SSH and select Auth. Click Browse and navigate to the private key you created above:Scroll back to the top of the Category window and click Session. Enter the hostname or IP address of your Linode. PuTTY’s default TCP port is
22
, the IANA assigned port for for SSH traffic. Change it if your server is listening on a different port. Name the session in the Saved Sessions text bar and click Save:Click the Open button to establish a connection. You will be prompted to enter a login name and password for the remote server.
Once you’re logged in to the remote server, configure it to authenticate with your SSH key pair instead of a user’s password. Create an
.ssh
directory in your home directory on your Linode, create a blankauthorized_keys
file inside, and set their access permissions:Open the
authorized_keys
file with the text editor of your choice (nano
, for example). Then, paste the contents of your public key that you copied in step one on a new line at the end of the file.Save, close the file, and exit PuTTY.
Verify that you can log in to the server with your key.
Using WinSCP
Uploading a public key from Windows can also be done using WinSCP:
Cautionauthorized_keys
file on your server. If you have already set up other public keys on your server, use the PuTTY instructions instead.In the login window, enter your Linode’s public IP address as the hostname, the user you would like to add your key to, and your user’s password. Click Login to connect.
Once connected, WinSCP will show two file tree sections. The left shows files on your local computer and the right shows files on your Linode. Using the file explorer on the left, navigate to the file where you saved your public key in Windows. Select the public key file and click Upload in the toolbar above.
You’ll be prompted to enter a path on your Linode where you want to upload the file. Upload the file to
/home/your_username/.ssh/authorized_keys
.Verify that you can log in to the server with your key.
Connect to the Remote Server with PuTTY
Start PuTTY and Load your saved session. You’ll be prompted to enter your server user’s login name as before. However, this time you will be prompted for your private SSH key’s passphrase rather than the password for your server’s user. Enter the passphrase and press Enter.
Troubleshooting
If your SSH connections are not working as expected, or if you have locked yourself out of your system, review the Troubleshooting SSH guide for troubleshooting help.
Upload your SSH Key to the Cloud Manager
It is possible to provision each new Linode you create with an SSH public key automatically through the Cloud Manager.
Log in to the Cloud Manager.
Click on your username at the top right hand side of the page. Then click on My Profile in the dropdown menu that appears:
Note
If you are viewing the Cloud Manager in a smaller browser window or on a smaller device, then the My Profile link will appear in the sidebar links. To view the sidebar links, click on the disclosure button to the left of the blue Create button at the top of the page.From the My Profile page, select the SSH Keys tab, and then click Add a SSH Key:
Create a label for your key, then paste in the contents of your public SSH key (
id_rsa.pub
):Click Add Key.
When you next create a Linode you’ll be given the opportunity to include your SSH key in the Linode’s creation. This key will be added to the root user of the new Linode.
In the Create Linode form, select the SSH key you’d like to include. This field will appear below the Root Password field:
Next Steps
Generate Ssh Key For Github
After you set up your SSH keys and confirm they are working as expected, review the How to Secure Your Server guide for instructions on disabling password authentication for your server.
Join our Community
Generate Ssh Key For Jenkins User Windows
This guide is published under a CC BY-ND 4.0 license.