| Linux OpenSSH 安裝和配置 來源:brimmer |
概述
許多網路程式,如 telnet、rsh、rlogin 或 rexec,用明文傳送檢驗和秘密的訊息,所以就可利用任何連接到網路上的電腦監聽這些程式和伺服器之間的通信,並獲取檢驗和秘密訊息。現在,telnet 程式對日常的管理工作是不可少的,但是它又是不安全的,那用什來替代它呢?OpenSSH 就是那些過時的、不安全的遠程登錄程式,如:telnet、rlogin、rsh、rdist 或 rcp 的替代品。
在 OpenSSH 的 README 中提到:ssh(Secure Shell)程式可以通過網路登錄到遠程主機並執行命令。它提供了很強的安全驗証可以在不安全的網路中進行安全的通信。
我們把 OpenSSH 配置成支援 TCP-Wrappers(inetd 超級伺服器),這樣能夠進一步地提高安全性。當客戶端的程式提出連接請求的時候,TCP-Wrappers 就會在把連接重定向到 OpenSSH 之前,對連接請求進行驗証和授權。OpenSSH 是自由軟體而且使用不受專利保護的加密算法。因此,我建議你使用 OpenSSH,而不使用 SSH1 和 SSH2(現在使用商用的許可協議)。
注意事項
下面所有的命令都是 Unix 兼容的命令。
源路徑都為“/var/tmp”(當然在實際情況中也可以用其它路徑)。
安裝在 RedHat Linux 6.1 和 6.2 下測試通過。
要用“root”用戶進行安裝。
OpenSSH 的版本是 1.2.3。
軟體的來源:http://violet.ibs.com.au/openssh/。
下載:openssh-1.2.3.tar.gz。
準備工作
編譯 OpenSSH 需要 zlib-devel 軟體。可以用 RedHat 6.1 或 6.2 的光碟安裝。
l 用下面的命令驗証一下在系統中是否已經安裝了 zlib-devel:
[root@deep /]# rpm -qi zlib-devel
l 用下面命令在系統中安裝 zlib-devel:
[root@deep /]# mount /dev/cdrom /mnt/cdrom/
[root@deep /]# cd /mnt/cdrom/RedHat/RPMS/
[root@deep RPMS]# rpm -Uvh zlib-devel-version.i386.rpm
gd ##################################################
[root@deep RPMS]# rpm -Uvh gd-devel-version.i386.rpm
zlib-devel ##################################################
[root@deep RPMS]# cd /; umount /mnt/cdrom/
在使用 OpenSSH 之前,還必須安裝 OpenSSL。因為就算你不使用 OpenSSL 建立或保存加密文件,OpenSSH 需要用 OpenSSL 的文件才能正常執行。
安裝軟體需要注意的問題
最好在編譯前和編譯都做一張系統中所有文件的列表,然用“diff”命令去比較它們,找出其中的差別並知道到底把軟體安裝在哪裡。只要簡單地在編譯之前執行一下命令“find /* >OpenSSH1”,在編譯和安裝完軟體之執行命令“find /* > OpenSSH2”,最用命令“diff OpenSSH1 OpenSSH2 > OpenSSH-Installed”找出變化。
編譯和安裝
把軟體(tar.gz)解壓:
[root@deep /]# cp openssh-version.tar.gz /var/tmp
[root@deep /]# cd /var/tmp
[root@deep tmp]# tar xzpf openssh-version.tar.gz
編譯和優化
第一步
轉到 OpenSSH 的新目錄下,先設置編譯器的編譯參數:
CC="egcs" \
CFLAGS="-O9 -funroll-loops -ffast-math -malign-double -mcpu=pentiumpro -march=pentiumpro -fomit-frame-
pointer -fno-exceptions" \
./configure \
--prefix=/usr \
--sysconfdir=/etc/ssh \
--with-tcp-wrappers \
--with-ipv4-default \
--with-ssl-dir=/usr/include/openssl
這些設置告訴編譯器如何編譯 OpenSSH:
l 鏈接上 libwrap 函數庫並且加上對 TCP Wrappers 的支援
l 禁止 Linux/glibc-2.1.2 中域名解析的延時,縮短建立連接的時間
l 設置 OpenSSL 函數庫的路徑,這樣 OpenSSH 才能正常執行
第二步
現在,編譯和安裝 OpenSSH:
[root@deep openssh-1.2.3]# make
[root@deep openssh-1.2.3]# make install
[root@deep openssh-1.2.3]# make host-key
[root@deep openssh-1.2.3]# install -m644 contrib/redhat/sshd.pam /etc/pam.d/sshd
“make”命令把源文件編譯成可執行的二進制文件,“make install”把二進制文件和配置文件安裝在合適的目錄下。“make host-key”產生主機密匙,“install”命令在 RedHat Linux 上安裝對 OpenSSH 的 PAM 支援。
清除不必要的文件
用下面的命令刪除不必要的文件:
[root@deep /]# cd /var/tmp
[root@deep tmp]# rm -rf openssh-version/ openssh-version.tar.gz
“rm”命令刪除所有編譯和安裝 OpenSSH 所需要的源程式,並且把 OpenSSH 軟體的壓縮包刪除掉。
配置
可以到這去下載“floppy.tgz”文件:http://www.openna.com/books/floppy.tgz。把“floppy.tgz”文件解開之,可以在相應的目錄下發現我們介紹的所有軟體的配置文件。這樣就沒有必要手工重新產生這些文件,或者用拷貝粘貼的方法把它們粘貼到配置文件中去。不管是打算自己動手產生配置文件還是拷貝現成的,你都要學會自己修改配置文件並且把配置文件拷貝到正確的目錄下。下面將具體說明。
為了執行 OpenSSH,必須建立或者把下面的文件拷貝到相應的目錄下:
l 把“sshd_config”文件拷貝到“/etc/ssh”目錄下
l 把“ssh_config”文件拷貝到“/etc/ssh”目錄下
l 把“ssh”文件拷貝到“/etc/pam.d/”目錄下
可以把“floppy.tgz”解壓之,找到上面列出來的文件,並拷貝到合適的目錄下,或者用拷貝粘貼的方法從本書中直接粘貼出。
配置“/etc/ssh/ssh_config”文件
“/etc/ssh/ssh_config”文件是 OpenSSH 系統範圍的配置文件,允許你通過設置不同的選項來改變客戶端程式的執行方式。這個文件的每一行包含“關鍵詞-值”的匹配,其中“關鍵詞”是忽略大小寫的。下面列出來的是最重要的關鍵詞,用 man 命令查看幫助頁(ssh (1))可以得到詳細的列表。
編輯“ssh_config”文件(vi /etc/ssh/ssh_config),添加或改變下面的參數:
# Site-wide defaults for various options
Host *
ForwardAgent no
ForwardX11 no
RhostsAuthentication no
RhostsRSAAuthentication no
RSAAuthentication yes
PasswordAuthentication yes
FallBackToRsh no
UseRsh no
BatchMode no
CheckHostIP yes
StrictHostKeyChecking no
IdentityFile ~/.ssh/identity
Port 22
Cipher blowfish
EscapeChar ~
下面逐行說明上面的選項設置:
Host *
選項“Host”只對能夠匹配面字串的電腦有效。“*”表示所有的電腦。
ForwardAgent no
“ForwardAgent”設置連接是否經過驗証代理(如果存在)轉發給遠程電腦。
ForwardX11 no
“ForwardX11”設置 X11 連接是否被自動重定向到安全的通道和顯示集(DISPLAY set)。
RhostsAuthentication no
“RhostsAuthentication”設置是否使用 rhosts 的安全驗証。
RhostsRSAAuthentication no
“RhostsRSAAuthentication”設置是否使用 RSA 算法的 rhosts 的安全驗証。
RSAAuthentication yes
“RSAAuthentication”設置是否使用 RSA 算法進行安全驗証。
PasswordAuthentication yes
“PasswordAuthentication”設置是否使用檢驗驗証。
FallBackToRsh no
“FallBackToRsh”設置如果用 ssh 連接出現錯誤是否自動使用 rsh。
UseRsh no
“UseRsh”設置是否在這台電腦上使用“rlogin/rsh”。
BatchMode no
“BatchMode”如果設為“yes”,passphrase/password(交互式輸入檢驗)的提示將被禁止。當不能交互式輸入檢驗的時候,這個選項對 Script 文件和批處理任務十分有用。
CheckHostIP yes
“CheckHostIP”設置 ssh 是否查看連接到伺服器的主機的 IP 地址以防止 DNS 欺騙。建議設置為“yes”。
StrictHostKeyChecking no
“StrictHostKeyChecking”如果設置成“yes”,ssh 就不會自動把電腦的密匙加入“$HOME/.ssh/known_hosts”文件,並且一旦電腦的密匙發生了變化,就拒絕連接。
IdentityFile ~/.ssh/identity
“IdentityFile”設置從哪個文件讀取用戶的 RSA 安全驗証標識。
Port 22
“Port”設置連接到遠程主機的端口。
Cipher blowfish
“Cipher”設置加密用的密碼。
EscapeChar ~
“EscapeChar”設置 escape 字符。
配置“/etc/ssh/sshd_config”文件
“/etc/ssh/sshd_config”是 OpenSSH 的配置文件,允許設置選項改變這個 daemon 的執行。這個文件的每一行包含“關鍵詞-值”的匹配,其中“關鍵詞”是忽略大小寫的。下面列出來的是最重要的關鍵詞,用man命令查看幫助頁(sshd (8))可以得到詳細的列表。
編輯“sshd_config”文件(vi /etc/ssh/sshd_config),加入或改變下面的參數:
# This is ssh server systemwide configuration file.
Port 22
ListenAddress 192.168.1.1
HostKey /etc/ssh/ssh_host_key
ServerKeyBits 1024
LoginGraceTime 600
KeyRegenerationInterval 3600
PermitRootLogin no
IgnoreRhosts yes
IgnoreUserKnownHosts yes
StrictModes yes
X11Forwarding no
PrintMotd yes
SyslogFacility AUTH
LogLevel INFO
RhostsAuthentication no
RhostsRSAAuthentication no
RSAAuthentication yes
PasswordAuthentication yes
PermitEmptyPasswords no
AllowUsers admin
下面逐行說明上面的選項設置:
Port 22
“Port”設置 sshd 監聽的埠。
ListenAddress 192.168.1.1
“ListenAddress”設置 sshd 伺服器限定的 IP 地址。
HostKey /etc/ssh/ssh_host_key
“HostKey”設置包含電腦私人密匙的文件。
ServerKeyBits 1024
“ServerKeyBits”定義伺服器密匙的位數。
LoginGraceTime 600
“LoginGraceTime”設置如果用戶不能成功登錄,在切斷連接之前伺服器需要等待的時間(以秒為單位)。
KeyRegenerationInterval 3600
“KeyRegenerationInterval”設置在多少秒之自動重新產生伺服器的密匙(如果使用密匙)。重新產生密匙是為了防止用盜用的密匙解密被截獲的訊息。
PermitRootLogin no
“PermitRootLogin”設置 root 能不能用 ssh 登錄。這個選項一定不要設成“yes”。
IgnoreRhosts yes
“IgnoreRhosts”設置驗証的時候是否使用“rhosts”和“shosts”文件。
IgnoreUserKnownHosts yes
“IgnoreUserKnownHosts”設置 ssh daemon 是否在進行 RhostsRSAAuthentication 安全驗証的時候忽略用戶的“$HOME/.ssh/known_hosts”
StrictModes yes
“StrictModes”設置 ssh 在接收登錄請求之前是否檢查用戶家目錄和 rhosts 文件的權限和所有權。這通常是必要的,因為新手經常會把自己的目錄和文件設成任何人都有寫權限。
X11Forwarding no
“X11Forwarding”設置是否允許 X11 轉發。
PrintMotd yes
“PrintMotd”設置 sshd 是否在用戶登錄的時候顯示“/etc/motd”中的訊息。
SyslogFacility AUTH
“SyslogFacility”設置在記錄來自 sshd 的消息的時候,是否給出“facility code”。
LogLevel INFO
“LogLevel”設置記錄 sshd 日誌消息的層次。INFO 是一個好的選擇。查看 sshd 的 man 幫助頁,已獲取更多的訊息。
RhostsAuthentication no
“RhostsAuthentication”設置只用 rhosts 或“/etc/hosts.equiv”進行安全驗証是否已經足夠了。
RhostsRSAAuthentication no
“RhostsRSA”設置是否允許用 rhosts 或“/etc/hosts.equiv”加上 RSA 進行安全驗証。
RSAAuthentication yes
“RSAAuthentication”設置是否允許只有 RSA 安全驗証。
PasswordAuthentication yes
“PasswordAuthentication”設置是否允許檢驗驗証。
PermitEmptyPasswords no
“PermitEmptyPasswords”設置是否允許用檢驗為空的帳號登錄。
AllowUsers admin
“AllowUsers”的面可以跟著任意的數量的用戶名的匹配串(patterns)或 user@host 這樣的匹配串,這些字符串用空格隔開。主機名可以是 DNS 名或 IP 地址。
配置 OpenSSH 使其使用 TCP-Wrappers inetd 超級伺服器
TCP-WRAPPERS 用來啟動和停止 sshd1 服務。當 inetd 執行的時候,它會從配置文件(默認為“/etc/inetd.conf”)中讀入配置訊息。在配置文件中每一行的不同項是用 TAB 或空格分開。
第一步
編輯“inetd.conf”文件(vi /etc/inetd.conf)並加入這一行:
ssh stream tcp nowait root /usr/sbin/tcpd sshd i
注意:“-i”參數很重要,它說明 sshd 是被 inetd 執行的。在加入這一行,通過發送一個 SIGHUP 信號(killall HUP inetd)來更新“inetd.conf”文件。
[root@deep /root]# killall -HUP inetd
第二步
編輯“hosts.allow”文件(vi /etc/hosts.allow)並加入這一行:
sshd: 192.168.1.4 win.openarch.com
這一行表示IP地址為“192.168.1.4”,主機名為“win.openarch.com”的電腦允許用 ssh 訪問伺服器。
下面這些“daemon”字符串(用TCP-WRAPPERS)被 sshd1 使用:
sshdfwd-X11 (允許/禁止 X11 轉發).
sshdfwd- (TCP 轉發).
sshdfwd- (port-name 在 /etc/services 中定義。用 TCP 轉發).
注意:如果準備使用 ssh,一定要用在所有的伺服器上。如果十台安全的伺服器和一台不安全的伺服器配在一起,也談不上什安全性。
更多的資料
如果想查找詳細的資料可以用 man 命令查幫助頁,讀取相關訊息:
$ man ssh (1) - OpenSSH secure shell client (remote login program)
$ man ssh [slogin] (1) - OpenSSH secure shell client (remote login program)
$ man ssh-add (1) - adds identities for the authentication agent
$ man ssh-agent (1) - authentication agent
$ man ssh-keygen (1) - authentication key generation
$ man sshd (8) - secure shell daemon
SSH1 每用戶配置
第一步
為本地伺服器建立私有和公用密匙,執行下面的命令:
[root@deep]# su username
[username@deep]$ ssh-keygen1
舉個例子,顯示出來的結果可能是:
Initializing random number generator...
Generating p: ............................++ (distance 430)
Generating q: ......................++ (distance 456)
Computing the keys...
Testing the keys...
Key generation complete.
Enter file in which to save the key (/home/username/.ssh/identity): 【按下 Enter 鍵】
Enter passphrase:
Enter the same passphrase again:
Your identification has been saved in /home/username/.ssh/identity.
Your public key is:
1024 37
14937757511251955533691120318477293862290049394715136511145806108870001764378494676831
29757784315853227236120610062314604405364871843677484233240919418480988907860997175244
46977589647127757030728779973708569993017043141563536333068888944038178461608592483844
590202154102756903055846534063365635584899765402181 username@deep.openarch.com
Your public key has been saved in /home/username/.ssh/identity.pub
注意:如果有多個帳號需要為每個帳號建立一個密匙。
你可能要為下面的伺服器建立密匙:
l Mail 伺服器
l Web 伺服器
l 網關伺服器
這允許對這些伺服器進行有限的訪問,例如,不允許用 Mail 伺服器的帳號訪問 Web 伺服器或網關伺服器。這樣可以增加整體的安全性,即使因為某種原因有一個密匙被泄密了,也不會影響到其它的伺服器。
第二步
把本機的公用密匙(identity.pub)拷貝到遠程主機的“/home/username/.ssh”目錄下,例如,使用“authorized_keys”這個名字。
注意:拷貝文件的一個方法使用 ftp 命令,另一個辦法是把公用密匙用 email(包含“~/.ssh/identity.pub”文件的內容)發給系統管理員。
改變 pass-phrase
用加上“-p”參數的“ssh-keygen”命令,在任何時候都可以改變 pass-phrase。用下面的命令,改變 pass-phrase:
[root@deep]# su username
[username@deep]$ ssh-keygen1 p
Enter file key is in (/home/username/.ssh/identity): [按下 Enter 鍵]
Enter old passphrase:
Key has comment username@deep.openarch.com
Enter new passphrase:
Enter the same passphrase again:
Your identification has been saved with the new passphrase.
OpenSSH 用戶工具
下面列出的是一些我們經常要用到的命令,當然還有很多其它的命令,更詳細的訊息可以查看 man 幫助頁或其它文檔。
ssh
ssh(Secure Shell)是用來登錄遠程電腦和在遠程電腦上執行命令的程式。它是用來替代 rlogin 和 rsh,以及在不安全的網路環境下在兩台電腦之間提供安全和加密的訊息交流。X11 連接和 TCP/IP 端口可以被轉發到一個安全的通道裡。
用下面的命令,登錄遠程電腦:
[root@deep]# ssh
例如:
[root@deep]# ssh username www.openarch.com
username@deep.openarch.com’s password:
Last login: Tue Oct 19 1999 18:13:00 -0400 from gate.openarch.com
Welcome to www.openarch.com on Deepforest.
是用來登錄 ssh 伺服器的用戶名,是 ssh 伺服器主機的地址。
scp
可以用這個命令把文件從本地電腦拷貝到遠程電腦,或者反之,甚至可以在兩台遠程電腦之間用“scp”命令拷貝文件。把遠程主機上的文件拷貝到當前目錄的一個簡單的方法如下。
用下面的命令把文件從遠程主機拷貝到本地主機上:
[root@deep /]# su admin
[admin@deep /]$ scp -p :/dir/for/file localdir/to/filelocation
例如:
[username@deep]$ scp -p username@mail:/etc/test1 /tmp
Enter passphrase for RSA key username@mail.openarch.com:
test1 | 2 KB | 2.0 kB/s | ETA: 00:00:00 | 100%
用下面的命令把文件從本地主機拷貝到遠程主機上:
[root@deep /]# su admin
[admin@deep /]$ scp -p localdir/to/filelocation :/dir/for/file
例如:
[username@deep]$ scp -p /usr/bin/test2 username@mail:/var/tmp
username@mails password:
test2 | 7 KB | 7.9 kB/s | ETA: 00:00:00 | 100%
注意:“-p”選項表示文件的改變和訪問時間屬性以及權限,在拷貝過程中被保留。通常是需要這樣的。
安裝到系統中的文件
> /etc/ssh
> /etc/ssh/ssh_config
> /etc/ssh/sshd_config
> /etc/ssh_host_key
> /etc/ssh_host_key.pub
> /usr/bin/ssh
> /usr/bin/slogin
> /usr/man/man1/ssh.1
> /usr/man/man1/scp.1
> /usr/man/man1/ssh-add.1
> /usr/man/man1/ssh-agent.1
> /usr/man/man1/ssh-keygen.1
> /usr/bin/scp
> /usr/bin/ssh-add
> /usr/bin/ssh-agent
> /usr/bin/ssh-keygen
> /usr/man/man1/slogin.1
> /usr/man/man8/sshd.8
> /usr/sbin/sshd
Windows 平台上免費的 SSH 客戶軟體
Putty
Putty的主頁:http://www.chiark.greenend.org.uk/~sgtatham/putty.html
Tera Term Pro and TTSSH
Tera Term Pro 的主頁:http://hp.vector.co.jp/authors/VA002416/teraterm.html
TTSSH Homepage:http://www.zip.com.au/~roca/download.html