Page not found – ShopingServer Wiki http://wiki.shopingserver.com Tutorials and Articles About Technology and Gadgets Wed, 02 Sep 2020 02:20:40 +0000 en-US hourly 1 https://wordpress.org/?v=5.5.14 http://wiki.shopingserver.com/wp-content/uploads/2018/07/cropped-favicon-150x150.png Page not found – ShopingServer Wiki http://wiki.shopingserver.com 32 32 Unix and Linux: Redirect Error Output To null Command http://wiki.shopingserver.com/unix-linux-redirect-error-output-null-command/ http://wiki.shopingserver.com/unix-linux-redirect-error-output-null-command/#respond Sat, 06 Jan 2018 10:23:51 +0000 http://wiki.shopingserver.com/?p=18602 I

‘m a new Linux system user. How can I redirect command error output /dev/null on a Linux or Unix-like system using Bash shell?

 

Your shell comes with three file descriptors as follows:

stdin – 0 – Standard Input (usually keyboard or file)

stdout – 1 – Standard Output (usually screen)

stderr – 2 – Standard Error (usually screen)

What is a null (/dev/null) file in a Linux or Unix-like systems?

/dev/null is nothing but a special file that discards all data written to it. The length of the null device is always zero. In this example, first, send output of date command to the screen and later to the /dev/null i.e. discards date command output:

Show on screen ###

date

 

Discards date command output ###

date > /dev/null

Syntax: Standard Error (stderr -2 no) to a file or /dev/null

The syntax is as follows:

command 2>/dev/null

command arg1 arg2 2>/dev/null

date bar 2>/dev/null

ls -foo 2>/dev/null

In this example, send output of find command to /dev/null:

$ find /etc -type f -name  *  2>/dev/null

 

The following example will cause the stderr ouput of a program to be written to a file called errors.txt:

$ find /etc/ -type f -name  *  2> errors.txt

Linux and Unix redirect all output and error to file

The syntax is:

send command output to output.txt and error message to error.txt ##

command > output.txt 2> error.txt

command  -arg1 -arg2 > output.txt 2> error.txt

If you want both stderr and stdout in same file, try:

command > log.txt 2>&1

Use cat command to display log.txt on screen:

cat log.txt

See man pages for more information – bash(1),ksh(1).

 

 

]]>
http://wiki.shopingserver.com/unix-linux-redirect-error-output-null-command/feed/ 0
sed Tip: Delete All Blank White Spaces http://wiki.shopingserver.com/sed-tip-delete-blank-white-spaces/ http://wiki.shopingserver.com/sed-tip-delete-blank-white-spaces/#respond Sat, 06 Jan 2018 10:22:36 +0000 http://wiki.shopingserver.com/?p=18600 I have a text file as follows:

foo

bar

foobar

How can I delete all leading and/or trailing blank spaces, tab from each line using sed command?

 

You can use sed command to delete all white (blank) spaces in a text file. You can also use other text processing utilities such as:

Perl.

Python.

Awk and friends.

Perl example

The syntax is:

perl -lape  s/\s+//sg  input > output

Sample outputs:

foo

bar

foobar

Or use -pie syntax to update file:

cat input

perl -lapi -e  s/\s+|^\n//sg   input

cat input

See perl(1) for more information.

Sed example

The syntax is:

sed -e  s/^[ \t]*//  -e  s/[ \t]*$//  input > output

OR updated file in a single go with the -i option:

sed -i -e  s/^[ \t]*//  -e  s/[ \t]*$//  input

See sed(1) for more information.

Awk example

The syntax is

awk  {$1=$1}{ print }  input > output

You can also use gsub() substring matching the regular expression function. See awk(1) for more information.

 

 

]]>
http://wiki.shopingserver.com/sed-tip-delete-blank-white-spaces/feed/ 0
Linux: Find Out Directory Size Command http://wiki.shopingserver.com/linux-find-directory-size-command/ http://wiki.shopingserver.com/linux-find-directory-size-command/#respond Sat, 06 Jan 2018 10:13:47 +0000 http://wiki.shopingserver.com/?p=18589 I am a new Linux user. How do I find out size of a directory on Linux operating systems using command line options?

 

You need to use the du command:

[a] Find and estimate file space usage.

[b] Summarize disk usage of each FILE/Directory/Folder.

[c] Shows the sizes of directories and files.

Syntax

The basic syntax is:

du

du dirName

du [options] dirName

Examples

Without any options, du command shows the names and used space for each directories including all sub-directories in the current directory:

du

 

Sample outputs:

Fig.01: du command in action

To find information about /etc and /home/nixcraft directory, enter:

du /path/to/dir

du /etc

du /home/nixcraft

du /root /home/nixcraft

Pass the -h option to get output in human readable format i.e. show output in kilobytes (K), megabytes (M) and gigabytes (G):

du -h /etc

du -h /dir1/file2

du -h /root

du -h

 

Sample outputs:

8.0K ./.vim

24K ./scripts

48K ./.ssh

16K ./.keychain

2.2M ./.lftp

2.4M .

Pass the -s option to see the total disk space used by a directory:

du -sh

du -sh /etc/

du -sh /etc /home/ /securebackup/

 

Sample outputs:

4.1M /etc

152K /home/

902M /securebackup/

Pass the -c to see a grand total for all of the files, type:

du -csh /root/ /etc/ /home/

 

Sample outputs:

2.4M /root/

4.1M /etc/

152K /home/

6.6M total

See also

UNIX disk usage command examples – include command line tool such as du, df, ncdu, and GUI tools.

See du(1) and examples.

 

 

]]>
http://wiki.shopingserver.com/linux-find-directory-size-command/feed/ 0
Linux: Find Out How Much Disk Space Left On Hard Drive http://wiki.shopingserver.com/linux-find-much-disk-space-left-hard-drive/ http://wiki.shopingserver.com/linux-find-much-disk-space-left-hard-drive/#respond Sat, 06 Jan 2018 10:12:32 +0000 http://wiki.shopingserver.com/?p=18587 I am a desktop support professional with experience working in a corporate call center environment. Recently, I started to admin RHEL based IBM Linux server. How do I determine how much disk space left in my Linux server?

How do I find out how much disk space I have in Linux for each partition?

 

You need to use the df command. It shows the amount of disk space available on the currently mounted file system. df is used to show or find out following information:

Used and available space.

File system mount points.

File system capacity.

The number of inodes available.

Find of whether there is sufficient space to upgrade or install new apps.

Syntax

The basic syntax is as follows:

df

df /path/to/dev

df [options]

df [options] /path/to/dev

Examples

Type the following command:

# df

# df -H

 

Sample outputs:

Fig.01: df command in action

The following example will provide information only for the partition/device that contains the /home directory:

# df /home

# df -h /home

 

To see inode usage instead of block usage, type:

# df -i

# df -i /

# df -ih /

# df -i /dev/md0

 

Sample outputs:

Filesystem            Inodes   IUsed   IFree IUse% Mounted on

/dev/md0             7872512   35813 7836699    1% /

Pass the -T to find out file system type:

# df -T -h

 

Sample outputs:

Filesystem    Type    Size  Used Avail Use% Mounted on

/dev/md0      ext4    119G  1.8G  111G   2% /

tmpfs        tmpfs   1002M     0 1002M   0% /lib/init/rw

udev         tmpfs   1000M  260K 1000M   1% /dev

tmpfs        tmpfs   1002M     0 1002M   0% /dev/shm

/dev/md2      ext4    1.5T  658G  745G  47% /data

/dev/mapper/cryptvg-mybackup

ext3    591G   78G  484G  14% /securebackup

DF COMMAND OPTIONS

From the df(1):

-a, –all             include dummy file systems

-B, –block-size=SIZE  use SIZE-byte blocks

–total           produce a grand total

-h, –human-readable  print sizes in human readable format (e.g., 1K 234M 2G)

-H, –si              likewise, but use powers of 1000 not 1024

-i, –inodes          list inode information instead of block usage

-k                    like –block-size=1K

-l, –local           limit listing to local file systems

–no-sync         do not invoke sync before getting usage info (default)

-P, –portability     use the POSIX output format

–sync            invoke sync before getting usage info

-t, –type=TYPE       limit listing to file systems of type TYPE

-T, –print-type      print file system type

-x, –exclude-type=TYPE   limit listing to file systems not of type TYPE

 

 

]]>
http://wiki.shopingserver.com/linux-find-much-disk-space-left-hard-drive/feed/ 0
Awk Floating Point Number Addition Results Are Unexpected http://wiki.shopingserver.com/awk-floating-point-number-addition-results-unexpected/ http://wiki.shopingserver.com/awk-floating-point-number-addition-results-unexpected/#respond Sat, 06 Jan 2018 10:09:47 +0000 http://wiki.shopingserver.com/?p=18583 I am using awk to grep ‘foo’ from a text file and cacluate sum of field # 7. But, result is rounded to an integer. I need exact result such as 385858.66 and not 385858 using the following command:

grep ‘foo’ 2012-2013.txt | awk ‘BEGIN{ sum=0.0}{ sub(“,”,””,$7); sum +=$7}END{ print “$” sum}’

$682444

I want $682444.57 as output. How can I force “awk” to do floating point math?

 

Floating-point numbers or “real” numbers are that have a fractional part. awk uses double-precision floating-point numbers to represent all numeric values. In other words, all numbers in awk are floating-point numbers i.e. all calculation done using floating-point numbers.

Example: Awk floating point calculation

The following example uses a file called trade.txt, which contains a list of week names as well as four profit values per week:

week1 12.5 12.5 13.5 18.5

week2 11.5 11.10 12.10 13.70

week3 8.5  8.10 8.5 12.5

week4 9.5 11.5 13.5 16.5

week5 8 7 13 17

The following awk program takes the file trade.txt and prints the sum of all four values:

awk  { sum = $2 + $3 + $4 + $5; print $1, sum }  trade.txt

Sample outputs:

week1 57

week2 48.4

week3 37.6

week4 51

week5 45

The following awk program takes the file trade.txt and prints the average of all four values:

awk  { sum = $2 + $3 + $4 + $5; avg = sum/4; print $1, sum , avg}  trade.txt

Sample outputs:

week1 57 14.25

week2 48.4 12.1

week3 37.6 9.4

week4 51 12.75

week5 45 11.25

To avoid surprises use printf to format text to make your output more beautiful and meaningful:

awk  { sum = $2 + $3 + $4 + $5; avg = sum/4; printf  %s: $%.2f ($%05.2f)\n ,$1, sum, avg}  trade.txt

OR

awk  { sum = $2 + $3 + $4 + $5; avg = sum/4; printf  %s: $%.2f ($%5.2f)\n ,$1, sum, avg}  trade.txt

Sample outputs:

week1: $57.00 ($14.25)

week2: $48.40 ($12.10)

week3: $37.60 ($ 9.40)

week4: $51.00 ($12.75)

week5: $45.00 ($11.25)

To fix your problem, replace the following awk code

grep  foo  2012-2013.txt  | awk  BEGIN{ sum=0.0}{ sub( , ,  ,$7); sum +=$7}END{ print  $  sum}

with:

grep  foo  2012-2013.txt  | awk  BEGIN{ sum=0.0}{ sub( , ,  ,$7); sum +=$7}END{  printf  $%.2f\n , sum}

You can skip the grep command and use awk as follows to match and perform sum of all $7:

awk  /foo/{ sub( , ,  ,$7); sum = old + $7; old=sum}END{ printf  $%.2f\n , sum}  2013-2014.txt

Recommended readings

See awk(1) for more info.

 

 

]]>
http://wiki.shopingserver.com/awk-floating-point-number-addition-results-unexpected/feed/ 0
Bash: Display Web Page Content In Terminal http://wiki.shopingserver.com/bash-display-web-page-content-terminal/ http://wiki.shopingserver.com/bash-display-web-page-content-terminal/#respond Sat, 06 Jan 2018 09:59:47 +0000 http://wiki.shopingserver.com/?p=18569 H

ow can I fetch HTML web page content from bash and display on screen using shell utilities?

 

You can use any one of the following tool or combination of all of them to get the contents of a webpage in a shell:

[1] curl command – It is a tool to transfer data from or to a server using http/https/ftp and much more.

[2] lynx command – It is a fully-featured World Wide Web (WWW) client/browser for users running terminals.

[3] wget command – It is a free utility for non-interactive download of files from the Web. It supports HTTP, HTTPS, and FTP protocols, as well as retrieval through HTTP proxies.

[4] w3m command – It is a text based Web browser and pager.

Installation

Above tools may not be installed on your Linux or Unix like operating systems.

Note: You need to login as root user to install required tools.

Debian / Ubuntu Linux install curl, wget, lynx, and w3m

Open a terminal and and then type:

$ sudo apt-get install curl wget lynx w3m

Fedora / RHEL / CentOS Linux install curl, wget, lynx, and w3m

Open a terminal and and then type:

$ sudo yum install curl wget lynx w3m

FreeBSD Unix install curl, wget, lynx, and w3m (binary package)

Open a terminal and and then type:

$ sudo pkg_add -v -r curl lynx w3m wget

Examples

You can use curl command to download the page:

curl http://www.cyberciti.biz/

curl http://www.cyberciti.biz/faq/bash-for-loop/

Use curl and store output into a variable as follows:

page= $(curl http://www.cyberciti.biz/)

page= $(curl http://www.cyberciti.biz/faq/bash-for-loop/)

To display content use echo or printf command as follows:

echo  $page

printf  %s  $page

lynx command examples

Use the lynx command as follows:

lynx -dump www.cyberciti.biz

lynx -dump www.cyberciti.biz/faq/bash-for-loop/

The -dump option dumps the formatted output of the default document or those specified on the command line to standard output. Unlike interactive mode, all documents are processed.

wget command examples

The syntax is as follows:

wget -O – http://www.cyberciti.biz

wget -O – http://www.cyberciti.biz/faq/bash-for-loop/

OR use wget command to grab the page and store it into a variable called page:

page= $(wget -O – http://www.cyberciti.biz)

display page ##

echo  $page

or pass it to lynx / w3m ##

echo  $page  | w3m -dump -T text/html

echo  $page  | lynx -dump -stdin

w3m command examples

The syntax is as follows to dump web page content in terminal using the w3m command:

w3m -dump http://www.cyberciti.biz/

w3m -dump http://www.cyberciti.biz/faq/bash-for-loop/

OR use w3m command to grab the page and store it into a variable called page:

page= $(w3m -dump http://www.cyberciti.biz/)

echo  $page

Practical examples

Get the definition of linux from a dictionary:

$ curl dict://dict.org/d:linux

 

Sample outputs:

220 pan.alephnull.com dictd 1.12.0/rf on Linux 3.0.0-14-server  <21853866.27331.1375614736@pan.alephnull.com>

250 ok

150 1 definitions retrieved

151  linux  wn  WordNet (r) 3.0 (2006)

Linux

n 1: an open-source version of the UNIX operating system

.

250 ok [d/m/c = 1/0/30; 0.000r 0.000u 0.000s]

221 bye [d/m/c = 0/0/0; 0.000r 0.000u 0.000s]

Backup your del.icio.us bookmarks:

$ wget –user=Your-Username-Here –password=Your-Password-Here https://api.del.icio.us/v1/posts/all -O my-old-bookmarks.xml

$ more my-old-bookmarks.xml

 

Grab all .mp3s from url:

mp3=$(lynx -dump http://server1.cyberciti.biz/media/index.html  | grep  http://  | awk  /mp3/{print $2} )

for i in $mp3

wget $i

done

See also

See man pages for more info – curl(1),w3m(1),lynx(1),wget(1)

 

 

]]>
http://wiki.shopingserver.com/bash-display-web-page-content-terminal/feed/ 0
Linux / Unix: Shell Script Find Out In Which Directory Script File Resides http://wiki.shopingserver.com/linux-unix-shell-script-find-directory-script-file-resides/ http://wiki.shopingserver.com/linux-unix-shell-script-find-directory-script-file-resides/#respond Sat, 06 Jan 2018 09:58:13 +0000 http://wiki.shopingserver.com/?p=18567 I need to find out in which directory my bash script resides so that I can read config file called .backup .ignore .target. For example, if my script resides in >/home/foo/script.sh, I need to read /home/foo/.{backup,ignore,target} files.

How do I find out the current directory location and shell script directory location in Bash running on Linux or Unix like operating systems?

 

You can use any one of the following method to find out the portion of pathname:

basename command – Display filename portion of pathname.

dirname command – Display directory portion of pathname.

Bash parameter substitution.

$0 expands to the name of the shell or shell script.

Examples: Shell script find out which directory the script file resides

The following example display directory path or portion of /home/nixcraft/scripts/foo.sh:

dirname /home/nixcraft/scripts/foo.sh

Sample outputs:

/home/nixcraft/scripts

The following line sets the shell variable i to /home/nixcraft/scripts:

i=dirname /home/nixcraft/scripts/foo.sh

echo  $i

OR

i=$(dirname /home/nixcraft/scripts/foo.sh)

echo  $i

In bash script use $0 instead of /home/nixcraft/scripts/foo.sh:

#!/bin/bash

script= $0

basename= $(dirname $script)

 

echo  Script name $script resides in $basename directory.

Sample outputs:

Script name /tmp/test.sh resides in /tmp directory.

Using bash shell ${var%pattern} syntax

To Remove from shortest rear (end) pattern use the following syntax:

var=${path%/*}

For example:

x= /Users/nixcraft/scripts/bar.sh

echo  ${x%/*}

y= ${x%/*}

echo  $y

An updated version of the above script:

#!/bin/bash

# Purpose : Linux / Unix shell script find out which directory this script file resides

# Author : nixCraft <http://www.cyberciti.biz> under GPL v2.x+

# ————————————————————————————-

script= $0

basename= ${script%/*}

config1= ${basename}/.backup

config2= ${basename}/.ignore

config3= ${basename}/.target

 

echo  Script name $script resides in $basename directory.

echo  Reading config file $config1 $config2 $config3, please wait…

Run it as:

$ chmod +x /tmp/test.sh

$ /tmp/test.sh

 

Sample outputs:

Fig.01 Sample run from test.sh

A note about finding physical or real path

You may not get a real physical path and real path may be a symbolic link. To get physical path use realpath command. The realpath command uses the realpath() function to resolve all symbolic links, extra / characters and references to /./ and /../ in path. This is useful for shell scripting and security related applications.

Another recommended option is to use the readlink command to display value of a symbolic link or canonical file name:

#!/bin/bash

# Purpose : Linux / Unix shell script find out which directory this script file resides

# Author : nixCraft <http://www.cyberciti.biz> under GPL v2.x+

# ————————————————————————————-

 

Who am i? ##

Get real path ##

_script= $(readlink -f ${BASH_SOURCE[0]})

 

Delete last component from $_script ##

_mydir= $(dirname $_script)

 

Delete /path/to/dir/ component from $_script ##

_myfile= $(basename $_script)

echo  Script : $_script

echo  Directory portion of $_script : $_mydir

echo  Filename portion of $_script : $_myfile

Save and close the file. Run it as follows:

./demo.bash

cd /home/vivek/

../../tmp/demo.bash

/tmp/demo.bash

Sample outputs:

Fig.02: Finding real path

See also

See man pages for more info – basename(1), dirname(1), bash(1)

 

 

]]>
http://wiki.shopingserver.com/linux-unix-shell-script-find-directory-script-file-resides/feed/ 0
Linux / Unix: Sort ls Command Output By Last Modified Date and Time http://wiki.shopingserver.com/linux-unix-sort-ls-command-output-last-modified-date-time/ http://wiki.shopingserver.com/linux-unix-sort-ls-command-output-last-modified-date-time/#respond Sat, 06 Jan 2018 09:48:54 +0000 http://wiki.shopingserver.com/?p=18555 I

download lots of files in ~/Downloads/ folder in Ubuntu Linux and OS X desktop. How do I show last downloaded file first using the ls command? How do I sort the output of ls command by last modified date?

 

You need to pass the -t option to the ls command. The -t option sort by time modified i.e. most recently modified first before sorting the operands by lexicographical order. In other words, last downloaded file can be displayed using the following command. Open the Terminal application and type the following command.

Syntax

The syntax is:

ls -t

ls -lt | less

ls -lt ~/Downloads/ | less

 

Sample outputs:

total 60754328

drwxr-xr-x  3 vivek  staff         102 Aug 25 13:18 ImageOptim.app

-rw-r–r–@ 1 vivek  staff   301746331 Aug 25 01:25 data-db2-sample.rar

-rw-r–r–@ 1 vivek  staff     1727030 Aug 25 01:14 testdisk-6.14.mac_intel.tar.tar.bz2

-rw-r–r–@ 1 vivek  staff       23850 Aug 24 22:36 english-68.zip

-rw-r–r–@ 1 vivek  staff       72488 Aug 24 22:05 36363537dkgpd.pdf

drwxr-xr-x@ 3 vivek  staff         170 Aug 24 19:58 backups

drwxr-xr-x@ 4 vivek  staff         306 Aug 24 19:56 tarballs

-rw——-@ 1 vivek  staff       39748 Aug 24 13:22 Account-xyz.pdf

-rw——-@ 1 vivek  staff       35583 Aug 24 13:21 Portfolio-Update-FY13-14.pdf

-rw——-@ 1 vivek  staff      141695 Aug 24 01:26 13290.pdf

drwxr-xr-x@ 2 vivek  staff         136 Aug 24 00:32 hd-video-raw-files

-rw-r–r–@ 1 vivek  staff  1359349025 Aug 23 21:04 youtube-sample.mp4

drwxr-xr-x@ 2 vivek  staff         170 Aug 23 21:03 delme

-rw-r–r–@ 1 vivek  staff      120587 Aug 23 19:00 Screenshot-System-Monitor.png

-rw-r–r–@ 1 vivek  staff       23301 Aug 22 13:21 sad-tux.png

….

..

….

Pass the -r option to reverse the order of the sort to get reverse lexicographical order or the oldest entries first (or largest files last, if combined with sort by size), enter:

ls -tr

ls -ltr | less

ls -ltr ~/Downloads/ | less

 

Sample outputs:

total 60754328

-rwxr-xr-x@ 1 vivek  staff      115262 Jan  1  1970 P4.pdf

-rw-r–r–  1 vivek  staff      135734 Sep 16  2011 game.idx

-rw-r–r–  1 vivek  staff       51111 Sep 16  2011 game.English.srt

-rwxr-xr-x@ 1 vivek  staff      331201 Sep  6  2012 Portfolio_Recommendations_Comprehensive_Sep12.pdf

-rw-r–r–  1 vivek  staff   301746245 Sep 25  2012 hp-ux-to-rhel6-guide.pdf

-rwxr-xr-x@ 1 vivek  staff       13350 Mar 13 16:44 DC-location-Towns.xlsx

-rw-r–r–@ 1 vivek  staff   574423040 Jun 10 16:33 data-center-opening.avi

-rw-r–r–@ 1 vivek  staff     2375468 Jun 15 05:04 backups.rar

-rw-r–r–  1 vivek  staff     2631709 Jun 15 05:07 last-good-know-backups.pdf

….

..

Sort by modification time, newest first and other options

Make sure you pass the -A (list all entries except for . and ..) or -a (include directory entries whose names begin with a dot) option to see hidden files:

ls -Altr ~/Downloads/ | less

ls -alt ~/Downloads/ | less

The following option turned on human readable output:

$ ls -halt

$ ls -halt | more

 

Sample outputs:

Fig.01: Print human readable sizes when used with the -l/-s option

Recommended readings

See ls(1) for more information.

 

 

]]>
http://wiki.shopingserver.com/linux-unix-sort-ls-command-output-last-modified-date-time/feed/ 0
Mac OS X: Set / Change $PATH Variable http://wiki.shopingserver.com/mac-os-x-set-change-path-variable/ http://wiki.shopingserver.com/mac-os-x-set-change-path-variable/#respond Sat, 06 Jan 2018 09:45:50 +0000 http://wiki.shopingserver.com/?p=18551 I

need to add dev tools (such as JDK and friends) to my PATH. How do I change $PATH variable in OS X 10.8.x? Where does $PATH get set in OS X 10.8 Mountain Lion?

 

$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

Sample outputs:

Fig.01: Displaying the current $PATH settings using echo / printf on OS X

OS X: Change your PATH environment variable

You can add path to any one of the following method:

$HOME/.bash_profile file using export syntax.

/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

Use $HOME/.bash_profile file when you need to generate the PATH variable for a single user account.

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.

See also:

Customize the bash shell environments from the Linux shell scripting wiki.

UNIX: Set Environment Variable

Man pages – bash(1), path_helper(8)

 

 

]]>
http://wiki.shopingserver.com/mac-os-x-set-change-path-variable/feed/ 0
OpenSSH Config File Examples http://wiki.shopingserver.com/openssh-config-file-examples-2/ http://wiki.shopingserver.com/openssh-config-file-examples-2/#respond Sat, 06 Jan 2018 09:39:40 +0000 http://wiki.shopingserver.com/?p=18545 H

ow do I create and setup an OpenSSH config file to create shortcuts for servers I frequently access under Linux or Unix desktop operating systems?

 

A global or local configuration file for SSH client can create shortcuts for sshd server including advanced ssh client options. You can configure your OpenSSH ssh client using various files as follows to save time and typing frequently used ssh client command line options such as port, user, hostname, identity-file and much more:

System-wide SSH client configuration files

/etc/ssh/ssh_config : This files set the default configuration for all users of OpenSSH clients on that desktop/laptop and it must be readable by all users on the system.

User-specific SSH client configuration files

~/.ssh/config or $HOME/.ssh/config : This is user’s own configuration file which, overrides the settings in the global client configuration file, /etc/ssh/ssh_config.

~/.ssh/config file rules

The rules are as follows to create an ssh config file:

You need to edit ~/.ssh/config with a text editor such as vi.

One config parameter per line is allowed in the configuration file with the parameter name followed by its value or values. The syntax is:

config value

config1 value1 value2

You can use an equal sign (=) instead of whitespace between the parameter name and the values.

config=value

config1=value1 value2

All empty lines are ignored.

All lines starting with the hash (#) are ignored.

All values are case-sensitive, but parameter names are not.

Tip : If this is a brand new Linux, Apple OS X/Unix box, or if you have never used ssh before create the ~/.ssh/ directory first using the following syntax:

mkdir -p $HOME/.ssh

chmod 0700 $HOME/.ssh

Examples

For demonstration purpose my sample setup is as follows:

Local desktop client – Apple OS X or Ubuntu Linux.

Remote Unix server – OpenBSD server running latest OpenSSH server.

Remote OpenSSH server ip/host: 75.126.153.206 (server1.cyberciti.biz)

Remote OpenSSH server user: nixcraft

Remote OpenSSH port: 4242

Local ssh private key file path : /nfs/shared/users/nixcraft/keys/server1/id_rsa

Based upon the above information my ssh command is as follows:

$ ssh -i /nfs/shared/users/nixcraft/keys/server1/id_rsa -p 4242 nixcraft@server1.cyberciti.biz

 

OR

$ ssh -i /nfs/shared/users/nixcraft/keys/server1/id_rsa -p 4242 -l nixcraft server1.cyberciti.biz

You can avoid typing all of the ssh command parameters while logging into a remote machine and/or for executing commands on a remote machine. All you have to do is create an ssh config file. Open the Terminal application and create your config file by typing the following command:

edit file in $HOME dir

 

vi ~/.ssh/config

OR

edit file in $HOME dir

 

vi $HOME/.ssh/config

Add/Append the following config option for a shortcut to server1 as per our sample setup:

Host server1

HostName server1.cyberciti.biz

User nixcraft

Port 4242

IdentityFile /nfs/shared/users/nixcraft/keys/server1/id_rsa

Save and close the file. To open your new SSH session to server1.cyberciti.biz by typing the following command:

$ ssh server1

Adding another host

Append the following to your ~/.ssh/config file:

Host nas01

HostName 192.168.1.100

User root

IdentityFile ~/.ssh/nas01.key

You can simply type:

$ ssh nas01

Putting it all together

Here is my sample ~/.ssh/config file that explains and create, design, and evaluate different needs for remote access using ssh client:

default for all ##

Host *

ForwardAgent no

ForwardX11 no

ForwardX11Trusted yes

User nixcraft

Port 22

Protocol 2

ServerAliveInterval 60

ServerAliveCountMax 30

 

override as per host ##

Host server1

HostName server1.cyberciti.biz

User nixcraft

Port 4242

IdentityFile /nfs/shared/users/nixcraft/keys/server1/id_rsa

 

Home nas server ##

Host nas01

HostName 192.168.1.100

User root

IdentityFile ~/.ssh/nas01.key

 

Login AWS Cloud ##

Host aws.apache

HostName 1.2.3.4

User wwwdata

IdentityFile ~/.ssh/aws.apache.key

 

Login to internal lan server at 192.168.0.251 via our public uk office ssh based gateway using ##

$ ssh uk.gw.lan ##

Host uk.gw.lan uk.lan

HostName 192.168.0.251

User nixcraft

ProxyCommand  ssh nixcraft@gateway.uk.cyberciti.biz nc %h %p 2> /dev/null

 

Our Us Proxy Server ##

Forward all local port 3128 traffic to port 3128 on the remote vps1.cyberciti.biz server ##

$ ssh -f -N  proxyus ##

Host proxyus

HostName vps1.cyberciti.biz

User breakfree

IdentityFile ~/.ssh/vps1.cyberciti.biz.key

LocalForward 3128 127.0.0.1:3128

Understanding ~/.ssh/config entries

Host : Defines for which host or hosts the configuration section applies. The section ends with a new Host section or the end of the file. A single * as a pattern can be used to provide global defaults for all hosts.

HostName : Specifies the real host name to log into. Numeric IP addresses are also permitted.

User : Defines the username for the SSH connection.

IdentityFile : Specifies a file from which the user’s DSA, ECDSA or DSA authentication identity is read. The default is ~/.ssh/identity for protocol version 1, and ~/.ssh/id_dsa, ~/.ssh/id_ecdsa and ~/.ssh/id_rsa for protocol version 2.

ProxyCommand : Specifies the command to use to connect to the server. The command string extends to the end of the line, and is executed with the user’s shell. In the command string, any occurrence of %h will be substituted by the host name to connect, %p by the port, and %r by the remote user name. The command can be basically anything, and should read from its standard input and write to its standard output. This directive is useful in conjunction with nc(1) and its proxy support. For example, the following directive would connect via an HTTP proxy at 192.1.0.253:

ProxyCommand /usr/bin/nc -X connect -x 192.1.0.253:3128 %h %p

LocalForward : Specifies that a TCP port on the local machine be forwarded over the secure channel to the specified host and port from the remote machine. The first argument must be [bind_address:]port and the second argument must be host:hostport.

Port : Specifies the port number to connect on the remote host.

Protocol : Specifies the protocol versions ssh(1) should support in order of preference. The possible values are 1 and 2.

ServerAliveInterval : Sets a timeout interval in seconds after which if no data has been received from the server, ssh(1) will send a message through the encrypted channel to request a response from the server. See blogpost “Open SSH Server connection drops out after few or N minutes of inactivity” for more information.

ServerAliveCountMax : Sets the number of server alive messages which may be sent without ssh(1) receiving any messages back from the server. If this threshold is reached while server alive messages are being sent, ssh will disconnect from the server, terminating the session.

Speed up ssh session

Multiplexing is nothing but send more than one ssh connection over a single connection. OpenSSH can reuse an existing TCP connection for multiple concurrent SSH sessions. This results into reduction of the overhead of creating new TCP connections. Update your ~/.ssh/config:

Host server1

HostName server1.cyberciti.biz

ControlPath ~/.ssh/controlmasters/%r@%h:%p

ControlMaster auto

See “Linux / Unix: OpenSSH Multiplexer To Speed Up OpenSSH Connections” for more info. In this example, I go through one host to reach another server i.e. jump host using ProxyCommand:

~/.ssh/config ##

Host internal

HostName 192.168.1.100

User vivek

ProxyCommand ssh vivek@vpn.nixcraft.net.in -W %h:%p

ControlPath ~/.ssh/controlmasters/%r@%h:%p

ControlMaster auto

For more info see following tutorials:

How To Reuse SSH Connection To Speed Up Remote Login Process Using Multiplexing

How To Setup SSH Keys on a Linux / Unix System

A note about shell aliases (outdated method)

WARNING! This bash shell aliased based setup may work out for you. However, I recommend that you use ~/.ssh/config file for better results in a long run. SSH config file is more advanced and elegant solutions. The alias command only used here for demo purpose and it is here due to historical reasons.

An alias is nothing but shortcut to commands and you can create the alias use the following syntax in your ~/.bashrc file:

create a new bash shell alias as follow ##

 

alias server1= ssh -i /nfs/shared/users/nixcraft/keys/server1/id_rsa -p 4242 nixcraft@server1.cyberciti.biz

Then, to ssh into the server1, instead of typing full ssh -i /nfs/shared/users/nixcraft/keys/server1/id_rsa -p 4242 nixcraft@server1.cyberciti.biz command, you would only have to type the command ‘server1’ and press the [ENTER] key:

$ server1

References

See ssh_config(5) for more information on syntax and some of the other available options.

Top 20 OpenSSH Server Best Security Practices

 

 

]]>
http://wiki.shopingserver.com/openssh-config-file-examples-2/feed/ 0