Showing posts with label ssh. Show all posts
Showing posts with label ssh. Show all posts

Wednesday, March 16, 2016

Remote ssh โดยกุญแจเข้ารหัส ไม่ต้องถาม password

การทำสคริป copy file ผ่าน ssh โดยกุญแจเข้ารหัส (ไม่ต้องถาม password)


วัตถุประสงค์
            ต้องการทำสคริปเพื่อก็อปปี้ไฟล์จากเครื่อง Server ไปเก็บไว้ยัง Backup Server ถ้าเราทำแบบแมนนวล เวลารันคำสคิป ก็ต้องคีย์ password ssh แต่ถ้าไม่อยากคีย์ก็ทำเป็น Text file ไว้แล้วให้สคิปไปเช็ค password กับ Text file นั้น
            การทำแบบนี้มีโอกาสโดนขโมยอ่าน password ได้ ดังนั้น จึงใช้วิธีการ Encryption password ไว้ ในรูปแบบกุญแจ 2 ลูก ลูกนึงวางไว้ที่เครื่อง Server ต้นทาง และอีกลูกนึงวางไว้ที่ Backup Server

วิธีการ
1. Key Encryption การทำกุญแจเข้ารหัสพาสเวิร์ด มีขั้นตอนดังนี้

เครื่อง Server

ใช้รูปแบบคำสั่ง ด้านล่าง
#ssh-keygen -t rsa -b 4096  ค่าปกติ 2048 bit

จะมี 2 ไฟล์  Generate ขึ้นมา ที่ /root/.ssh/ ดูภาพด้านล่าง



id_rsa
id_rsa.pub

เสร็จแล้วให้ก็อปปี้ไฟล์ id_rsa.pub ไปยังเครื่อง Backup Server ด้วยคำสั่ง
scp id_rsa.pub user@192.168.x.x:/path/backup

เครื่อง Backup Server

เข้าไปยัง path ที่เก็บไฟล์ id_rsa.pub แล้ว convert ไฟล์ไปยัง authorized_keys ที่ /root/.ssh/ ด้วยคำสั่ง
ต่อไปนี้

cp authorized_keys authorized_keys.bak ก็อปปี้ไฟล์กุญแจไว้ก่อน

cat id_rsa.pub >> /root/.ssh/authorized_keys

cat authorized_keys เพื่อดูคีย์ที่เราเพิ่มเข้าไป มีการเข้ารหัสไว้

ทดสอบใช้งาน

กลับไปที่เครื่อง Server แล้วลองก็อปปี้ไฟล์ ไปเก็บไว้ยัง Backup Server ด้วยคำสั่งเดิมอีกคร้้ง

scp test.txt user@192.168.x.x:/path/backup

วิธีการส่งคำสั่งไปรันที่เครืองรีโมทย์
ssh 192.168.10.xx "command 1 && command2"
ssh 192.168.10.xx "update && date"

จะเห็นว่าไม่มีการถาม password เหมือนตอนแรก

2. ทำสคริปเพื่อให้ก็อปปี้ไฟล์จากเครื่อง Server ไปยังเครื่อง Backup Server ดังนี้

crontab -e
ใส่สคริปให้ทำงาน เวลา 20:00 น. ของทุกวัน

0 20 * * *      scp -r /etc/postfix user@192.168.xx.xx:/pathe/backup

เรื่องที่เกี่ยวข้อง
1. คำสั่ง scp
2. Key Encryption




Wednesday, February 24, 2016

Mount path sshfs (ssh)

ต้องการ Map drive file จากเครื่อง Server มาที่เครื่องตนเองผ่านทาง ssh เพื่อความปลอดภัย

วิธีการ

1. บนเครื่อง Server ต้องติดตั้ง package ต่อไปนี้
   1.1 CentOS
yum -y install fuse-sshfs

   1.2 Debian
apt-get install openssh-server

2. สำหรับเครื่อง Client ที่จะ sshfs เข้าไป ต้องติดตั้ง Package ต่อไปนี้

   2.1 CentOS
yum -y install fuse-sshfs

   2.2 Ubuntu
apt-get install sshfs

3. ที่เครื่อง Client สร้าง Folder ขึ้นมาเพื่อ Map drive

#mkdir /home/user/Downloads

4. สำหรับ Ubuntu เจอปัญหาว่า Map network แล้ว user ไม่สามารถเข้าไปใช้งาน Drive ได้เนื่องจากติดสิทธิ์ Root วิธีแก้ปัญหาคือ
   4.1 แก้ไขไฟล์ /etc/fuse.conf
user_allow_other --> เปิดใช้งาน
   4.2 แก้ไข /etc/modules
fuse --> เพ่ิมเข้าไปบรรทัดล่างสุด
   4.3 ต้อง Restart เครื่องก่อนถึงจะใช้งาน แต่สามารถรันคำส่ง modprobe fuse เพื่อใช้ทดสอบใช้งานได้
#modprobe fuse

Ref.
lylebackenroth

5. การรันคำสั่ง Mount sshfs
   5.1 กรณีต้องการใส่ Password

sshfs user@192.168.0.101:/home/admin/Downloads /home/user/Downloads -p 55xxx -o uid=1000 -o gid=1000 -o allow_other

คำอธิบาย

user= user ที่อยู่เครื่อง Server
@192.168.0.101 คือไอพีเครื่อง Server
:/home/admin/Downloads คือ Path บนเครื่อง Server
/home/user/Downloads คือ Path บนเครื่อง Client (อ้างอิงข้อ 3.)
-p = port ssh เดิมเป็น 22 แต่ server มีการปรับไปใช้ port อื่น เช่น 55xxx
-o uid= user id ของ user local (/etc/passwd)
-o gid= group id ของ user local (/etc/group)
-o allow_other --> เป็นการยอมให้ user local มีสิทธิ์ RW share file ได้ อ้างอิงข้อ 4.1

   5.2 กรณีไม่ต้องการใส่ Password

echo passwords | sshfs user@192.168.0.101:/home/admin/Downloads /home/user/Downloads -p 55xxx -o uid=1000 -o gid=1000 -o allow_other -o password_stdin

คำอธิบายเพิ่มเติม

echo passwords | --> เป็นการใส่ password ที่ใช้ Login ssh
-o password_stdin --> เป็นการอ่านค่า password จาก stdin

Ref.
darklaunch





Friday, February 19, 2016

Create alias on linux (command short cut)

การรีโมทย์ ssh แบบสั้นและแทนการที่ต้องพิมพ์ให้ยืดยาว ดังนี้

hades@mail:~/ssh$ alias test_samba='ssh -p port number user@172.16.xx.xx'

เวลาเรียกใช้งาน ให้พิมพ์

hades@mail:~/ssh$ test_samba

Ref.
www.techsuii.com

show alias
#alias

Cancel alias
#unalias term

Ref.
linuxhowtos.org

add alias to Permanent

ให้พิมพ์คำสั่ง alias เข้าไปที่ไฟล์ ~/.bashrc ด้านล่างสุด ดังนี้

alias command1='ssh -p port number root@172.16.xx.xx'
alias command2='ssh  root@172.16.xx.xx'
alias ls='/usr/local/bin/exa-linux-x86_64'

เสร็จแล้วพิมพ์คำสั่ง . ~/.bashrc
แล้วลองเรียกคำสั่ง alias เช็คดูอีกครั้ง
Ref.
askubuntu.com




Thursday, February 18, 2016

ตัด session ssh ที่ login ไว้แล้วเกิดไม่ Response ค้างไม่สามารถใช้งานได้

วิธีแก้ไขปัญหาคือให้กดปุ่ม Enter แล้วพิมพ์ ~.

Reference: ได้มาจากเว็บไซต์
www.techsuii.com

Thursday, January 28, 2016

sshd_conf for centos 6.xx

เซท ssh มีความปลอดภัย login ได้เฉพาะ user ที่เรากำหนดไว้ ห้ามไม่ให้ root login

For CentOs 6.xx
-----------------------------------------------------------------------
Port 55001
ListenAddress 0.0.0.0

Protocol 2
HostKey /etc/ssh/ssh_host_rsa_key
HostKey /etc/ssh/ssh_host_dsa_key
ServerKeyBits 1024

# Logging
SyslogFacility AUTH
LogLevel INFO

LoginGraceTime 600
PermitRootLogin no
MaxAuthTries 3
MaxSessions 5

RhostsRSAAuthentication no
IgnoreRhosts yes

PermitEmptyPasswords no
PasswordAuthentication yes
AllowUsers user1 user2

ChallengeResponseAuthentication no

GSSAPIAuthentication yes
GSSAPICleanupCredentials yes

UsePAM yes

AcceptEnv LANG LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY LC_MESSAGES
AcceptEnv LC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE LC_MEASUREMENT
AcceptEnv LC_IDENTIFICATION LC_ALL LANGUAGE
AcceptEnv XMODIFIERS

X11Forwarding yes
Subsystem       sftp    /usr/libexec/openssh/sftp-server
-------------------------------------------------------------------