http://10.1.1.35/ule_basic/ule09.txt

來源:互聯網
上載者:User
--

ssh 服務

/etc/init.d/sshd --sshd的服務啟動指令碼

[root@li ~]# netstat -ntlup |grep sshd
tcp 0 0 :::22 :::* LISTEN 2804/sshd

vim /etc/ssh/sshd_config --設定檔

cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak--改設定檔前的好習慣,做一個備份


#Port 22--預設連接埠是22
#Protocol 2,1--協議版本現在是2版本
Protocol 2
#AddressFamily any
#ListenAddress 0.0.0.0--監聽的位址範圍,預設 是所有
#ListenAddress ::

# HostKey for protocol version 1
#HostKey /etc/ssh/ssh_host_key
# HostKeys for protocol version 2
#HostKey /etc/ssh/ssh_host_rsa_key
#HostKey /etc/ssh/ssh_host_dsa_key--ssh 等效性,指的是ssh不要求輸入密碼

# Lifetime and size of ephemeral version 1 server key
#KeyRegenerationInterval 1h--每隔預設的一小時重建立立一次key
#ServerKeyBits 768--server key的長度
# Logging
# obsoletes QuietMode and FascistLogging
#SyslogFacility AUTH
SyslogFacility local 7--日誌等級
#SyslogFacility AUTHPRIV
#LogLevel INFO
# Authentication:

#LoginGraceTime 2m--寬限時間2分鐘
#PermitRootLogin yes--允許root使用者登入
#StrictModes yes
#MaxAuthTries 6
.................................................



例子一:

修改ssh服務預設連接埠,增加安全性
#Port 22
Port 222--修改連接埠

/etc/init.d/sshd restart--重啟服務

[root@li ~]# netstat -ntlup |grep 222--服務重啟之後,看到監聽的連接埠號碼為222
tcp 0 0 :::222 :::* LISTEN 3088/sshd


vim /etc/services--參考這個檔案,有服務與對應的連接埠,連接埠不要亂改

連接埠有關知識:
範圍:0-65535
註冊連接埠:0-1024
隨機連接埠:1025~65535


[root@snake ~]# ssh 10.1.1.35 -p 222--加上-p參數後接連接埠號碼



例子二:

允許空密碼ssh登陸
在/etc/ssh/sshd_config裡加上

PermitEmptyPasswords yes


然後去掉某一個使用者在/etc/passwd第二欄的x

再ssh測試

例子三:

直接在ssh服務組態檔裡指定拒絕某使用者,或者某些使用者ssh登陸
在/etc/ssh/sshd_config裡加上

Denyusers a --指定拒絕a使用者ssh登陸

重啟服務生效


例子四:

ssh等效
原理:產生一對密鑰,自己持有一個私密金鑰,公開金鑰給對方,需要一對才能解密

ssh-keygen --輸入這個命令後,三次斷行符號產生空密碼key對

[root@li ~]# ssh-keygen
Generating public/private rsa key pair.--預設是rsa加密方式
Enter file in which to save the key (/root/.ssh/id_rsa):
/root/.ssh/id_rsa already exists.
Overwrite (y/n)? y
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
ad:2d:f9:af:8a:c7:ab:0b:8e:de:47:ed:73:68:f6:b3 root@li.cluster.com


scp /root/.ssh/id_rsa.pub 10.1.1.104:/root/.ssh/authorized_keys--把公開金鑰傳給104的機器後,重命令名為authorized_keys

ssh 10.1.1.104--直接ssh不要求輸入密碼了

ssh-keygen -t dsa--可以這樣使用dsa的加密方式
ssh-keygen -t rsa--預設的加密方式


--如何多台機向同一台機配置ssh等效性
因為第二台機如果也象上面那樣scp過去,會覆蓋authorized_keys檔案,那麼把第二台機產生的id_rsa.pub用vi開啟,複製粘到對方的authorized_keys檔案裡就可以了,或者用追加做 (>>)











-----------------------------------------------------------

linux的日誌管理

ls /var/log/--此目錄下很多系統日誌都存放在此
/var/log/messages--非常重要的記錄檔,幾乎所有系統發生的重要錯誤都會記錄在此
/var/log/secure--ssh ftp telnet pop3等有關
/var/log/dmesg --開機時核心檢測過程中所產生的資訊
/var/log/cron--查看時間任務有關的日誌
/var/log/wtmp--用last查看,不能直接cat
/var/log/btmp--用lastb查看,不能直接cat
/var/log/boot--boot相關
/var/log/rpmpkgs--rpm相關
/var/log/maillog--mail相關
/var/log/spooler--news,uucp
/var/log/acpid--進階電源管理

還有一種日誌就是每個服務所儲存的日誌,路徑因服務會有所不同
一般是在/var/log/服務名/下


/etc/init.d/syslog--系統log服務,開啟它才能記錄系統日誌

yum list |grep log
sysklogd.i386--相關軟體包
logrotate.i386


vim /etc/syslog.conf --設定檔
格式為 : 日誌裝置(串連符號)記錄層級


裝置:
auth -- pam產生的日誌
authpriv--包括許可權,登入相關的資訊
cron--時間任務相關的資訊,crontab,at
mail--郵件相關的資訊
kern --核心產生的資訊
uucp--unix to unxi copy 指的是unix系統之間拷貝協議相關的資訊
news--新聞群組相關的資訊
local0 到 local7 --程式自訂的日誌裝置
mark --syslog服務內部的一些資訊,產生一個時間標識
user --使用者程式產生的相關資訊


記錄層級:
debug --調試,產生的日誌是最多的一個層級
info --一般資訊的日誌,最常用
notice --最具有重要性的普通條件的資訊
warning或warn --警告層級
err或error--錯誤層級,阻止某個功能或者模組不能正常工作的資訊
crit--嚴重層級,阻止整個系統或者整個軟體不能正常工作的資訊
alert--需要馬上修改的資訊
emerg或panic--核心崩潰等嚴重錯誤
none--什麼都不記錄
--上面的記錄層級,從上往下層級從低到高,記錄的資訊是多到少,層級低的記錄的資訊包括層級高的


串連符號
.記錄大於等於此等級
.=只記錄等於此等級的資訊
.!不等於此等級,也就是記錄此等級之外的資訊


# Log all kernel messages to the console.
# Logging much else clutters up the screen.
#kern.* /dev/console
--/dev/console指的是一種外接的終端裝置,有些封閉主機(沒有screen,keyboard,mouse等),可能會需要外接裝置串連
# Log anything (except mail) of level info or higher.
# Don't log private authentication messages!
*.info;mail.none;authpriv.none;cron.none /var/log/messages
--記錄所有日誌類型的info層級的資訊到/var/log/messages,除了mail,authpriv,cron的資訊
# The authpriv file has restricted access.
authpriv.* /var/log/secure
--記錄authpriv的所有層級的資訊到/var/log/secure
# Log all the mail messages in one place.
mail.* -/var/log/maillog
--記錄郵件的所有層級的資訊到/var/log/maillog,關於減號-的意思為:因為郵件的資訊相比於其他的來說是比較大的,所以是採用非同步方式,先存放在記憶體裡,然後按一定的事件觸發,或者到達一定的大小寫到磁碟,而不是象其他的日誌那樣,產生了就馬上寫到磁碟。

# Log cron stuff
cron.* /var/log/cron
--記錄時間任務所有層級的資訊到/var/log/cron

# Everybody gets emergency messages
*.emerg *
--記錄所有日誌裝置的emerg資訊以wall的方式發送給每個登入到系統的人知道
# Save news errors of level crit and higher in a special file.
uucp,news.crit /var/log/spooler
--記錄uucp,new的crit資訊到/var/log/spooler
# Save boot messages also to boot.log
local7.* /var/log/boot.log
--另外把boot messages記錄到/var/log/boot.log


格式的寫法有兩種:
uucp.crit;news.crit
uucp,new.crit
--上面這兩種寫法是一致


例一:
mail.=info -/var/log/newmaillog
--只記錄mail的info層級資訊到/var/log/newmaillog

例二:
cron.info;cron.!err/var/log/newcron
--記錄時間任務的info層級以上的所有資訊(除了err外)到/var/log/newcron



例三:
要實現把sshd服務的日誌自訂儲存到/var/log/newsshd.log

vim /etc/sshd/sshd_config

syslogFacility LOCAL5--修改日誌為local5


vim /etc/syslog.conf--加上下面一句
local5.* /var/log/newsshd.log

/etc/init.d/sshd restart

/etc/init.d/syslog restart



---------------------------------------



遠程系統日誌管理
管理多台主機,有時為了方便,可以把多台主機的日誌給傳到管理機(只一台)上,然後,系統管理員只要在管理機上查看,就可以查到多台主機的日誌



管理機的操作:


[root@li ~]# vim /etc/sysconfig/syslog --相關的全域設定檔
# Options to syslogd
# -m 0 disables 'MARK' messages.--預設是關掉了mark資訊
# -r enables logging from remote machines--加上-r參數就是開啟了遠端日誌管理功能
# -x disables DNS lookups on messages recieved with -r--這是當使用遠程日誌管理時,用-x參數關掉DNS尋找功能
# See syslogd(8) for more details
SYSLOGD_OPTIONS="-m 0 -r "--在這裡加上-r參數,開啟遠程日誌功能;注意的是-r加在前面或者後面都可以,不要加在-m和0的中間 -m 180 每隔180時間產生一個時間資訊

/etc/init.d/syslog restart --重啟服務

[root@li ~]# netstat -ntlup |grep sys--加了-r參數後開啟了遠程日誌功能,那麼就會多了一個監聽連接埠,為udp的514連接埠
udp 0 0 0.0.0.0:514 0.0.0.0:* 4252/syslogd


客戶機的配置:

例一:
vim /etc/syslog.conf
authpriv.*@10.1.1.35--把authpriv.*的日誌全部傳給管理機10.1.1.35

/etc/init.d/syslog restart --重啟服務後,就可以在10.1.1.35的管理機上看到客戶機的authpriv的日誌資訊


--遠程日誌要注意理解的是: 客戶機只定義把日誌傳給誰(這裡代表管理機),管理機自己定義儲存在哪個記錄檔(也是/etc/syslog.conf裡定義)
--上面的例子可以看到 客戶機定義傳給35這台機,35管理機的/etc/syslog.conf裡也有authpriv.* /var/log/secure這一句,意思就是用戶端的authpriv.*的資訊傳送到了管理機的/var/log/secure檔案裡



例二:
還是客戶機的配置
vim /etc/syslog.conf
authpriv.*@li.cluster.com--使用網域名稱的方式定義傳給誰

那客戶機還要做一個ip與主機名稱的對應
vim /etc/hosts --加上
10.1.1.35li.cluster.com




--------------------------------------------------------------------------

關於umask

[root@li test]# umask
0022
mkdir 一個目錄 預設建立的目錄的許可權為777-umask
touch一個檔案 預設建立的檔案的許可權為666-umask


----------------------------------------------------------------------------


日誌輪轉rotate

常見的輪轉的日誌有:
/var/log/messages
/var/log/cron
/var/log/boot.log
/var/log/maillog
/var/log/secure

vim /etc/logrotate.conf --設定檔


# see "man logrotate" for details
# rotate log files weekly
weekly--預設是一個星期rotate一次

# keep 4 weeks worth of backlogs
rotate 4--預設保留4個

# create new (empty) log files after rotating old ones
create--roate後,建立一個新的代替

# uncomment this if you want your log files compressed
#compress--預設沒有開啟日誌壓縮功能

# RPM packages drop log rotation information into this directory
include /etc/logrotate.d--這表示/etc/logrotate.d目錄下的所有設定檔都生效

# no packages own wtmp -- we'll rotate them here
/var/log/wtmp {--針對wtmp單獨的配置,這裡是絕對路徑
monthly--一個月rotate一次,取代上面的全域配置裡的一個星期一次
minsize 1M--指定最小大小為1M,才rotate,就算是時間到了,大小沒有1M,也不rotate
create 0664 root utmp--定義建立的為屬性為0664,屬主為root,屬組utmp
rotate 1--保留一個
}


logrotate -vf /etc/logrotate.conf --查看輪轉的過程



vim /etc/logrotate.d/syslog


/var/log/messages /var/log/secure /var/log/maillog /var/log/spooler /var/log/boot.log /var/log/cron {--對哪些記錄檔進行定義
sharedscripts--指令碼開始
postrotate--表示rotate結束
/bin/kill -HUP `cat /var/run/syslogd.pid 2> /dev/null` 2> /dev/null || true
/bin/kill -HUP `cat /var/run/rsyslogd.pid 2> /dev/null` 2> /dev/null || true
endscript--指令碼結束
}


sharedscripts
presrotate

postrotate
endscript


logger -t 'aa' 'bb'--在記錄檔裡加上一個標記
[root@li test]# tail -n 1 /var/log/messages
May 8 15:33:02 li aa: bb--可以看到剛才加的標記



logrotate -vf /etc/logrotate.d/syslog--強制rotate,然後查看輪轉時記錄檔的重新命名過程

-f 強制rotate
-v 顯示過程


--&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&

man logrotate裡的協助

/var/log/messages {
rotate 5
weekly
postrotate
/sbin/killall -HUP syslogd
endscript
}

"/var/log/httpd/access.log" /var/log/httpd/error.log {
rotate 5
mail www@my.org
size 100k
sharedscripts
postrotate
/sbin/killall -HUP httpd
endscript
}

/var/log/news/* {
monthly
rotate 2
olddir /var/log/news/old
missingok
postrotate
kill -HUP ‘cat /var/run/inn.pid‘
endscript
nocompress
}

--&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&


--@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

超級守護進程

chkconfig --list |grep xinetd

守護進程分為:
stand alone --指的非xinet下的那些服務
super daemon --指的就是xinetd下的那些服務


super daemon相當於是它下面的所有服務的管理程式,在安全性上更加了一道門


以rsync服務為例
vim /etc/xinetd.d/rsync

service rsync--服務名
{
disable = yes--disable等於yes表示此服務關閉
socket_type = stream--tcp的連線機制
wait = no--不等待,可以同時進行大量連線功能
user = root--用root身份啟動服務
server = /usr/bin/rsync--定義你的rsync的服務執行檔案的位置
server_args = --daemon--服務參數
log_on_failure += USERID--登入錯誤時,額外記錄你的使用者id
}
~

例子一:
實現把ssh拖管到super daemon下管理

man xinetd.conf--寫法去這裡找協助

vim /etc/xinetd.d/ssh --建立一個檔案,寫上下面的內容


service ssh {
disable = no
socket_type =stream
protocol=tcp
wait=no
user=root
server=/usr/sbin/sshd
server_args= -i
}


/etc/init.d/xinetd restart
/etc/init.d/sshd restart

netstat -ntlup
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 8182/xinetd --這時候查看你的連接埠會發現守護進程為xinetd,不是以前的sshd




例子二:
在上面例子的基礎上再加上存取控制
service ssh {
disable = no
socket_type =stream
protocol=tcp
wait=no
user=root
server=/usr/sbin/sshd
server_args= -i
only_from = 10.1.1.0/24--允許10.1.1.0/24網段訪問
no_access =10.1.1.104--拒絕10.1.1.104訪問
}

例子三:
介紹一個super daemon下的一個時間同步的方法
[root@li test]# vim /etc/xinetd.d/time-dgram
disable = no
[root@li test]# vim /etc/xinetd.d/time-stream
disable = no

/etc/init.d/xinetd restart

用戶端使用 rdate -s 10.1.1.35 來同步時間




--------------------------------------

tcpwrapper


看程式是否支援tcpwrapper方式

比如說看sshd
[root@li test]# which sshd
/usr/sbin/sshd

[root@li test]# ldd /usr/sbin/sshd |grep wrap
libwrap.so.0 => /lib/libwrap.so.0 (0x00d39000)--有此模組就表示支援


由兩個檔案來控制
/etc/hosts.allow
/etc/hosts.deny
存取控制機製為先去匹配/etc/hosts.allow,再去匹配/etc/hosts.deny,如果都沒有,則都允許



vim /etc/hosts.deny

sshd: 10.1.1.0/255.255.255.0 EXCEPT 10.1.1.104--拒絕10.1.1.0網段的人ssh訪問,除了104外;注意子網路遮罩不支援/24寫法

sshd: all EXCEPT 10.1.1.104















聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.