connect Jenkins agent using "ssh username with private key"

using agent

managing node

stackoverflow

cloudbees

Step

1. prepare on agent
# create user(name could be others then jenkins)
useradd -d /var/lib/jenkins jenkins  // use /home/jenkins as home directory is available too

# setup new user password
passwd jenkins

# change user
su - jenkins

# create ssh key
mkdir /var/lib/jenkins/.ssh
touch /var/lib/jenkins/.ssh/authorized_keys
ssh-keygen -f /var/lib/jenkins/.ssh/jenkins_key

# you need add public key to authorized_keys
cd /var/lib/jenkins/.ssh
cat jenkins_key.pub > authorized_keys 

# chagne permission
chown -R jenkins /var/lib/jenkins/.ssh
chmod 600 /var/lib/jenkins/.ssh/authorized_keys
chmod 700 /var/lib/jenkins/.ssh


2. create a Jenkins SSH credential
Kind: SSH Username with private key;
id: set-this-by-yourself
username: agent user name
private key: paste the agent private key at here

3. on master, scan agent key to known_hosts
cd /var/lib/jenkins/.ssh
ssh-keyscan -H dell-per740-17.rhts.eng.pek2.redhat.com >> ./known_hosts

4. create node
name: node name
Number of executors: don't exceed number of cpus
Remote root directory: a directory on agent which the agent username could access
Labels:
Launch method : Launch agent via SSH
Host: ip or hostname
Credentials: the credential you created for it
Host Key Verification Strategy : Known hosts file Verification Strategy