網頁

2017年10月17日 星期二

對於使用Linux的人來說終端機(terminal)是不可或缺的工具,今天介紹的 Guake Terminal 和一般的 terminal 最大的不同就是它會定在螢幕的上方,按 F12 就會顯示出來,非常方便。 guake-1.jpg
安裝方式非常簡單: sudo apt-get install guake sudo apt-get update 安裝後如果希望在登入後就啟動常駐,在LXDE桌面系統下可以執行:
sudo ln -s /usr/share/applications/guake.desktop /etc/xdg/autostart/ 

更改設定可以在視窗西岸的情況下按下滑鼠右鍵,選擇 Preferences :
guake-2.jpg

可以修改一些設定或是自訂快捷鍵: guake-3.jpg

2017年10月15日 星期日

Virtual Box 設定分享資料夾 (Ubuntu)

這裡示範在 Mac OSX 中的 VirtualBox 如何設定 Guest OS 為 Ubuntu Linux的分享資料夾。也就是在VirtualBox裡的Ubuntu可以存取MacOSX的資料夾。

1.首先安裝VBOXADDITION,到VB的 Device 選單選擇Insert Guest Addition CD image ,之後會掛載一個CD Image,打開後執行autorun.sh。

vb_share-3.jpg

2. 然後設定 Host OS 中要分享給Guest OS的分享資料夾,從「Device」選單中開啟「Shared Folders」設定視窗。點選右方的「Add Shared Folder」圖示。選擇要分享的資料夾路徑與名稱,另外把「Auto-mount」與「Make Permanent」兩個選項也勾起來。vb_share-1.jpg 3. 確認一下路徑是否正確,然後按下「OK」。這樣在 Host OS 中 VirtualBox 這邊的設定就完成了,接下來是 Guest OS (Ubuntu)中的設定。vb_share-2.jpg 4. 在Ubuntu中(在 Guest OS 中)修改 /etc/group,將所有需要用到分享資料夾的帳號加入 vboxsf 群組。首先找尋「vboxsf」這個群組,正常來說應該會像這樣:vboxsf : x : 999 : 然後加上我的帳號名字jkchang,如果要加入多個帳號,就用逗號分隔。

vb_share-4.jpg

或是下指令: sudo usermod -a -G vboxsf username

5. 重新啟動Ubuntu Linux。
6. 開機後,分享的資料夾會掛載在 /media/sf_linux_share,如果想在桌面上建立一個捷徑,可以下指令: ln -s /media/sf_linux_share ~/Desktop

如此桌面上就可以看到OSX分享的資料夾了。vb_share-5_800.jpg

你可以參考: 1. VMware Ubuntu 與 OSX 共享資料夾

 

2017年3月10日 星期五

SSH 登入免密碼

自從兒子上大學後免不了要連線到學校協助解決一些"課業"上的問題,既然是用連線那就需要用到SSH,但每次SSH登入都需要輸入密碼,對於我這懶人來說是有點麻煩,(這學校密碼錯誤太多次 ip會被鎖住) 所以如何可以快速登入呢? 這裡介紹一個方式就是在Client端產生一組金鑰,然後把把公開金鑰複製到Server的信任名單中,如此一來client端在連線的時候Server端會檢查這組金鑰如果符合自然就不需要密碼了。
要連接SSH的 client端(本機)需要產生一組主機使用的金鑰組。這組金鑰可以在 ~/.ssh 中找到,檔名是 id_rsa (私鑰),id_rsa.pub (公鑰),一般來說公鑰的檔名像是這樣:
~/.ssh/id_rsa.pub
把公鑰複製到server端的信任名單,通常會是這樣:
~/.ssh/authorized_keys

接下來就示範如何操作
1. 在client端產生金鑰 輸入下面指令 ssh-keygen -t rsa 參數 -t 是指定密碼類型,如果沒有則內定是SSH-2的RSA "Enter passphrase"的地方請直接壓enter,這樣才能不用輸入密碼。 過程中會顯示金鑰的指紋(fingerprint)與 randomart。 RSA-01.jpg
到~./ssh 目錄下查看,會產生兩個檔案分別是id_rsa 和 id_rsa.pub, id_rsa是私鑰(private key): 這是要保護好的金鑰,它等同於你的密碼,放在自己的電腦中。 id_rsa.pub是公鑰(public key): 這是可以對外公開的金鑰,之後要將它放在遠端的 Linux 伺服器上作認證使用。 RSA-2.jpg

2. 接著把公鑰的內容放在Server的信任清單中 在 client端下以下指令,金鑰就會放進Server的信任清單中了。
cat ~/.ssh/id_rsa.pub | ssh server-user@server-host “cat >> ~/.ssh/authorized_keys"

也可以用 ssh-copy-id 的方式直接將金鑰複製到 server,它預設會將 ~/.ssh/id_rsa.pub 這個公開金鑰複製到伺服器上 ,輸入 ssh-copy-id User@Host
若要指定使用的金鑰,可以使用 -i 參數:
ssh-copy-id -i ~/.ssh/id_rsa.pub User@Host

以上兩個步驟做完後下次登入這台SSH主機就不用輸入密碼囉。

補充一下:
Server端的公鑰原始是有寫入權限的,這樣會蠻不安全的,建議可將authorized_keys設定成唯讀。
RSA-3.jpg





2016年12月30日 星期五

UNIX入門課程1051_ex9

http://erdos.csie.ncnu.edu.tw/~klim/unix-intro/unix-intro-1051.html

作業九: figure out the average for each user(awk programming)
與作業七相同, 但改用 awk 來作. input 的 data 中的 name 可能會重複出現.
$ cat ex9-k1
klim 10 20 milk 199 joe 20
# klim is appeared again!
oak 100 red 200 10 klim 30
joe 199  marry 20 julie 2000  #joe joe
$ ./ex9.awk < ex9-k1
klim = 20.00
milk = 199.00
oak = 100.00
red = 105.00
joe = 109.50
marry = 20.00
julie = 2000.00
$

解答:
#!/usr/bin/awk -f
BEGIN {
}
{
  sub(/#.*/,"")
  if ( $0 != "" ) {
    for (i=1; i<=NF; i++) {
      if ( $i ~ /[a-z]/ ) {  # $1 is name
        name=$i
      }
      else {                 # $1 is number
        array[name]=array[name]+$i
        num_count[name]=num_count[name]+1
      }
    } #for i END
  } #if END
}
END {
for (i in array)
  printf("%s = %.2f\n",i, array[i]/num_count[i])
}
執行結果:


 

2016年12月23日 星期五

UNIX入門課程1051_ex8

http://erdos.csie.ncnu.edu.tw/~klim/unix-intro/unix-intro-1051.html

作業八: figure out the average and the max three (awk programming)
跟作業六一樣, 但改用 awk 來寫.
請全用 awk 來寫, 不要混用 sh 或 sed.
請寫成一個 script file, 並設定成可執行.
input 的 data 內可能有 comment.

$ cat k1
10 20 30 #this is a comment
8 7
# this is also a comment
2
$ ./ex8.awk k1
the max three are: 30 20 10
the average is   : 12.83
$

解答:

#!/usr/bin/awk -f
BEGIN {
}
{
  sub(/#.*/ , "")
  if ( $0 != "" ) {
    for (i=1; i<=NF; i++) {
      num[count]=$i
      sum+=$i
      count++
    } #for END
  } #if END
}
 
END {
  asort(num) #sort num[] array
  printf("the max three are: %d %d %d\n",num[count],num[count-1],num[count-2])
  printf("the average is   : %.2f\n",sum/count)
} 

注意:程式中asort函數在GNU awk 3.1才有提供


執行結果:



2016年12月16日 星期五

UNIX入門課程1051_ex7

http://erdos.csie.ncnu.edu.tw/~klim/unix-intro/unix-intro-1051.html

作業七: figure out the average for each user(sh programming)
$ cat ex7-k1
klim 10 20 30 milk 199 
oak 100 red 200 10
joe 199 20 marry 20 julie 2000
$ ./ex7.sh < ex7-k1
klim = 20.00
milk = 199.00
oak = 100.00
red = 105.00
joe = 109.50
marry = 20.00
julie = 2000.00
$

解答:

#!/bin/bash
function AVERAGE () {
  avg=$(echo "scale=2; $2/$3" | bc)
  echo "$1 = $avg"
}
function DATA () {
  #if [[ $1 != [0-9]*  &&  -n $1 ]]; then
  if [[ $1 =~ [a-z]+  &&  -n $1 ]]; then
    #echo "name:$1"
    name[index]=$1
    let index+=3
    sum=0
    count=0
  else
    let sum=sum+$1
    if [ ! -z $1 ]; then
      let count++
    fi
      name[index-2]=$sum
      name[index-1]=$count
  fi 
}
index=1
while read line
do
  if [ "$line" != "" ]; then
    set $(echo $line)
    field=$#    #*** $# is field count
    for ((i=1; i<=field; i++))
    do
      #echo "data:$1"
      DATA $1
      shift
    done
  fi
done #while END
for ((i=1; i<=index-3; i=i+3))
do
  #echo "Name:${name[$i]}  sum=${name[i+1]}  count=${name[i+2]}"
  AVERAGE ${name[$i]} ${name[i+1]} ${name[i+2]}
done

執行結果:




2016年12月6日 星期二

UNIX入門課程1051_ex6

http://erdos.csie.ncnu.edu.tw/~klim/unix-intro/unix-intro-1051.html

作業六: figure out the average and the max three (sh programming)
寫一個 sh 的 program, 算出所有數字的平均及最大的三個數
數字從檔案讀入(若有給檔名的話), 或是從 keyboard 讀入(即 stdin, 若沒給檔名的話).
平均數要算到小數第二位.
若數字的個數小於三個, 則補 0.

$ cat k1
10 20 30
8 7
2
$ ./ex6.sh k1
the max three are: 30 20 10
the average is   : 12.83
$ cat k2
888 2
30
11 222
$ ./ex6.sh k1 k2
the max three are: 888 222 30
the average is   : 111.81
$ ./ex6.sh
1 2 3
4
5 6
the max three are: 6 5 4
the average is   : 3.50
$


解答:
#!/bin/bash
#calc average function
function AVERAGE () {
    avg=$(echo "scale=2; $1/$2" | bc )
    #echo "the average is   : $avg"
}
#bubble sort function
function SORT() {
    for ((i=0; i<count; i++))
    do
        for ((j=i; j<count; j++))
        do
            if [ ${data[j]} -gt ${data[i]} ]; then
                let temp=data[j]
                let data[j]=data[i]
                let data[i]=temp
            fi
        done
    done
}

if [ -z $1 ]; then  #manual input
  while read line
  do
    if [ "$line" != "" ]; then
      #set $(echo $line | tr : ' ')
      set $(echo $line)
      field=$#
      for ((i=1; i<=field; i++))
      do
        data[count]=$1
        let sum+=$1
        let count+=1
        shift
      done
    else
      break
    fi
  done<&0  
else  #file(s) input
  total_file=$#
  for ((i=1; i<=$total_file; i++)) #file name put in f[] array
  do
    f[i]=$1
    shift
  done
  for ((j=1; j<=$total_file; j++))
  do
    exec<"${f[j]}"
    while read line
    do
      set $(echo $line)
      field=$#
      for ((i=1; i<=field; i++))
      do
        data[count]=$1
        let sum+=$1
        let count+=1
        shift
      done
    done
  done #for END
fi
SORT #Run Bubble sort function
echo "the max three are: ${data[0]} ${data[1]} ${data[2]}"
AVERAGE $sum $count #Run calc average function
echo "the average is   : $avg"
執行結果: