Reputation: 1320
Using 'Execute shell script on remote host using ssh' option and need sudo rights on remote server to change permissions and remove protected files. How to run session with this rights?
Getting message
sudo: sorry, you must have a tty to run sudo
when trying to run sudo command.
Upvotes: 4
Views: 8729
Reputation: 1
on ubuntu22.04
sshCommand(remote: remoteConfig, command: 'echo 1 | sudo -S systemctl daemon-reload')
Use it like this
stage("ssh upload") {
steps {
withCredentials([sshUserPrivateKey(
credentialsId: "${REMOTE_CRED}",
keyFileVariable: "privateKeyFilePath"
)]) {
script {
remoteConfig = [:]
remoteConfig.name = "my-remote-server"
remoteConfig.host = "${REMOTE_HOST}"
remoteConfig.port = 22367
remoteConfig.allowAnyHosts = true
remoteConfig.user = "ubuntu"
// SSH private key
remoteConfig.identityFile = privateKeyFilePath
sshPut(remote: remoteConfig, from: 'cyberVillageServer', into: '/home/ubuntu/myapp/')
sshCommand(remote: remoteConfig, command: 'echo 1 | sudo -S systemctl daemon-reload')
}
}
}
}
Upvotes: 0
Reputation: 239
To run sudo
remotely you have 2 options
sudo
commands without a password.Append username ALL=(ALL) NOPASSWD: ALL
the /etc/sudoers
file with sudo visudo
. Alternatively you can modify this line to only allow certain sudo
commands to be run without a password
sudo
password when requsted.To do this run ssh -t username@host command_to_execute
Upvotes: 5
Reputation: 12561
If the remote server accepts the direct login of the root
user you can simply do:
ssh -l root yourserver command_to_execute
Similar syntax is:
ssh root@yourserver command_to_execute
Mind that allowing the login of the root
user via ssh to a remote server isn't always a good solution.
A better solution would be change the owner / permissions to allow a non-root user to modify the protected files.
Upvotes: 1