4,035 瀏覽數

Ubuntu + SSH

安裝好的 Ubuntu Desktop 是不允許透過 SSH 連進來的,因為預設是沒有安裝該服務,安裝也很簡單,直接安裝套件 openssh-server

root@ben-virtual-machine:~#sudo apt-get install openssh-server
  • Sudo

    下次透過 Putty ,Tera Term 以及其它遠端的連線成是進行連結即可.不過 Ubuntu 不允許 root 從遠單登入,所以我們可以用一般使用者登入再轉換(sudo su -)成 root 的使用者

    appledeAir:~ ben$ ssh ben@172.16.0.42
    login as: ben
    ben@172.16.0.42's password:
    ...
    ben@ben-virtual-machine:~$ sudo su -
    [sudo] password for ben:
    

    不過不是每個使用者都是可以使用 sudo

    appledeAir:~ ben$ ssh benben@172.16.0.42
    login as: benben
    ben@172.16.0.42's password:
    ...
    benben@ben-virtual-machine:~# sudo su -
    [sudo] password for benben: 
    benben is not in the sudoers file.  This incident will be reported.
    

    這時候需要修改 sudoers 設定檔.

    root@ben-virtual-machine:~# vi /etc/sudoers
    # User privilege specification
    root    ALL=(ALL:ALL) ALL
    benben  ALL=(ALL:ALL) ALL
    
    appledeAir:~ ben$ ssh benben@172.16.0.42
    login as: benben
    ben@172.16.0.42's password:
    ...
    benben@ben-virtual-machine:~# sudo su -
    [sudo] password for benben: 
    root@ubuntu:~#
    
  • PermitRootLogin

    如果要讓 Root 可以登入,需要修改 SSH 設定檔 sshd_config

    root@ben-virtual-machine:~# vim /etc/ssh/sshd_config
    #PermitRootLogin prohibit-password
    PermitRootLogin yes
    root@ben-virtual-machine:~# initctl restart sshd
    
    appledeAir:~ ben$ ssh root@172.16.0.42
    root@172.16.15.208's password: 
    Welcome to Ubuntu 16.04.1 LTS (GNU/Linux 4.4.0-47-generic x86_64)
    
     * Documentation:  https://help.ubuntu.com
     * Management:     https://landscape.canonical.com
     * Support:        https://ubuntu.com/advantage
    
    3 packages can be updated.
    0 updates are security updates.
    
    Last login: Thu Nov 24 23:13:27 2016 from 172.16.15.1
    root@ben-virtual-machine:~#
    

到這邊結束??現在程式是安裝完就可以用了?? 在哪些 runlevel 會執行呢!!

首先該程式在安裝完成後就在執行了嗎??

root@ben-virtual-machine:~# /etc/init.d/ssh status
Rather than invoking init scripts through /etc/init.d, use the service(8)
utility, e.g. service ssh status

Since the script you are attempting to invoke has been converted to an
Upstart job, you may also use the status(8) utility, e.g. status ssh
ssh start/running, process 4616

什麼 /etc/init.d/ssh status 沒有辦法看,是因為 Ubuntu 的開機方式變成 upstart??幸好 #service ssh status 還可以使用.

root@ben-virtual-machine:~# service ssh status
ssh start/running, process 4616

試試看 upstart 所提供的指令 status (start , stop…)

root@ben-virtual-machine:~# status ssh
ssh start/running, process 4616

恩恩!!跟 #service ssh status 顯示出一樣的結果.

那 哪些 runlevel 會執行呢!! 預設安裝沒有 chkconfig

root@ben-virtual-machine:~# chkconfig
The program 'chkconfig' is currently not installed. You can install it by typing:
apt-get install chkconfig

好吧!!下載裝一下好了!!什麼沒有提供 chkconfig ~~~

root@ben-virtual-machine:~# apt-get install chkconfig
Reading package lists... Done
Building dependency tree
Reading state information... Done
Package chkconfig is not available, but is referred to by another package.
This may mean that the package is missing, has been obsoleted, or
is only available from another source

E: Package 'chkconfig' has no installation candidate

上網查了一下,看樣子在 Ubuntu 下是另外一隻程式可以取代 sysv-rc-conf

root@ben-virtual-machine:~# apt-get install sysv-rc-conf
root@ben-virtual-machine:~# sysv-rc-conf

奇怪怎麼看起來怪怪的.
sysv-rc-conf01

  • Ubuntu14.04 – upstart

    ssh 並沒有在任何的 Runlevel 中執行,這是因為 upstart 的原因嗎?? 是的 請參考 System v 與 Upstart 的說明.

    Ubuntu 14.04 採用 upstart 啟動方式,需要透過 #initctl list 來管理或是看服務狀態,至於 ssh 設定檔則要看 /etc/init/ssh.conf .

    root@ben-virtual-machine:~# initctl list |grep -i ssh
    ssh start/running, process 1080
    
    root@ben-virtual-machine:~# cat /etc/init/ssh.conf 
    # ssh - OpenBSD Secure Shell server
    #
    # The OpenSSH server provides secure shell access to the system.
    
    description	"OpenSSH server"
    
    start on runlevel [2345]
    stop on runlevel [!2345]
    
    respawn
    respawn limit 10 5
    umask 022
    
    env SSH_SIGSTOP=1
    expect stop
    
    # 'sshd -D' leaks stderr and confuses things in conjunction with 'console log'
    console none
    
    pre-start script
        test -x /usr/sbin/sshd || { stop; exit 0; }
        test -e /etc/ssh/sshd_not_to_be_run && { stop; exit 0; }
    
        mkdir -p -m0755 /var/run/sshd
    end script
    
    # if you used to set SSHD_OPTS in /etc/default/ssh, you can change the
    # 'exec' line here instead
    exec /usr/sbin/sshd -D
    

    因為沒有提供 #chkconfig 所以只能透過修改設定檔來決定哪一些 runlevel 需要執行.設定方式如下:
    start on runlevel [2345]
    stop on runlevel [!2345]

    開機進入 runlevel 2345 的時候需要執行,切換到 runlevel 2345 (不一樣的 runlevel 時) 需要停止.

    要注意一點的是 Ubuntu 的 runlevel 跟 RedHat 的不太一樣.
    0 : System halt.
    1 : Single-User mode.
    2 : Graphical multi-user plus networking (DEFAULT)
    3 : Same as “2″, but not used.
    4 : Same as “2″, but not used.
    5 : Same as “2″, but not used.
    6 : System reboot.

  • Ubuntu16.04 – systemd

    Ubuntu16.04 採用了 systemd 指令需要改成 #systemctl 請參考 http://benjr.tw/94315

網友的想法

  1. 怪事發生,剛安裝好 ubuntu 14.04 desktop AMD64 版本,怎麼透過 Ubuntu Software Center 或是 #apt-get 都找不到 openssh-server.

    # apt-get install openssh-server
    Reading package lists… Done
    Building dependency tree
    Reading state information… Done
    Package openssh-server is not available, but is referred to by another package.
    This may mean that the package is missing, has been obsoleted, or
    is only available from another source
    
    E: Package ‘openssh-server’ has no installation candidate
    

    後來先更新了 #apt-get update ,才能找到.

    #apt-get update
    #apt-get install openssh-server
    

    不過在 Ubuntu Software Center 還是一樣找不到 openssh-server .
    Ubuntu_software_center01

發表迴響