<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Le blog de Jean-Eric &#187; Linux</title>
	<atom:link href="http://blog.jesc.ch/category/linux/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.jesc.ch</link>
	<description>Family and thoughts on life, computers and others</description>
	<lastBuildDate>Wed, 30 Jun 2010 05:38:50 +0000</lastBuildDate>
	<language>fr</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='blog.jesc.ch' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://www.gravatar.com/blavatar/4e0dc4fb3c9a5ab4b437fb316b1aadda?s=96&#038;d=http://s2.wp.com/i/buttonw-com.png</url>
		<title>Le blog de Jean-Eric &#187; Linux</title>
		<link>http://blog.jesc.ch</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://blog.jesc.ch/osd.xml" title="Le blog de Jean-Eric" />
	<atom:link rel='hub' href='http://blog.jesc.ch/?pushpress=hub'/>
		<item>
		<title>Which profile does bash run at login?</title>
		<link>http://blog.jesc.ch/2009/08/07/which-profile-does-bash-run-at-login/</link>
		<comments>http://blog.jesc.ch/2009/08/07/which-profile-does-bash-run-at-login/#comments</comments>
		<pubDate>Fri, 07 Aug 2009 06:18:35 +0000</pubDate>
		<dc:creator>Jean-Eric Cuendet</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Travail]]></category>

		<guid isPermaLink="false">http://blog.jesc.ch/?p=36</guid>
		<description><![CDATA[Have you ever wondered which of the multiple files does bash run when you login to a Linux machine? Remember, there are at least those files that are potentially run: /etc/profile /etc/bash.bashrc /etc/bash_completion .profile .bash_profile .bashrc .bash_completion Depending if you login interactively (getting a shell) or just running a command,some of these files are sourced. [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.jesc.ch&blog=442511&post=36&subd=jesc&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<p>Have you ever wondered which of the multiple files does bash run when you login to a Linux machine?</p>
<p>Remember, there are at least those files that are potentially run:</p>
<ul>
<li>/etc/profile</li>
<li>/etc/bash.bashrc</li>
<li>/etc/bash_completion</li>
<li>.profile</li>
<li>.bash_profile</li>
<li>.bashrc</li>
<li>.bash_completion</li>
</ul>
<p>Depending if you login interactively (getting a shell) or just running a command,some of these files are sourced.</p>
<h3><span id="more-36"></span>Assumptions</h3>
<p>We will take the case where:</p>
<ul>
<li>You login through SSH</li>
<li>Or you change to a user using su</li>
<li>You have an Ubuntu or Debian Linux machine</li>
</ul>
<p>I suspect that Redhat/Fedora and others are not far away from these assumptions, but I have none to check&#8230;</p>
<p>There are in fact 6 scenarios</p>
<h2>First Scenario : interactive shell with SSH</h2>
<p>You login to a remote machine using SSH, getting a shell. This is the most current use of SSH.</p>
<p>With:</p>
<pre>     ssh user@machine</pre>
<p>You then get a shell.</p>
<p>The files that are sourced are:</p>
<ul>
<li>/etc/profile</li>
<li>/etc/bash.bashrc</li>
<li>.profile</li>
<li>.bashrc</li>
<li>/etc/bash_completion</li>
<li>.bash_completion</li>
</ul>
<p>That&#8217;s right only if <strong>there is no .bash_profile</strong> file in your HOME directory. Else the files sourced are:</p>
<ul>
<li>/etc/profile</li>
<li>/etc/bash.bashrc</li>
<li>.bash_profile</li>
</ul>
<p>In fact, bash sources <em>.bash_profile</em> if it exists, else <em>.bashrc</em></p>
<p>Note that</p>
<ul>
<li><em>/etc/bash.bashrc</em> is sourced by <em>/etc/profile</em> if the shell is <em>bash</em></li>
<li><em>.bashrc</em> is sourced by <em>.profile<br />
</em></li>
<li><em>/etc/bash_completion</em> is in fact sourced by <em>.bashrc</em> (not <em>bash</em> itself directly)</li>
</ul>
<p>So we could summarize this way.</p>
<p>When asking for an interactive shell, bash sources:</p>
<ul>
<li><em>/etc/profile</em></li>
<li><em>.bash_profile</em></li>
<li>Or <em>.profile</em> if <em>.bash_profile</em> is not found</li>
</ul>
<h2>Second scenario : running a command through SSH</h2>
<p>You login to a remote machine using SSH, asking to run a command (no interactive shell)</p>
<pre>     ssh user@machine "command to run" (Ex: ssh user@machine "ls /tmp")</pre>
<p>The files that are sourced are:</p>
<ul>
<li><em>/etc/bash.bashrc</em></li>
<li><em>.bashrc</em></li>
</ul>
<p>Note that <em>.bashrc</em> is run in place of <em>.bash_profile</em> in this case.</p>
<h2>Third scenario : getting an interactive shell using <em>su -</em></h2>
<p>You are logged with root and want to get a shell as another user:</p>
<pre>     su - user</pre>
<p>Note that the <em>minus</em> sign means that you want to source the config files for the target user.</p>
<p>The files that are sourced, are</p>
<ul>
<li><em>/etc/profile</em></li>
<li><em>.bash_profile</em></li>
<li>Or <em>.profile</em> if <em>.bash_profile</em> is not found</li>
</ul>
<h2>Fourth scenario : running a command using <em>su -</em></h2>
<p>You are logged with root and want to get a shell as another user:</p>
<pre>     su - user -c "touch /tmp/file"</pre>
<p>The files that are sourced, are</p>
<ul>
<li><em>/etc/profile</em></li>
<li><em>.bash_profile</em></li>
<li>Or <em>.profile</em> if <em>.bash_profile</em> is not found</li>
</ul>
<p>Note that in this case, <em>.bashrc</em> is <strong>NOT</strong> run from <em>/etc/profile</em> since the shell is not interactve! (PS1 is not defined)</p>
<h2>Fifth scenario : getting an interactive shell using <em>su (without the minus sign)<br />
</em></h2>
<p>You are logged with root and want to run a shell as another user:</p>
<pre>
<pre>     su user</pre>
</pre>
<p>Surprise! Even since you thought that the minus sign would source NO config files, the files that are sourced, are</p>
<ul>
<li><em>/etc/bash.bashrc</em></li>
<li><em>.bashrc</em></li>
</ul>
<p>Note that the sourced files are the same as running a command through SSH!</p>
<h2>Sixth scenario : running a command using <em>su (without the minus sign)<br />
</em></h2>
<p>You are logged with root and want to run a shell as another user:</p>
<pre>
<pre>     su user -c "touch /tmp/file"</pre>
</pre>
<p>And this time, <strong>no surprise</strong>, the files that are sourced, are &#8230; <strong>NONE</strong>!</p>
<p>That&#8217;s exactly what the minus sign means. Become &lt;user&gt;, but don&#8217;t source its config files.</p>
<h2>And the bonus scenario</h2>
<p>What happens when you run a command from a crontab?</p>
<p>First, the default shell when running from a CRON, is /bin/sh. You can change this to bash putting:</p>
<pre>     SHELL=/bin/bash</pre>
<p>as the first line of your crontab file (<em>crontab -e</em> is your friend for this one).</p>
<p>Then sourced config files when the command is run are &#8230; <strong>NONE</strong> also.</p>
<h2>Conclusion</h2>
<p>Every time I need to run remote command on a machine or run a script as a user using <em>su</em>, I ask myself the same questions about those config files!</p>
<p>And we can see above that it&#8217;s not that clear which files are run when&#8230;</p>
<p>So now that&#8217;s done, for the posterity, on my blog so it should last for some time.</p>
<p>Comments of course welcome!</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jesc.wordpress.com/36/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jesc.wordpress.com/36/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/jesc.wordpress.com/36/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/jesc.wordpress.com/36/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/jesc.wordpress.com/36/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/jesc.wordpress.com/36/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/jesc.wordpress.com/36/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/jesc.wordpress.com/36/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/jesc.wordpress.com/36/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/jesc.wordpress.com/36/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.jesc.ch&blog=442511&post=36&subd=jesc&ref=&feed=1" />]]></content:encoded>
			<wfw:commentRss>http://blog.jesc.ch/2009/08/07/which-profile-does-bash-run-at-login/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/a0bc15a12faa532d9ca600d5b444ccc0?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">jesc</media:title>
		</media:content>
	</item>
	</channel>
</rss>