- Newest
- Most votes
- Most comments
Hello.
What version of Ubuntu are you using?
In Ubuntu versions 24-based, the ssh process uses something called ssh.socket.
Therefore, even if you rewrite the settings in "/etc/ssh/sshd_config", the port number will not change.
https://www.reddit.com/r/Ubuntu/comments/1gybsi7/changing_ssh_port_does_not_work_ubuntu_2410/
You need to either disable ssh.socket as follows, or rewrite the settings in "/usr/lib/systemd/system/ssh.socket" and perform a daemon reload to overwrite the ssh.socket settings.
- systemctl stop ssh.socket
- systemctl disable ssh.socket
Incidentally, the following sample Terraform code is code that I was able to confirm works on my AWS account using an Ubuntu 24.04 AMI in the Tokyo region.
locals {
cloud_init_config = <<-YAML
#cloud-config
runcmd:
- sed -i 's/ListenStream=0.0.0.0:22/ListenStream=0.0.0.0:10022/' /usr/lib/systemd/system/ssh.socket
- sed -i 's/ListenStream=\\[::\\]:22/ListenStream=[::]:10022/' /usr/lib/systemd/system/ssh.socket
- systemctl daemon-reexec
- systemctl daemon-reload
- systemctl restart ssh.socket
- sed -i 's/^#\?Port 22/Port 10022/' /etc/ssh/sshd_config
- systemctl restart ssh.service
YAML
}
resource "aws_instance" "example" {
ami = "ami-0f8faa29480e7e6de"
instance_type = "t3.micro"
key_name = aws_key_pair.example.key_name
vpc_security_group_ids = [aws_security_group.ec2_sg.id]
user_data = base64encode(local.cloud_init_config)
}
Based on your situation, here are some recommendations for reliably rebooting your EC2 instances and applying SSH configuration changes:
Understanding the Reboot Command
The AWS CLI reboot-instances command is asynchronous - it only queues a request to reboot the specified instances rather than waiting for the reboot to complete. This means your Terraform provisioner may be completing before the instance has actually rebooted and come back online, which could explain the timing issues you're experiencing.
Recommended Approaches
-
Add Wait Conditions: After issuing the reboot command, you should wait for the instance to complete its reboot cycle. Consider adding a wait command that checks for the instance to return to a running state and pass status checks before proceeding.
-
Handle the sshd Service Properly: For Ubuntu instances, the SSH service might be managed differently. Instead of trying to restart
sshd.service, you could:
- Use
sudo systemctl restart ssh.service(without the 'd') - Or include a reboot command at the end of your user_data script using
shutdown -r +1to schedule a reboot after the configuration changes are written
- Improve Your user_data Script: Consider making your user_data script more robust by:
- Checking if the port already exists in the config before adding it
- Using
sedor proper configuration management to modify sshd_config - Adding logging to track what's happening during instance initialization
- Ensuring the SSH configuration is valid before attempting any service restart
- Alternative to null_resource: Instead of using a separate null_resource to reboot, you could handle the reboot within the user_data itself after making the configuration changes, though you mentioned this didn't work - this might be due to timing of when user_data executes.
The key issue is likely the asynchronous nature of the reboot operation combined with timing dependencies in your Terraform workflow. Adding proper wait conditions and status checks should help ensure the instances are fully operational before Terraform considers the provisioning complete.
Sources
reboot-instances — AWS CLI 2.27.41 Command Reference
Relevant content
- asked 2 years ago
- asked 3 years ago

Thank you for sharing your approach. I'm using AMIs ami-019715e0d74f695be and ami-05d2d839d4f73aafb. I tested and found a working method for my project. It seems that the following
user_datais enough for what I'm trying to do--I didn't need to edit the
systemdfiles for the services.However, I also discovered the following through my testing--
systemctlcommands need to be run in the following order:daemon-reexec,restart ssh.socketandrestart ssh.service. This order works for both Terraform code and manually executing them after connecting to the instance. They can be shortened as below--sudoisn't necessary to run the commands, but required for adding the ports to the file usingtee.user_datacommands don't execute. Code editor might cause problems here with their auto-indentation.