2016年5月29日 星期日

linux 加入PATH 環境變數

http://linux.vbird.org/linux_basic/0320bash.php#settings

http://www.linuxfromscratch.org/blfs/view/svn/postlfs/profile.html


fast.

修改 ~/.bashrc 或 ~/.profile
  1. 若該變數為擴增變數內容時,則可用 "$變數名稱" 或 ${變數} 累加內容,如下所示:
    『PATH="$PATH":/home/bin』或『PATH=${PATH}:/home/bin』
  2. 若該變數需要在其他子程序執行,則需要以 export 來使變數變成環境變數
    『export PATH』

ex.

vim ~/.bashrc
or
vim ~/.profile

加入以下

# User specific environment and startup programs
PATH=${PATH}:/opt/bin
export PATH


2016年5月26日 星期四

NAS learn



開啟ssh PubkeyAuthentication 服務
ref.https://www.chainsawonatireswing.com/2012/01/15/ssh-into-your-synology-diskstation-with-ssh-keys//?from=@


To start the process, you need to edit the SSH daemon’s config file to allow access via keys.

Edit/etc/ssh/sshd_config using vim & change these lines:

#RSAAuthentication yes 
#PubkeyAuthentication yes 
#AuthorizedKeysFile .ssh/authorized_keys

To this:

RSAAuthentication yes 
PubkeyAuthentication yes 
AuthorizedKeysFile .ssh/authorized_keys


Now get your permissions set correctly on that directory & file:> chmod 700 .ssh

> chmod 600 .ssh/authorized_keys

> chmod 700 .ssh/

再來是client 端 ,基本的概念就是使用private key 去開在遠端server public key

生成privatekey 和publickey
#ssh-keygen 

copy 到遠端的~/.ssh下
#scp id_rsa.pub username@serverip:~/.ssh/
登入遠端server
ssh username@serverip

將public key 加入.ssh/authorized_keys  (我的cat 曾經動過手腳 alias 成 cat -n 害我卡了一整個晚上  哭哭)
cat .ssh/id_rsa.pub >> .ssh/authorized_keys 

這樣子就可以 key 認證登入, 不需輸入密碼.


重開遠端server ssh
Restarted sshd via synoservicectl --restart sshd and by restarting whole NAS.
or
rebooot

2016年5月24日 星期二

linux server 記事 ubuntu 16.04LTS

家裡退休的筆電閒了下了,因此打算架個 linux server 來玩玩
在這邊紀錄一下架設日誌,過程,供日後參考。

安裝版本為 ubuntu 16.04 LTS desktop x64 english



*更新軟體
apt-get update
apt-get upgrade

*備份
rsync + ssh + crontab

1.先處理免密碼登入的 RSA key

2.編輯vim backup_script.ssh 內容只有一行
rsync -av /* username@serverip::NetBackup/ngulinux/ --rsh='ssh -i userPrivateKey' --exclude={/dev/*,/home/username/.cache/*,/proc/*,/sys/*,/tmp/*,/run/*,/mnt/*,/media/*,/lost+found}

不知道為何我上面的指令可以直接 單run script 執行沒問題,但是加到crontab時 ㄍ--exclude 指令沒起到作用,因此只好把此指令拔掉將除外的檔案塞到額外的檔案,作法大概是
rsync -av /* username@serverip::NetBackup/ngulinux/ --exclude-from='/root/rsync_bk/excludeList'  --rsh='ssh -i userPrivateKey' -

然後建立一個檔案叫 excludeList 在路徑上
檔案的寫法大概是
/dev/
/proc/
/sys/
/tmp/
/run/
/mnt/
/media/
/lost+foun/
.cache/
google-chrome/
/var/tmp/
/var/log/

大概說明一下
/dev/    代表略過此目錄(連目錄都不產生)
/dev/*  複製此目錄但是略過此目錄下所有檔案
dev/     所有檔案或目錄的名稱的任意位置包含 dev 的都略過

3.將backup_script.ssh 加進crontab
vim /etc/crontab

* * * * * root /bin/sh /cronfiles/rsync.sh
(分 時 日月 周) (執行者) (執行shell) (script)

10 5    * * *   root    /root/rsync_bk/rsync.sh >> /var/log/rsync.log 2>&1

收割
#/etc/init.d/cron restart


*遠端桌面
1.anydesk  http://anydesk.com/remote-desktop
2.teamviewer  https://www.teamviewer.com/
3.VNC xrdp
ref.http://programingman.blogspot.tw/search/label/raspberry

*文字介面開機

vim /etc/default/grub

修改成以下參數

#GRUB_HIDDEN_TIMEOUT=0
#GRUB_HIDDEN_TIMEOUT_QUIET=true
GRUB_TIMEOUT=3
GRUB_CMDLINE_LINUX_DEFAULT="text"
GRUB_CMDLINE_LINUX="text"
GRUB_TERMINAL=console

開啟多人模式
sudo systemctl enable multi-user.target --force 
sudo systemctl set-default multi-user.target
更新
update-grub

若要開啟x-window 不知道為何我的 startx 無效
需要
service lightdm restart
or 
 /etc/init.d/lightdm restart
才可以再開啟x-window

到此告一段落,我做出的效果差不多就是開機為tty1 文字模式
遠端可以隨時xrdp 回來x-window (使用Windows),當server 有問題時也可以使用x-window 來控制

在這邊不得不推崇一下 Teamviewer,發現使用teamviewer 設定好後不用登入到 x-window 畫面
在TTY1 純文字下就可以連進來下 command 不知道怎麼做的,有點厲害
若在TTY1 下使用 service lightdm restart ,畫面也會自動轉為x-window , 是個蠻棒的環境

所以在我的環境下
1.可以使用xrdp 直接遠端進我的 Sever 桌面 (不需要 service lightdm restart)
2.可以使用teamviewer 進來下 service lightdm restart 後直接轉成我的桌面
3.SSH 近來我的server

完成,媽呀 因為我的是筆電,每次重開機時只要系統偵測到是蓋著的就自動休眠

# 按下電源 預設行為為關機
HandlePowerKey=poweroff
 # 按下暫停
 HandleSuspendKey=suspend 
# 按下休眠 
HandleHibernateKey=hibernate
 # 闔上螢幕
# HandleLidSwitch=suspend
HandleLidSwitch=ignore

vim /etc/systemd/logind.conf

*必備軟體
sudo apt-get install vim
sudo apt-get install tmux

chewing
upnpc
openssh-server

*wake-on-Lan 網路喚醒

#ethtool interfaceName
查看 這兩個參數
Supports Wake-on: pg
Wake-on: d

d為disable 的意思  g為magic packet 可以喚醒的意思

使用指令將 Wake-on 改為 g

#ethtool -s eth0 wol g

之後就可以關機試試看效果了
另一台電腦使用
wakelan or wakeonlan 之類的指令試試看可不可以喚醒

wakeonlan YOUR_TARGET_MAC -b 192.168.0.255

-b 為指定brocast packet  因為 中間的router or switch 若因為arp timeout 有可能default 的行為不是flooding 而是drop 使用brocast packet 是比較保險的做法

其中ethtool -s eth0 wol g 只有一次性,因此需要用你所想的道的方式設定他always on
ex.加入排程 crontab

way2. 使用nmcli 指令設定
# nmcli con show
NAME    UUID                                  TYPE            DEVICE
enp9s0  e33fd8e4-bf42-4652-8a85-55e4ebc49d24  802-3-ethernet  enp9s0

查看wol 是否有開啟
# nmcli c show "enp9s0" | grep 802-3-ethernet.wake-on-lan
802-3-ethernet.wake-on-lan:             80 (broadcast, magic)
802-3-ethernet.wake-on-lan-password:    --

修改成接收broadcast and magic paacket 
nmcli c modify "enp9s0" 802-3-ethernet.wake-on-lan magic,broadcast

(後記:在我的環境兩種方式都設定過了還是不work ,發包的magic packet 沒有錯誤,另一台電腦可以順立wol,因此猜測應該是硬體支援的問題,
先暫時放棄此功能,原本想法是有要連進來再利用gateway 將server 喚醒避免不必要的耗電,畢竟當server 是不太會關機的)


*sudo 免密碼

因為常常在 sudo 我的密碼又很大一串,所以索性用個免密碼sudo 這樣就不用先轉成 root 在做事

修改方式大概為

(舊版方式)
在username最後的一個ALL 前加入 NOPASSWD
username ALL=(ALL) NOPASSWD: ALL
username ALL=(ALL:ALL) NOPASSWD: ALL

上面兩種用法就看你的系統是用哪種語法跟著原本的方式寫就是了

注意若此檔修改錯誤會造成無法使用 sudo 指令,因此語法要注意一下
最好的方式是使用visudo 指令修改,會幫你檢查與法有無錯誤

更新 新版方式更安全些寫在/etc/sudoers.d

vi /etc/sudoers.d/nopasswd4sudo
输入 
yourusername ALL=(ALL) NOPASSWD : ALL
ESC :wq!




*SAMBA 參考 (點我)

但是瑞凡  我失敗了,不知道為何 在我的Raspberry mate 15.04 是成功的 在16.04失敗,也不知從何Debug 起 ,先擱著好了。

# Un-comment the following (and tweak the other settings below to suit)
# to enable the default home directory shares. This will share each
# user's home directory as \\server\username
[homes]
comment = Home Directories
browseable = yes

結果是這段的 [homes] 沒有打開,ㄍㄋㄋ  搞了我一個晚上