笔记:在CentOS上开始你的工作

版权声明:本文为博主原创文章,转载请注明出处,谢谢合作! https://blog.csdn.net/sch0120/article/details/79312604

基本设置

联网

CentOS安装后默认是不联网的,网卡处于down的状态,可以通过下面的步骤启动网卡。

# cd /etc/sysconfig/network-scripts
# ls ifcfg-* | grep -v ifcfg-lo
ifcfg-eno1
# ifup eno1

现在我们可以用过ip addr命令来验证网卡的启动情况了。

然而,这个设置只是临时的,如果需要让CentOS每次启动时都会为我们自动启动这块网卡。我们可以修改/etc/sysconfig/network-scripts/ifcfg-eno1配置文件,将其中的ONBOOT=no改为ONBOOT=yes来实现网卡的自动启动。

设置主机名

我们可以通过下面的命令完成对主机名的设置:

sudo hostname cheshi-desktop1.nay.redhat.com
sudo sh -c "echo cheshi-desktop1.nay.redhat.com > /etc/hostname"

重新打开终端窗口后就可以看到变化。

设置时区

我们可以使用下面的命令将系统时区设置为中国:

# ls -la /etc/localtime
lrwxrwxrwx. 1 root root 38 Jan 29 20:40 /etc/localtime -> ../usr/share/zoneinfo/America/New_York
# rm -f /etc/localtime
# ln -s ../usr/share/zoneinfo/Asia/Shanghai /etc/localtime
# ls -la /etc/localtime
lrwxrwxrwx. 1 root root 35 Jan 31 15:27 /etc/localtime -> ../usr/share/zoneinfo/Asia/Shanghai

重启系统后新的时区设置就会生效。

创建用户

创建账户

添加一个名为cheshi的用户,并为其设置密码:

# useradd cheshi
# passwd cheshi

删除账户

此外,删除一个名为cheshi的用户,方法如下:

# userdel cheshi

userdel命令的-r参数用于同时删除用户的家目录,-f参数用于强制删除一个正在登录的用户。

设置为sudoer

使用visudoer命令,打开/etc/sudoers配置文件,找到名为root的用户,并在其下方添加新用户,保存后这个用户就具有了sudo权限。

更改后的配置文件大约会是这个样子:

## Allow root to run any commands anywhere
root    ALL=(ALL)       ALL
cheshi  ALL=(ALL)       ALL

配置VNC

安装GUI界面

首先我们需要安装桌面环境,也就是GUI界面,它是VNC的基础,而默认最小安装的CentOS是不带GUI界面的。

但我们可以通过下面的命令来安装GNOME桌面环境:

# yum groupinstall "GNOME Desktop" "Graphical Administration Tools"

安装大概需要下载600MB左右的文件。

参考文献:http://www.linuxidc.com/Linux/2017-03/141465.htm

安装VNC Server

通过下面的命令安装VNC Server:

$ sudo yum install tigervnc-server

配置VNC

配置VNC有两种方法,一种是通过vncserver命令直接启动,这个方法简单直接,另一种是通过systemctl来启动VNC服务,这个方法可以自动启动VNC。

实测第二种方法启动后的界面有些问题,无法正常使用,因此这里只介绍第一种方法。
有关这个问题的bug可以参见:https://github.com/TigerVNC/tigervnc/issues/577

首先使用vncpasswd命令设置VNC密码,然后使用vncserver命令创建VNC桌面。一个VNC桌面对应一个VNC Server进程,也对应一个端口号。使用的时候通过在客户端指定不同的端口号,连接不同的VNC桌面。

$ vncpasswd
Password:
Verify:
Would you like to enter a view-only password (y/n)? y
Password:
Verify:

$ vncserver

New 'dhcp-15-173.nay.redhat.com:1 (cheshi)' desktop is dhcp-15-173.nay.redhat.com:1

Starting applications specified in /home/cheshi/.vnc/xstartup
Log file is /home/cheshi/.vnc/dhcp-15-173.nay.redhat.com:1.log

首次运行vncserver命令,将会产生一个端口号为1的桌面(这个端口号是VNC虚拟的端口号,一般来说与真实的物理端口号相差5900,后面还会说到)。

设置防火墙

由于使用第一种方法,所以VNC Server并没有对应的服务用于添加到防火墙的白名单里。所以我们可以找到VNC Server对应的端口,然后将其添加到防火墙的白名单,以达到相同的目的。

通过netstat -anotl命令或nmap命令(可通过sudo yum install nmap命令安装),可以得到VNC桌面的对应端口号:

$ nmap localhost

Starting Nmap 6.40 ( http://nmap.org ) at 2018-01-29 22:23 EST
Nmap scan report for localhost (127.0.0.1)
Host is up (0.00063s latency).
Other addresses for localhost (not scanned): 127.0.0.1
Not shown: 994 closed ports
PORT     STATE SERVICE
22/tcp   open  ssh
25/tcp   open  smtp
111/tcp  open  rpcbind
631/tcp  open  ipp
5901/tcp open  vnc-1
6001/tcp open  X11:1

Nmap done: 1 IP address (1 host up) scanned in 0.05 seconds

接下来我们把对应的端口号5901/tcp加入到防火墙的白名单中:

$ sudo firewall-cmd --get-zones
block dmz drop external home internal public trusted work
$ sudo firewall-cmd --get-default-zone
public
$ sudo firewall-cmd --add-port=5901/tcp
success

连接VNC

通过另外一台计算机上的VNC Viewer或者Remmina一类的软件,连接我们前面配置好的VNC Server即可。

连接时直接指定VNC Server上的虚拟端口号即可,例如:vncviewer 10.0.0.1:1

重新配置VNC桌面

验证过VNC一切正常后,下面就可以精心设置自己的VNC桌面了。为了避免资源浪费,我们首先需要刚才建立的VNC桌面:

$ vncserver -kill :1
Killing Xvnc process ID 3064

然后我们更改一下VNC桌面的配置。经过刚才的步骤,VNC应该已经为我们自动创建好了配置文件,它的位置应该是~/.vnc/config。我们需要向其中写入默认的桌面大小,将geometry=1920x1080加入到配置文件中,这样当我们没有通过vncserver命令指定桌面大小时,VNC就会使用这个默认值。

可选的配置还有很多,这里就不再赘述。接下来,我们重新启动刚才的桌面,这次我们可以通过指定端口号的方式,启动一个VNC桌面:

$ vncserver :1

New 'dhcp-15-173.nay.redhat.com:1 (cheshi)' desktop is dhcp-15-173.nay.redhat.com:1

Starting applications specified in /home/cheshi/.vnc/xstartup
Log file is /home/cheshi/.vnc/dhcp-15-173.nay.redhat.com:1.log

再次登录这个桌面,应该就会看到效果。

高级设置

使用ZSH

安装ZSH

ZSH就是”Z Shell”,最小化安装的CentOS并不带有这个shell,这一点可以通过zsh --version来验证。它的安装方法也很简单:

$ sudo yum install zsh

参考文献: https://github.com/robbyrussell/oh-my-zsh/wiki/Installing-ZSH

配置ZSH

第一次运行时,ZSH会提示我们进行配置,所有的配置信息都会被保存到~/.zshrc中,但是由于一会儿我们要使用”Oh My Zsh”的模板覆盖这个配置文件,因此这里我们选择”0”以跳过设置。

$ zsh

This is the Z Shell configuration function for new users,
zsh-newuser-install.
You are seeing this message because you have no zsh startup files
(the files .zshenv, .zprofile, .zshrc, .zlogin in the directory
~).  This function can help you with a few settings that should
make your use of the shell easier.

You can:

(q)  Quit and do nothing.  The function will be run again next time.

(0)  Exit, creating the file ~/.zshrc containing just a comment.
     That will prevent this function being run again.

(1)  Continue to the main menu.

--- Type one of the keys in parentheses --- 

如果想要设置ZSH为当前用户默认的Shell,可以执行chsh -s $(which zsh)这条命令。

使用”Oh My Zsh”

“Oh My Zsh”是一个开源项目,它提供了很多ZSH的主题,使得ZSH更加强大并易于使用。它的全部代码和文档都可以在GitHub上找到。

安装”Oh My Zsh”最便捷的方式是执行官方提供的install.sh,不过在此之前我们需要执行sudo yum install -y git,来安装脚本所需的git命令。

接下来,我们就可以开始安装”Oh My Zsh”了:

$ sh -c "$(curl -fsSL https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"
Cloning Oh My Zsh...
Cloning into '/home/cheshi/.oh-my-zsh'...
remote: Counting objects: 849, done.
remote: Compressing objects: 100% (713/713), done.
remote: Total 849 (delta 16), reused 769 (delta 10), pack-reused 0
Receiving objects: 100% (849/849), 578.52 KiB | 181.00 KiB/s, done.
Resolving deltas: 100% (16/16), done.
Looking for an existing zsh config...
Found ~/.zshrc. Backing up to ~/.zshrc.pre-oh-my-zsh
Using the Oh My Zsh template file and adding it to ~/.zshrc
Time to change your default shell to zsh!
Changing shell for cheshi.
Password: 
Shell changed.
         __                                     __   
  ____  / /_     ____ ___  __  __   ____  _____/ /_  
 / __ \/ __ \   / __ `__ \/ / / /  /_  / / ___/ __ \ 
/ /_/ / / / /  / / / / / / /_/ /    / /_(__  ) / / / 
\____/_/ /_/  /_/ /_/ /_/\__, /    /___/____/_/ /_/  
                        /____/                       ....is now installed!


Please look over the ~/.zshrc file to select plugins, themes, and options.

p.s. Follow us at https://twitter.com/ohmyzsh.

p.p.s. Get stickers and t-shirts at https://shop.planetargon.com.

→  ~ 

执行下面的命令以更改主题为”agnoster”,这是一款很酷的主题:

$ sed -i 's/^ZSH_THEME=.*/ZSH_THEME="agnoster"/' ~/.zshrc

当你下次进入ZSH的时候,可能会发现它变得一团糟,这是因为你还没有安装主题所需的字体,以及更改终端颜色。

为了配合ZSH的大部分主题,我们需要安装Power Line字体。安装的命令如下:

git clone https://github.com/powerline/fonts.git --depth=1
cd fonts && ./install.sh
cd .. && rm -rf fonts

接下来,我们要设置终端的颜色,步骤略微有点复杂:
1. 点击终端窗口菜单栏上的”Edit” > “Preferences”
2. 翻到”Profiles”选项卡,并点击”Clone”按钮
3. 在”General”选项中更改”Profile name”为自己喜欢的名字,例如”Profile for Zsh”
4. 勾选”Custom font”并将字体设置为”Monospace Regular - 11pt”(或者是”Noto Mono for Powerline Regular - 12pt”以及其他你喜欢的字体,只要显示没有乱码即可)
5. 翻到”Colors”选项卡,取消勾选”Use colors from system theme”,并更改”Built-in schemes”为”Solarized dark”
8. 在下面设置”Palette”的地方,更改”Built-in schemes”为”Solarized”
9. 点击最下面的”Close”按钮完成设置
10. 回到之前的窗口,将”Profile used when launching a new terminal”选为刚刚创建的Profile
11. 点击最下面的”Close”按钮完成设置

现在,重新打开新的终端,就可以看到效果了。

提示:重启系统或VNC桌面后,ZSH将变为当前用户的默认Shell。

“Oh My Zsh”官方文档:https://github.com/robbyrussell/oh-my-zsh/blob/master/README.md

使用EPEL

首先找到最新的EPEL安装包,CentOS7对应的安装包位于http://dl.fedoraproject.org/pub/epel/7/x86_64/Packages/下,我们可以通过下面的命令进行安装:

$ wget http://dl.fedoraproject.org/pub/epel/7/x86_64/Packages/e/epel-release-7-11.noarch.rpm
$ sudo rpm -ivh epel-release-7-11.noarch.rpm
$ sudo yum-config-manager epel

常用工具

JSON分析工具 - jq

sudo yum install -y jq

猜你喜欢

转载自blog.csdn.net/sch0120/article/details/79312604