课程: Building an Ubuntu Server

System management tips

- [Instructor] Before we move on, I want to share a few general tips for managing your system. When we set up the server, we provided a host name and if for some reason we need to change it, we can use the hostnamectl command. Hostnamectl by itself provides us some useful information about the system, as we'll see in just a moment. But it also provides a way to test the system's host name and make any associated changes that we might otherwise miss trying to change the name manually in configuration files. I won't change my system's name here, I like this name. But you can use hostnamectl, set-hostname, and a new name if you need to. And then reboot in order for the change to take effect. Right now, the host name isn't terribly important because we're using the IP address to connect to the system. But if you plan to use DNS later on, the host name will be more relevant. Modern systems with internet access will usually set the time on their clocks themselves through NTP or network time protocol. You can check the date and time on your server with the date command. Notice here that this time is represented in UTC, coordinated universal time and not in my local time zone. Computers track time in UTC and servers are often set to operate using UTC as their time zone. This has a variety of benefits, especially when you have lots of servers scattered all over the world. For a single server, like you might have at home or running in the lab or something like that, you can choose whether to use a UTC or to use your local time zone. Whichever you pick will be the setting used for things like logs and the displayed date and time for file creation and modification. You can change the local time zone in a few ways. One option is by going through the reconfiguration of the tzdata or time zone data package, with dpkg-reconfigure tzdata. Which gives us text user interface or TUI to select the time zone. And another option is to use timedatectl with the set-timezone option. This requires that you know the properly formatted version of whatever your time zone is. You can see them all with timedatectl list-timezones. I'll move down and up the list with the F and B keys for forward and backward. Here's my time zone, America/Los Angeles. I could also use US Pacific in this case, but I'll stick with the more specific one. I'll select this and copy it with Control + Shift + C. Then I'll press Q to exit, and then I'll run timedatectl, set-timezone and I'll paste my time zone with Control + Shift + V. Then I'll run timedatectl to see that change has taken effect. My local time is now tracked in Pacific Daylight Time, and I can see that my time zone has been changed to America/Los Angeles. Pacific Daylight Time, seven hours behind GMT. I can also see that this system is configured to synchronize with a time server. So now and then it'll reach out to make sure its clock is set correctly, based on the network of NTP servers that keeps systems around the world ticking to the same beat. When we're working with a system remotely through SSH, it's a good idea to take care that any interruption in your connection doesn't cause problems on the server. In many cases, it's not a big deal if the SSH connection is unexpectedly terminated. But in some cases, it can be quite dangerous. One example of this can occur when running software installation or updates remotely. When an SSH connection is terminated, the shell that was running within it is also terminated, and anything running in that shell is ended. So while the package manager is running, if it's unexpectedly terminated, it can leave the system in a broken state. As we'll see in a little bit, the package manager can take a surprising amount of time to accomplish what it needs to do. A solution to this is to use a program like Tmux, short for Terminal Multiplexer, which provides what is effectively a virtual session which we can run programs inside of, and which keeps running if our SSH connection shell is terminated. To run Tmux, I'll write tmux, and that opens. This window has one pane right now, and I can create another by pressing Control + B and then C. And I can switch between them with Control + B, followed by P for previous, and Control + B followed by N for next. I'll start something running here, and then I'll close my terminal. Oops, my SSH connection broke. No worries, I'll open up my terminal again and reconnect to my server. Then I'll type tmux attach to connect to the existing session. My process is still running, even though I disconnected. To close the Tmux pane, I'll use Control + B and then X. And to disconnect from Tmux but leave it running in the background, I'll use Control + B followed by D. Tmux will keep running as long as the system isn't rebooted. And when the system reboots, you'll need to start it up again. Tmux can be a little tricky to get used to, but it's worth it if you need to run jobs that should keep going after you disconnect, or just to protect against unexpected disconnections. Ubuntu Server also includes screen, another tool that operates in a similar way. For more details about both of these tools, take a look at Linux: Multitasking at the Command Line here on LinkedIn Learning. Sometimes there are operations that require the system to be restarted, and sometimes we need to shut down the system safely. For example, when we need to add or change hardware, or if we need to move the server around to dust it. It's important to dust around your electronics and servers are no exception, but are often forgotten. To restart or shutdown a system, we'll use the shutdown command with either the -r option to restart, or the -h option to halt or power off the system. These commands both take an argument about when to do that. And in many cases, we'll just use now as the argument. The shutdown command tells the system to move to its shutdown state, and that triggers any running software to perform its designated shutdown tasks. Closing files, flushing buffers, and things like that that help ensure that data isn't lost or damaged, as it might be if you surprised the system and pull its power cord. With those general tips behind us, let's move on to more specific system management tasks.

内容