Merge pull request #3096 from GOLinux/master

[Translated]20150730 Must-Know Linux Commands For New Users.md
This commit is contained in:
joeren 2015-07-31 10:56:52 +08:00
commit 5faf6af237
2 changed files with 185 additions and 186 deletions

View File

@ -1,186 +0,0 @@
Translating by GOLinux!
Must-Know Linux Commands For New Users
================================================================================
![Manage system updates via the command line with dnf on Fedora.](http://www.linux.com/images/stories/41373/fedora-cli.png)
Manage system updates via the command line with dnf on Fedora.
One of the beauties of Linux-based systems is that you can manage your entire system right from the terminal using the command line. The advantage of using the command line is that you can use the same knowledge and skills to manage any Linux distribution.
This is not possible through the graphical user interface (GUI) as each distro, and desktop environment (DE), offers its own user interfaces. To be clear, there are cases in which you will need different commands to perform certain tasks on different distributions, but more or less the concept and ideas remain the same.
In this article, we are going to talk about some of the basic commands that a new Linux user should know. I will show you how to update your system, manage software, manipulate files and switch to root using the command line on three major distributions: Ubuntu (which also includes its flavors and derivatives, and Debian), openSUSE and Fedora.
*Let's get started!*
### Keep your system safe and up-to-date ###
Linux is secure by design, but the fact is that all software has bugs and there could be security holes. So it's very important to keep your system updated. Think of it this way: Running an out-of-date operating system is like being in an armored tank with the doors unlocked. Will the armor protect you? Anyone can enter through the open doors and cause harm. Similarly there can be un-patched holes in your OS which can compromise your systems. Open source communities, unlike the proprietary world, are extremely quick at patching holes, so if you keep your system updated you'll stay safe.
Keep an eye on news sites to be aware of security vulnerabilities. If there is a hole discovered, read about it and update your system as soon as a patch is out. Either way you must make it a practice to run the update commands at least once a week on production machines. If you are running a complicated server then be extra careful and go through the changelog to ensure updates won't break your customization.
**Ubuntu**: Bear one thing in mind: you must always refresh repositories (aka repos) before upgrading the system or installing any software. On Ubuntu, you can update your system with the following commands. The first command refreshes repositories:
sudo apt-get update
Once the repos are updated you can now run the system update command:
sudo apt-get upgrade
However this command doesn't update the kernel and some other packages, so you must also run this command:
sudo apt-get dist-upgrade
**openSUSE**: If you are on openSUSE, you can update the system using these commands (as usual, the first command is meant to update repos)
sudo zypper refresh
sudo zypper up
**Fedora**: If you are on Fedora, you can use the 'dnf' command which is 'kind' of equivalent to zypper and apt-get:
sudo dnf update
sudo dnf upgrade
### Software installation and removal ###
You can install only those packages which are available in the repositories enabled on your system. Every distro comes with some official or third-party repos enabled by default.
**Ubuntu**: To install any package on Ubuntu, first update the repo and then use this syntax:
sudo apt-get install [package_name]
Example:
sudo apt-get install gimp
**openSUSE**: The commands would be:
sudo zypper install [package_name]
**Fedora**: Fedora has dropped 'yum' and now uses 'dnf' so the command would be:
sudo dnf install [package_name]
The procedure to remove the software is the same, just exchange 'install' with 'remove'.
**Ubuntu**:
sudo apt-get remove [package_name]
**openSUSE**:
sudo zypper remove [package_name]
**Fedora**:
sudo dnf remove [package_name]
### How to manage third party software? ###
There is a huge community of developers who offer their software to users. Different distributions use different mechanisms to make third party software available to their users. It also depends on how a developer offers their software to users; some offer binaries and others offer it through repositories.
Ubuntu heavily relies on PPAs (personal package archives) but, unfortunately, there is no built-in tool which can assist a user in searching PPAs. You will need to Google the PPA and then add the repository manually before installing the software. This is how you would add any PPA to your system:
sudo add-apt-repository ppa:<repository-name>
Example: Let's say I want to add LibreOffice PPA to my system. I would Google the PPA and then acquire the repo name from Launchpad, which in this case is "libreoffice/ppa". Then add the ppa using the following command:
sudo add-apt-repository ppa:libreoffice/ppa
It will ask you to hit the Enter key in order to import the keys. Once it's done, refresh the repos with the 'update' command and then install the package.
openSUSE has an elegant solution for third-party apps. You can visit software.opensuse.org, search for the package and install it with one click. It will automatically add the repo to your system. If you want to add any repo manually, use this command:.
sudo zypper ar -f url_of_the_repo name_of_repo
sudo zypper ar -f http://download.opensuse.org/repositories/LibreOffice:Factory/openSUSE_13.2/LibreOffice:Factory.repo LOF
Then refresh the repo and install software:
sudo zypper refresh
sudo zypper install libreoffice
Fedora users can simply add RPMFusion (both free and non-free repos) which contain a majority of applications. In case you do need to add a repo, this is the command:
dnf config-manager --add-repo http://www.example.com/example.repo
### Some basic commands ###
I have written a few [articles][1] on how to manage files on your system using the CLI, here are some of the basic commands which are common across all distributions.
Copy files or directories to a new location:
cp path_of_file_1 path_of_the_directory_where_you_want_to_copy/
Copy all files from a directory to a new location (notice the slash and asterisk, which implies all files within that directory):
cp path_of_files/* path_of_the_directory_where_you_want_to_copy/
Move a file from one location to another (the trailing slash means inside that directory):
mv path_of_file_1 path_of_the_directory_where_you_want_to_move/
Move all file from one location to another:
mv path_of_directory_where_files_are/* path_of_the_directory_where_you_want_to_move/
Delete a file:
rm path_of_file
Delete a directory:
rm -r path_of_directory
Remove all content from the directory, leaving the directory folder intact:
rm -r path_of_directory/*
### Create new directory ###
To create a new directory, first enter the location where you want to create a directory. Let's say you want to create a 'foundation' folder inside your Documents directory. Let's change the directory using the cd (aka change directory) command:
cd /home/swapnil/Documents
(exchange 'swapnil with the user on your system)
Then create the directory with mkdir command:
mkdir foundation
You can also create a directory from anywhere, by giving the path of the directory. For example:
mdkir /home/swapnil/Documents/foundation
If you want to create parent-child directories, which means directories within other directories then use the -p option. It will create all directories in the given path:
mdkir -p /home/swapnil/Documents/linux/foundation
### Become root ###
You either need to be root or the user should have sudo powers to perform some administrative tasks such as managing packages or making changes to the root directories or files. An example would be to edit the 'fstab' file which keeps a record of mounted hard drives. It's inside the 'etc' directory which is within the root directory. You can make changes to this file only as a super user. In most distros you can become root by 'switching user'. Let's say on openSUSE I want to become root as I am going to work inside the root directory. You can use either command:
sudo su -
Or
su -
That will ask for the password and then you will have root privileges. Keep one point in mind: never run your system as root user unless you know what you are doing. Another important point to note is that the files or directories you modify as root also change ownership of those files from that user or specific service to root. You may have to revert the ownership of those files otherwise the services or users won't be able to to access or write to those files. To change users, this is the command:
sudo chown -R user:user /path_of_file_or_directory
You may often need this when you have partitions from other distros mounted on the system. When you try to access files on such partitions, you may come across a permission denied error. You can simply change the ownership of such partitions to access them. Just be extra careful, don't change permissions or ownership of root directories.
These are the basic commands any new Linux user needs. If you have any questions or if you want us to cover a specific topic, please mention them in the comments below.
--------------------------------------------------------------------------------
via: http://www.linux.com/learn/tutorials/842251-must-know-linux-commands-for-new-users
作者:[Swapnil Bhartiya][a]
译者:[译者ID](https://github.com/译者ID)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创翻译,[Linux中国](https://linux.cn/) 荣誉推出
[a]:http://www.linux.com/community/forums/person/61003
[1]:http://www.linux.com/learn/tutorials/828027-how-to-manage-your-files-from-the-command-line

View File

@ -0,0 +1,185 @@
新手应知应会的Linux命令
================================================================================
![Manage system updates via the command line with dnf on Fedora.](http://www.linux.com/images/stories/41373/fedora-cli.png)
在Fedora上通过命令行使用dnf来管理系统更新
基于Linux的系统的优点之一就是你可以通过终端中使用命令该ing来管理整个系统。使用命令行的优势在于你可以使用相同的知识和技能来管理随便哪个Linux发行版。
对于各个发行版以及桌面环境DE而言要一致地使用图形化用户界面GUI却几乎是不可能的因为它们都提供了各自的用户界面。要明确的是有那么些情况你需要在不同的发行版上使用不同的命令来部署某些特定的任务但是或多或少它们的概念和意图却仍然是一致的。
在本文中我们打算讨论Linux用户应当掌握的一些基本命令。我将给大家演示怎样使用命令行来更新系统、管理软件、操作文件以及切换到root这些操作将在三个主要发行版上进行Ubuntu也包括其定制版和衍生版还有DebianopenSUSE以及Fedora。
*让我们开始吧!*
### 保持系统安全和最新 ###
Linux是基于安全设计的但事实上是任何软件都有缺陷会导致安全漏洞。所以保持你的系统更新到最新是十分重要的。这么想吧运行过时的操作系统就像是你坐在全副武装的坦克里头而门却没有锁。武器会保护你吗任何人都可以进入开放的大门对你造成伤害。同样在你的系统中也有没有打补丁的漏洞这些漏洞会危害到你的系统。开源社区不像专利世界在漏洞补丁方面反应是相当快的所以如果你保持系统最新你也获得了安全保证。
留意新闻站点,了解安全漏洞。如果发现了一个漏洞,请阅读之,然后在补丁出来的第一时间更新。不管怎样,在生产机器上,你每星期必须至少运行一次更新命令。如果你运行这一台复杂的服务器,那么就要额外当心了。仔细阅读变更日志,以确保更新不会搞坏你的自定义服务。
**Ubuntu**牢记一点你在升级系统或安装不管什么软件之前都必须要刷新仓库也就是repos。在Ubuntu上你可以使用下面的命令来更新系统第一个命令用于刷新仓库
sudo apt-get update
仓库更新后,现在你可以运行系统更新命令了:
sudo apt-get upgrade
然而,这个命令不会更新内核和其它一些包,所以你也必须要运行下面这个命令:
sudo apt-get dist-upgrade
**openSUSE**如果你是在openSUSE上你可以使用以下命令来更新系统照例第一个命令的意思是更新仓库
sudo zypper refresh
sudo zypper up
**Fedora**如果你是在Fedora上你可以使用'dnf'命令它是zypper和apt-get的'同类'
sudo dnf update
sudo dnf upgrade
### 软件安装与移除 ###
你只可以安装那些你系统上启用的仓库中可用的包,各个发行版默认都附带有并启用了一些官方或者第三方仓库。
**Ubuntu**: To install any package on Ubuntu, first update the repo and then use this syntax:
**Ubuntu**要在Ubuntu上安装包首先更新仓库然后使用下面的语句
sudo apt-get install [package_name]
样例:
sudo apt-get install gimp
**openSUSE**:命令是这样的:
sudo zypper install [package_name]
**Fedora**Fedora已经丢弃了'yum',现在换成了'dnf',所以命令是这样的:
sudo dnf install [package_name]
移除软件的过程也一样,只要把'install'改成'remove'。
**Ubuntu**
sudo apt-get remove [package_name]
**openSUSE**
sudo zypper remove [package_name]
**Fedora**
sudo dnf remove [package_name]
### 如何管理第三方软件? ###
在一个庞大的开发者社区中,这些开发者们为用户提供了许多的软件。不同的发行版有不同的机制来使用这些第三方软件,将它们提供给用户。同时也取决于开发者怎样将这些软件提供给用户,有些开发者会提供二进制包,而另外一些开发者则将软件发布到仓库中。
Ubuntu严重依赖于PPA个人包归档但是不幸的是它却没有提供一个内建工具来帮助用于搜索这些PPA仓库。在安装软件前你将需要通过Google搜索PPA然后手工添加该仓库。下面就是添加PPA到系统的方法
sudo add-apt-repository ppa:<repository-name>
样例比如说我想要添加LibreOffice PPA到我的系统中。我应该Google该PPA然后从Launchpad获得该仓库的名称在本例中它是"libreoffice/ppa"。然后使用下面的命令来添加该PPA
sudo add-apt-repository ppa:libreoffice/ppa
它会要你按下回车键来导入秘钥。完成后,使用'update'命令来刷新仓库,然后安装该包。
openSUSE拥有一个针对第三方应用的优雅的解决方案。你可以访问software.opensuse.org一键点击搜索并安装相应包它会自动将对应的仓库添加到你的系统中。如果你想要手工添加仓库可以使用该命令
sudo zypper ar -f url_of_the_repo name_of_repo
sudo zypper ar -f http://download.opensuse.org/repositories/LibreOffice:Factory/openSUSE_13.2/LibreOffice:Factory.repo LOF
然后,刷新仓库并安装软件:
sudo zypper refresh
sudo zypper install libreoffice
Fedora用户只需要添加RPMFusionfree和non-free仓库一起该仓库包含了大量的应用。如果你需要添加仓库命令如下
dnf config-manager --add-repo http://www.example.com/example.repo
### 一些基本命令 ###
我已经写了一些关于使用CLI来管理你系统上的文件的[文章][1]下面介绍一些基本米ing令这些命令在所有发行版上都经常会用到。
拷贝文件或目录到一个新的位置:
cp path_of_file_1 path_of_the_directory_where_you_want_to_copy/
将某个目录中的所有文件拷贝到一个新的位置(注意斜线和星号,它指的是该目录下的所有文件):
cp path_of_files/* path_of_the_directory_where_you_want_to_copy/
将一个文件从某个位置移动到另一个位置(尾斜杠是说在该目录中):
mv path_of_file_1 path_of_the_directory_where_you_want_to_move/
将所有文件从一个位置移动到另一个位置:
mv path_of_directory_where_files_are/* path_of_the_directory_where_you_want_to_move/
删除一个文件:
rm path_of_file
删除一个目录:
rm -r path_of_directory
移除目录中所有内容,完整保留目录文件夹:
rm -r path_of_directory/*
### 创建新目录 ###
要创建一个新目录首先输入你要创建的目录的位置。比如说你想要在你的Documents目录中创建一个名为'foundation'的文件夹。让我们使用 cd 即change directory改变目录命令来改变目录
cd /home/swapnil/Documents
(替换'swapnil'为你系统中的用户)
然后,使用 mkdir 命令来创建该目录:
mkdir foundation
你也可以从任何地方创建一个目录,通过指定该目录的路径即可。例如:
mdkir /home/swapnil/Documents/foundation
如果你想要创建父-子目录,那是指目录中的目录,那么可以使用 -p 选项。它会在指定路径中创建所有目录:
mdkir -p /home/swapnil/Documents/linux/foundation
### 成为root ###
你或许需要成为root或者具有sudo权力的用户来实施一些管理任务如管理软件包或者对根目录或其下的文件进行一些修改。其中一个例子就是编辑'fstab'文件,该文件记录了挂载的硬件驱动器。它在'etc'目录中,而该目录又在根目录中,你只能作为超级用户来修改该文件。在大多数的发行版中,你可以通过'切换用户'来成为root。比如说在openSUSE上我想要成为root因为我要在根目录中工作你可以使用下面的命令之一
sudo su -
su -
该命令会要求输入密码然后你就具有root特权了。记住一点千万不要以root用户来运行系统除非你知道你正在做什么。另外重要的一点需要注意的是你以root什么对目录或文件进行修改后会将它们的拥有关系从该用户或特定的服务改变为root。你必须恢复这些文件的拥有关系否则该服务或用户就不能访问或写入到那些文件。要改变用户命令如下
sudo chown -R user:user /path_of_file_or_directory
当你将其它发行版上的分区挂载到系统中时,你可能经常需要该操作。当你试着访问这些分区上的文件时,你可能会碰到权限拒绝错误,你只需要改变这些分区的拥有关系就可以访问它们了。需要额外当心的是,不要改变根目录的权限或者拥有关系。
这些就是Linux新手们需要的基本命令。如果你有任何问题或者如果你想要我们涵盖一个特定的话题请在下面的评论中告诉我们吧。
--------------------------------------------------------------------------------
via: http://www.linux.com/learn/tutorials/842251-must-know-linux-commands-for-new-users
作者:[Swapnil Bhartiya][a]
译者:[GOLinux](https://github.com/GOLinux)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创翻译,[Linux中国](https://linux.cn/) 荣誉推出
[a]:http://www.linux.com/community/forums/person/61003
[1]:http://www.linux.com/learn/tutorials/828027-how-to-manage-your-files-from-the-command-line