lundi 27 octobre 2014

Set / Change $PATH Variable

Again a very interesting article from a blog that I don't want to take the risk to lost: Modified from:
http://www.cyberciti.biz/faq/appleosx-bash-unix-change-set-path-environment-variable/


Mac OS X: Set / Change $PATH Variable


$PATH is nothing but an environment variable on Linux, OS X, Unix-like operating systems, and Microsoft Windows. You can specify a set of directories where executable programs are located using $PATH. The $PATH variable is specified as a list of directory names separated by colon (:) characters. To print the current settings, open the Terminal and then type:

echo "$PATH" 
OR
printf "%s\n" $PATH
OS X: Change your PATH environment variable
First remember that the shell used can be obtained using this command:
echo $SHELL
An interactive shell reads /etc/profile and its private equivalent ~/.bash_profile at startup.
An interactive non-login shell is normally started at the command-line using a shell program (e.g., [prompt]$/bin/bash) or by the /bin/su command. An interactive non-login shell is also started with a terminal program such as xterm or konsole from within a graphical environment. This type of shell invocation normally copies the parent environment and then reads the user's ~/.bashrc file for additional startup configuration instructions.
You can add path to any one of the following method:
  1. $HOME/.bash_profile file using export syntax.
  2. /etc/paths.d directory.

Method #1: $HOME/.bash_profile file

The syntax is as follows:
export PATH=$PATH:/new/dir/location1
export PATH=$PATH:/new/dir1:/dir2:/dir/path/no3
In this example, add /usr/local/sbin/modemZapp/ directory to $PATH variable. Edit the file $HOME/.bash_profile, enter:
vi $HOME/.bash_profile
OR
vi ~/.bash_profile
Append the following export command:
export PATH=$PATH:/usr/local/sbin/modemZapp
Save and close the file. To apply changes immedialty enter:
source $HOME/.bash_profile
OR
. $HOME/.bash_profile
Finally, verify your new path settings, enter:
echo $PATH
Sample outputs:
/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/X11/bin:/usr/local/sbin/modemZapp

Method #2: /etc/paths.d directory

Apple recommends the path_helper tool to generate the PATH variable i.e. helper for constructing PATH environment variable. From the man page:
The path_helper utility reads the contents of the files in the directories /etc/paths.d and /etc/manpaths.d and appends their contents to the PATH and MANPATH environment variables respectively.
(The MANPATH environment variable will not be modified unless it is already set in the environment.)
Files in these directories should contain one path element per line.
Prior to reading these directories, default PATH and MANPATH values are obtained from the files /etc/paths and /etc/manpaths respectively.
To list existing path, enter:
ls -l /etc/paths.d/
Sample outputs:
total 16
-rw-r--r--  1 root  wheel  13 Sep 28  2012 40-XQuartz
You can use the cat command to see path settings in 40-XQuartz:
cat /etc/paths.d/40-XQuartz
Sample outputs:
/opt/X11/bin
To set /usr/local/sbin/modemZapp to $PATH, enter: 
sudo -s 'echo "/usr/local/sbin/modemZapp" > /etc/paths.d/zmodemapp'
OR use vi text editor as follows to create /etc/paths.d/zmodemapp file:
sudo vi /etc/paths.d/zmodemapp
and append the following text:
/usr/local/sbin/modemZapp
Save and close the file. You need to reboot the system. Alternatively, you can close and reopen the Terminal app to see new $PATH changes.

Conclusion

  1. Use $HOME/.bash_profile file when you need to generate the PATH variable for a single user account.
  2. Use /etc/paths.d/ directory via the path_helper tool to generate the PATH variable for all user accounts on the system. This method only works on OS X Leopard and higher

mardi 21 octobre 2014

ssh without password

I copy an article from another blog because I don't want to risk to lost it:

SSH login without password

Your aim

You want to use Linux and OpenSSH to automate your tasks.
Therefore you need an automatic login from host A / user a to Host B / user b.
You don't want to enter any passwords, because you want to call ssh
from a within a shell script.

How to do it

First log in on A as user a and generate a pair of authentication keys.
Do not enter a passphrase:
> ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/a/.ssh/id_rsa): 
Created directory '/home/a/.ssh'.
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /home/a/.ssh/id_rsa.
Your public key has been saved in /home/a/.ssh/id_rsa.pub.
The key fingerprint is:
3e:4f:05:79:3a:9f:96:7c:3b:ad:e9:58:37:bc:37:e4 a@A
Now use ssh to create a directory ~/.ssh as user b on B.
(The directory may already exist, which is fine):
> ssh b@B mkdir -p .ssh
b@B's password: 
Finally append a's new public key to b@B:.ssh/authorized_keys and
enter b's password one last time:
> cat .ssh/id_rsa.pub | ssh b@B 'cat >> .ssh/authorized_keys'
b@B's password: 
From now on you can log into B as b from A as a without password:
> ssh b@B
A note from one of our readers: Depending on your version of SSH you
might also have to do the following changes:
  • Put the public key in .ssh/authorized_keys2
  • Change the permissions of .ssh to 700
  • Change the permissions of .ssh/authorized_keys2 to 640