Giả sử tôi muốn định cấu hình tùy chọn ssh
của mình cho 30 máy chủ có cùng thiết lập trong tệp .ssh
config
:
Host XXX
HostName XXX.YYY.com
User my_username
Compression yes
Ciphers arcfour,blowfish-cbc
Protocol 2
ControlMaster auto
ControlPath ~/.ssh/%[email protected]%h:%p
IdentityFile ~/.ssh/YYY/id_rsa
trong đó điều duy nhất thay đổi giữa 30 máy này là XXX
.
Thay vì lặp lại cấu trúc trên 30 lần trong tệp config
của tôi, có cách nào khác để xác định một phạm vi máy không?
Từ trang ssh_config(5)
man:
Host Restricts the following declarations (up to the next Host key‐ Word) to be only for those hosts that match one of the patterns given after the keyword. If more than one pattern is provided, they should be separated by whitespace.
...
HostName Specifies the real Host name to log into. This can be used to specify nicknames or abbreviations for hosts. If the hostname contains the character sequence ‘%h’, then this will be replaced with the Host name specified on the commandline (this is useful for manipulating unqualified names).
Vì thế:
Host XXX1 XXX2 XXX3
HostName %h.YYY.com
Để giảm thiểu thiết lập, bạn có thể có .ssh/config
như cái này
Host X01
HostName X01.YYY.com
Host X02
HostName X02.YYY.com
...
Host X01 X02 ...
User my_username
Compression yes
Ciphers arcfour,blowfish-cbc
Protocol 2
ControlMaster auto
ControlPath ~/.ssh/%[email protected]%h:%p
IdentityFile ~/.ssh/YYY/id_rsa
Host X01 X02 ...
có thể được thay thế bằng Host *
nếu mọi Máy chủ có cấu hình sau
Chỉ cần sử dụng *
Xem man ssh_config
:
THỰC TRẠNG Một mẫu bao gồm 0 hoặc nhiều ký tự không phải khoảng trắng, ‘*, (một ký tự đại diện khớp với 0 hoặc nhiều ký tự) hoặc‘?, (Một ký tự đại diện khớp chính xác với một ký tự). Ví dụ: để chỉ định một tập hợp các khai báo cho bất kỳ Máy chủ lưu trữ nào trong bộ tên miền .co.uk., có thể sử dụng mẫu sau:
Host *.co.uk
The following pattern would match any Host in the 192.168.0.[0-9] network range:
Host 192.168.0.?
A pattern-list is a comma-separated list of patterns. Patterns within pattern-lists may be negated by preceding them with an
exclamation mark (‘!’). For example, to allow a key to be used from anywhere within an organisation except from the “dialup”
pool, the following entry (in authorized_keys) could be used:
from="!*.dialup.example.com,*.example.com"
Từ câu trả lời của Ignacio Vazquez-Abrams và H.-Dirk Schmitt, người ta có thể thêm các câu sau vào .ssh/config
Host XXX*
HostName %h.YYY.com
User myname
và sau đó, ví dụ, bạn có thể đăng nhập với tên [email protected] bằng cách
ssh XXX2
cái này hiệu quả với tôi
[.___.] Canonicalizehostname có [.__.] .] user myuser [.__.] Host * .yyy.aogi.ac.nz [.__.] user myuser [.
điều này cho phép một người sử dụng tên trong miền và thay đổi tên người dùng:
[.___.] bluebption
Cách làm việc sau đây.
Host 10.10.* 10.11.*
User vagrant