-C 注釋 (加上這個也是為了最后進行對服務器訪問人員進行辨別的一個關鍵點)
將公鑰上傳到服務器上的.ssh/authorized_keys 文件中。ssh-copy-id 命令會自動在服務器上創建.ssh/authorized_keys文件,即使該目錄不存在,并自動賦予600權限。
[root@rsyslog ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.30.72root@192.168.30.72\\\'s try logging into the machine, with ssh \\\'root@192.168.30.72\\\', and check in: .ssh/authorized_keys to make sure we haven\\\'t added extra keys that you weren\\\'t expecting.
4.3 client 2(192.168.30.71) 上同樣的操作,用戶lisi
View Code
上傳公鑰到服務器上
[root@swift3 ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.30.72The authenticity of host \\\'192.168.30.72 (192.168.30.72)\\\' can\\\'t be established.RSA key fingerprint is 8f:a7:1b:8d:e4:92:ad:ae:ea:1b:fb:67:0b:0b:7c:ac. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added \\\'192.168.30.72\\\' (RSA) to the list of known hosts. root@192.168.30.72\\\'s try logging into the machine, with ssh \\\'root@192.168.30.72\\\', and check in: .ssh/authorized_keys to make sure we haven\\\'t added extra keys that you weren\\\'t expecting.
4.4 現在去服務器上驗證下該文件。
View Code
現在上面兩個客戶端已經可以免密鑰登陸了,現在去服務器上配置,并創建腳本。
五、配置服務器
5.1 在log目錄下創建一個 keys 文件,用于登陸時存進去公鑰,之后對其進行取出判斷的
[root@open1 ~]# touch /var/log/keys
創建檢測腳本,內容如下:
View Code
5.2 配置 profile,在文件末尾添加一行內容,如下:
[root@open1 ~]# echo test -f /etc/CheckUser.sh && . /etc/CheckUser.sh >> /etc/profile
在/etc/bashrc 末尾添加下面內容:
[root@open1 ~]# tail -1f /etc/bashrc
test -z $BASH_EXECUTION_STRING || { test -f /etc/CheckUser.sh && . /etc/CheckUser.sh; logger -t -bash -s HISTORY $SSH_CLIENT USER=$NAME_OF_KEY CMD=$BASH_EXECUTION_STRING >/dev/null 2>&1;}
5.3 修改sshd 配置文件,開啟debug 模式,并重啟sshd 服務
[root@open1 ~]# sed -i \\\'s/#LogLevel INFO/LogLevel DEBUG/g\\\' /etc/ssh/sshd_config [root@open1 ~]# service sshd restart Stopping sshd: [ OK ] Starting sshd: [ OK ]
六、驗證
6.1 在client1 上進行登陸,并刪除個文件試下(zhangsan)
6.2 在client2 上進行登陸,也刪除個文件,并執行個重啟服務的命令(lisi)
6.3 去服務器上查看下 messages 日志,內容如下
通過上圖,可以看出,不通用戶的客戶端通過公鑰登陸的方式,分辨出了誰操作了什么,什么時候操作的。
(注:上圖第4段 swift1 是這臺服務器的主機名,由于我只是運行了hostname 命令修改主機名,并沒有修改networks,所以內核里還是之前的名字:swift1。)
七、結束
通過這種方式,極大的解決了多root 用戶登陸操作,無法審計的問題。并且可以結合日志轉發,將系統日志轉發到其它服務器,即使主機被黑了,也能具體的審查登陸時間以及做了哪些操作。
更多關于云服務器,域名注冊,虛擬主機的問題,請訪問三五互聯官網:www.shinetop.cn