Ubuntu设置root用户,以及开启Ubuntu的ssh连接,更改apt-get源

Scroll Down

1、设置root用户(Ubuntu16)

【1】默认没有开启root用户,切换root如下所示

dk@dk-machine:~$ su root
密码: 
su:认证失败
dk@dk-machine:~$ 

使用命令sudo passwd root,然后输入用户名和密码,接下来输入root用户的密码并确认,然后使用命令su root,输入root的密码后,既转为root用户:

dk@dk-machine:~$ sudo passwd root
[sudo] dk 的密码: 
输入新的 UNIX 密码: 
重新输入新的 UNIX 密码: 
passwd:已成功更新密码
dk@dk-machine:~$ su root
密码: 
root@dk-machine:/home/dk# 

【2】修改配置文件

root@dk-machine:/home/dk# cd /usr/share/lightdm/lightdm.conf.d/
root@dk-machine:/usr/share/lightdm/lightdm.conf.d# ls
50-disable-log-backup.conf  50-guest-wrapper.conf  50-unity-greeter.conf
50-greeter-wrapper.conf     50-ubuntu.conf         50-xserver-command.conf
root@dk-machine:/usr/share/lightdm/lightdm.conf.d# gedit 50-unity-greeter.conf 

改文件原来的内容如下:

[Seat:*]
greeter-session=unity-greeter

修改为:

[Seat:*]
greeter-session=unity-greeter

user-session=ubuntu
greeter-show-manual-login=true
all-guest=false

用root用户在图形界面登录有错误---读取/root/.profile时发生错误:mesg:tty n还需要做如下修改:

使用命令gedit /root/.profile打开该文件

# ~/.profile: executed by Bourne-compatible login shells.

if [ "$BASH" ]; then
  if [ -f ~/.bashrc ]; then
    . ~/.bashrc
  fi
fi

mesg n || true

修改为

# ~/.profile: executed by Bourne-compatible login shells.

if [ "$BASH" ]; then
  if [ -f ~/.bashrc ]; then
    . ~/.bashrc
  fi
fi

tty -s && mesg n || true

保存重启,之后点击登陆,输入root,然后输入root密码即可使用root登陆。

1、设置root用户(Ubuntu18)

  • 打开文件/etc/pam.d/gdm-autologinauth required pam_succeed_if.so user != root quiet_success 注释, 保存。

  • 打开文件/etc/pam.d/gdm-passwordauth required pam_succeed_if.so user != root quiet_success 注释,保存。

  • 打开文件/root/.profile将最后一行修改为:tty -s && mesg n || true  

2、开启Ubuntu的ssh连接

【1】Ubuntu默认没有开启ssh的连接,所以使用xshell无法连接到虚拟机,下面开启。首先安装shh

apt-get install openssh-server

查看ssh是否启动:

ps -e|grep ssh
root@dk-machine:~# ps -e|grep ssh
  3733 ?        00:00:00 sshd
root@dk-machine:~# 

如果没有启动使用下面命令启动:

service ssh start

【2】ssh设置root远程登陆

gedit /etc/ssh/sshd_config

将第28行的内容注释掉

#PermitRootLogin prohibit-password

修改为:

PermitRootLogin yes

如下:

# Authentication:
LoginGraceTime 120
#PermitRootLogin prohibit-password
PermitRootLogin yes
StrictModes yes

接着重启电脑就可以使用Xshell连接了。

Connecting to 192.168.80.93:22...
Connection established.
To escape to local shell, press 'Ctrl+Alt+]'.

Welcome to Ubuntu 16.04.4 LTS (GNU/Linux 4.13.0-36-generic x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

512 个可升级软件包。
354 个安全更新。

有新版本“18.04.2 LTS”可供使用
运行“do-release-upgrade”来升级到新版本。

Last login: Thu Aug  8 18:38:24 2019 from 192.168.80.1
root@dk-machine:~# 

3、修改Ubuntu的apt-get源为国内镜像源

【1】原文件备份

sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak

【2】编辑源列表文件

sudo vim /etc/apt/sources.list

【3】将原来的列表删除,添加如下内容(中科大镜像源)

deb http://mirrors.ustc.edu.cn/ubuntu/ xenial main restricted universe multiverse
deb http://mirrors.ustc.edu.cn/ubuntu/ xenial-security main restricted universe multiverse
deb http://mirrors.ustc.edu.cn/ubuntu/ xenial-updates main restricted universe multiverse
deb http://mirrors.ustc.edu.cn/ubuntu/ xenial-proposed main restricted universe multiverse
deb http://mirrors.ustc.edu.cn/ubuntu/ xenial-backports main restricted universe multiverse
deb-src http://mirrors.ustc.edu.cn/ubuntu/ xenial main restricted universe multiverse
deb-src http://mirrors.ustc.edu.cn/ubuntu/ xenial-security main restricted universe multiverse
deb-src http://mirrors.ustc.edu.cn/ubuntu/ xenial-updates main restricted universe multiverse
deb-src http://mirrors.ustc.edu.cn/ubuntu/ xenial-proposed main restricted universe multiverse
deb-src http://mirrors.ustc.edu.cn/ubuntu/ xenial-backports main restricted universe multiverse

【4】运行sudo apt-get update

步骤如下:

root@dk-machine:~# cd /etc/apt
root@dk-machine:/etc/apt# ls
apt.conf.d     sources.list      sources.list.d  trusted.gpg.d
preferences.d  sources.list.bak  trusted.gpg
root@dk-machine:/etc/apt# cp /etc/apt/sources.list /etc/apt/sources.list.bak
root@dk-machine:/etc/apt# gedit /etc/apt/sources.list
root@dk-machine:/etc/apt# sudo apt-get update
获取:1 http://mirrors.ustc.edu.cn/ubuntu xenial InRelease [247 kB]
获取:2 http://mirrors.ustc.edu.cn/ubuntu xenial-security InRelease [109 kB]
获取:3 http://mirrors.ustc.edu.cn/ubuntu xenial-updates InRelease [109 kB]
获取:4 http://mirrors.ustc.edu.cn/ubuntu xenial-proposed InRelease [260 kB]
获取:5 http://mirrors.ustc.edu.cn/ubuntu xenial-backports InRelease [107 kB]
获取:6 http://mirrors.ustc.edu.cn/ubuntu xenial/main Sources [868 kB]
获取:7 http://mirrors.ustc.edu.cn/ubuntu xenial/restricted Sources [4,808 B]