Ubuntu:开启root、apt & apt-get、修改 apt-get 源为国内镜像源、开启SSH、update hostname、改时区

Ubuntu

Ubuntu中的root帐号

Ubuntu中的root帐号默认是被禁用了的,所以登陆的时候没有这个账号

打开终端开启root账户

sudo passwd -u root
#设置root密码,输入两次
sudo passwd   root
#切换root账号
su -
#退出root账户
exit

apt & apt-get

简单来说就是:apt = apt-get、apt-cache 和 apt-config 中最常用命令选项的集合。

我应该使用apt还是apt-get?
既然两个命令都有用,那么我该使用 apt 还是 apt-get 呢?作为一个常规 Linux 用户,系统极客建议大家尽快适应并开始首先使用 apt。不仅因为广大 Linux 发行商都在推荐 apt,更主要的还是它提供了 Linux 包管理的必要选项。 最重要的是,apt 命令选项更少更易记,因此也更易用,所以没理由继续坚持 apt-get。

apt 可以看作 apt-get 和 apt-cache 命令的子集, 可以为包管理提供必要的命令选项。
apt-get 虽然没被弃用,但作为普通用户,还是应该首先使用 apt。

apt 命令 取代的命令 命令的功能
apt install apt-get install 安装软件包
apt remove apt-get remove 移除软件包
apt purge apt-get purge 移除软件包及配置文件
apt update apt-get update 刷新存储库索引
apt upgrade apt-get upgrade 升级所有可升级的软件包
apt autoremove apt-get autoremove 自动删除不需要的包
apt full-upgrade apt-get dist-upgrade 在升级软件包时自动处理依赖关系
apt search apt-cache search 搜索应用程序
apt show apt-cache show 显示安装细节

Ubuntu 16.04 发布时,一个引人注目的新特性便是 apt 命令的引入。其实早在 2014 年,apt 命令就已经发布了第一个稳定版,只是直到 2016 年的 Ubuntu 16.04 系统发布时才开始引人关注。

随着 apt install package 命令的使用频率和普遍性逐步超过 apt-get install package,越来越多的其它 Linux 发行版也开始遵循 Ubuntu 的脚步,开始鼓励用户使用 apt 而不是 apt-get。

那么,apt-get 与 apt 命令之间到底有什么区别呢?如果它们有类似的命令结构,为什么还需要新的 apt 命令呢?是否 apt 真的比 apt-get 更好?普通用户应该使用新的 apt 命令还是坚持旧有习惯继续使用 apt-get 呢?

修改 apt-get 源为国内镜像源的方法

1、原文件备份

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

注意:

切换源之前需要先安装一个gpg,在docker中做的源切换,当换了镜像源后执行apt-get update发现报错 提示我no_pubkey,镜像地址的公钥没有加进去,所以需要加进去,但是添加密钥的时候有提示我如下错误

gnupg, gnupg2 and gnupg1 do not seem to be installed, but one of them is required for this operation

查找解决方案,发现需要先安装gnupg2

#安装gnupg2
apt-get update && apt-get install -y gnupg2

安装成功后切换源,编辑下面的操作。

2、编辑源列表文件

sudo vi /etc/apt/sources.list

3、将原来的列表删除,添加如下内容(阿里镜像源)

cat << EOF > /etc/apt/sources.list
#复制如下内容
deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse 
deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse 
deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse 
deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse 
deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse 
deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse 
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse 
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse 
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse 
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse12345678910 
EOF
#回车结束

4、编辑完成后执行更新命令

#安装公钥
root@master:/etc/apt# apt-key adv --recv-keys --keyserver keyserver.ubuntu.com 9D6D8F6BC857C906
Executing: /tmp/apt-key-gpghome.RLStB8FALc/gpg.1.sh --recv-keys --keyserver keyserver.ubuntu.com 9D6D8F6BC857C906
gpg: key 9D6D8F6BC857C906: 15 signatures not checked due to missing keys
gpg: key 9D6D8F6BC857C906: public key "Debian Security Archive Automatic Signing Key (8/jessie) <[email protected]>" imported
gpg: Total number processed: 1
gpg:               imported: 1
apt-get update
apt update

5、如果出现以下错误:

	E: 无法获得锁 /var/lib/apt/lists/lock - open (11: 资源暂时不可用) 
	E: 无法对目录 /var/lib/apt/lists/ 加锁

是因为上一次更新源的时候非常规中断导致的,解决方法如下:

sudo rm /var/lib/apt/lists/lock

ubuntu开启SSH服务远程登录

ssh–secure shell,提供安全的远程登录。从事嵌入式开发搭建linux开发环境中,ssh的服务的安装是其中必不可少的一步。ssh方便一个开发小组中人员登录一台服务器,从事代码的编写、编译、运行。方便代码的共享及管理。ssh是一种安全协议,主要用于给远程登录会话数据进行加密,保证数据传输的安全。

安装ssh-server服务

sudo apt install openssh-server

查看安装的服务:

dpkg -l | grep ssh

确认ssh-server是否启动了

#如果看到sshd那说明ssh-server已经启动了。
honglei@ubuntu:~$ ps -e | grep ssh
  2904 ?        00:00:00 ssh-agent
  4170 ?        00:00:00 sshd

如果没有则可以这样启动:sudo /etc/init.d/ssh start或sudo service ssh start
配置相关:
ssh-server配置文件位于/etc/ssh/sshd_config,在这里可以定义SSH的服务端口,默认端口是22,你可以自己定义成其他端口号,如222。(或把配置文件中的”PermitRootLogin without-password”加一个”#”号,把它注释掉,再增加一句”PermitRootLogin yes”)
然后重启SSH服务:

sudo /etc/init.d/ssh stop 
sudo /etc/init.d/ssh start

登陆SSH

ssh [email protected]
其中,username为192.168.39.4机器上的用户,需要输入密码。
断开连接:exit

ubuntu开启SSH服务

SSH分客户端openssh-client和openssh-server
如果你只是想登陆别的机器的SSH只需要安装openssh-client(ubuntu有默认安装,如果没有则sudo apt-get install openssh-client),如果要使本机开放SSH服务就需要安装openssh-server
sudo apt-get install openssh-server
然后确认sshserver是否启动了:
ps -e |grep ssh
如果看到sshd那说明ssh-server已经启动了。
如果没有则可以这样启动:sudo /etc/init.d/ssh start
ssh-server配置文件位于/ etc/ssh/sshd_config,在这里可以定义SSH的服务端口,默认端口是22,你可以自己定义成其他端口号,如222。
然后重启SSH服务:
sudo /etc/init.d/ssh stop
sudo /etc/init.d/ssh start
然后使用以下方式登陆SSH:
ssh [email protected] username为192.168.1.112 机器上的用户,需要输入密码。
断开连接:exit

update hostname

查看hostname

uname -a
#或者
hostname

临时修改hostname

#重启系统,hostname又恢复成原来的。并且,修改以后,当前终端下不会即时生效,需要重开一个终端窗口
hostname new_hostname

永久修改hostname

ubuntu系统永久修改hostname只需要修改/etc/hostname文件即可,对于其他linux发行版hostname存在/etc/sysconfig/network中,所以修改hostname时要区分linux发行版。

sudo vi /etc/hostname
k8s-node2-ubunte

修改完hostname以后,建议同时修改下/etc/hosts:

sudo  vi /etc/hosts
127.0.0.1	localhost
127.0.1.1	k8s-node2-ubuntu

/etc/hosts存放的是域名和ip的对应关系,域名和主机名并没有直接关系,可以为一个ip指定任意对应名称,但是建议解析一个ip对应hostname。

重启服务器

重启命令(root用户使用) : 
    1、reboot 
    2、shutdown -r now 立刻重启
    3、shutdown -r 10 过10分钟自动重启
    4、shutdown -r 20:35 在时间为20:35时候重启

    如果是通过shutdown命令设置重启的话,可以用shutdown -c命令取消重启 

    关机命令(root用户使用) : 

    1、halt   立刻关机 
    2、poweroff 立刻关机 
    3、shutdown -h now 立刻关机
    4、shutdown -h 10 10分钟后自动关机 

    如果是通过shutdown命令设置关机的话,可以用shutdown -c命令取消重启

改时区

首先查看时区

honglei@k8s-node2-ubuntu:~$ date -R
Sat, 19 Dec 2020 01:16:14 -0800

修改时区

honglei@k8s-node2-ubuntu:~$ sudo tzselect
[sudo] password for honglei: 
Please identify a location so that time zone rules can be set correctly.
Please select a continent, ocean, "coord", or "TZ".
 1) Africa
 2) Americas
 3) Antarctica
 4) Asia
 5) Atlantic Ocean
 6) Australia
 7) Europe
 8) Indian Ocean
 9) Pacific Ocean
10) coord - I want to use geographical coordinates.
11) TZ - I want to specify the time zone using the Posix TZ format.
#? 4
Please select a country whose clocks agree with yours.
 1) Afghanistan		  18) Israel		    35) Palestine
 2) Armenia		  19) Japan		    36) Philippines
 3) Azerbaijan		  20) Jordan		    37) Qatar
 4) Bahrain		  21) Kazakhstan	    38) Russia
 5) Bangladesh		  22) Korea (North)	    39) Saudi Arabia
 6) Bhutan		  23) Korea (South)	    40) Singapore
 7) Brunei		  24) Kuwait		    41) Sri Lanka
 8) Cambodia		  25) Kyrgyzstan	    42) Syria
 9) China		  26) Laos		    43) Taiwan
10) Cyprus		  27) Lebanon		    44) Tajikistan
11) East Timor		  28) Macau		    45) Thailand
12) Georgia		  29) Malaysia		    46) Turkmenistan
13) Hong Kong		  30) Mongolia		    47) United Arab Emirates
14) India		  31) Myanmar (Burma)	    48) Uzbekistan
15) Indonesia		  32) Nepal		    49) Vietnam
16) Iran		  33) Oman		    50) Yemen
17) Iraq		  34) Pakistan
#? 9
#? 9
Please select one of the following time zone regions.
1) Beijing Time
2) Xinjiang Time
#? 1
The following information has been given:

	China
	Beijing Time

Therefore TZ='Asia/Shanghai' will be used.
Selected time is now:	Sat Dec 19 17:18:55 CST 2020.
Universal Time is now:	Sat Dec 19 09:18:55 UTC 2020.
Is the above information OK?
1) Yes
2) No
#? 1

查找时区

root@ubuntu:~# timedatectl list-timezones | grep Asia/Shanghai
Asia/Shanghai

设置时区

sudo timedatectl set-timezone Asia/Shanghai

猜你喜欢

转载自blog.csdn.net/shenhonglei1234/article/details/111418614
今日推荐