2021年10月26日 星期二

SFTP Chroot with Public Key Authentication and SELiunx in CentOS

Chroot 後

SSHD 對於 chroot的 userhome/.ssh 的讀寫權限,受到selinux的限制

可以在 /var/log/audit.log裡面看到類似

type=AVC msg=audit(1635139929.544:379): avc:  denied  { read } for  pid=144254 comm="sshd" name="authorized_keys" dev="sda2" ino=50348313 scontext=system_u:system_r:sem_r:sshd_t:s0-s0:c0.c1023 tcontext=unconfined_u:object_r:var_t:s0 tclass=file permissive=0

 

解法1 :

 把 authorized_key 放到其他資料夾

參考 https://wiki.archlinux.org/title/SFTP_chroot

 

Match Group sftponly
AuthorizedKeysFile /etc/ssh/authorized_keys/%u
ChrootDirectory %h ForceCommand internal-sftp AllowTcpForwarding no X11Forwarding no PasswordAuthentication no


解法2 :

如果要保持放在 userhome/.ssh

 

安裝 semanage 

yum install policycoreutils-python-utils

 針對  choort 的 userhome/.ssh

執行

semanage fcontext -a -t ssh_home_t /path/to/users/homedirectory/.ssh/
restorecon -v /path/to/users/homedirectory/.ssh/


2021年3月18日 星期四

GCP Instance Multiple Network Interface Access Problem

 GCP 上面創建Instance時,如果有多個network interface設定,需要額外創建routing rule

 


  1. nic 0 / default / 10.140.0.2
    nic 1 / interface-1 / 10.40.0.2
    nic 2 / interface-2 / 10.60.0.2


  2. 創建好VM後,進入linux


  3. sudo echo "100 rt1" | sudo tee -a /etc/iproute2/rt_tables
    sudo echo "200 rt2" | sudo tee -a /etc/iproute2/rt_tables 

  4. vim /root/iproute.sh

    #!/bin/bash

    sleep 10
    ip route add 10.40.0.1 src 10.40.0.2 dev eth1 table rt1
    ip route add default via 10.40.0.1 dev eth1 table rt1
    ip rule add from 10.40.0.2/32 table rt1
    ip rule add to 10.40.0.2/32 table rt1

    ip route add 10.60.0.1 src 10.60.0.2 dev eth2 table rt2
    ip route add default via 10.60.0.1 dev eth2 table rt2
    ip rule add from 10.60.0.2/32 table rt2
    ip rule add to 10.60.0.2/32 table rt2

  5. 注意一定要 sleep 10 (因為google 執行 startup-scritp的時候,有可能network interface還沒啟動)


  6. 在 Instance 加入 metadata :
    metadata key : startup-script
    metadata value :
    #!/bin/bash
    /root/iproute.sh

  7. 重新啟動 instance,應該就可以正常存取
  8. 可以去 /var/log/message 看啟動資訊,關鍵字 GCEMetadataScripts