I am trying to bootstrap a Centos7 EC2 instance (ami-02eac2c0129f6376b) with bash user-data. Because it runs as root and I need to create a lot of things as the centos user, I use sudo -Hu centos <cmd>
many times. Each call introduces a 25 second delay. I have hundreds. What can I do to speed this up?
This is a known issue, but I cannot find any solutions.
I have tried:
- Add and
/etc/hosts
entry for my hostname
printf "\n127.0.0.1 %s %s\n" "$(hostname -f)" "$(hostname -s)" | tee -a /etc/hosts
- Remove myhostname from /etc/nsswitch.conf
sed -Ei 's/\s*myhostname//' /etc/nsswitch.conf
These delays are EXTREMELY painful because I am currently in the Trial and Error phase of building out new user-data scripts.
What can I do?
While it's better to figure out why
sudo
is slow and resolve that, you can group your commands together to only call sudo once. Inside your script, you could do something like this:A bug in the SELinux policy included with RHEL and CentOS 7 caused sudo to wait for exactly 25 seconds on each command. This bug was fixed with the
selinux-policy-3.13.1-229.el7_6.10
package. You should create a new AMI with this package (and preferably the whole system) updated.