如何给EC2服务器安装图形化桌面

因为AWS的虚拟机EC2默认都是server版的操作系统,对于用惯了界面的童鞋未免有些残忍,今天我们就来DIY一款带桌面的服务器吧!

-- D.C

启动EC2,选择合适的镜像。

这里以centos7为例,启动实例,搜索AMI ID 为 ami-0a809ad76fc7e8390 的centos7镜像,启动。

登录EC2,安装插件。

ssh -i "yourkey.pem" centos@ec2-xxx-xxx-xxx-xx.cn-northwest-1.compute.amazonaws.com.cn

安装必要的图形化插件和vnc server。

sudo yum groups install "X Window System" -y
sudo yum install gnome-classic-session gnome-terminal nautilus-open-terminal control-center liberation-mono-fonts -y
sudo systemctl set-default graphical.target
sudo yum install tigervnc-serve

创建vncserver的配置服务并修改。

sudo cp /lib/systemd/system/vncserver@.service /etc/systemd/system/vncserver@:1.service
sudo vi /etc/systemd/system/vncserver@:1.service

修改为:

[Unit]
Description=Remote desktop service (VNC)
After=syslog.target network.target

[Service]
Type=forking
User=root
# Clean any existing files in /tmp/.X11-unix environment
ExecStartPre=/bin/sh -c '/usr/bin/vncserver -kill %i > /dev/null 2>&1 || :'
ExecStart=/sbin/runuser -l root -c "/usr/bin/vncserver %i -geometry 1280x720"
PIDFile=/root/.vnc/%H%i.pid
ExecStop=/bin/sh -c '/usr/bin/vncserver -kill %i > /dev/null 2>&1 || :'

[Install]
WantedBy=multi-user.target

保存,退出。

设置vnc登录密码并启动vnc服务。

sudo vncpasswd
Password:
Verify:
Would you like to enter a view-only password (y/n)? n
A view-only password is not used

启动服务:

$sudo systemctl daemon-reload

$sudo systemctl start vncserver@:1.service

$sudo systemctl status -l vncserver@:1.service
● vncserver@:1.service - Remote desktop service (VNC)
   Loaded: loaded (/etc/systemd/system/vncserver@:1.service; disabled; vendor preset: disabled)
   Active: active (running) since Tue 2020-05-12 09:28:38 UTC; 2s ago
  Process: 2764 ExecStart=/sbin/runuser -l root -c /usr/bin/vncserver %i -geometry 1280x720 (code=exited, status=0/SUCCESS)
  Process: 2759 ExecStartPre=/bin/sh -c /usr/bin/vncserver -kill %i > /dev/null 2>&1 || : (code=exited, status=0/SUCCESS)
 Main PID: 2782 (Xvnc)
   CGroup: /system.slice/system-vncserver.slice/vncserver@:1.service
           ‣ 2782 /usr/bin/Xvnc :1 -auth /root/.Xauthority -desktop ip-xxx-xxx-xxx-xxx.cn-northwest-1.compute.internal:1 (root) -fp catalogue:/etc/X11/fontpath.d -geometry 1280x720 -pn -rfbauth /root/.vnc/passwd -rfbport 5901 -rfbwait 30000

May 12 09:28:35 ip-xxx-xxx-xxx-xxx.cn-northwest-1.compute.internal systemd[1]: Starting Remote desktop service (VNC)...
May 12 09:28:38 ip-xxx-xxx-xxx-xxx.cn-northwest-1.compute.internal systemd[1]: Started Remote desktop service (VNC).

$sudo systemctl enable vncserver@:1.service   #开机启动 

确认下:

$ ps -ef | grep vnc
root      2782     1  0 09:28 ?        00:00:00 /usr/bin/Xvnc :1 -auth /root/.Xauthority -desktop ip-xxx-xxx-xxx-xxx.cn-northwest-1.compute.internal:1 (root) -fp catalogue:/etc/X11/fontpath.d -geometry 1280x720 -pn -rfbauth /root/.vnc/passwd -rfbport 5901 -rfbwait 30000
root      2787     1  0 09:28 ?        00:00:00 /bin/sh /root/.vnc/xstartup
centos    3255  1155  0 09:29 pts/0    00:00:00 grep --color=auto vnc

添加防火墙规则

允许vnc通过5901端口访问服务器。

sudo firewall-cmd --add-port=5901/tcp --permanent

修改这台EC2的安全组SG

在安全组添加一条TCP规则,允许5901端口。

tcp

在本地安装VNCViewer

下载软件 VNCViewer

输入EC2的公网IP:5901, 回车。

vnc

确认以后,输入你的vncpasswd,点击连接,bingo~

login

enjoy~

PS: 其余系统安装步骤都差不多

番外

黄沙百战穿金甲,不破楼兰终不还。