Let supervisor access all host environment variables - x11

There is a host environment variable "DISPLAY" which I want in my supervisor environment.
Also, I do not want to meddle with my .bashrc file.

Related

How do I set an environment variable for Ansible itself not using shell?

I would like to keep tmp directory on the VM in my test region. There is the following solution for the problem: setting ANSIBLE_KEEP_REMOTE_FILES to 1 on the Ansible machine.
The issue is that the ansible machine is a local docker container so I need to ensure that this variable is always set. Otherwise I'm loosing some documents. When I reboot my system and start this docker container with Ansible I'm loosing this variable.
Is there a way to set this environment variable somewhere in Ansible configuration = or in a playbook configuration somewhere? I need a permanent solution in order not to forget this variable.
Thank you!
Q: "Is there a way to set this environment variable somewhere in Ansible configuration?"
A: Yes. It is. For example
$ cat ansible.cfg
[defaults]
keep_remote_files = true
See DEFAULT_KEEP_REMOTE_FILES.

how to change ubuntu display name?

I'm wondering how I can change the displaying name when I log into my server.
So for example I SSH into the server and it display my username # ec2 instance ip.
example: username#ip-10.0.0.129
what I want: username#production
I don't want it to affect any DNS, host, hostname or Ip stuff only to give it a nickname, any suggestions?
another example: When Cron finishes its job it sends an email from root
Cron <root#ip-10-0-0-129>.....
what I want: Cron <root#production>.....
My ec2 instance on aws is called production so could I somehow instead tell the system to use that name instead of the ip address?
Thanks!
You just need to customize your bash prompt settings in your ~/.bashrc file and change the value of the PS1 variable that controls the presentation of the bash prompt. There are many examples of how to do this online but here is one.
If you want to hardcode it to your username#production, you can do something like this:
PS1='\u#production: '
You can test this first right from your bash prompt before editing the bashrc file, i.e. just enter what I have above, so something like this:
DEFAULT=$PS1 #save your existing setting
PS1='\u#production: ' #try a new setting
PS1=$DEFAULT #restore the original setting
You can also just edit the hostname of your system as detailed in the AWS documentation here.

Reading an environment variable from the remote host

I'm attempting to write an Ansible task that utilizes an environment variable on the remote host.
Based on the docs I've thought to use either lookup('env', 'SSH_AUTH_SOCK') oransible_env.SSH_AUTH_SOCK` but neither is returning the correct value. If I use the former it returns the value from my local host (not the remote host). If I use the latter is returns nothing.
If I ssh into the machine I'm able to run echo $SSH_AUTH_SOCK without issue.
My understanding was that ansible_env was the proper access point for remote host environment variables but that seems to not be the case.
Any help is appreciated.
It is possible the env variable (SSH_AUTH_SOCK) is not in the remote's env, so it is returning nothing. One way to rule this out is to get something that is always available, eg: USER or SSH_CLIENT. If you can get that value, then you can safely assume SSH_AUTH_SOCK is not set in remote's env.
- debug: msg={{ ansible_env.USER }}
The reason you see SSH_AUTH_SOCK is set when you ssh into the machine could be: Your login profile or bash script is starting ssh-agent which sets SSH_AUTH_SOCK variable with the unix socket so that ssh-add works correctly.

Cygwin inherits environment variables from Windows sometimes

If I set the environment variable CVSROOT in Windows and give it a value like cvsserver:/home/cvs, if then
1) open Windows CMD shell and do "echo %CVSROOT%", I get "cvsserver:home/cvs"
2) open Cygwin bash shell and do "echo $CVSROOT", I get "cvsserver:home/cvs"
3) from Linux, ssh to the machine and do "echo $CVSROOT", I get nothing.
If I want the ssh session to have a value for CVSROOT, I need to insert it into the .bashrc.
Is there something that can be done so that the ssh session also inherits the environment variable from Windows?
edit:
4) from Linux, do
ssh machine "printenv CVSROOT"
with the environment variable set in .bashrc, I get nothing. At an interactive prompt, I get the variable value, but this way gives nothing.
I found a nice solution here: http://www.smithii.com/node/44
It looks for system variable in registry and sets variables in session opened via ssh.
Then, call the small piece of script from your ssh client (while connecting to your cygwin server), as this:
ssh $WINDOWSUSER#$WINDOWSBUILDSERVER "source /etc/profile ; echo $CVSROOT "
OK, I've figured it out...
For whatever reason, the bash shell is not inheriting the CVSROOT variable that is set on the system. I need to put this line
export CVSROOT=cvsserver:/home/cvs
into both the .bashrc and the .bash_profile files.
The line is needed in the .bashrc file so that non-interactive logins will get the CVSROOT variable. For example:
ssh machine "printenv CVSROOT"
needs this line in the .bashrc file so that CVSROOT exists.
The line is needed in the .bash_profile file so that interactive logins will get the CVSROOT variable. For example:
ssh machine
printenv CVSROOT
If you have your SSH Service running on a windows box (I'm assuming you do) and it's via Cygwin.
The service seems to take a snapshot of the environment variables when it starts up and doesn't refresh them.
Restarting the service should be enough.
One thing is Windows->Cygwin the other thing is ssh
have you tried forwarding your Windows variable to your linux explicitly:
ssh -t user#host "export CVSROOT=%CVSROOT%;/bin/bash --login"
Restarting your computer will solve this problem. Environment variable changes don’t just propagate to all your running programs.
I hit upon the same problem. Got this working by setting the variable like this:
ssh machine "export MYVARIABLE=1 && printenv MYVARIABLE"

Can we set easy-to-remember hostnames for EC2 instances?

I'm running a couple of standard Fedora instances on EC2. I feel the public hostnames of the instances assigned by Amazon are too weird and hard to remember. I'd like to change them to something short (like red/blue/green/etc).
Is there any draw back in doing this? And how do I set it up such that it persists after reboots?
Thanks.
Before you get started, try running hostname and hostname --fqdn and take note of what the responses are.
You can edit /etc/hostname and set a hostname, which will stick around after rebooting. You can force the hostname to be "reloaded" by using hostname -F /etc/hostname to read that value into the hostname. The bash prompt will change after you logout and login.
warning / note:
Yes, it is nice to have the hostname in the bash prompt set to something more useful than ip-123-123-123-123 but I've decided to leave mine (at least for now) because it seems like a lot of things really count on having the hostname on ec2 instances set in a standard way. After editing /etc/hostname and changing the hostname to webserver a lot of the services seems to fail because the hostname would not resolve, and apache wouldn't start. Next I edited /etc/hosts and added in
127.0.0.1 webserver
as the second line. Apache would then start but complained that it couldn't find the FQDN. I confirmed that running hostname --fqdn no longer worked.
Next I consulted man hostname and learned that while you can set the hostname it appears that the FQDN is what is returned via a DNS lookup.
THE FQDN
You can't change the FQDN (as returned by hostname --fqdn) or the DNS domain name (as returned by dnsdomainname) with this command. The FQDN of the system is the name that the resolver(3) returns for the host name.
Technically: The FQDN is the name getaddrinfo(3) returns for the host name returned by gethostname(2). The DNS domain name is the part after the first dot.
Therefore it depends on the configuration (usually in /etc/host.conf) how you can change it. Usually (if the hosts file is parsed before DNS or NIS) you can change it in /etc/hosts.
I think it might be possible to set the system / fool the system into return the FQDN, something like ip-123-123-123-123.ec2.internal even though the hostname is webserver but at this point it started to seem like more trouble than it was worth, and that for me to have a nicer bash prompt might cause a lot software and configuration problems down the road and so I decided to give up.
I also learned that a lot of amazon ec2 instances use something called cloud-init:
cloud-init is the Ubuntu package that handles early initialization of a cloud instance. It is installed in the Ubuntu Cloud Images and also in the official Ubuntu images available on EC2.
Some of the things it configures are:
setting a default locale
setting hostname
generate ssh private keys
adding ssh keys to user's .ssh/authorized_keys so they can log in
setting up ephemeral mount points
cloud-init's behavior can be configured via user-data. User-data can be given by the user at instance launch time. This is done via the --user-data or --user-data-file argument to ec2-run-instances
I also found this which talks about how the hostname is configured with cloud-init:
On EBS instances, a shutdown and later start would end up with a different IP address.
In the case where the user has not modified /etc/hostname from its original value (seeded by metadata's 'local-hostname'), then cloud-init will again set the hostname and update /etc/hostname.
In the case where the user has modified /etc/hostname, it will remain user managed.
Additionally, if /etc/cloud/cloud.cfg contains 'preserve_hostname' value set to a True value, then /etc/hostname will not ever be touched.
The interesting takeaway is that if you don't change the hostname the cloud-init package will keep it up to date for you.
If someone else has a workaround or can address some of the issues mentioned and help reassure that nothing will break on ec2 instances because of changing the hostname I would be happy to hear it.
Another way is to simply edit ~/.bashrc and prepend PS1 with the nickname of the machine.
Edit: perhaps more correctly, machine-wide, e.g. on the AWS Linux AMI (an example) (paste this into console or add to your arbitrary install .sh):
cat << EOF | sudo tee /etc/profile.d/ps1.sh
if [ "$PS1" ]; then
PS1="[\u#myinst1:\l \t \! \W]\\$ "
fi
EOF
Edit /etc/sysconfig/network as root.
Replace
HOSTNAME=localhost.localdomain
with
HOSTNAME=hostname.DOMAIN_NAME
Then, either reboot or run /etc/init.d/network restart
The server then should report its name as a FQDN.
From this site:
Change the hostname on a running system
On any Linux system you can change its hostname with the command hostname (surprised?)…
Here are some quick usages of the command line hostname:
$> hostname
without any parameter it will output the current hostname of the system.
$> hostname --fqd
it will output the fully qualified domain name (or FQDN) of the system.
$> hostname NEW_NAME
will set the hostname of the system to NEW_NAME.
You can also edit /etc/hostname (at least on Ubuntu).
To make sure it stays after a reboot in AWS, either add the command in /etc/rc.local so it runs when the machine starts.
There's also a way to set the hostname dynamically via USER_DATA:
USER_DATA=`/usr/bin/curl -s http://169.254.169.254/latest/user-data`
HOSTNAME=`echo $USER_DATA`
IPV4=`/usr/bin/curl -s http://169.254.169.254/latest/meta-data/public-ipv4`
hostname $HOSTNAME
echo $HOSTNAME > /etc/hostname
To change the system hostname to a public DNS name
Follow this procedure if you already have a public DNS name registered
Open the /etc/sysconfig/network configuration file in your favorite text editor and change the HOSTNAME entry to reflect the fully qualified domain name (such as webserver.mydomain.com).
HOSTNAME=webserver.mydomain.com
Reboot the instance to pick up the new hostname.
[ec2-user ~]$ sudo reboot
Log into your instance and verify that the hostname has been updated. Your prompt should show the new hostname (up to the first ".") and the hostname command should show the fully qualified domain name.
[ec2-user#webserver ~]$ hostname
webserver.mydomain.com
To change the system hostname without a public DNS name
Open the /etc/sysconfig/network configuration file in your favorite text editor and change the HOSTNAME entry to reflect the desired system hostname (such as webserver).
HOSTNAME=webserver.localdomain
Open the /etc/hosts file in your favorite text editor and add an entry beginning with 127.0.1.1 (on DHCP systems) or eth0's address (on static IP systems) to match the example below, substituting your own hostname. (127.0.0.1 should be left as the localhost line.)
127.0.0.1 localhost localhost.localdomain
127.0.1.1 webserver.example.com webserver
Reboot the instance to pick up the new hostname.
[ec2-user ~]$ sudo reboot
Log into your instance and verify that the hostname has been updated. Your prompt should show the new hostname (up to the first ".") and the hostname command should show the fully qualified domain name.
[ec2-user#webserver ~]$ hostname
webserver.localdomain
Note: You can also change the shell prompt without affecting the hostname. Refer to this AWS documentation.
Sure, you can do that if you have your own domain (setup a CNAME to point to the Amazon hostname). Otherwise, you're pretty much stuck with the one they give you (or an Elastic IP, if you set one of those up).
The /etc/rc.local solution worked for me for a basic hostname but does not give me a FQDN.
In my Linux AMI (a snapshot of other instance).. none of the above formula worked. Then, I simply changed HOSTNAME field in file: /etc/init.d/modifyhostname and did a normal reboot.
You will need to do multiple things to set the hostname:
hostname newname - sets the hostname, but is volatile
edit /etc/hostname - sets the hostname for the next reboot
edit /etc/hosts - to keep sudo from complaining
I put these together into a script and uploaded it as a gist:
https://gist.github.com/mnebuerquo/5443532036af8b48995547e2817dba85
sudo hostname *yourdesiredhostnamehere*
sudo /etc/init.d/networking restart
then the hostname is changed. On my server all other services like apache and postfix works. Server is Ubuntu 12.04 LTS

Resources