Ubuntu Desktop – SSH Server

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

root@ubuntu:~# apt-get install openssh-server

下次透過 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@ubuntu:~$ sudo su -
[sudo] password for ben:

user is not in the sudoers file

不過不是每個使用者都是可以使用 sudo , ben 這個使用者是在安裝的時就創建的具有 sudo 的權限,先建一個使用者 benben .

root@ubuntu:~# adduser ben sudo
The user `ben' is already a member of `sudo'.
root@ubuntu:~# adduser benben
Adding user `benben' ...
Adding new group `benben' (1001) ...
Adding new user `benben' (1001) with group `benben' ...
Creating home directory `/home/benben' ...
Copying files from `/etc/skel' ...
Enter new UNIX password: 
Retype new UNIX password: 
passwd: password updated successfully
Changing the user information for benben
Enter the new value, or press ENTER for the default
	Full Name []: 
	Room Number []: 
	Work Phone []: 
	Home Phone []: 
	Other []: 
Is the information correct? [Y/n] 
appledeAir:~ ben$ ssh benben@172.16.0.42
login as: benben
benben@172.16.0.42's password:
...
benben@ubuntu:~$ sudo su -
[sudo] password for benben: 
benben is not in the sudoers file.  This incident will be reported.

解決方式有兩種.

  • 修改 /etc/sudoers 設定檔
    root@ubuntu:~# visudo
    # User privilege specification
    root    ALL=(ALL:ALL) ALL
    benben  ALL=(ALL:ALL) ALL
    
  • 使用者加入到 sudo 群組
    root@ubuntu:~# adduser benben sudo
    Adding user `benben' to group `sudo' ...
    Adding user benben to group sudo
    Done.
    
appledeAir:~ ben$ ssh benben@172.16.0.42
login as: benben
benben@172.16.0.42's password:
...
benben@ubuntu:~$ sudo su -
[sudo] password for benben: 

PermitRootLogin

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

root@ubuntu:~#  vim /etc/ssh/sshd_config
#PermitRootLogin prohibit-password
PermitRootLogin yes
root@ubuntu:~# 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

ssh 在哪個 Runlevel 中執行,不同 Ubuntu 版本採用不同的方式

  • Ubuntu14.04 – upstart

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

    root@ubuntu:~# initctl list |grep -i ssh
    ssh start/running, process 1080
    
    root@ubuntu:~# 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.
    詳細請參考 System v 與 Upstart – http://benjr.tw/38611 的說明.

  • Ubuntu16.04 – systemd

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

  • http://benjr.tw/96968

One thought on “Ubuntu Desktop – SSH Server

  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

發表迴響

你的電子郵件位址並不會被公開。 必要欄位標記為 *

這個網站採用 Akismet 服務減少垃圾留言。進一步瞭解 Akismet 如何處理網站訪客的留言資料