Jenkins declarative pipeline ssh agent plugin
As noted, you should select a credential which does reference the right remote username, as seen in the SSH Agent Jenkins plugin:
node { sshagent (credentials: ['deploy-dev']) { sh 'ssh -o StrictHostKeyChecking=no -l cloudbees 192.168.1.106 uname -a' }}
Plus, I would execute only one script which would have the all sequence of commands you want to execute remotely.
Well, so far this is the best way to do so, in-spite of repetition!
pipeline { agent any stages { stage('Deploy') { steps { sshagent(['nginx-ec2']) { // some block sh "ssh -o StrictHostKeyChecking=no -l ubuntu <remote_ip> 'whoami'" sh "ssh -o StrictHostKeyChecking=no -l ubuntu <remote_ip> 'sudo apt update && sudo apt install -y docker.io'" sh "ssh -o StrictHostKeyChecking=no -l ubuntu <remote_ip> 'sudo usermod -aG docker ubuntu'" sh "ssh -o StrictHostKeyChecking=no -l ubuntu <remote_ip> 'source .bashrc'" sh "ssh -o StrictHostKeyChecking=no -l ubuntu <remote_ip> 'docker run -d -nginx'" } } } }}