linux遠程登錄(Telnet、SSH)


系統:RHEL 5.5 64位,使用CentOS的yum源並作更新處理

參考書目《Linux兵書》/電子工業出版社/劉麗霞,細節之處稍有變動。

一、Telnet(遠程登錄推薦SSH)

1、安裝、啟動Telnet

1.1、安裝和啟動一般需要兩個包:

     telnet-client提供客戶端程序

     telnet-server提供服務器端程序

1.2、安裝之前,確定是否已經安裝Telnet

[root@localhost Server]# rpm -qa|grep telnet
telnet-server-0.17-39.el5
telnet-0.17-39.el5

說明已經安裝,若未安裝,可掛載RHEL5.5的DVD光盤鏡像,在Server文件夾下找到並安裝下面三個包:

[root@localhost Server]# rpm -ivh xinetd-2.3.14-10.el5.x86_64.rpm    #先安裝這個包

[root@localhost Server]# rpm -ivh telnet-

telnet-0.17-39.el5.x86_64.rpm telnet-server-0.17-39.el5.x86_64.rpm   #這兩個包的安裝順序是從左到右依次安裝

1.3、啟動:

方法一:

用# setup命令,選擇系統服務,在里面找到Telnet,用空格鍵選中(前面加*),然后用Tab鍵選擇確定,退出。

                                

方法二:xinetd啟動

# vim /etc/xinetd.d/telnet

修改后文件如下:

# default: on
# description: The telnet server serves telnet sessions; it uses \
# unencrypted username/password pairs for authentication.
service telnet
{
#disable = no            這里將no改為yes
disable = yes
flags = REUSE
socket_type = stream
wait = no
user = root
server = /usr/sbin/in.telnetd
log_on_failure += USERID
}
View Code

重啟服務

[root@localhost Server]# service xinetd restart              #這種方式在這里有問題哈。。。。。
bash: service: command not found
[root@localhost Server]# /etc/init.d/xinetd restart          #用這個吧
停止 xinetd:                                              [失敗]
啟動 xinetd:                                              [確定]

2、配置Telnet

2.1、設置端口

# vim /etc/services ,修改默認23端口為未使用的端口號,如22182:
telnet          22183/tcp
telnet          22183/udp
[root@localhost Server]# /etc/init.d/xinetd restart    #重啟服務

2.2、Telnet服務限制

Telnet用明文傳送口令和數據,有時有必要對其服務范圍進行限制

# vim /etc/xinetd.d/telnet修改文件如下:

# default: on
# description: The telnet server serves telnet sessions; it uses \
#       unencrypted username/password pairs for authentication.
service telnet
{
        disable = no
        #disable        = yes
        flags           = REUSE
        socket_type     = stream
        wait            = no
        user            = root
        server          = /usr/sbin/in.telnetd
        log_on_failure  += USERID
        bind            =192.168.111.23  #主機地址
        only_from       =192.168.0.0/16  #只允許192.168.0.0-192.168.255.255網段之間的計算機登錄
        #only_from       = .edu.cn        #只允許教育網接入
        #no_access        =210.45.160.{115,116}   #這兩個IP地址不可用
        #access_times    =7:00-12:00 15:00-23:00  #每天這兩個時間段開放服務
}
View Code

2.3、Telnet的root用戶登錄

Telnet默認不允許root登錄,若要root登錄,方法如下:

方法一:

  # vim /etc/pam.d/login

找到下面一行,注釋掉:

#account    required     pam_nologin.so   將這一行加上注釋

方法二:

# mv /etc/securetty /etc/securetty_bankup

注意:非特殊情況,請勿這樣操作

3、Telnet的會話示例:

登陸命令(只須提供登陸的主機名和用戶):

telnet  [-l user ]  [-a]   host-name   [port]

[root@localhost Server]# telnet 127.0.0.1
Trying 127.0.0.1...
Connected to localhost.localdomain (127.0.0.1).
Escape character is '^]'.
Red Hat Enterprise Linux Server release 5.5 (Tikanga)
Kernel 2.6.18-194.el5 on an x86_64 
login: vnc
Password: 
Last login: Wed May  6 17:59:20 on :0
[vnc@localhost ~]$ su
口令:
[root@localhost vnc]# exit
exit
[vnc@localhost ~]$ exit
logout

Connection closed by foreign host.
[root@localhost Server]# 
View Code

注意:需要關閉防火牆,或直接打開22183端口,

(參考文檔:http://blog.sina.com.cn/s/blog_866c5a5d0101ckr1.html

# vim /etc/sysconfig/iptables

添加下面一行代碼:

-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 22183 -j ACCEPT

用Xshell打開截圖如下:

                     

二、SSH

SSH優點:傳輸的數據經過加密,有效防止“中間人”攻擊、DNS和IP欺騙;傳輸數據經過壓縮,傳輸速度快。

SSH有很多功能,既可代替Telnet,又可為FTP、POP、甚至PPP提供安全通道

注意:有兩個不兼容的版本1.x和2.x;

        OpenSSH是SSH的免費替代軟件,應用較廣泛;

        若准備使用SSH,一定要應用到所有服務器上,如9台安全的服務器和一台不安全的服務器配在一起是沒安全性可言的。

1、安裝啟動SSH

1.1、查看有無安裝SSH

[root@localhost Server]# rpm -qa|grep ssh
openssh-4.3p2-41.el5
openssh-server-4.3p2-41.el5
openssh-clients-4.3p2-41.el5
openssh-askpass-4.3p2-41.el5

如無安裝,掛載系統鏡像,安裝下列軟件:

[root@localhost Server]# rpm -ivh openssh-
openssh-4.3p2-41.el5.x86_64.rpm          openssh-clients-4.3p2-41.el5.x86_64.rpm
openssh-askpass-4.3p2-41.el5.x86_64.rpm  openssh-server-4.3p2-41.el5.x86_64.rpm
View Code

1.2、啟動

三種方式:

# service sshd start    方法一
啟動 sshd:                                                [確定]
# /etc/rc.d/init.d/sshd restart    方法二
停止 sshd:                                                [確定]
啟動 sshd:                                                [確定]

方法三(開機啟動):

#setup,在系統服務中找到sshd,前面加上*,確定,退出即可

2、簡單測試使用SSH

測試SSH提供的安全登錄功能:

ssh  -l  [your accountname on the remote host]  [address of the remote host]

例子:

# ssh -l root 127.0.0.1
The authenticity of host '127.0.0.1 (127.0.0.1)' can't be established.
RSA key fingerprint is 76:b3:02:d2:35:18:60:e6:c7:99:a3:01:08:fa:24:85.
Are you sure you want to continue connecting (yes/no)? ye
Please type 'yes' or 'no': yes
Warning: Permanently added '127.0.0.1' (RSA) to the list of known hosts.
root@127.0.0.1's password: 
[root@localhost ~]# who
vnc      :0           2015-05-06 17:59
vnc      pts/1        2015-05-06 17:59 (:0.0)
root     pts/2        2015-05-06 20:08 (localhost.localdomain)
[root@localhost ~]# who am i
root     pts/2        2015-05-06 20:08 (localhost.localdomain)
[root@localhost ~]#
View Code

3、兩個配置文件

/etc/ssh/ssh_config 為OpenSSH系統范圍內的配置文件
 1 [root@localhost ~]# cat /etc/ssh/ssh_config 
 2 #       $OpenBSD: ssh_config,v 1.21 2005/12/06 22:38:27 reyk Exp $
 3 
 4 省略。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
 5 # Host *                               #只對能夠匹配后面字符串的計算機有效,*代表所有主機
 6 #   ForwardAgent no
 7 #   ForwardX11 no
 8 #   RhostsRSAAuthentication no         #是否用RSA算法的基於rhosts的安全驗證
 9 #   RSAAuthentication yes              #是否用RSA算法進行安全驗證
10 #   PasswordAuthentication yes         #是否用口令驗證
11 #   HostbasedAuthentication no
12 #   BatchMode no
13 #   CheckHostIP yes                    #設置SSH是否查看連接到服務器的主機的IP地址,以防止DNS欺騙
14 #   AddressFamily any
15 #   ConnectTimeout 0
16 #   StrictHostKeyChecking ask
17 #   IdentityFile ~/.ssh/identity       #設置從哪個文件讀取用戶的RSA安全驗證標識
18 #   IdentityFile ~/.ssh/id_rsa
19 #   IdentityFile ~/.ssh/id_dsa
20 #   Port 22                       #設置連接到遠程主機的端口
21 #   Protocol 2,1
22 #   Cipher 3des                   #設置加密用的密碼
23 #   Ciphers aes128-cbc,3des-cbc,blowfish-cbc,cast128-cbc,arcfour,aes192-cbc,aes256-cbc
24 #   EscapeChar ~                  #設置escape字符
25 #   Tunnel no
26 #   TunnelDevice any:any
27 #   PermitLocalCommand no
28 Host *
省略。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
View Code
/etc/ssh/sshd_config 為OpenSSH的配置文件,關鍵詞忽略大小寫。(參數說明,參考:http://blog.sina.com.cn/s/blog_59e866610100aulb.html
[root@localhost ~]# cat /etc/ssh/sshd_config 
省略。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
#Port 22                                #設置sshd監聽的端口號
#Protocol 2,1
Protocol 2                              #指定ssh支持的通信協議版本
#AddressFamily any
#ListenAddress 0.0.0.0                  #設置sshd服務器綁定的IP地址
#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

# Lifetime and size of ephemeral version 1 server key
#KeyRegenerationInterval 1h
#ServerKeyBits 768                     #定義服務器秘鑰的位數

# Logging
# obsoletes QuietMode and FascistLogging
#SyslogFacility AUTH
SyslogFacility AUTHPRIV
#LogLevel INFO

# Authentication:

#LoginGraceTime 2m             #設置如果用戶不能成功登陸,在切斷連接之前服務器需要等待的時間(以秒為單位)
#PermitRootLogin yes           #是否允許root登陸
#StrictModes yes
#MaxAuthTries 6                #限制用戶錯誤登陸次數

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

# For this to work you will also need host keys in /etc/ssh/ssh_known_hosts
#RhostsRSAAuthentication no
# similar for protocol version 2
#HostbasedAuthentication no
# Change to yes if you don't trust ~/.ssh/known_hosts for
# RhostsRSAAuthentication and HostbasedAuthentication
#IgnoreUserKnownHosts no
# Don't read the user's ~/.rhosts and ~/.shosts files
#IgnoreRhosts yes

# To disable tunneled clear text passwords, change to no here!
#PasswordAuthentication yes
#PermitEmptyPasswords no
PasswordAuthentication yes                   #是否允許口令驗證

# Change to no to disable s/key passwords
#ChallengeResponseAuthentication yes
ChallengeResponseAuthentication no

# Kerberos options
#KerberosAuthentication no
#KerberosOrLocalPasswd yes
#KerberosTicketCleanup yes
#KerberosGetAFSToken no

# GSSAPI options
#GSSAPIAuthentication no
GSSAPIAuthentication yes
#GSSAPICleanupCredentials yes
GSSAPICleanupCredentials yes

省略。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
#UsePAM no
UsePAM yes

# Accept locale-related environment variables
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
#AllowTcpForwarding yes
#GatewayPorts no
#X11Forwarding no
X11Forwarding yes
#X11DisplayOffset 10
#X11UseLocalhost yes
#PrintMotd yes
#PrintLastLog yes
#TCPKeepAlive yes
#UseLogin no
#UsePrivilegeSeparation yes
#PermitUserEnvironment no
#Compression delayed
#ClientAliveInterval 0   #指定服務器端向客戶端請求消息的時間間隔, 默認是0, 不發送.如ClientAliveInterval 60表示每分鍾發送一次, 然后客戶端響應, 這樣就保持長連接了
#ClientAliveCountMax 3   #使用默認值3即可,表示服務器發出請求后客戶端沒有響應的次數達到一定值, 就自動斷開. 正常情況下, 客戶端不會不響應.
#ShowPatchLevel no
#UseDNS yes
#PidFile /var/run/sshd.pid
#MaxStartups 10
#PermitTunnel no
#ChrootDirectory none

# no default banner path
#Banner /some/path

# override default of no subsystems
Subsystem       sftp    /usr/libexec/openssh/sftp-server
View Code

 4、SSH的秘鑰管理

SSH的秘鑰管理主要包括兩方面:生成公鑰/私鑰對和公鑰的分發

4.1、生成用戶自己的密鑰對

4.1.1、好處:

(1)、可以防止“中間人”這種攻擊方式

(2)、可以只用一個口令就登陸到所有用戶想登陸的服務器上

4.1.2、生成密鑰:

# ssh-keygen                                            #1.x版本,若為2.x版本用 # ssh-keygen -d
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):   回車即可
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:

此時,用戶有一對密鑰:公鑰要發布到所有用戶想用SSH登陸的遠程主機上去;私鑰要妥善保管,防止他人知曉

查看/root/.ssh/目錄下相應文件的訪問權限,必須是“-rw------"

4.2、分發公用密鑰

在每一個用戶需要用SSH連接的遠程服務器上,用戶在自己的主目錄下創建一個 .ssh 的子目錄,把用戶的公用密鑰 id_rsa.pub復制到此目錄下,並重命名為

authorized_keys,然后執行如下命令:

# chmod 644 .ssh/authorized_keys 

若除了用戶還有他人知對此文件有寫的權限,SSH就不會工作。

若用戶想從不同的計算機登陸到遠程主機,authorized_keys 文件也可以有多高公用密鑰,此時必須重新生成一對密鑰,然后把id_rsa.pub復制到遠程主機的

authorized_keys里。另外,新的計算機上用戶必須有一個賬號,而且密鑰是用口令保護的。

 

SSH安全攻略:

1、關閉無關端口

為避免被掃描到,除必要端口外全部關掉,建議關閉icmp端口,並設置規則,丟棄icmp包。

丟棄icmp包可在iptables里加入下列代碼:

-A INPUT -p icmp -j DROP 

2、更改SSH端口

將默認端口22更改為10 000以上,以降低端口被掃描到的概率,方法如下:

# vim /etc/ssh/ssh_config 
# vim /etc/ssh/sshd_config 

將端口均更改為:

Port 22
Port 18438    #加入新的Port值,

重啟ssh服務:

# service sshd restart

注意:

  <1>此處設置兩個SSH端口,主要是為防止出錯導致SSH無法登陸,登陸成功后則須編輯上面兩個配置文件刪除22端口

  <2>登陸成功后須在在iptables里刪除22端口,添加新配置的18438端口

  <3>若SSH登陸的是弱密碼,則可以設置一個復雜密碼

3、限制IP登陸

/etc/hosts.allow 和 /etc/hosts.deny 文件時tcpd服務器的配置文件,tcpd服務器可以控制外部IP對本機服務的訪問。

這兩個配置文件的格式如下:

# 服務進程名:主機列表:當規則匹配時可選的命令操作 server_name:hosts-list[:commond]

 /etc/hosts.allow 控制可以訪問本機的IP地址,/etc/hosts.deny 控制禁止訪問本機的IP地址。若兩者配置有沖突,以/etc/hosts.allow 為主。

為便於理解和管理,將允許進入的寫入 /etc/hosts.allow當中,將不允許的寫入 /etc/hosts.deny 當中

例子1:

# vim /etc/hosts.allow   #添加以下內容:
ALL:127.0.0.1           #允許本機訪問本地所有服務
sshd:192.168.75.130     #允許192.168.75.130登錄
smbd:192.168.0.0/255.255.255.0 #允許 192.168.0.0網段的IP訪問smbd服務
  (舉例說明:192.168.6.100代表一個主機,192.168.6.代表整個網段。同理,test.com代表一台主機,.test.com代表test.com域內所有主機。)
# vim /etc/hosts.deny #添加以下內容:
sshd:all:deny   #不允許所有IP登錄sshd,hosts.allow優先級大於hosts.deny
in.telnet:ALL
ALL:ALL EXCEPT 192.168.0.1/255.255.255.0,192.168.2.21,\   #不允許的IP
202.10.5.0/255.255.255.0

注意:service_name必須與xinetd或/etc/rc.d/init.d/*里面的程序名稱相同:

例子2:

(1)只允許140.116.44.0/255.255.255.0和140.116.79.0/255.255.255.0這兩個網域,以及140.116.141.99這個主機進入Telnet服務器

(2)其他IP全部攔截

首先,# vim /etc/hosts.allow ,添加以下內容:

telnet: 140.116.44.0/255.255.255.0 :allow  #允許
telnet: 140.116.79.0/255.255.255.0 :allow  #允許
telnet: 140.116.141.99                     #允許

其次,# vim /etc/hosts.deny, 設置為全部攔截:

telnet: ALL : deny

ALL代表全部,更安全的設置是:當有人掃描Telnet端口時,就記住其IP,作為查詢與認證之用。修改如下:

# vim /etc/hosts.deny,內容為:

#telnet: ALL : deny
telnet: ALL : spawn (echo Security notice fromo host '/bin/hostname'; \
echo; /usr/sbin/safe_fingetr @%h) | \
/bin/mail -s "%d-%h security" root & \
:twist ( /bin/ehco -e "\n\nWARNING connection not allowed.Your attempt has been logged.
#警告您尚未允許登錄,您的聯機將會被記錄,並且作為以后的參考

注意:

       <1>root可以寫成個人賬號或其他E-mail。

       <2>某些沒有安裝tcp_wrappers的套件的distribution中,由於沒有safe_finger等程序,無法執行相關的功能。

 
        

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM